1.简介
PHP_CodeSniffer是PEAR中的一个用PHP5写的用来检查和嗅探PHP代码是否有违反一组预先设置好的编码标准的一个包,它是确保你的代码简洁一致的必不可少的开发工具,甚至还可以帮助程序员减少一些语义错误。
PHP_CodeSniffer中的编码标准是一组嗅探文件的集合,每一个嗅探文件只检查一部分编码标准。一次安装即可实现多个不同项目的编码标准的检查。PHP_CodeSniffer使用的默认的编码标准是 PEAR编码标准。
2.安装
快速安装(需要先安装php和pear)
pear install PHP_CodeSniffer
使用sublime text的同学可以直接安装插件phpcs,安装方法(Ctrl+Shift+P->pi(package install)->phpcs,安装成功后右键即可看到PHP Code Sniffer选项)
Pyrus安装
php pyrus.phar install pear/PHP_CodeSniffer
3.查看安装
[root@vb002 ~]# /usr/local/php/bin/phpcs -i
The installed coding standards are PSR1, Squiz, PHPCS, MySource, PEAR, Zend and PSR2
4.检查代码
[root@vb002 ~]# /usr/local/php/bin/phpcs /root/.jenkins/jobs/zentaotest/workspace/src/Money.php
FILE: /root/.jenkins/jobs/zentaotest/workspace/src/Money.php
----------------------------------------------------------------------
FOUND 6 ERRORS AFFECTING 6 LINES
----------------------------------------------------------------------
2 | ERROR | Missing file doc comment
3 | ERROR | Missing class doc comment
5 | ERROR | Private member variable "amount" must be prefixed with
| | an underscore
7 | ERROR | Missing function doc comment
12 | ERROR | Missing function doc comment
17 | ERROR | Missing function doc comment
----------------------------------------------------------------------
Time: 29ms; Memory: 3Mb
5.卸载安装
[root@vb002 ~]# pear uninstall PHP_CodeSniffer-2.3.3
6.使用示例
想要检查某个文件是否符合PEAR编码标准, 只需要指定文件的路径即可:
使用PHP_CodeSniffer检查文件
$ phpcs /path/to/code/myfile.php
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
51 | ERROR | Missing function doc comment
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------
或者,如果你想要检查整个目录,你可以指定目录路径而不是文件路径:
使用PHP_CodeSniffer检查目录
$ phpcs /path/to/code
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
51 | ERROR | Missing function doc comment
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------
FILE: /path/to/code/yourfile.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
21 | ERROR | PHP keywords must be lowercase; expected "false" but found
| | "FALSE"
21 | WARNING | Equals sign not aligned with surrounding assignments
--------------------------------------------------------------------------------
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮