1. introduction & nbsp; PHP_CodeSniffer is a package written in PHP5 in PEAR used to check and sniff whether PHP code violates a set of preset encoding standards, it is an essential development tool to ensure that your code is concise and consistent, and can even help programmers reduce semantic errors. & Nbsp; the encoding standard in PHP_CodeSniffer is a collection of sniffing files, each 1. Overview
? PHP_CodeSniffer is a package written in PHP5 in PEAR to check and sniff whether PHP code violates a set of preset encoding standards, it is an essential development tool to ensure that your code is concise and consistent, and can even help programmers reduce semantic errors.
? The encoding standard in PHP_CodeSniffer is a set of sniffing files. each sniffing file only checks part of the encoding standard. Once installed, you can check the coding standards of multiple different projects. What is the default encoding standard used by PHP_CodeSniffer? PEAR encoding standard.
2. install quick installation (php and pear must be installed first)
Pear install PHP_CodeSniffer
Users who use sublime text can directly install the plug-in phpcs. installation method (Ctrl + Shift + P-> pi (package install)-> phpcs, right-click to view the PHP Code Sniffer option)
Install Pyrus
Php pyrus. phar install pear/PHP_CodeSniffer
3. example
To check whether a file complies with the PEAR encoding standard, you only need to specify the file path:
Use PHP_CodeSniffer to check the file
$ 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
--------------------------------------------------------------------------------
Alternatively, if you want to check the entire directory, you can specify the directory path instead of the file path:
Use PHP_CodeSniffer to check the Directory
$ 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
--------------------------------------------------------------------------------
More details: http://pear.php.net/manual/en/package.php.php-codesniffer.intro.php