Improving code quality with the PHP_CodeSniffer tool

Improving code quality with the PHP_CodeSniffer tool

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.

Installation

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
composer global require "squizlabs/php_codesniffer=*" 
cURL
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
Wget
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.

Composer

Possible parameters for phpcs script in Composer

Possible parameters for phpcbf script in Composer

cURL, Wget

php phpcs.phar -h
php phpcbf.phar -h

Use

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:

Example of test results in phpcs script with errors in coding standard

Each error marked with an "x" can be fixed automatically using the phpcbf script.

Automatic fixing of errors in coding standard with 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.

Installation

We install the module via Composer: composer require drupal/coder

Use

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".

Checking the coding standard in Coder, the Drupal module

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.

phpcs

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

The view of the settings of PHP_CodeSniffer in the Quality Tools tab in PHPStorm

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.

Adding a path to the phpcs script for PHP_CodeSniffer in PHPStorm

Then, to enable code validation and choose the given standard, go to Settings -> Editor -> Inspections -> Quality Tools and select the PHP_CodeSniffer validation option.

Validation of phpcs script for PHP_CodeSniffer in PHPStorm

Coder

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.

Adding the ruleset from Coder, the Drupal module, in PHPStorm

In order for the script to check the .module, .install, .theme files, add these extensions to the “Check files with extensions”.

Adding .module, .install, theme extensions to the Check files with extensions list in PHPStorm

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.

Example of the information on the rule that has been broken in PHPStorm

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  light  Alt+Enter.

Fixing the wrong code element in PHPStorm using PHP Code Beautifier and Fixer

Summary

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.

Best practices for software development teams