Custom ruleset of PHP CodeSniffer sniffs for linting PHP
Custom ruleset for PHP CodeSniffer with Slevomat sniffs. Ruleset is based on PSR2 + PSR12 with additional rules, more detailed list is below. Ruleset is possible to use with space indentation and also tabs indentation.
Install with composer:
composer require --dev arxeiss/coding-standards
and run CodeSniffer
./vendor/bin/phpcs --standard=./vendor/arxeiss/coding-standards/Rules/phpcs-spaces.xml .
Better way is to create custom file phpcs.xml
, with content like phpcs.example.xml. After that it is possible to run
./vendor/bin/phpcs --standard=./phpcs.xml
More info about PHP CodeSniffer can be found in the PHP CS wiki
Package consist of multiple files based on the used sniffs.
There are 3 files basic files including all selected sniffs. Use phpcs-spaces.xml or phpcs-tabs.xml, not both. Optionally it is possible to add also phpcs-strict.xml.
It can be handy to not include all sniffs at once, specially when migrating big project. Files above just including these partial files:
If building own ruleset based on partial files, always use Parts/phpcs-psr.xml, then Parts/phpcs-use-spaces.xml or Parts/phpcs-use-tabs.xml.
Later more additional rules can be added. See phpcs.example.xml.
See SniffList, I wrote one sentence explanation for each used sniff into XML files.
Also Slevomat rules have some comments about in their repository Readme https://github.com/slevomat/coding-standard
This is used from my own blog in Laravel, where main folder app
is BlogApp
namespace.
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array">
<element key="app" value="BlogApp"></element>
</property>
</properties>
</rule>
mixed
typehint is not requiredExplanation can be found here https://github.com/arxeiss/php-coding-standards/pull/6
If you want to force linter to use mixed
typehint, you can enable by adding this into phpcs.xml file.
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"> <!-- Check correct type hints -->
<properties>
<property name="enableMixedTypeHint" value="true"></property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"> <!-- Check type hint for class property -->
<properties>
<property name="enableMixedTypeHint" value="true"></property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint"> <!-- Correct return type hint -->
<properties>
<property name="enableMixedTypeHint" value="true"></property>
</properties>
</rule>