Skip to main content

joeri poesen / bio / blog / contact

Home / Articles / PHP/Drupal Code Quality Quick Wins, Part 2: Validating Code Standards

The Drupal project has coding standards, available at, based on the PEAR coding standards (

These code standards are a set of guidelines that cover file organisation, internal documentation, naming conventions, whitespace/indentation settings, line length, line wrapping, concatenation styles, etc. The general aim is to have a more uniform code base throughout the project, to make working with someone else’s code easier, to reduce bugs and to allow for certain automated processes such as documentation generation and patch testing.

The easiest way to start producing code that lives up to the standards is by simply having PHP_CodeSniffer (, also often referred to as phpcs, report on the state of your code and learn from the remarks.

Phpcs simply parses any given code and basically validates it against a given set of rules. By default phpcs validates against the PEAR standard, but additional standards can be added. In our case, we will download Drupal’s coder.module ( to a central location (/opt/coder for example). Amongst other things, this module contains Drupal Code Standards integration support for phpcs, so we just need to make phpcs aware of the existence of this integration code and we’re ready to go.


# Install phpcs with PEAR.
$ pear config-set auto_discover 1
$ pear install PHP_CodeSniffer
# Install Drupal’s coder module.
$ cd /opt/; drush dl coder
# Make phpcs aware of our ‘Drupal’ code standard.
$ phpcs --config-set installed_paths /path/to/coder/coder_sniffer
# Optional: make the ‘Drupal’ code standard default.
# phpcs --config-set default_standard Drupal


$ phpcs —standard=drupal example.php
$ phpcs —standard=drupal —extensions=“php, module, install, inc, test” /path/to/my/code/example.php

Example output:

  2 | ERROR   | Missing file doc comment
  8 | ERROR   | Spaces must be used to indent lines; tabs are not allowed
  8 | ERROR   | Line indented incorrectly; expected 2 spaces, found 1
 10 | ERROR   | Missing function doc comment
 19 | WARNING | Line exceeds 80 characters; contains 102 characters
 19 | ERROR   | Inline comments must start with a capital letter
 19 | ERROR   | Inline comments must end in full-stops, exclamation marks, or
     |         	      | question marks
 20 | ERROR   | Missing function doc comment
 24 | ERROR   | Inline comments must start with a capital letter
 26 | ERROR   | Missing function doc comment
 26 | ERROR   | Visibility must be declared on method "tearDown"
 29 | ERROR   | Missing function doc comment

Retro-actively checking a big-ish module can generate dozens if not hundreds of warnings. Don’t be discouraged: cleaning up your code is totally worth it. Trust me on this one.

IDE integration

Being able to run linters and tools like phpcs from the command line is pretty nice, but sometimes having to switch between your editor and the command line is too much of an interruption for some. Luckily, because these tools are open sourced, and open source people are awesome human beings, support and integration for these tools exists for most browsers.

Here’s a quick overview of how different IDEs integrate phpcs:

Sublime Text 2 and 3:




Have fun.