PHP code specification and quality check tool PHPCS. Note the installation and configuration of PHPMD. you may need ~ /. Composer/vendor/bin/add it to the PATH environment variable. Otherwise, the command cannot be found.
PHPCS
Install
Install PHPCS in the following ways:
Use composer:
Composer global require "squizlabs/php_codesniffer ="
Note that you may need ~ /. Composer/vendor/bin/add it to the PATH environment variable. Otherwise, the command cannot be found.
Use PEAR:
Pear install PHP_CodeSniffer
Download and install:
Curl OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
Php phpcs. phar h
Curl OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
Php phpcbf. phar h
Then move to the command Directory:
Mv phpcs. phar/usr/bin/phpcs
Mv phpcbf. phar/usr/bin/phpcbf
// You may need sudo
Chmod + x/usr/bin/phpcs
Chmod + x/usr/bin/phpcbf
Phpcbf here is a code repair tool.
Use
View help:
Phpcs help
Add Standard:
Phpcs configset installed_paths PATH_TO_SEARCH_STANDARDS
Note: assume that the standard is Weibo and the directory is:
/Users/overtrue/code_standards/Weibo
Here, ruleset. xml is in Weibo, so the PATH_TO_SEARCH_STANDARDS corresponding to the above should be:
Phpcs configset installed_paths/Users/overtrue/code_standards
View installed standards:
Phpcs I
View configuration:
Phpcs configshow
Check code specifications:
Phpcs./codes/Example. php
// Or
Phpcs./codes/
Specify criteria:
Phpcs./codes/Example. php standard = PSR2
Report format:
Phpcs report = summary/path/to/code
Available formats ):
Full, xml, checkstyle, csv
Json, emacs, source, summary, diff
Svnblame, gitblame, hgblame or policysend
Fix code
First, install patches using diff:
Phpcs reportdiff =/path/to/changes. diff/path/to/code
Patch p0 ui/path/to/changes. diff
# Patching file/path/to/code/file. php
Method 2: use PHP Code Beautifier and Fixer:
Phpcbf/path/to/code
The above Command will automatically repair the original file. if you do not want to directly overwrite the original file, you can use suffix to specify the fix code suffix:
Phpcs/path/to/code suffix =. fixed
For more use of PHPCS, see: https://github.com/squizlabs/PHP_CodeSniffer/wiki
PHPMD
Install
The following installation methods are also available:
Download and install the phar file:
Wget c http://static.phpmd.org/php/latest/phpmd.phar
Mv phpmd. phar/usr/bin/phpmd
Chmod + x/usr/bin/phpmd
Install with Composer:
Composer global require phpmd/phpmd
Use
Check code quality:
# Phpmd code path report format
Phpmd/path/to/source text
Or specify the rule to be checked:
# Phpmd code path report format rule list
Phpmd/path/to/source text codesize, unusedcode, naming
Or use xml to specify the check rules:
# Phpmd code path report format rules xml file
Phpmd/path/to/source text/phpmd_ruleset.xml
The report formats include:
Xml, output in XML format;
Text, a simple text format;
Html: output to a single html;