1. Introduction
Php_codesniffer is a package written by PHP5 in Pear that examines and sniffs whether PHP code violates a set of predefined coding standards, an essential development tool to ensure that your code is concise and consistent, and even helps programmers reduce semantic errors.
The coding standard in Php_codesniffer is a collection of sniffer files, each of which examines only a subset of the coding standards. A single installation will enable you to check the coding standards for multiple different projects. The default encoding standard used by Php_codesniffer is the Pear encoding standard.
2. Installation
Quick Install (you need to install PHP and pear first)
Pear Install Php_codesniffer
Students using sublime text can directly install Plug-ins phpcs, installation Method (CTRL+SHIFT+P->PI (package install)->phpcs, after the successful installation of the right button can see the PHP Code sniffer options)
Pyrus installation
PHP Pyrus.phar Install Pear/php_codesniffer
3. View installation
[Root@vb002 ~]#/usr/local/php/bin/phpcs-i
The installed coding standards are PSR1, Squiz, Phpcs, MySource, PEAR, Zend and PSR2
4. Check code
[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 is prefixed with
| | An underscore
8 S ERROR | Missing function Doc Comment
12 | ERROR | Missing function Doc Comment
17 | ERROR | Missing function Doc Comment
----------------------------------------------------------------------
Time:29ms; Memory:3mb
5. Uninstall Installation
[root@vb002 ~]# Pear Uninstall php_codesniffer-2.3.3
6. Using the example
To check whether a file conforms to the Pear encoding standard, simply specify the path to the file:
Using Php_codesniffer to check files
$ 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
--------------------------------------------------------------------------------
Or, if you want to check the entire directory, you can specify the directory path instead of the file path:
Use Php_codesniffer to check directories
$ 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
--------------------------------------------------------------------------------