Tools for Automating Coding Standards Reviews for Drupal 6, 7, and 8

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Sprout Video

Implementing the Drupal coding standards is important, and shouldn't be ignored. But doing so can be a bit tedious. Especially when you're first getting started. There are a lot of rules to learn, and maybe even some old habits to break. So it's understandable that you might not get it perfect every time. Having your work sent back to you by a reviewer because you formatted a comment incorrectly, or placed a closing bracket on the wrong line, can be frustrating. Luckily there are all kinds of great tools to help us automate the review process. Letting us scan our own code, or that written by others for coding standards violations, and getting them fixed right away.

While the content of this tutorial is based on Drupal 7 most of the same tools, and techniques, will work just fine with other versions of Drupal.

The JetBrains website has the most up-to-date information about configuring PhpStorm to work with Drupal.…

In this tutorial we'll look at a few different ways that we can automate our coding standards reviews using tools that read in our PHP code, and apply a set of predefined rules in order to help point out mistakes. We'll start by looking at the Coder module and the UI that it provides. Then, we'll install PHP_CodeSniffer, and use that in conjunction with the rules contained in the Coder module, and the drush command line utility, to run coding standards reviews at the command line. And, in some cases even in our IDE's that support PHPCS.

Since there are a handful of commands to execute, here is the walkthrough on installing and using PHPCS + drush.

This command downloads the coder module globally. Instead of putting it in the Drupal module's directory, you're downloading it to the ~/.drush/ directory. Since this is in your drush $PATH it's globally available and you can use it on any project without having to install coder on every Drupal project you work on.

drush pm-download coder --destination=$HOME/.drush

You then need to clear the internal drush cache so that it locates the new command contained in the just installed coder module.

drush cache-clear drush

Then, you need to install phpcs. There are a lot of ways to install it, and depending on your environment you might even already have it installed. Check with:

which phpcs

If you don't, the easiest way to install it is with drush:

drush install-php-code-sniffer
phpcs --version

Then you can check an individual file, or a directory, for coding standards violations with:

drush drupalcs path/to/code

Additional resources