windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

为什么使用PHP_CodeSniffer

一个开发团队统一的编码风格,有助于他人对代码的理解和维护,对于大项目来说尤其重要。

PHP_CodeSniffer是PEAR中的一个用PHP5写的用来检查嗅探PHP代码是否有违反一组预先设置好的编码标准的一个包,它是确保你的代码简洁一致的必不可少的开发工具,甚至还可以帮助程序员减少一些语义错误。

什么是Pear

由于PHP_CodeSniffer的安装依赖PHP和Pear环境,那么我们有必要了解下什么是Pear。

来自百度百科

PEAR是PHP扩展与应用库(the PHP Extension and Application Repository)的缩写。它是一个PHP扩展及应用的一个代码仓库,简单地说,PEAR之于PHP就像是CPAN(Comprehensive Perl Archive Network)之于Perl。
PEAR的基本目标是发展成为PHP扩展和库代码的知识库,而这个项目最有雄心的目标则是试图定义一种标准,这种标准将帮助开发者编写可移植、可重用的代码。

安装Pear

在已经安装了PHP环境的前提下,进入php目录,如果没有go-pear.php文件,就到http://pear.php.net/go-pear.phar下载go-pear.php文件,该地址在浏览器打开可以看到一段PHP的代码,直接保存文件另存为go-pear.php到php根目录下面。

使用管理员方式打开命令行,输入以下命令:

 cd c:\php
php go-pear.phar

这是出现:

 Are you installing a system-wide PEAR or a local copy?
(system|local) [system] :

直接回车默认system继续,出现如下:

 Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations. . Installation base ($prefix) : C:\php
. Temporary directory for processing : C:\php\tmp
. Temporary directory for downloads : C:\php\tmp
. Binaries directory : C:\php
. PHP code directory ($php_dir) : C:\php\pear
. Documentation directory : C:\php\docs
. Data directory : C:\php\data
. User-modifiable configuration files directory : C:\php\cfg
. Public Web Files directory : C:\php\www
. System manual pages directory : C:\php\man
. Tests directory : C:\php\tests
. Name of configuration file : C:\WINDOWS\pear.ini
. Path to CLI php.exe : C:\php -, 'all' or Enter to continue:

直接回车,出现如下,表示安装成功,

/*省略*/
The 'pear' command is now at your service at c:\php\pear.bat
/*省略*/

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

在php根目录下面会看到如下几个文件:

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

双击pear.bat文件,注册pear到当前环境。

安装PHP_CodeSniffer

在安装完pear之后,就可以安装php_CodeSniffer了,继续在cmd中输入:

 pear install PHP_CodeSniffer

等待安装完成,安装完成后php根目录下回出现以下两个文件:

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

按照下图依次打开文件夹,在看如下目录结构:

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

在php->pear->PHP->CodeSniffer->Standards中可以看到一些php的规范,Generic是通用规范。

现在我们就可以使用这些规范来检测我们的php代码了,先说说在命令行中如何使用。

我们可以使用phpcs -h来看看使用帮助:

phpcs -h

看到的如下:

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

这里我只简单的说明如何检查单个文件或整个文件目录:

 phpcs -n F:\Hg\web\application\controllers\  //检测文件目录
phpcs -n F:\Hg\web\application\controllers\home_controller.php //检测单个文件

看到如下结果(单个文件):

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

这样,我们就可以根据这些错误信息去修改我们的代码,使其符合规范。

我们可以指定使用某一个规范进行检测,方法如下:

 phpcs -n --standard=Zend F:\Hg\web\application\controllers\

不指定标准,会使用php通用规范Generic。

安装CodeIgniter标准

https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer下载包解压,复制src目录到php->pear->PHP->CodeSniffer->Standards目录下,并且改名为CodeIgniter

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

上图为解压后图

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

上图为放到php代码规范下后的图。

现在就可以使用CodeIgniter标准检测代码了:

 phpcs -n --standard=CodeIgniter F:\Hg\web\application\controllers\

PHPSTORM配置PHP_CodeSniffer检测环境

打开phpstorm的配置框,找到Languages & Frameworks -> php-> Code Sniffer,不同版本的phpstorm可能会有出入,直接搜索Code Sniffer也可以。

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

点击如下进行编辑:

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

设置PHP Code Sniffer path为phpcs.bat的路径。

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

点击Validate,出现如下图表示设置成功:

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

打开配置搜索Inspections, 展开PHP,勾选PHP Code Sniffer validation, 选择Coding standard为CodeIgniter, 点击OK确定。

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

接下来,在编码PHP的时候就会出现规范提示

windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

如上图,鼠标移动到有波浪提示的地方,就会出现phpcs的规范提示了。

配置到此结束,希望可以帮到需要的程序猿!

最规范的代码就是不出现任何的波浪提示。

参考:

http://baike.baidu.com/subview/20453/16587839.htm

上一篇:20165326 java第四周学习笔记


下一篇:《DSP using MATLAB》示例Example5.22