Add PHP Compatibility Checks to Your Process

This week, I’m going to walk you through what it takes to start checking your plugin’s PHP compatibility.

Every time I start a new plugin, one of the first things I do is configure a composer script that checks PHP compatibility for me.

If you are writing a WordPress plugin, you should be checking it’s PHP compatibility

You hope that your plugin is installed on as many sites as possible, right?

Well, one thing that comes with that, is wildly different hosting environments with different versions of PHP.

Until just recently, WordPress had to support PHP versions all the way back to 5.6. And thank goodness plugins don’t have support that far back now.

Verifying PHP compatibility does 3 things:

  • It ensures that users running on each version of PHP have the same experience
  • It prevents you from releasing code that breaks with specific versions of PHP
  • It shows you where you need to handle deprecations before you release the code

Setting up some compatibility checking is a rather minor process that provides a ton of value.

Here’s how, step-by-step:

Install The Required Dependencies

If you are not using composer, I’m not going to walk through how to set up your plugin to use it because I’ve written about it before.

Come back when you’ve set it up. (this can wait 😄)

Once you have configured Composer, you just need to require a couple of packages:

Add the following lines to the require-dev section of your composer.json file.

"require-dev": {
    "phpcompatibility/php-compatibility": "*",
    "squizlabs/php_codesniffer": "3.*",
    "dealerdirect/phpcodesniffer-composer-installer": "^0.6.0"
},
"prefer-stable" : true

This will install PHP CodeSniffer and its package for checking PHP compatibility along with a composer plugin that helps easily install code sniffing rulesets.

Once you’ve added that to composer, you can install them with:

composer install

Once you’ve installed the dependencies, you can run the following command and check your codebase for compatibility issues (PHP 7.4 as an example):

phpcs your/plugin/directory/to/check -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.4

That command will scan your plugin’s PHP files and let you know if it found any compatibility issues with PHP 7.4.

If you see any output in your terminal after that command, you have an issue. Running that command shouldn’t produce any output if everything checks out fine.

Set Up PHP Compatibility Scripts

Composer has a scripts section where you can configure specific scripts to run with an alias. It’s an easy way to provide commonly used scripts for others that clone your project.

In the scripts section of composer (I typically add it right below require-dev), add these commands:

"scripts": {
    "compatibility:php-7.1": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.1",
    "compatibility:php-7.2": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.2",
    "compatibility:php-7.3": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.3",
    "compatibility:php-7.4": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.4",
    "compatibility:php-8.0": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 8.0",
    "compatibility:php-8.1": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 8.1",
    "compatibility:php-8.2": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 8.2"
  }

Now, when you want to run a compatibility check, you can just type:

composer run compatibility:php-7.4

However, that can still be tedious if you want to check a bunch of versions of PHP at once. We can simplify all those commands into a single command (but still be able to run the checks individually if needed):

"scripts": {
    "compatibility:php-7.1": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.1",
    "compatibility:php-7.2": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.2",
    "compatibility:php-7.3": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.3",
    "compatibility:php-7.4": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 7.4",
    "compatibility:php-8.0": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 8.0",
    "compatibility:php-8.1": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 8.1",
    "compatibility:php-8.2": "phpcs ./src -s --standard=PHPCompatibilityWP --runtime-set testVersion 8.2",
    "compatibility": [
      "@compatibility:php-7.1",
      "@compatibility:php-7.2",
      "@compatibility:php-7.3",
      "@compatibility:php-7.4",
      "@compatibility:php-8.0",
      "@compatibility:php-8.1",
      "@compatibility:php-8.2"
    ]
  }

Within the scripts section of Composer, you can also use the @ to call other scripts and if you provide an array of script commands, they’ll all run.

So by adding that last section, we can run composer compatibility and all of our compatibility checks will run, showing you if there are any issues in any of the PHP versions you have configured.

Now that you have a command, you can run to check PHP compatibility issues, you can run it during your development process, and verify that your PHP is compatible with tons of different versions of PHP.