Check PHP version compatibility

Before the version of PHP running on a server can be updated we need to confirm the sites on the server support that version of PHP.

This guide outlines a process for checking if a site supports a newer PHP version. It assumes you'll be using Lando with Composer installed.

Check code compatibilty

We're going to be using PHP CodeSniffer and the PHP Compatibility Coding Standard for PHP CodeSniffer.

Install PHP CodeSniffer

lando composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
lando composer require --dev phpcompatibility/php-compatibility:"^10.0.0@dev"

Hopefully phpcs is already configured in the lando tooling, but if not add it with:

tooling:
  ...
  phpcs:
    service: appserver
    cmd: /app/bin/phpcs

Check your code

The following will check code compatibility between PHP version 8.1 8.3. You will want to update this for you current and target versions.

lando phpcs -p web --standard=PHPCompatibility --runtime-set testVersion 8.1-8.3

On a Drupal site with lost of modules this will take a long time to run and generate a fair number of false positives in Drupal core. As long we Drupal core is up to date we only need to check the contrib and custom modules and themes.

Check all the modules with:

lando phpcs -p web/modules --standard=PHPCompatibility --extensions=php,module,inc,install,test,profile,theme --runtime-set testVersion 8.1-8.3  

In the resulting report you're looking for errors and warnings in contrib modules that mean they need to be updated. Errors in custom modules mean the custom code needs to be fixed

You'll need to do the same with themes and profiles (if you are using them).

lando phpcs -p web/themes --standard=PHPCompatibility --error-severity=1 --warning-severity=6 --runtime-set testVersion 8.1-8.3  

Automatically fix custom code

If there are lots of errors from PHP CS for custom code it can be easier to automatically fix this using Rector, but be warned that will likely make a lot more changes than the minimum required.

Install Rector

lando composer require rector/rector --dev

Add rector to the Lando tooling by editing the Lando file and adding the tool configuration:

tooling:
  ...
  rector:
    service: appserver
    cmd: /app/bin/rector

Configure Recotr

Create a rector.php file in the gitroot with the following, but replacing the PHP version number with the version you're checking support for.

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/web',
    ])
    ->withPhpSets(
        php83: true,
    );

Fix custom code

First check what it's going to change with:

lando rector process --dry-run web/modules/custom/my_custom_module

If you're happy with this run the fix with:

lando rector process web/modules/custom/my_custom_module

Check the site

Once you have updated all the code needed to support the desired version of PHP, update the version running in Lando by editing the .lando.yml file and changing the PHP version, then run a lando rebuild -y.

Then fully test the site, making sure you check the Drupal error log for PHP warnings and errors.

PHPCS can miss things so this step is very important.

Once you're happy the there are no errors you can commit the changes and do further tesing on a dev/stage site.

Last updated: