An important part of good code quality is the compliance with coding standards. The following rules can be part of the coding standards:

  • Naming of variables and functions
  • Tabs or spaces
  • Positions of braces
  • Allowed blank lines
  • Allowed line length
  • Maximum code complexity
  • Number of parameters for a function
  • Unused (dead) code
  • Many more…

Wouldn’t it be great to have these coding standards checked automatically?

Integrated tools in Magento 2

Magento 2 comes with two tools which can check code quality based on certain rules. Basic rules are also included in Magento 2.

PHPCS

PHP Codesniffer (PHPCS)

PHPMD

PHP Mess Detector (PHPMD)

 

Both check for slightly different parts of the PHP code: PHP CodeSniffer checks for formatting errors mostly, while PHP Mess Detector uses metrics to check for “soft” code quality.
And both are great tools. They can be used to check the code quality on the command line or in the IDE (if you configure it correctly). If you want to do so, you can find the rulesets which are used in Magento 2.2 in the following paths:

  • /dev/tests/static/framework/Magento for PHPCS
  • /dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml for PHPMD

Automating with GrumPHP

GrumPHP

Using the free GrumPHP tool, you can have your code checked on every commit. Once set up, it automatically registers as a Git Hook which will be triggered on commit. If everything is fine, it can look as follows:

On the other hand, if you have issues in your code, it looks like this:

In the example, we have issues violating the PHPCS rules (with hints on how to fix them with another tool called “PHP Code Beautifier” (phpcbf)) and, below, issues violating the PHPMD rules. Two other checks named “Composer” and “Blacklist” are performed. If an issue is found, the code is not committed, so you have to fix the issue and commit again.

False Positives

If you are familiar with Magento 2 backend development, you might have noticed the error message Avoid unused parameters such as '$subject'. for a plugin. You cannot avoid to have the $subject variable in your method signature for plugins though, and if you don’t need to call it, it’s perfectly reasonable. So what to do with errors which are reported wrongly?
Just add a comment which tells PHPMD to ignore that error like this:

You can find a list of all PHPMD rules and their identifiers at phpmd.org/rules/index.html.

For the PHP Code Sniffer, there is a similar mechanism although there are usually fewer reasons to ignore them. You can find more information at github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage.

Setup GrumPHP in two Steps

We only need two steps to setup GrumPHP. The first step consists of three command line commands:

The first two commands are for downgrading existing packages so they are compatible with both Magento 2 and GrumPHP.

The result should look like this:

GrumPhp Installation Result

As you can see, the git hook is created automatically on installation.
The second step is to put a file called grumphp.yml into the main directory of your Magento 2 project with the following content:

You might have to adjust the magento_dir variable and the path of the composer.json.
Otherwise, it’s ready to go.

Feel free to add more keywords to the blacklist, add more tasks or adjust the rules. See the GrumPHP documentation on how to create, configure and activate custom and existing tasks.

Andreas von Studnitz

Author: Andreas von Studnitz

Andreas von Studnitz is a Magento developer and one of the Managing Directors at integer_net. His main areas of interest are backend development, Magento consulting and giving developer trainings. He is a Magento Certified Developer since 2011 (and 2018 for Magento 2) and a Magento Certified Solution Specialist since 2014 (Magento 1) respective 2017 (Magento 2).

More Information · Twitter · GitHub · LinkedIn