[转载]使用PHP_CodeSniffer规范php代码

为何使用PHP_CodeSniffer

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

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

什么是Pear

因为PHP_CodeSniffer的安装依赖PHP和Pear环境,那么咱们有必要了解下什么是Pear。git

来自百度百科程序员

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.phar到php根目录下面。这里提供我的百度网盘下载:https://pan.baidu.com/s/1jIy6HEmgithub

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

1 cd c:\php
2 php go-pear.phar

 

这是出现:浏览器

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

 

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

复制代码
1 Below is a suggested file layout for your new PEAR installation.  To
 2 change individual locations, type the number in front of the
 3 directory.  Type 'all' to change all of them or simply press Enter to
 4 accept these locations.
 5 
 6  1. Installation base ($prefix)                   : C:\php
 7  2. Temporary directory for processing            : C:\php\tmp
 8  3. Temporary directory for downloads             : C:\php\tmp
 9  4. Binaries directory                            : C:\php
10  5. PHP code directory ($php_dir)                 : C:\php\pear
11  6. Documentation directory                       : C:\php\docs
12  7. Data directory                                : C:\php\data
13  8. User-modifiable configuration files directory : C:\php\cfg
14  9. Public Web Files directory                    : C:\php\www
15 10. System manual pages directory                 : C:\php\man
16 11. Tests directory                               : C:\php\tests
17 12. Name of configuration file                    : C:\WINDOWS\pear.ini
18 13. Path to CLI php.exe                           : C:\php
19 
20 1-13, 'all' or Enter to continue:
复制代码

 

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

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

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

双击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来看看使用帮助:

phpcs -h

 看到的以下:

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

1 phpcs -n F:\Hg\web\application\controllers\  //检测文件目录
2 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的规范提示了。

配置到此结束,但愿能够帮到须要的程序猿!

 

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

原文连接:http://www.cnblogs.com/huangbx/p/php_codesniffer.html