Determine the minimum PHP version of your code

Determine the minimum PHP version of your code

Share on facebook
Share on twitter
Share on pinterest

Remember when I showed you how to test code on older PHP without having to downgrade? Well, those were baby steps, because that solution can only handle snippets. The PHP CompatInfo CLI tool I’m presenting today is a bazooka. It’s for entire projects. While it doesn’t run your code on older PHP, it knows how it would run, as it analyzes language constructs and your syntax to find the minimum required PHP version. I believe everyone should have this tool at their disposal, before releasing production code, as in WordPress plugins or themes.

Installing and Running PHP CompatInfo

Unfortunately, this tool is not for the faint of heart. It runs on Linux. For me, this meant a considerable learning curve, but trust me it’s worth it. You can get your hands on an Ubuntu running in a Virtual Machine to facilitate using this tool. Other options include a Linux machine (cheapest being the Raspberry Pi), or a VPS with SSH access (perhaps you are already paying for something like this). I’m using the Ubuntu+VM way, and base the rest of the article on that. We are going to determine the minimum version of WordPress itself. Come on; it’s not that bad!

Ubuntu files window showing PHP CompatInfo for determining minimum required PHP version of WordPress

  • Extract WordPress or your project.
  • Open a terminal from the right-click menu of the Files window (looking at the Downloads).
  • Remember, to use copy paste features in the terminal, use Ctrl+Shift+V and similar.
  • If you don’t have PHP, then run these in order – provide your password, and answer yes with Y:
sudo apt update
sudo apt install php7.2-cli
sudo apt install php7.2-sqlite3

Then you can immediately run this, and you’ll find the report in the compatinfo.log file after the process is completed.

php phpcompatinfo-5.0.12.phar analyser:run . --output=compatinfo.log

If you like the tool and want to have it available as a system-wide command, the also perform the following:

chmod +x phpcompatinfo-5.0.12.phar
sudo mv phpcompatinfo-5.0.12.phar /usr/local/bin/phpcompatinfo

Then you can run it from any folder like this:

phpcompatinfo analyser:run . --output=compatinfo.log

Determining the minimum required PHP version based on the log file

Upon opening the log, you’ll see a very detailed analysis of your code. Here is a list of what it does for you, I believe it’s magical:

  • It shows you how many files and folders your project has.
  • Analyses PHP extensions you use, such as CURL or SimpleXML.
  • List your Namespaces, Interfaces, Traits if any.
  • Classes Analysis
    • Generate a list of every Class and how many times you use them.
    • The total number of the different Classes you use.
    • Furthermore, it indicates whether a Class is your own, or core, or is from an extension.
  • Functions Analysis
    • Every function ever used and how many times.
    • The total number of functions.
    • Also, it shows whether a function is yours or comes from a 3rd party (core/extension).
    • You can see anonymous functions (closures) like this: “closure-125-129”. It means that some file has a function from line 125 or 129, but it doesn’t tell you which file.
  • Constants Analysis provides a complete list of Constants, use counts, total count, and their origin.
  • Conditions Analysis lists every call to class_exists()defined()extension_loaded()function_exists()interface_exists()method_exists() functions to further determine what features you are using.

Of course, there is the granular minimum required PHP version data for each Namespace, Interface, Trait, Class, Function, Constant, and Condition. Look at the very end of the file, where you’ll find the calculated overall minimum PHP version.

The PHP all column tells us which version is required when all code features are used.

Example results

For WordPress 5.0.2, the result says:

Requires PHP 5.6.0alpha3 (min), PHP 5.6.33 (max), PHP 7.0.2 (all).

Here is the generated file as an example, so you know what to expect.

If this were new to me, I’d be curious to know all these stats about my creation. Just for fun, I’ve run it for some popular plugins:

  • Yoast SEO 9.3
    Requires PHP 5.5.0 (min), but they indicate 5.2.4 or higher.
  • Jetpack 6.8.1
    Requires PHP 5.6.0beta1 (min).
  • Justified Image Grid v3.9.2
    Requires PHP 5.2.1 (min), PHP 5.3.0 (all).
  • Contact Form 7.5.1.1
    Requires PHP 5.3.0 (min).
  • WooCommerce 3.5.3
    Requires PHP 5.6.0beta1 (min), PHP 5.6.33 (max).
  • Ok, I’ll stop now, it’s not even the definition of fun. 😥

Why could it show a different version than the official requirement?

It’s hard to tell, but please be advised that the tool is very smart. It catches those that some developers skim over. For example, take a good look at the docs page for ob_start() and especially the flags parameter. It mentions that you can use constants like PHP_OUTPUT_HANDLER_CLEANABLE while showing at the top that ob_start() has been around for a while and it’s available in PHP 4, PHP 5, PHP 7. It’s easy to miss that the constants were added only in PHP 5.4! Only if you question literally everything will you find this page and refrain from using constants like these if you sadly need to support PHP 5.3. While this is just an example, you get the idea of how meticulous is the database of PHP CompatInfo. This is how everything is registered in that:

{
    "ext_name_fk": 7,
    "name": "PHP_OUTPUT_HANDLER_CLEANABLE",
    "ext_min": "5.4.0",
    "ext_max": "",
    "php_min": "5.4.0",
    "php_max": "",
    "php_excludes": ""
},

Is PHP CompatInfo trustworthy?

All this begs the question, whether or not you can rely on this information. I’ve found that it is accurate. On the rare occasion when something erroneous stood out (a function requiring the latest PHP), the developer fixed the bug I reported quite quickly. I cite the (all) or (max) value when different from the (min) value as the minimum PHP version for products created by me. This process has yet to betray me. Of course, if you want to be sure, you would need to install the mentioned PHP version and test your product thoroughly. I don’t go back to versions earlier than PHP 5.6 for testing since I trust the analysis.

This site is powered by Elementor

  • This site is powered by Elementor

Related Posts

Check out Justified Image Grid, my top-selling WordPress gallery that shows photos without cropping!

Show your photos with Justified Image Grid!