|
1 | 1 | # PHP Codesniffer Rulesets + Sniffs |
| 2 | + |
| 3 | +Using CIG PHP coding standards ruleset for PHP*CodeSniffer, you can analyse the |
| 4 | +codebase of your project for PHP compatibility with other \_itcig* projects. |
| 5 | + |
| 6 | +## What's in this repo ? |
| 7 | + |
| 8 | +A ruleset for PHP*CodeSniffer to check for PHP compatibility and standards |
| 9 | +across all \_itcig* PHP projects. |
| 10 | + |
| 11 | +This WordPress specific ruleset prevents false positives from the |
| 12 | +[PHPCompatibility standard](https://github.com/PHPCompatibility/PHPCompatibility) |
| 13 | +by excluding back-fills and poly-fills which are provided by WordPress. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +> These will all be installed automatically by this ruleset so you do not need |
| 18 | +> to include them explicitly. |
| 19 | +
|
| 20 | +- [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer). Use the |
| 21 | + latest stable release of |
| 22 | + [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) for the best |
| 23 | + results. The minimum _recommended_ version of PHP_CodeSniffer is version |
| 24 | + 3.0.2. |
| 25 | +- [PHPCompatibility](https://github.com/PHPCompatibility/PHPCompatibility) |
| 26 | + 9.0.0+. |
| 27 | + |
| 28 | +## Installation instructions |
| 29 | + |
| 30 | +The only supported installation method is via |
| 31 | +[Composer](https://getcomposer.org/). |
| 32 | + |
| 33 | +If you already have a Composer PHP_CodeSniffer plugin installed, run: |
| 34 | + |
| 35 | +```bash |
| 36 | +composer require --dev itcig/php-coding-standards:"*" |
| 37 | +composer install |
| 38 | +``` |
| 39 | + |
| 40 | +Next, run: |
| 41 | + |
| 42 | +```bash |
| 43 | +vendor/bin/phpcs -i |
| 44 | +``` |
| 45 | + |
| 46 | +If all went well, you will now see that the `CIG`, `CIG-Docs`, `CIG-Wordpress`, |
| 47 | +`CIG-Core`, `PHPCompatibility`, `PHPCompatibilityWP` and some more |
| 48 | +PHPCompatibility standards are installed for PHP_CodeSniffer. |
| 49 | + |
| 50 | +## How to use |
| 51 | + |
| 52 | +Now you can use the following command to inspect your code: |
| 53 | + |
| 54 | +```bash |
| 55 | +./vendor/bin/phpcs -p . --standard=CIG |
| 56 | +``` |
| 57 | + |
| 58 | +By default, you will only receive notifications about formatting and deprecated |
| 59 | +and/or removed PHP features. |
| 60 | + |
| 61 | +To get the most out of the CIG coding and PHPCompatibility standards, you should |
| 62 | +specify a `testVersion` to check against. That will enable the checks for both |
| 63 | +deprecated/removed PHP features as well as the detection of code using new PHP |
| 64 | +features. |
| 65 | + |
| 66 | +If you want to enforce the minimum PHP requirement, either add |
| 67 | +`--runtime-set testVersion 7.2-` to your command-line command or add |
| 68 | +`<config name="testVersion" value="7.2-"/>` to your |
| 69 | +[custom ruleset](https://github.com/PHPCompatibility/PHPCompatibility#using-a-custom-ruleset). |
| 70 | + |
| 71 | +For example: |
| 72 | + |
| 73 | +```bash |
| 74 | +# For a project which should be compatible with PHP 7.2 and higher: |
| 75 | +./vendor/bin/phpcs -p . --standard=CIG --runtime-set testVersion 7.2- |
| 76 | +``` |
| 77 | + |
| 78 | +For more detailed information about setting the `testVersion`, see the README of |
| 79 | +the generic |
| 80 | +[PHPCompatibility](https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions) |
| 81 | +standard. |
| 82 | + |
| 83 | +### Testing PHP files only |
| 84 | + |
| 85 | +By default PHP_CodeSniffer will analyse PHP, JavaScript and CSS files. As the |
| 86 | +PHPCompatibility sniffs only target PHP code, you can make the run slightly |
| 87 | +faster by telling PHP_CodeSniffer to only check PHP files, like so: |
| 88 | + |
| 89 | +```bash |
| 90 | +./vendor/bin/phpcs -p . --standard=CIG --extensions=php --runtime-set testVersion 7.2- |
| 91 | +``` |
0 commit comments