PHP_CodeSniffer along with WordPress coding standards rule-set for phpcs is used to detect violations of a defined set of coding standards.
- phpcs is command line tool, instead of using windows bash it is recommended to use Git bash for this tutorial.
- First, check if Composer is already installed in your system by typing
composer
in command prompt. If the composer is not installed go to getcomposer and install it.
- Install PHPCS by running following command in your terminal –
composer global require squizlabs/php_codesniffer=*
- Install WordPress Coding Standards for PHP_CodeSniffer by command
composer global require wp-coding-standards/wpcs
- Configure phpcs to use WordPress coding standards rulesets by running following command
phpcs --config-set installed_paths /c/Users/$(whoami)/AppData/Roaming/Composer/vendor/wp-coding-standards/wpcs
- Now you have installed and configured phpcs and WordPress coding standards rules with phpcs.
- Open your project in the terminal and If you don’t have already, add the phpcs ruleset to your project.
- Use the following command to run phpcs check.
phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php')
- phpcs also comes with a beautifier which can fix simple errors automatically, run this with a command –
phpcbf --standard=phpcs.ruleset.xml $(find . -name '*.php')
# Generate Log File: # Generate Log File:
Syntax:
phpcs --report-full=/path/to/full.log --report-summary=/path/to/summary.log /path/to/code
Use:
phpcs --report-full=phpcs-full.log --report-summary=phpcs-summary.log $(find . -name '*.php')
—–
It’ll generate phpcs-full.log & phpcs-summary.log files.