wimg/php-compatibility

A set of sniffs for PHP_CodeSniffer that checks for PHP version compatibility.

Installs: 275 455

Dependents: 30

Suggesters: 0

Stars: 701

Watchers: 60

Forks: 107

Open Issues: 36

Type:phpcodesniffer-standard

8.0.1 2017-08-07 19:39 UTC

README

Latest Stable Version Latest Unstable Version Awesome License Flattr this git repo

Build Status Scrutinizer Code Quality Coverage Status Dependency Status

Tested Runtime Badge Not Tested Runtime Badge

This is a set of sniffs for PHP CodeSniffer that checks for PHP version compatibility. It will allow you to analyse your code for compatibility with higher and lower versions of PHP.

PHP Version Support

The project aims to cover all PHP compatibility changes introduced since PHP 5.0 up to the latest PHP release. This is an ongoing process and coverage is not yet 100% (if, indeed, it ever could be). Progress is tracked on our Github issue tracker.

Pull requests that check for compatibility issues in PHP 4 code - in particular between PHP 4 and PHP 5.0 - are very welcome as there are still situations where people need help upgrading legacy systems. However, coverage for changes introduced before PHP 5.1 will remain patchy as sniffs for this are not actively being developed at this time.

Requirements

  • PHP 5.3+ for use with PHP CodeSniffer 1.x and 2.x.
  • PHP 5.4+ for use with PHP CodeSniffer 3.x.

PHP CodeSniffer: 1.5.6, 2.2.0+ or 3.0.2+.

The sniffs are designed to give the same results regardless of which PHP version you are using to run PHP CodeSniffer. You should get reasonably consistent results independently of the PHP version used in your test environment, though for the best results it is recommended to run the sniffs on PHP 5.4 or higher.

PHP CodeSniffer 1.5.6 is required for 90% of the sniffs, PHP CodeSniffer 2.6.0 or later is required for full support, notices may be thrown on older versions.

As of version 8.0.0, the PHPCompatibility standard can also be used with PHP CodeSniffer 3.x.

Thank you

Thanks to all contributors for their valuable contributions.

WPEngine

Thanks to WP Engine for their support on the PHP 7.0 sniffs.

⚠️ Upgrading to PHPCompatibility 8.0.0 ⚠️

As of version 8.0.0, the installation instructions have changed. For most users, this means they will have to run a one-time-only extra command, make a change to their Composer configuration and/or adjust their build scripts.

Please read the changelog for version 8.0.0 carefully before upgrading.

Installation in a Composer project (method 1)

  • Add the following lines to the require-dev section of your composer.json file.
    "require-dev": {
        "squizlabs/php_codesniffer": "^2.2 || ^3.0.2",
        "wimg/php-compatibility": "*"
    },
    "prefer-stable" : true
  • Next, PHP CodeSniffer has to be informed of the location of the standard.
    • If PHPCompatibility is the only external PHP CodeSniffer standard you use, you can add the following to your composer.json file to automatically run the necessary command:

      "scripts": {
          "post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wimg/php-compatibility",
          "post-update-cmd" : "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wimg/php-compatibility"
      }
    • Alternatively - and strongly recommended if you use more than one external PHP CodeSniffer standard - you can use any of the following Composer plugins to handle this for you.

      Just add the Composer plugin you prefer to the require-dev section of your composer.json file.

    • As a last alternative in case you use a custom ruleset, and only if you use PHP CodeSniffer version 2.6.0 or higher, you can tell PHP CodeSniffer the path to the PHPCompatibility standard by adding the following snippet to your custom ruleset:

      <config name="installed_paths" value="vendor/wimg/php-compatibility" />
  • Run composer update --lock to install both PHP CodeSniffer, the PHPCompatibility coding standard and - optionally - the Composer plugin.
  • Verify that the PHPCompatibility standard is registered correctly by running ./vendor/bin/phpcs -i on the command line. PHPCompatibility should be listed as one of the available standards.
  • Now you can use the following command to inspect your code:
    ./vendor/bin/phpcs -p . --standard=PHPCompatibility

Installation via a git check-out to an arbitrary directory (method 2)

  • Install PHP CodeSniffer via your preferred method.

    PHP CodeSniffer offers a variety of installation methods to suit your work-flow: Composer, PEAR, a Phar file, zipped/tarred release archives or checking the repository out using Git.

    Pro-tip: Register the path to PHPCS in your system $PATH environment variable to make the phpcs command available from anywhere in your file system.

  • Download the latest PHPCompatibility release and unzip/untar it into an arbitrary directory.

    You can also choose to clone the repository using git to easily update your install regularly.

  • Add the path to the directory in which you placed your copy of the PHPCompatibility repo to the PHP CodeSniffer configuration using the below command from the command line:

    phpcs --config-set installed_paths /path/to/PHPCompatibility

    I.e. if you placed the PHPCompatibility repository in the /my/custom/standards/PHPCompatibility directory, you will need to add that directory to the PHP CodeSniffer installed_paths configuration variable.

    Warning: ⚠️ The installed_paths command overwrites any previously set installed_paths. If you have previously set installed_paths for other external standards, run phpcs --config-show first and then run the installed_paths command with all the paths you need separated by comma's, i.e.:

    phpcs --config-set installed_paths /path/1,/path/2,/path/3

    Pro-tip: Alternatively, in case you use a custom ruleset and only if you use PHP CodeSniffer version 2.6.0 or higher, you can tell PHP CodeSniffer the path to the PHPCompatibility standard(s) by adding the following snippet to your custom ruleset:

    <config name="installed_paths" value="/path/to/PHPCompatibility" />
  • Verify that the PHPCompatibility standard is registered correctly by running phpcs -i on the command line. PHPCompatibility should be listed as one of the available standards.

  • Now you can use the following command to inspect your code:

    phpcs -p . --standard=PHPCompatibility

Sniffing your code for compatibility with specific PHP version(s)

  • Run the coding standard from the command-line with phpcs -p . --standard=PHPCompatibility.
  • By default, you will only receive notifications about deprecated and/or removed PHP features.
  • To get the most out of the PHPCompatibility standard, you should specify a testVersion to check against. That will enable the checks for both deprecated/removed PHP features as well as the detection of code using new PHP features.
    • You can run the checks for just one specific PHP version by adding --runtime-set testVersion 5.5 to your command line command.
    • You can also specify a range of PHP versions that your code needs to support. In this situation, compatibility issues that affect any of the PHP versions in that range will be reported: --runtime-set testVersion 5.3-5.5.
    • As of PHPCompatibility 7.1.3, you can omit one part of the range if you want to support everything above or below a particular version, i.e. use --runtime-set testVersion 7.0- to run all the checks for PHP 7.0 and above.

More information can be found on Wim Godden's blog.

Using a custom ruleset

Like with any PHP CodeSniffer standard, you can add PHPCompatibility to a custom PHP CodeSniffer ruleset.

<?xml version="1.0"?>
<ruleset name="Custom ruleset">
    <description>My rules for PHP CodeSniffer</description>

    <!-- Run against the PHPCompatibility ruleset -->
    <rule ref="PHPCompatibility"/>

    <!-- Run against a second ruleset -->
    <rule ref="PSR2"/>

</ruleset>

You can also set the testVersion from within the ruleset:

    <!-- Check for cross-version support for PHP 5.6 and higher. -->
    <config name="testVersion" value="5.6-"/>

Other advanced options, such as changing the message type or severity of select sniffs, as described in the PHPCS Annotated ruleset wiki page are, of course, also supported.

PHPCompatibility specific options

At this moment, there is one sniff which has a property which can be set via the ruleset. More custom properties may become available in the future.

The PHPCompatibility.PHP.RemovedExtensions sniff checks for removed extensions based on the function prefix used for these extensions. This might clash with userland functions using the same function prefix.

To whitelist userland functions, you can pass a comma-delimited list of function names to the sniff.

    <!-- Whitelist the mysql_to_rfc3339() and mysql_another_function() functions. -->
    <rule ref="PHPCompatibility.PHP.RemovedExtensions">
        <properties>
            <property name="functionWhitelist" type="array" value="mysql_to_rfc3339,mysql_another_function"/>
        </properties>
    </rule>

This property was added in PHPCompatibility version 7.0.1. As of PHPCompatibility version 8.0.0, this custom property is only supported in combination with PHP CodeSniffer > 2.6.0 due to an upstream bug (which was fixed in PHPCS 2.6.0).

License

This code is released under the GNU Lesser General Public License (LGPL). For more information, visit http://www.gnu.org/copyleft/lesser.html