Writing code according to the standards allows you to speed up the project creation. It's more easily extensible and more legible – thanks to this, new people on the project can more quickly deploy and provide optimal solutions. The PHP community is constantly striving to improve code quality. Tools to assist in writing the code that is compliant with the standards have been developed over the years. In this text, I'll present one of them: PHP_CodeSniffer.
I'll also introduce you to the Drupal Coder module, which contains standards for writing the code for Drupal. It can also be used to automatically fix the code using phpcbf.
What is PHP CodeSniffer
PHP_CodeSniffer helps keep your code clean and compliant with the standards. The tool consists of two scripts.
- phpcs - tokenizes the PHP, JS and CSS code and detects deviations from the defined code-writing standard,
- phpcbf - automatically fixes the code according to the defined rules.
PHP version 5.4.0 or higher is required for the correct operation of the tool.
PHP_CodeSniffer can be installed using Composer, cURL or Wget. I recommend installing it via Composer, but it's worth noting that it's also possible to install it via Composer inside a Docker container – if you're using Docker, use this installation method.
composer global require "squizlabs/php_codesniffer=*"
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
After downloading the packages, you can test them.
php phpcs.phar -h php phpcbf.phar -h
Both the phpcs and phpcbf scripts use the PEAR coding standard. The coding standard can be specified using the -- standard parameter.
In order to test a single file, use the command:
$ phpcs filename.php $ phpcbf filename.php
You can also test an entire directory:
$ phpcs /path/to/directory $ phpcbf /path/to/directory
The phpcs script will inform you about finding errors in the coding standard. Example of test results:
Each error marked with an "x" can be fixed automatically using the phpcbf script.
Fixing the remaining errors requires the intervention of the programmer. Usually, these are errors such as missing correct tags in the class comment.
Coder - Drupal module
The Coder module provides a set of programming principles used in Drupal. Used together with PHP_CodeSniffer, it helps with fixing bugs in the coding standard.
We install the module via Composer: composer require drupal/coder
Checking the coding standard is similar to what I showed earlier; you only use the --standard parameter, to which you give the value of "Drupal".
Integration with PHPStorm
Both the PHP_CodeSniffer script and ruleset from the Coder module can be integrated with the most popular IDEs. Let's use PHPStorm as an example.
First, you need to provide the path to the phpcs script in PHPStorm. In order to do this, go to Settings -> Languages and Frameworks -> PHP -> Quality Tools -> PHP_CodeSniffer
Then you add a new configuration and in the PHP_CodeSniffer path, you provide the path to the script. You can check the correctness of the path by clicking the validate button.
Then, to enable code validation and choose the given standard, go to Settings -> Editor -> Inspections -> Quality Tools and select the PHP_CodeSniffer validation option.
To add the ruleset from the Coder module, select Custom in the Coding Standard select list. In the pop-up, add the path to the ruleset.xml file provided with the module.
In order for the script to check the .module, .install, .theme files, add these extensions to the “Check files with extensions”.
At this point, phpcs is already correctly integrated into the PHPStorm.
In the event of breaking any rule, PHPStorm will highlight the line that does not comply with the standards, as well as provide precise information on the rule that has been broken.
PHPStorm can be integrated with PHP Code Beautifier and Fixer, and it doesn't require any additional configuration. From the IDE level, you can automatically fix all errors in the file that you were previously fixing with this script in the terminal. In order to do this, find any error in the file and click on or press Alt+Enter.
Both PHP_CodeSniffer and the Coder module improve the work of programmers in PHP development services at our agency. The code written in accordance with the standards is easier to maintain and develop. New people joining the project will deploy faster. And if the project is transferred to a new team, the implementation process will also take less time. At Droptica, PHP_CodeSniffer and the Coder module is the standard used by every programmer.