Testing old code for PHP 8 compatibility

This post will show you how to use PHP Compatibility to test your code for compatibility with newer versions of PHP. You can use it to test an entire folder or a specific file.

Hasn’t everyone switched to PHP 8 already?

Many have. But if like us you’ve had some non-critical, low-maintenance code humming along on PHP 7, you might not have felt the need to do anything about it. We’re big fans of NearlyFreeSpeech.net hosting and have PHP code written years ago running on the platform. Their recent No More PHP 7 announcement was the kick in the backside we needed to revisit some of that code and check for compatibility issues with PHP 8. They write:

The PHP developers are ardent adherents of “move fast & break things,” and backward compatibility is the thing they break the most.

Testing for compatibility

If you have good test coverage of your code, you’ll most likely catch compatibility issues early. If you don’t, you’ll notice and fix them when things break, or when you get complaints from users.

There are a number of tools available to help you identify issues in your code, and some that will even update your code automatically for you. But the only one that we found that could be used to identify only compatibility issues was PHP Compatibility, so that’s what we’ll cover here.

At the time of writing, the last release of PHP Compatibility was in 2019, but the git repository is active and the steps here will show you how to use the latest code from the repository.

What you’ll need

  • Access to the terminal (or command prompt on Windows)
  • Composer to install PHP Compatibility’s dependencies
  • (Optional) Git (if you don’t want to use the git command, you can download a zip archive from GitHub and unzip it instead of running git clone).

Installation

Install PHP Compatibility in a separate directory from the code you want to test. We’ll use the git command and /home/private/ in the steps below.

  1. cd /home/private
  2. git clone https://github.com/PHPCompatibility/PHPCompatibility.git
  3. cd PHPCompatibility
  4. composer install

That last step will install all the dependencies.

Running

PHP Compatibility is essentially a set of rules for PHP CodeSniffer. So to run it, we need to execute the PHP CodeSniffer script in vendor/phpc/bin/phpcs. We’ll execute this and pass the directory or file we want to test as an argument:

./vendor/bin/phpcs -p --standard=PHPCompatibility --runtime-set testVersion [php-version] --ignore=.css,.js [directory-or-file-to-test]

The command above will check the files in [directory-to-test] for compatibility with [php-version]. It will ignore .css and .js files and show its progress (-p). Let’s say we want to test PHP files in /home/public for PHP 8.2 compatibility. The command would be:

./vendor/bin/phpcs -p --standard=PHPCompatibility --runtime-set testVersion 8.2 --ignore=.css,.js /home/public

For additional parameters, e.g. to save the report to a file, ignore certain directories, see usage and advanced usage.