Check your code base is PHP 7 ready with PHP Compatibility

Published by at 25th February 2018 5:22 pm

I've recently started a new job and as part of that I'm working on a rather substantial legacy code base. In fact, it was so legacy that it was still in Subversion - needless to say the very first thing I did was migrate it to Git. One of the jobs on our radar for this code base is to migrate it to from PHP 5.4 to 5.6, and subsequently to PHP 7. I've been using it locally in 5.6 without issue so far, but I've also been looking around for an automated tool to help catch potential problems.

I recently discovered PHP Compatibility which is a set of sniffs for PHP CodeSniffer that can be used to detect code that will be problematic in a particular PHP version. As I use CodeSniffer extensively already, it's a good fit for my existing toolset.

To install it, add the following dependencies to your composer.json:

1 "require-dev": {
2 "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3",
3 "squizlabs/php_codesniffer": "^2.5",
4 "wimg/php-compatibility": "^8.1"
5 },

Then update your phpcs.xml to look something like this:

1<ruleset name="PHP_CodeSniffer">
2 <description>The coding standard for my app.</description>
3 <file>./</file>
4 <arg value="np"/>
5 <rule ref="PSR2"/>
6 <rule ref="PHPCompatibility"/>
7 <config name="testVersion" value="7.2-"/>

As you can see, it's possible to use it alongside existing coding standards such as PSR2. Note the testVersion config key - the value specified is the PHP version we're testing against. Here we're specifying PHP 7.2.

Obviously, the very best way to guard against breakages in newer versions of PHP is to have a comprehensive test suite, but legacy code bases by definition tend to have little or no tests. By using PHP Compatibility, you should at least be able to catch syntax problems without having to audit the code base manually.