iodigital-com/php-code-sniffer-standard
Extends PHP_CodeSniffer with iO Digital coding standards. Install via Composer, add an IO ruleset to your phpcs.xml, configure excluded paths and PHP version ranges, and run vendor/bin/phpcs. Includes guidance for properly ignoring sniff violations.
Extending the default PHP_CodeSniffer with iO rules
Require the package:
composer require --dev iodigital-com/php-code-sniffer-standard
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 -->
<!-- Configure minimum supported PHP version (used by PHP_CodeSniffer and Slevomat sniffs) -->
<config name="php_version" value="80100"/>
<!-- Configure PHP version or range of PHP versions (used by PHPCompatibility sniffs) -->
<config name="testVersion" value="8.2-8.5"/>
</ruleset>
Modify the excluded paths and PHP versions and optionally include custom rulesets for your project.
Since you now have a phpcs.xml file in the root of your project, you can run the default phpcs command: vendor/bin/phpcs.
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:
phpcs:ignoreFile annotation or, better, add an <exclude-pattern> to the ruleset.xml of the project.phpcs:ignore over phpcs:disable and phpcs:enable, i.e. use phpcs:ignore when this is possible and when the placement of the phpcs:ignore does not introduce any other sniff violations, use phpcs:disable and phpcs:enable otherwise. Rationale: using phpcs:disable and phpcs:enable might disable more code than initially intended when adding new code or moving existing code, for instance when refactoring code.phpcs:ignore Squiz.WhiteSpace.FunctionSpacing.BeforeFirst, Squiz.WhiteSpace.FunctionSpacing.AfterLast instead of phpcs:ignore Squiz.WhiteSpace.FunctionSpacing or phpcs:ignore without any arguments.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, the phpcs:ignore annotation can quickly exceed the line length limit; this is not checked when the phpcs:ignore annotation is placed on a separate line before the violation, but it is checked when the phpcs:ignore annotation is placed on the line of the violation itself.-- 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) {
}
If you want 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.
Note: Adding new phpcs rules to this package must result in a major version update!
How can I help you explore Laravel packages today?