Skip to content

PHP linting for PHP version compatibility

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

PHP linting an application’s code on a local machine increases the likelihood of a smooth update to a new PHP version on a VIP Platform environment

Prerequisites

On the user’s local machine:

  • The application’s wpcomvip GitHub repository is git cloned.
  • PHPCS is installed.
  • The updated version of PHP is installed:
    • macOS 
    • Windows 
    • Linux using apt:
      Identify the PHP package on the Debian wiki page for the updated 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 the distribution does not yet support the PHP version that is needed.

Considerations

Performing scans with PHP linting 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 on a non-production VIP Platform environment with the updated version of PHP is strongly recommended.

  • All issues detected by 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 and the PHP version that will be running after the update. 

The PHP linting command

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

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 the installed version of PHP: This path value will differ across machines and should be identified by the user using which php (macOS / Linux) or where php (Windows). The path value '/usr/local/bin/php' is used in the command example.

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

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

Last updated: December 26, 2023

Relevant to

  • WordPress