PHP 5.4 compatibility checks using PHP_CodeSniffer

Update (27 Nov) : Support for PHP 5.5alpha is included.

For those of you who are new to this concept, check my blog post from a while ago. It will explain the basic concept of using PHP_CodeSniffer to automate compatibility checks. But don’t use the download links, because they point to the old (PHP 5.3) version !

What’s new ?

Quite a few things have changed in this new release :

  • There’s no version specific release anymore. The previous codesniffer standard was called PHP53Compatibility, but it seemed quite stupid to make a new standard for every PHP version out there, especially since that would keep certain people from upgrading to the latest major PHP version. So the new PHPCompatibility standard works for 5.0 – 5.4
  • But since some people simply can’t upgrade to the latest version, I added version information to all the checks. For example : the deprecated  function checker will now tell you that session_register() is deprecated since PHP 5.3 and removed since PHP 5.4 – if you’re running 5.2 and want to move to 5.3, at least you know right away that you’ll have to fix that problem, because otherwise you can’t ever upgrade to 5.4. This version check is available on deprecated/removed function, deprecated/removed php.ini directives and deprecated/removed extensions.
  • Default timezone check has been added : since PHP 5.4, you need to have a default timezone set or PHP will complain. This is ofcourse only useful if you run the tests on a system with identical settings as your production environment.
  • A check for the removed functionality on break and continue was added. (Using a variable or function call as a parameter on break and continue is no longer allowed.)
  • 2 algorithms were removed in the hash extension, so there’s a check for that as well

Where to get it

2 options :

  • Using git : run this in your PHP_CodeSniffer/Standards directory :
    ~ > git clone git://github.com/wimg/PHPCompat_CodeSniffer.git PHPCompatibility
  • Downloading a zip : download here and unzip the file in PHP_CodeSniffer/Standards/PHPCompatibility

How to run it

Start PHP_CodeSniffer like this :
phpcs --standard=PHPCompatibility

Enjoy !

As always, any feedback (or patches on Github) welcome !

34 thoughts on “PHP 5.4 compatibility checks using PHP_CodeSniffer”

  1. I enjoy a lot! My compagny have a huge code base to validate for PHP 5.3, and your sniffs help us a lot. I still looking for sniffs to ensure code is compatible with 5.2 AND 5.3.
    Thanks for your help.

    1. This codesniffer standard will actually tell you which of the features are incompatible with which specific version (5.0, 5.1, 5.2, 5.3 or 5.4). Ofcourse, if you want to use it in a continuous integration environment, you will get errors about 5.4 even when you’re not running 5.4 yet.
      You can ofcourse always run the old PHP53Compat standard for those environments. Check the first link (pointing to my previous blog post) for that codesniffer standard, which will check for 5.3 compatibility.

  2. Thanks for putting this together as it’s really helpful.

    I’d just like to make a note that might maybe help some people regarding the Default timezone check.

    My date.timezone was setup correctly, but I was still getting an error message. It was not until I actually sat down and had a little think, that I realized that I have 2 php.ini files. One for Apache, and one for php cli. My Apache config was correct, but my cli had no timezone configured.

  3. Hi,

    I used your package, and it is really useful, so I am wondering if you could help me. I am currently looking for a specific sniffer.
    I have three big PHP project, and someone had the good idea to use the silent operator for some function calls.
    So, I would be happy if you could help me to write (or if you have one already) a sniffer to detect all function call using the silent operator.

    Thanks.

  4. After installing PHP_CodeSniffer i try using it and got error:

    ERROR: the “PHPCompatibility” coding standard is not installed. The installed coding standards are PHPCS, PSR1, MySource, Zend, PEAR, Squiz and PSR2

    best regards

    Witek.

    1. You need to download PHPCompatibility from Github and install it in /usr/share/pear/PHP/CodeSniffer/Standards/ (in its own PHPCompatibility directory)

  5. hi,

    i get an error.

    ls -ls /usr/share/php/test/PHP_CodeSniffer/CodeSniffer/Standards/PHPCompatibility
    4 -rw-r–r– 1 root root 771 Mar 4 2012 README
    4 -rw-r–r– 1 root root 916 Aug 6 15:11 README.md
    4 -rw-r–r– 1 root root 167 Mar 4 2012 ruleset.xml
    4 drwxr-xr-x 3 root root 4096 Aug 6 15:11 Sniffs

    pear list | grep CodeS
    PHP_CodeSniffer 1.4.6 stable

    I tried the git and the zip.

    Message if i try to execute the tool:
    ERROR: the “PHPCompatibility” coding standard is not installed. The installed coding standards are PSR1, MySource, PHPCS, Zend, Squiz, PSR2 and PEAR

    All the best

    1. That looks like an odd location for the CodeSniffer. Normally it’s located in /usr/share/pear/PHP/CodeSniffer and the standard should be installed in /usr/share/pear/PHP/CodeSniffer/Standards/

  6. PHP Notice: Undefined offset: 3 in /usr/share/pear/PHP/CodeSniffer.php on line 1036
    PHP Fatal error: Cannot redeclare class PHPCompatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff in /usr/share/pear/PHP/CodeSniffer/Standards/PHPCompatibility/Sniffs/DeprecatedIniDirectivesSniff.php on line 23

  7. Sorted! correct Path must be
    /usr/share/pear/PHP/CodeSniffer/Standards/PHPCompatibility/Sniffs/PHP/DeprecatedIniDirectivesSniff.php
    before was
    /usr/share/pear/PHP/CodeSniffer/Standards/PHPCompatibility/Sniffs/DeprecatedIniDirectivesSniff.php

  8. Okay so I downloaded PHPCompatibility-1.5.zip — Version 1.5

    I need to run compatibility check for php 5.4 running wordpress. Help me see the exact steps for running this file. Thanks!

    1. Best thing to do is follow the steps under ‘Installation’ on https://github.com/wimg/PHPCompatibility

      Note that you won’t be able to do 5.4 specific checks unless you install the latest PHP_CodeSniffer master branch. But I recommend doing 5.5 checks at the same time, so you don’t really need that.

  9. Hello and thank you for all. I have juste a probleme I think I have the solution but I prefer ask.

    I install this on my website and when I run phpCompatility I have some error like that

    Undefined property: PHPCompatibility_Sniffs_PHP_NewFunctionsSniff::$forbiddenFunctionNames
    Undefined property: PHPCompatibility_Sniffs_PHP_NewFunctionsSniff::$patternMatch

    Is there possible I see cause my phpcs version is 1.3 beta, and I must have 1.5?

    Thanks for answer

  10. I am trying to use this now to test a WordPress theme that works in 5.2 but crashes WordPress in 5.4. How long should it take? Its just sitting there with no indication that its working or not…

    For reference I think I am using the command correctly…

    phpcs –runtime-set testVersion 5.4 –standard=PHPCompatibility

    1. Don’t forget to add the path at the end. So in case you’re in the right directory already, just add a dot (.)

  11. I’m having the same path problem for the sniffs. Is there a way to output or find the paths this installation is looking for?

  12. hi wimg, looking you are so kind that replying each comment may you be blessed.
    I am running XAMP on windows and using phpcs –version command i have checked that CodeSniffer version 1.5.4 is installed BUT in c:\xamp\php there is no folder PHP_CodeSniffer and then Standards where i should put your PHPCompatibility folder, please help. i made directries myself but it didn’t worked and still reports PHPCompatibility standard not installed. please help m stuk in it and suffering a lot.

    1. Try running it on a directory. If there is no output at all, all is well 😉
      You can also use -v to get more verbose output (or -vv or -vvv for even more).

  13. That’s great i have done it thanks to you v much for your kind response. One thing more can’t i use it for checking a single file? as you said check for dir i did and it worked!!!

    1. You can. Just specify the file. So phpcs –standard=PHPCompatibility filename.php
      If you get no output, it means PHPCompatibility didn’t find any issues. You can ofcourse still get more verbose output with the -v(vv) option.

      Keep in mind that PHPCompatibility can not test 100% of the incompatibilities, but it’s a good start 😉

  14. When I run the following:
    phpcs --standard=PHPCompatibility /var/www/html/

    I get:
    PHP Fatal error: Access level to PHPCompatibility_Sniffs_PHP_DeprecatedFunctionsSniff::$forbiddenFunctions must be public (as in class Generic_Sniffs_PHP_ForbiddenFunctionsSniff) in /usr/share/pear/PHP/CodeSniffer/Standards/PHPCompatibility/Sniffs/PHP/DeprecatedFunctionsSniff.php on line 23

      1. The error:

        “PHP Fatal error: Access level to PHPCompatibility_Sniffs_PHP_DeprecatedFunctionsSniff::$forbiddenFunctions must be public (as in class Generic_Sniffs_PHP_ForbiddenFunctionsSniff) in /usr/share/pear/PHP/CodeSniffer/Standards/PHPCompatibility/Sniffs/PHP/DeprecatedFunctionsSniff.php on line 23”

        Still happens if you are using the direct download link. Can you upload the updated file, if possible? I try using the git, but it says “repostory not found” (I’m not familiar with the program, so maybe I’m doing something wrong, but it would be helpful if I could have the updated direct download link).

        Thank you.

  15. Hi! Thanx for compatibility ! But in my case it not working ((
    I put PHPCompatibility in standards directory , but when ‘phpcs -i’ i have only – ‘The installed coding standards are MySource, PSR1, Zend, PHPCS, Squiz, PSR2 and PEAR’ (((((

  16. Hi,

    after spending yesterday trying to install it (pear is still not working) I would like to know ho to install it manually:
    Where should I Place the Folder?
    Where can I get the folder I need (I tried the links given, but was not able to get anything to work) ?
    How to run it?

    Thanks for your help.

  17. Is there a way to check for errors that appear in PHP 5.4 when using PHP 5.5 syntax about return values in write context.
    Typically, if I use something like this:
    if (!empty(someFunction())) {

    }
    I will get this type of error:
    Fatal error: Can’t use function return value in write context in …(file.php)… on line x

    Apparently, phpcs doesn’t catch that. Did you know?

Leave a Reply to Ali Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.