From time to time, you are obliged to drift to a different and new PHP version for your latest projects but, to use a different version, you need to make yourself compatible with it as much as you are with the one you are currently working on.
One simple technique says that you just install the PHP version you want to shift to, run php–l and look for any syntax errors, thereby examining the PHP documentation for any issues and hoping for best results. Or else, a few third party tools are available which help you check your compatibility with the new PHP version for your project.
PHPCompatibility – A Promising Tool
PHPCompatibility’s present iteration fully backs PHP versions till PHP 7.
How to install PHPCompatibility?
It is easy to install PHPCompatibility through Pear or Composer. For PHP7, for an instance, you can install PHPCS via Composer, thereby using the PHPCompatibility coding standards straight on top of it.
For Local installation Composer need “squizlabs/php_codesniffer=2.*”
After the installation of PHPCS, let’s move over to the PHPCS /Standards folder that’s located in /vendor/squizlabs/php_codesniffer/CodeSniffer/Standards.
Once the PHPCS is installed, prefer heading on to the PHPCS /Standards folder located in /vendor/squizlabs/php_codesniffer/CodeSniffer/Standards.
git clone https://github.com/wimg/PHPCompatibility.git
The command helps install PHPCompatibility coding standard precisely into your standards folder along with coding standards, which are already tied in PHPCS. To monitor the success of PHPCS and PHPCompatibility installation, run the command: ./vendor/bin/phpcs -i
A list of installed standards will appear to help you see PHPCompatibility.On the other hand, global installation also follows the same method with
Composer’s global require “squizlabs/php_codesniffer=2.*”
Thereafter, clone PHPCompatibility into the folder mentioned below:
For a newbie, it’s better to begin with ./vendor/bin/phpcs -h.It leads you to the PHPCS help section.Before exploring how to use the PHPCompatibility standard, it is important to learn about a few PHPCS commands, which let you conduct your compatibility tests in a faster and more effective manner, in adherence to the results you wish to see:
-i – do not print warnings. It is of help in case you have a long directory of messages, which you seek to sift out the warnings to concentrate on the errors.
-l – will check the local directory alone, without recursion.
-p – show the run progress. It particularly helps in big projects by maintaining status.
-i – show a complete list of installed coding standards.
<file> – the file or folder to verify.
<extensions> – a list of file extensions separated by commas to check.
<generator> – it either uses the HTML, Markdown or Text generator. Also, it focuses on documentation generation rather than doing the check alone.
<patterns> – a list of patterns separated by commas to ignore files and folders (for an instance, vendor).
<severity> – the minimum severity of an issue necessary to show an error or warning.
<standard> – the coding standard name or path to use.
<runtime-set> – a few individual standards need specific configuration options, which applies to PHPCompatibility as well.
This syntax of the command is: phpcs –runtime-set’
This list is not just comprehensive. Rather, it is highly helpful when it comes to fine tuning your checks.
How to check compatibility?
Checking compatibility becomes easier with the command that follows:
./vendor/bin/phpcs –standard=PHPCompatibility –runtime-set testVersion 7
This command allows you to use the PHPCompatibility standard where the runtime-set option remains the same as earlier. Now you are checking the compatibility with PHP 7 on defined file or folder.
The output will be something like the following:
It is imperative to remember that if you are checking backwards compatibility, you must run PHPCS on the newest PHP version available. For instance, with the keyword yield in your code, PHPCS will only be able to identify it if PHP 5.5 or a higher version is running on your system. If it is familiar with what it actually is, it will intimate you that previous PHP versions do not support it.
In case you only want to check for .php files, prefer using the option. Add it to the previous command:
./vendor/bin/phpcs –standard=PHPCompatibility –extensions=php –runtime-set testVersion 7
A full report can be obtained by using the option- -report-full=.txt
If you want to ignore any files or folders in compatibility test, such as, a folder with tests and the vendor folder mentioned above, you can choose the option- -ignore=*/tests/*,*/vendor/*
As a result, you will not be testing the designated files and folders for compatibility.Every PHPCS option you have considered till now can help you use the tool better.
We have a real life example for this:You must be curious to know how it applies to a real life, production type application. Here’s how it works on PHPMailer.
git clone https://github.com/PHPMailer/PHPMailer.git mailer
It allows you to clone PHPMailer into the /mailer folder, while installing all its dependencies. Later, you must deploy PHPCS as well as the PHPCompatibility standard:
Composer require “squizlabs/php_codesniffer”
git clone https://github.com/wimg/PHPCompatibility.git
At length, execute the PHPCompatibility standard on a single file or on the entire project if you want.
./vendor/bin/phpcs –standard=PHPCompatibility –extensions=php –runtime-set testVersion 5.6 class.phpmailer.php
You can observe that PHPMailer is not completely ready for PHP 5.6, but you have been able to gather all the required information to upgrade the code.
We hope you will find this tool introduction helpful to test the latest PHP version for compatibility with your project.