Assessing Compatibility with PHP 8

Kevin
2 min readMar 24, 2023

--

Photo by Shahadat Rahman on Unsplash

With the release of PHP 8, many new features and optimizations have been introduced, along with several deprecations and potential backward incompatibilities. This poses a challenge for developers, who must ensure that their existing code is compatible with the latest version of PHP.

We will explore how to use PHP Compatibility Coding Standard for PHP CodeSniffer to automatically detect potential compatibility issues in your codebase. We will also provide code examples to demonstrate how to configure and run the tool effectively.

What is PHP CodeSniffer?
PHP CodeSniffer is a popular tool that can help developers maintain consistent coding standards across their PHP projects. It works by tokenizing PHP, JavaScript, and CSS files to detect violations of a predefined set of coding standards. With the PHP Compatibility Coding Standard, PHP CodeSniffer can also analyze your code for potential compatibility issues with different PHP versions.

Setting up PHP CodeSniffer and PHP Compatibility Coding Standard
To get started, first, ensure that you have Composer installed. Then, run the following commands to install PHP CodeSniffer and the PHP Compatibility Coding Standard:

composer global require "squizlabs/php_codesniffer=*"
composer global require "phpcompatibility/php-compatibility"

Next, register the PHP Compatibility standard with PHP CodeSniffer:

phpcs --config-set installed_paths ~/.composer/vendor/phpcompatibility/php-compatibility

Running a PHP 8.2 compatibility check

To check your code for PHP 8.2 compatibility issues, navigate to your project’s root directory and run the following command:

phpcs --standard=PHPCompatibility --runtime-set testVersion 8.2 --extensions=php --report-full=./report.txt .

This command will analyze your PHP code for compatibility issues with PHP 8.2, only checking .php files. The results will be saved to a file named report.txt in your project's root directory.

Let’s break down the command:

  • --standard=PHPCompatibility: Specifies the coding standard to use, in this case, the PHP Compatibility standard.
  • --runtime-set testVersion 8.2: Sets the PHP version to check for compatibility. In this example, we're checking for PHP 8.2 compatibility.
  • --extensions=php: Limits the analysis to files with the .php extension.
  • --report-full=./report.txt: Outputs a full report of detected issues to a file named report.txt.
  • .: The final dot represents the directory to analyze, in this case, the current directory.

Code Example

<?php

$response = '1234567890';
$header = 'Content-length: ' . strlen($response) + 1;
header($header);

Running the compatibility check command mentioned earlier will generate a report like this:

FILE: /path/to/your/project/example.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
3 | ERROR | Using an unparenthesized expression containing a "." before a "+" or "-" has been deprecated in PHP 7.4 and removed in PHP 8.0

--

--