Skip to content

Prepare application code for a PHP version upgrade

Before a PHP version is upgraded on a WordPress VIP Platform environment, all PHP files in an application’s GitHub repository should be scanned for potential incompatibility issues, and the identified issues should be resolved. PHP linting and PHPCS scanning an application’s code on a local machine increases the likelihood for a smooth upgrade to a new PHP version on the VIP environment. 

Prerequisites

On the user’s local machine:

  • The application’s wpvip GitHub repository is git cloned.
  • PHPCS is installed.
  • The upgraded version of PHP is installed:
    • macOS 
    • Windows 
    • Linux using apt:
      Identify the PHP package on the Debian wiki page for the upgraded version. Use the apt install phpX.Y command to install the package, where X.Y represents the version number. Upgrading the system or installing a third-party package may be required if Debian distribution does not yet support the PHP version that is needed.
    • Linux using yum:
      The yum install php-X.Y command can be used to install PHP on yum based systems, where X.Y represents the PHP version number. Upgrading the system or installing a third-party package may be required if distribution does not yet support the PHP version that is needed.

PHP linting

PHP linting identifies code syntax problems present in a repository’s PHP files.

find <path-to-repository> -name '*.php' -type f -exec <path-to-local-php-version> -l '{}' \;

The PHP linting command requires the following values to be passed:

  • Path to the cloned repository: The value of the path to the cloned repository is relative to the directory from which the PHP linting command is run. If the user is in the directory Desktop in the terminal prompt, and the example-site repository has been cloned to Desktop/working-folder/example-site, the repository’s path value in the command will be working-folder/example-site.
  • Path to installed version of PHP: This path value will differ across machines, and should be identified by the user. The path value '/usr/bin/php8.0' is used in the command example, but other installed PHP versions can be used to lint, such as PHP 8.1.

This example command is formatted to PHP lint a cloned repository named example-site against PHP version 8.0:

findexample-site -name '*.php' -type f -exec '/usr/bin/php8.0' -l '{}' \;

All branches of a repository that deploy code to a VIP Platform environment should be PHP linted for PHP version compatibility. Check out each branch locally using git checkout, then lint the checked out branch’s code with the PHP command.

PHPCS scanning

PHPCS is a tool that scans a repository’s PHP files using a variety of standards to analyze code for security and performance.

Note

PHPCS commands on a local machine running Windows may require different formatting than the command examples shown below.

Add the PHPCompatibilityWP standard

The PHPCompatibilityWP standard analyzes a codebase for PHP cross-version compatibility.

This command will install the PHPCompatibilityWP standard globally on a local machine:

composer g require --dev phpcompatibility/phpcompatibility-wp:"*"

Run a PHPCS scan for PHP version compatibility

phpcs --standard=PHPCompatibilityWP --severity=1 --runtime-set testVersion <php-version>- --extensions=php <path-to-repository>

The PHPCS command requires the following values to be passed:

  • Path to the cloned repository: The value of the path to the cloned repository is relative to the directory from which the PHP linting command is run. If the user is in the directory Desktop in the terminal prompt, and the example-site repository has been cloned to Desktop/working-folder/example-site, the repository’s path value in the command will be working-folder/example-site.
  • Version of PHP to scan against: Specify the version of PHP that the code should be scanned against with the --runtime-set parameter. “8.0-” here refers to PHP version 8.0. For PHP 8.1, this would be “8.1-”. This can be any PHP version supported by PHPCompatibilityWP. Please refer to documentation for PHPCompatibilityWP on support for the version you are using.

Note

PHPCompatibility is a dependency of PHPCompatibilityWP. The upcoming release version 10.0.0 of the PHPCompatibility standard will include:

  • Improved compatibility with PHP 8.0: Currently, PHP 7.4 should be used rather than PHP 8.0 when scanning with PHPCS using the PHPCompatibilityWP standard. The dependencies of the PHPCompatibilityWP standard have some remaining incompatibilities with the PHP 8.0 interpreter.
  • Improved detection of PHP 8.0 incompatibilities: At this time, some PHP 8 incompatibilities are not yet detected by the PHPCompatibilityWP standard and its dependencies.

This example command is formatted to scan a cloned repository named example-site for compatibility with PHP version 8.0:

phpcs --standard=PHPCompatibilityWP --severity=1 --runtime-set testVersion 8.0- --extensions=php example-site

All branches of a repository that deploy code to a VIP Platform environment should be PHPCS scanned for PHP version compatibility. Check out each branch locally using git checkout, then scan the checked out branch’s code with the formatted PHPCS command.

Resolving detected issues

All of the issues detected by PHPCS and PHP linting should be investigated and resolved as required. It is possible for some of the reported issues to be false positives that do not need resolution.

In some cases, compatibility code may be required to ensure that an application’s code works with both the current version of PHP as well as the PHP version that will be running after the upgrade. 

The scanning steps outlined in this article will not detect every possible PHP version compatibility issue, but they will help identify the most common issues. As a next step, thorough testing of an application’s code is strongly recommended on a non-production VIP Platform environment with the upgraded version of PHP.

Last updated: May 04, 2022