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根目录下面。
利用管理员办法打开命令行,输入以下命令:
1 cd c:\php\r2 php go-pear.phar
这是涌现:
1 Are you installing a system-wide PEAR or a local copy?\r2 (system|local) [system] :
直接回车默认system连续,涌现如下:
1 Below is a suggested file layout for your new PEAR installation. To\r 2 change individual locations, type the number in front of the\r 3 directory. Type 'all' to change all of them or simply press Enter to\r 4 accept these locations.\r 5 \r 6 1. Installation base ($prefix) : C:\php\r 7 2. Temporary directory for processing : C:\php\tmp\r 8 3. Temporary directory for downloads : C:\php\tmp\r 9 4. Binaries directory : C:\php\r10 5. PHP code directory ($php_dir) : C:\php\pear\r11 6. Documentation directory : C:\php\docs\r12 7. Data directory : C:\php\data\r13 8. User-modifiable configuration files directory : C:\php\cfg\r14 9. Public Web Files directory : C:\php\www\r15 10. System manual pages directory : C:\php\man\r16 11. Tests directory : C:\php\tests\r17 12. Name of configuration file : C:\WINDOWS\pear.ini\r18 13. Path to CLI php.exe : C:\php\r19 \r20 1-13, 'all' or Enter to continue:
直接回车,涌现如下,表示安装成功,
/省略/\rThe 'pear' command is now at your service at c:\php\pear.bat\r/省略/
在php根目录下面会看到如下几个文件:
双击pear.bat文件,注册pear到当前环境。
安装PHP_CodeSniffer
在安装完pear之后,就可以安装php_CodeSniffer了,连续在cmd中输入:
1 pear install PHP_CodeSniffer
等待安装完成,安装完成后php根目录下回涌现以下两个文件:
按照下图依次打开文件夹,在看如下目录构造:
在php->pear->PHP->CodeSniffer->Standards中可以看到一些php的规范,Generic是通用规范。
现在我们就可以利用这些规范来检测我们的php代码了,先说说在命令行中如何利用。
我们可以利用phpcs -h来看看利用帮助:
看到的如下:
这里我只大略的解释如何检讨单个文件或全体文件目录:
1 phpcs -n F:\Hg\web\application\controllers\ //检测文件目录\r2 phpcs -n F:\Hg\web\application\controllers\home_controller.php //检测单个文件
看到如下结果(单个文件):
这样,我们就可以根据这些缺点信息去修正我们的代码,使其符合规范。
我们可以指定利用某一个规范进行检测,方法如下:
1 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
上图为解压后图
上图为放到php代码规范下后的图。
现在就可以利用CodeIgniter标准检测代码了:
1 phpcs -n --standard=CodeIgniter F:\Hg\web\application\controllers\
PHPSTORM配置PHP_CodeSniffer检测环境打开phpstorm的配置框,找到Languages & Frameworks -> php-> Code Sniffer,不同版本的phpstorm可能会有出入,直接搜索Code Sniffer也可以。
点击如下进行编辑:
设置PHP Code Sniffer path为phpcs.bat的路径。
点击Validate,涌现如下图表示设置成功:
打开配置搜索Inspections, 展开PHP,勾选PHP Code Sniffer validation, 选择Coding standard为CodeIgniter, 点击OK确定。
接下来,在编码PHP的时候就会涌现规范提示
如上图,鼠标移动到有波浪提示的地方,就会涌现phpcs的规范提示了。
配置到此结束,希望可以帮到须要的程序猿!
最规范的代码便是不涌现任何的波浪提示。