Ideally, we should go into the PHP 8 migration with an action plan in hand. A critical part of this action plan includes getting an idea of how many potential BC (backwards-compatability) breaks exist in our current code base. We show how to develop a BC break sniffer that automates the process of looking through hundreds of code files for potential BC breaks.

As we know about BC issues that might arise in PHP 8.

An overview of BC breaks

We already know, having read the previous chapters in this course, that potential code breaks originate from several sources. Let’s briefly summarize the general trends that might lead to code failure after a migration since we are familiar with these topics.

  • Resource-to-object migration

  • Minimum versions for supporting OS libraries

  • Iterator to IteratorAggregate migration

  • Removed functions

  • Usage changes

  • Magic method signature enforcement

Many of the changes can be detected by adding a simple callback based upon preg_match() or strpos(). Usage changes are much more difficult to detect because, at a glance, there’s no way for an automated break scanner to detect the result of usage without making extensive use of eval().

Let’s now have a look at how a break scan configuration file might appear.

Creating a BC break scan configuration file

A configuration file allows us to develop a set of search patterns independently of the BC break scanner class. Using this approach, the BC break scanner class defines the actual logic used to conduct the search, whereas the configuration file provides a list of specific conditions along with a warning and suggested remedial actions.

Quite a few potential code breaks can be detected by simply looking for the presence of the functions that have been removed in PHP 8. For this purpose, a simple strpos() search will suffice. On the other hand, a more complex search might require that we develop a series of callbacks. Let’s first have a look at how configuration might be developed based on a simple strpos() search.

Defining a simple strpos() search configuration

In the case of a simple strpos() search, all we need to do is provide an array of key/value pairs, where the key is the name of the removed function, and the value is its suggested replacement. The search logic in the BC break scanner class can then do this:

Get hands-on with 1400+ tech skills courses.