Previously we blogged about the Continuous Integration practices we use here at NN4M. We thought we’d give you an update as things have changed slightly. In short, when a change is made to the code base an automated build process is triggered which runs a series of testing/validation tools. This allows us to get instant notification of errors.

This post focuses on two of the tools that are executed when the automatic build process is started and improves the quality and stability of our code base.

PHP Code Sniffer

The backend/PHP team follow a PHP coding standard called PSR-2. A coding standard is essentially formatting rules you must follow when writing code. Some of the rules in PSR-2 are:

  • There MUST NOT be trailing white space at the end of non-blank lines.
  • Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.

We use a tool called PHP Code Sniffer (aka PHPCS) that detects violations of this coding standard. Whenever a code change is committed to our code base, the Continuous Integration environment runs PHP Code Sniffer to notify us of any violations.

Example of an error:

Bill Blog 1

PHP Code Sniffer can be configured to use different or even custom coding standards. So it isn’t tied to just checking PSR-2 standards.

Following a coding standard is important for many reasons:

  • Code written in a consistent style is easier to understand
  • Removes the chance of a developer running into any surprises as they are maintaining the code base

There are also packages available for PHP editors (our favourite being Sublime Text) that will check your code for standards violations every time you save. This adds an extra layer of validation that provides the developer nearly instant notification of a violation.

PHP Mess Detector
Another tool we use on every build is PHP Mess Detector (aka PHPMD). PHPMD inspects your source code for possible issues like:

  • Dead code
  • Complicated expressions
  • Excessively long functions/methods
  • Excessively short/long variable names

Example of an error in PHPMD:

Bill Blog 2

PHPMD also allows you to fully customize the ruleset that it checks against. So you aren’t forced to use the default ruleset.

You can find more information at these sites:

PHP Code Sniffer
https://github.com/squizlabs/PHP_CodeSniffer

PSR-2 Coding Standards
http://www.php-fig.org/psr/psr-2/

PHP Mess Detector
https://github.com/phpmd/phpmd