The end of life for PHP 7.4 is fast approaching, and many of us are working on a strategy to update and test your sites. If you have a lot of sites or a large project you need to get compliant before the switchover, this article will guide you through the initial testing.
Depending on your host, you may or may not already have composer installed on your environment. If you do not, you can read the get started guid here: getcomposer.org. For those of you lucky enough to be hosted on MODX, it is as easy as SSHing into your site and running:
curl http://modx.co/scripts/install.sh | sh source ~/.profile
Install PHP CodeSniffer and PHPCompatability
Once composer is available, you can install PHP CodeSniffer and PHPCompatability. Currently, the PHP 8 standards are in the "dev" branch of PHPCompatability, so you will first need to allow that in your composer by running:
composer global config minimum-stability dev composer global config prefer-stable true
The above command basically tells composer you would like to allow dev branches of code, but you would prefer not to if unspecified. Now you are able to install PHP CodeSniffer and PHPCompatability.
composer global require --dev "squizlabs/php_codesniffer=*" "dealerdirect/phpcodesniffer-composer-installer" "phpcompatibility/php-compatibility=dev-develop"
The above command will install it globally, so you can access it anywhere. This is my preferance, however you can also install it as a dev dependency of a specific project if you would prefer.
Run the Test
Now the fun part. You can use PHP CodeSniffer with the plugin PHPCompatability tool to test a specific directory. For example, if you are currently in the directory you want to test, you can just use
. as the path. This would look like:
phpcs . --standard=PHPCompatibility
Additionally, if you only want to scan for errors and ignore things like deprecation warnings, you can add the
phpcs -n . --standard=PHPCompatibility
The above commands will output the test in your terminal. However, if you want to generate a report of your scan, you can pipe it into a text file like so:
phpcs -n . --standard=PHPCompatibility > php8-testing.txt
Reviewing the Results
The results generated by this test will usually be pretty straight forward. If you do not choose to ignore warnings, then it may have a little (or a lot) of garbag related to minified files and unmatched line endings that you will need to parse through.
For the most part, if you are working on a MODX site, you can fairly safely ignore anything in the "/manager/" folder, and anything in the "/core/" folder excluding the "/core/components/" directory.
You can actually set up a custom configuration file for what you do and don't want to test. E.g.
<?xml version="1.0"?> <ruleset name="PHP8_Testing"> <description>PHP 8 Testing</description> <file>core/components</file> <file>assets</file> <!-- Ignore everything but errors. --> <arg value="n" /> <!-- Set the basepath to the current directory --> <arg name="basepath" value="."/> <!-- Ignore vendor files --> <exclude-pattern>*/vendor/*</exclude-pattern> <!-- Ignore manager folder --> <exclude-pattern>manager</exclude-pattern> <!-- Our base rule: set to PHPCompatibility--> <rule ref="PHPCompatibility"/> </ruleset>
Once set up, place the XML in your project's root directory and run
phpcs > php8-testing.txt