PHP code specifications and quality check tools PHPCS and PHPMD installation and configuration

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.