Check for compatibility with specific PHP version using Travis CI

My Lazy Loader plugin needs PHP 5.3 or higher. If users count on that, it is annoying if this minimum version is raised through a inadvertency (like using the [] array syntax) in a patch or minor release.

To prevent that, I use Travis CI to check my code for compatibility with PHP 5.3.

The PHPCompatibility project for PHP_CodeSniffer makes it possible to check code for compatibility with a specific PHP version (or multiple versions). The installation is possible with Composer and described in the readme of the project. The require-dev part of my composer.json looks like that:

"require-dev": { "squizlabs/php_codesniffer": "*", "wimg/php-compatibility": "*", "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4" },
Code language: JSON / JSON with Comments (json)

To check the three class files of my plugin in the src folder, I can use the following command in the root directory of my project after running composer install:

vendor/bin/phpcs -p src/ --standard=PHPCompatibility --runtime-set testVersion 5.3
Code language: Bash (bash)

In the best case, the result looks like ... 3 / 3 (100%) and not something like .E. 3 / 3 (100%)

This command can be used with Travis CI. The script part of my .travis.yml looks like this:

script: - composer install - vendor/bin/phpcs -p src/ --standard=PHPCompatibility --runtime-set testVersion 5.3
Code language: YAML (yaml)

If the check detects an error, the build will fail and I cannot longer skip the PHP 5.3 compatibility by mistake ?

You can find Lazy Loader on GitHub, where you can have a look at the .travis.yml and composer.json.

Leave a Reply

Your email address will not be published. Required fields are marked *