一聚教程网:一个值得你收藏的教程网站

热门教程

Centos6.6下PHP_CodeSniffer安装 卸载及使用实例

时间:2022-06-24 16:19:00 编辑:袖梨 来源:一聚教程网

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

--------------------------------------------------------------------------------

热门栏目