In addition to the coding assistance and inspections provided by PhpStorm, extra checks on code style can be performed using the PHP Code Sniffer (PHPCS) tool integration. In this tutorial, we'll have a look at how we can validate our code for consistence with a coding standard of choice, and how PHP Code Sniffer integrates with PhpStorm.
To make use of PHP Code Sniffer, we first have to install it on our system.
A Composer package for PHP Code Sniffer is available. It can be used to do a system-wide install, using the following command:
composer global require "squizlabs/php_codesniffer=*"
If you prefer to install into the vendor directory, a dependency can be added to composer.json instead. This can be done using Composer Support in PhpStorm or manually. Composer will install PHP Code Sniffer under the following path in your project: ./vendor/bin/phpcs and ./vendor/bin/phpcs.bat.
Use the Composer | Add dependency... context menu on your project:
Add the following to the composer.json file:
Another way to install PHP Code Sniffer is by using PEAR:
pear install PHP_CodeSniffer
This will install the PHP Code Sniffer classes into our PEAR installation folder (for example C:\PHP\5.5\pear\PHP\CodeSniffer.php and will create a bash / batch script in the PHP installation folder (for example C:\PHP\5.5\phpcs and C:\PHP\5.5\phpcs.bat).
Through Project Settings | PHP | Code Sniffer, we can specify the path to PHP Code Sniffer, as well as some additional options (such as timeout) or files to ignore.
Once PhpStorm knows the path to the PHP Code Sniffer executable, we can configure it as a PhpStorm inspection through Project Settings | Inspections and then enabling the PHP Code Sniffer validation inspection.
We can configure several additional parameters:
Whenever we want to learn about how well (or how poorly) we are adhering to the selected coding standard, we can run PHP Code Sniffer in "batch mode." Using the Code | Inspect code menu, we can let PhpStorm run the PHP Code Sniffer validation on all files in our project (or on a subset). We will have to specify the inspection profile to use, which defaults to the inspection profile that is active for the current project.
In the screenshot above, I have created a customized inspection profile which will only run PHP Code Sniffer and skips all other PhpStorm inspections.
Once PhpStorm and PHP Code Sniffer finish their work, the inspection results will be shown. Warnings and errors found by PHP Code Sniffer will be listed, grouped by folder. Clicking a warning on the left will display details on the right.
Using the toolbar on the left, we can change filtering and grouping options, as well as export the inspection results as XML or HTML.
While PHP Code Sniffer ships with many supported code styles, sometimes we have to use a custom code style for the project we are working on. For example, Drupal projects typically adhere to the Drupal Code Standard. Luckily, PHP Code Sniffer supports adding custom code styles.
There is no formal way of obtaining custom code styles. Most ship with a readme that explains how to install the required files on our system or in our project.
Even though there is no standard way of obtaining additional code style configurations, installing them is more or less the same. Typically installation consists of:
The Drupal code style for PHP Code Sniffer ship as a Composer package. We can add it to our project by installing the drupal/coder package, either system-wide or in our project. When installing Drupal's Coder package, code styles will reside in the ./vendor/drupal/coder/coder_sniffer/Drupal folder.
To install these code styles, we can copy the ./vendor/drupal/coder/coder_sniffer/Drupal folder to the Standards folder of the PHP Code Sniffer installation. When installed through Composer, this would mean copying the folder to ./vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Drupal.
We can configure PhpStorm to make use of custom code styles through Project Settings | Inspections, updating the PHP Code Sniffer validation inspection settings. From the Coding Standard dropdown list, we can select the coding standard we just copied into PHP Code Sniffer's installation directory.
Depending on how the code style was developed, it is not always required to copy the code style into the PHP Code Sniffer installation folder. For many code styles, we can select the Custom code style trough Project Settings | Inspections, and specify the path to the code style we want to use in PhpStorm.