iodigital-com / php-code-sniffer-standard
iO PHP_CodeSniffer Standard
Installs: 15 629
Dependents: 8
Suggesters: 1
Security: 0
Stars: 1
Watchers: 5
Forks: 1
Open Issues: 2
Type:phpcodesniffer-standard
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.7 || ^1.0
- phpcompatibility/php-compatibility: ^9.3
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: ^3.6.0
Requires (Dev)
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
Conflicts
- isaac/php-code-sniffer: *
Replaces
- dev-develop
- dev-main
- v29.2.0
- v29.1.0
- 29.0.1
- 29.0.0
- dev-task/remove-php-7.4-support
- dev-fix/add-php-8.2-service-to-lando-configuration
- dev-fix/remove-version-property-from-composer.json
- dev-fix/increase-slevomat/coding-standard-dependency-to-v7.1
- dev-task/add-support-for-dealerdirect/phpcodesniffer-composer-installer-v1
- dev-task/add-php-8.3-support
- dev-feature/test-mirror
This package is auto-updated.
Last update: 2024-10-19 15:07:22 UTC
README
Extending the default PHP_CodeSniffer with iO rules
Note: Adding new phpcs-rules to this package must result in a major version update!
Installation
Require the package:
composer require --dev iodigital-com/php-code-sniffer-standard
Setup
Create a phpcs.xml
-file in the root of your project, and include the default iO ruleset:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
<arg name="cache"/>
<!-- include root folder of project , change to your framework requirements -->
<file>.</file>
<!-- exclude paths -->
<exclude-pattern>./src/Migrations</exclude-pattern>
<exclude-pattern>./vendor</exclude-pattern>
<exclude-pattern>./local-repo</exclude-pattern>
<!-- include all rules in iO ruleset -->
<rule ref="IO"/>
<rule ref="YourFramework"/> <!-- include your framework standards -->
</ruleset>
Modify the excluded paths and/or include custom rulesets for your project.
PHPCompatibility
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.
Include the testVersion by adding a config rule in your phpcs.xml
. Examples:
<config name="testVersion" value="7.0"/> <!-- check for compatability with php 7.0 -->
<config name="testVersion" value="7.1-"/> <!-- check for 7.1 and higher -->
<config name="testVersion" value="7.0-7.2"/> <!-- check within range 7.0 to 7.2 -->
Look here for more information: https://github.com/PHPCompatibility/PHPCompatibility#using-a-custom-ruleset
Usage
Since you now have a phpcs.xml
file in the root of your project, you can run the default phpcs-command: vendor/bin/phpcs
. To save yourself some time use GrumPHP or Githooks to trigger the sniffing.
Ignoring sniff violations
Sometimes a violation of a sniff cannot be resolved. In this case, the violation should be ignored using the phpcs:ignore
and phpcs:disable
/ phpcs:enable
annotations.
In order to do this, please take the following approach:
- Ignore only the parts of the file that cause the violation, not the file itself. If it is really the case the file should be ignored, you can use the
phpcs:ignoreFile
annotation or, better, add an<exclude-pattern>
to theruleset.xml
of the project. - Prefer
phpcs:ignore
overphpcs:disable
andphpcs:enable
, i.e. usephpcs:ignore
when this is possible and when the placement of thephpcs:ignore
does not introduce any other sniff violations, usephpcs:disable
andphpcs:enable
otherwise. Rationale: usingphpcs:disable
andphpcs:enable
might disable more code than initially intended when adding new code or moving existing code, for instance when refactoring code. - Always indicate the exact sniff or sniffs that are going to be ignored, use the complete sniff name, not only the sniff group. So for instance use
phpcs:ignore Squiz.WhiteSpace.FunctionSpacing.BeforeFirst, Squiz.WhiteSpace.FunctionSpacing.AfterLast
instead ofphpcs:ignore Squiz.WhiteSpace.FunctionSpacing
orphpcs:ignore
without any arguments. - Prefer placing the
phpcs:ignore
annotation on a separate line before the violation over placing it on the line of the violation itself. Rationale: when ignoring multiple sniffs, thephpcs:ignore
annotation can quickly exceed the line length limit; this is not checked when thephpcs:ignore
annotation is placed on a separate line before the violation, but it is checked when thephpcs:ignore
annotation is placed on the line of the violation itself. - Add an explanation why the sniff is ignored using
--
followed by a short explanation.
Example:
try { $this->logger->log(LogLevel::INFO, new DateTimeImmutable()); //phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- DateTimeImmutable creation cannot fail in this case } catch (Exception $exception) { }
Contributing
If you want to to contribute, create a merge request with one sniff per merge request. Please provide an example in the description of what the sniff is about with a good and bad code snippet.