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= --runtime-set testVersion 5.4 

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 ;-)

ShareFlattr the authorTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

flattr this!

Tags: , , , , , , ,

6 Responses to “PHPCompatibility update”

  1. Sanjay Rakholiya says:

    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. Ken Guest says:

    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.

    • wimg says:

      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. Simon says:

    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 :)

    • wimg says:

      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

Leave a Reply