PHPCompatibility update

Update (Nov 3, 2013) : changed the parameters to reflect the new changes in PHP_CodeSniffer

Just a quick post about the updates made to PHPCompatibility.

I did a talk on PHP 5.4/5.5 at Zendcon, not just about what’s new and why you should upgrade, but also about the way PHPCompatibility can ease the pain. I received very good feedback, as well as very interesting questions. As a result, I added a feature I’ve wanted to add for a long time : the option to specify which PHP version to check for.

Here’s the caveat : it requires a change to PHP_CodeSniffer. I’m hoping that change will be accepted soon. From that point onwards, if you have the latest PHP_CodeSniffer (you might want to get it from Github).

So then you will be able to do :

phpcs --standard=PHPCompatibility --runtime-set testVersion 5.4 <path-of-your-php-files>

You can also test for backwards compatibility of your code by specifying versions like 5.2, which causes it to check whether you’re using functions or keywords not known to this older version. Quite nice if you’re coding on PHP 5.5, but your system engineer tells you half way through the project the project will have to be deployed on 5.2.

Important note : if you want to test backwards compatibility, it’s best to run on the latest PHP version available (5.5 right now), for the simple reason that if you have, for example, the keyword ‘yield’ in your code, PHP_CodeSniffer can only recognize it when you’re running PHP 5.5. It can only tell you it’s not available in previous versions if it actually knows what it is.

Expect more compatibility tools soon, as I have some more ideas, such as how to automatically figure out whether and where you’re still relying on register_globals and magic quotes ๐Ÿ˜‰

11 thoughts on “PHPCompatibility update”

  1. how to automatically figure out whether and where youโ€™re still relying on register_globals and magic quotes ?

    Awaiting for auto detect tool for that.

    Thanks

  2. You didn’t mention that it’s possible to use –config-set to indicate which version of PHP to scan for compatibility with ๐Ÿ˜‰

    One example of this would be:

    $ sudo phpcs –config-set testVersion 5.3

    Though why one would want to check for compatibility with only 5.3 is… odd.

    1. The reason why people would do this : if they’re coding on 5.4 or 5.5 for new projects and they need to work on an old project which is running on a 5.2 or 5.3 production server…
      –config-set is only useful if you want to set the version permanently in the PHP_CodeSniffer settings.

  3. Hey, I am running phpcs 1.5.2 and it does not like the –standard= being blank. Is there something I have to do to make this work as I would like to just check my code has nothing hidden that will break in PHP5.3 having developed on 5.5 and been told our production environment is 5.3 ๐Ÿ™‚

    1. You should specify –standard=PHPCompatibility if you want to use the PHPCompatibility standard. Otherwise you won’t be doing any checks.
      In your case the full command line would be :
      phpcs –standard=PHPCompatibility –runtime-set testVersion 5.3

  4. I used this:
    phpcs --standard=PHPCompatibility --runtime-set testVersion 5.3 /path/to/project > result.txt
    Then I got blank file.

    But when I used this:
    phpcs --standard=PHPCompatibility --runtime-set testVersion 5.4 /path/to/project > result.txt
    I got “Error: Default timezone is required since PHP 5.4” everywhere.

    I need to locate something like this code: $array = []; which is only available on PHP 5.4 and overall I need to check backward compatibility.

  5. I can’t get this to work for some reason. I’ve installed with Composer and I’m using:

    phpcs .vendor/bin/phpcs --standard=PHPCompatibility --runtime-set testVersion 5.2 ./folder-with-files

    Keeps telling me

    ERROR: option "--runtime-set" not known

  6. Hi,

    First of all, thanks a lot for the great tool!
    However, I have a question: is this tool checking also that a function that takes a reference parameter is not called by value? Because my codebase is quite big and I have no ideas now how to check it.

    Thanks!
    -Marco

Leave a Reply to wimg 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.