This project is a collection of PHP_CodeSniffer rules (sniffs) to validate code developed for WordPress. It ensures code quality and adherence to coding conventions, especially the official WordPress Coding Standards. Show
Project history
InstallationRequirementsThe WordPress Coding Standards require PHP 5.4 or higher and PHP_CodeSniffer version 3.7.1 or higher. ComposerStandards can be installed with the Composer dependency manager:
Running this command will:
For the convenience of using 4 as a global command, you may want to add the path to the 5 directory to a 8 environment variable for your operating system.Installing WPCS as a dependencyWhen installing the WordPress Coding Standards as a dependency in a larger project, the above mentioned step 3 will not be executed automatically. There are two actively maintained Composer plugins which can handle the registration of standards with PHP_CodeSniffer for you:
It is strongly suggested to 9 one of these plugins in your project to handle the registration of external standards with PHPCS for you.Standalone
To summarize: cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs And then add the 0 directory to your 8 environment variable via your 2.You should then see 4.RulesetsStandards subsetsThe project encompasses a super-set of the sniffs that the WordPress community may need. If you use the 5 standard you will get all the checks.You can use the following as standard names when invoking 4 to select sniffs, fitting your needs:
Note: The WPCS package used to include a Using a custom rulesetIf you need to further customize the selection of sniffs for your project - you can create a custom ruleset file. When you name this file either <config name="installed_paths" value="/path/to/wpcs" />4, <config name="installed_paths" value="/path/to/wpcs" />5, <config name="installed_paths" value="/path/to/wpcs" />6 or <config name="installed_paths" value="/path/to/wpcs" />7, PHP_CodeSniffer will automatically locate it as long as it is placed in the directory from which you run the CodeSniffer or in a directory above it. If you follow these naming conventions you don't have to supply a <config name="installed_paths" value="/path/to/wpcs" />8 arg. For more info, read about . See also provided <config name="installed_paths" value="/path/to/wpcs" />9 file and fully annotated example in the PHP_CodeSniffer documentation. Customizing sniff behaviourThe WordPress Coding Standard contains a number of sniffs which are configurable. This means that you can turn parts of the sniff on or off, or change the behaviour by setting a property for the sniff in your custom <config name="installed_paths" value="/path/to/wpcs" />6 file. You can find a complete list of all the properties you can change in the wiki. Recommended additional rulesetsThe PHPCompatibility ruleset and its subset PHPCompatibilityWP come highly recommended. The PHPCompatibility sniffs are designed to analyse your code for cross-PHP version compatibility. The PHPCompatibilityWP ruleset is based on PHPCompatibility, but specifically crafted to prevent false positives for projects which expect to run within the context of WordPress, i.e. core, plugins and themes. Install either as a separate ruleset and run it separately against your code or add it to your custom ruleset, like so: <config name="testVersion" value="5.6-"/> <rule ref="PHPCompatibilityWP"> <include-pattern>*\.php$</include-pattern> </rule> Whichever way you run it, do make sure you set the cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs1 to run the sniffs against. The cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs1 determines for which PHP versions you will receive compatibility information. The recommended setting for this at this moment is cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs3 to support the same PHP versions as WordPress Core supports. For more information about setting the cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs1, see: How to useCommand lineRun the 4 command line tool on a given file or directory, for example:
Will result in following output:
Using PHPCS and WPCS from within your IDE
Running your code through WPCS automatically using CI tools
Fixing errors or ignoring themYou can find information on how to deal with some of the more frequent issues in the wiki. Tools shipped with WPCSSince version 1.2.0, WPCS has a special sniff category cd ~/projects git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs6. This sniff category contains some tools which, generally speaking, will only be needed to be run once over a codebase and for which the fixers can be considered risky, i.e. very careful review by a developer is needed before accepting the fixes made by these sniffs. The sniffs in this category are disabled by default and can only be activated by adding some properties for each sniff via a custom ruleset. |