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.
- phpcs-spaces.xml - Contains all sniffs with space indentation
- phpcs-tabs.xml - Contains all sniffs with tabs indentation
- phpcs-strict.xml - Contains extra, more strict rules based on Slevomat rules
It can be handy to not include all sniffs at once, specially when migrating big project. Files above just including these partial files:
- Parts/phpcs-psr.xml - PSR2 + PSR12 standards
- Parts/phpcs-use-spaces.xml or Parts/phpcs-use-tabs.xml - Include only one file on the indentation preferences
- Parts/phpcs-generic.xml - Additional Generic rules
- Parts/phpcs-pear.xml - Additional PEAR rules
- Parts/phpcs-squiz.xml - Additional Squiz rules
- Parts/phpcs-slevomat.xml - Selected Slevomat rules
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"/>
</property>
</properties>
</rule>
Explanation can be found here #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"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"> <!-- Check type hint for class property -->
<properties>
<property name="enableMixedTypeHint" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint"> <!-- Correct return type hint -->
<properties>
<property name="enableMixedTypeHint" value="true"/>
</properties>
</rule>