about
PHP CodeSniffer is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers. It is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations.
goal
More consistent indentation, spacing, and formatting.
So your team will have less conflicts from git, and a more consistent code base to maintain and grow.
install
install via composer
> php composer.phar require --dev squizlabs/php_codesniffer:3.*
or edit your composer.json and add
"require-dev": {
"squizlabs/php_codesniffer": "3.*"
}
and then
> composer install
configure
create a config file where you composer.json is located
phpcs.xml
example of PHP CodeSniffer's phpcs.xml
But that is overly verbose. A simpler config using PSR12 as a base rule, plus adding in your app dirs, excluding some shared libs/dirs, and some rules exclusions due to your app might be:
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description>PHP Code Sniffer configuration file.</description>
<!-- https://github.com/squizlabs/PHP_CodeSniffer -->
<!-- check all these dirs/files -->
<file>app</file>
<file>bin</file>
<file>cfg</file>
<file>src</file>
<!-- but don't check these -->
<exclude-pattern>composer$</exclude-pattern>
<exclude-pattern>public$</exclude-pattern>
<!-- phpcs argument options -->
<arg name="basepath" value="./"/>
<arg name="colors"/>
<arg name="tab-width" value="4"/>
<arg name="extensions" value="php,js,css"/>
<!-- how many files to check at once -->
<arg name="parallel" value="10"/>
<!-- base rule: set to PSR12-->
<!-- https://www.php-fig.org/psr/psr-12/ -->
<!-- https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties -->
<rule ref="PSR12">
<!-- add any exclusions here -->
</rule>
<!-- Don't hide tokenizer exceptions -->
<rule ref="Internal.Tokenizer.Exception">
<type>error</type>
</rule>
<!-- require 4 spaces, css -->
<rule ref="Squiz.CSS.Indentation">
<properties>
<property name="indent" value="4" />
</properties>
</rule>
<!-- lines can be lineLimit chars long (warnings), errors at absoluteLineLimit chars -->
<rule ref="Generic.Files.LineLength">
<properties>
<!-- 120 is PSR12; cannot be 0; large for sql, arrays -->
<property name="lineLimit" value="360"/>
<!-- 0 to not show as error -->
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
<!-- ban some functions -->
<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>
<property name="forbiddenFunctions" type="array">
<element key="sizeof" value="count"/>
<element key="delete" value="unset"/>
<element key="print" value="echo"/>
<element key="is_null" value="null"/>
<element key="create_function" value="null"/>
</property>
</properties>
</rule>
</ruleset>
Now setup PHP CodeSniffer for some ides
results
A large amount of the corrections will probably be a mix of tabs vs spaces (use 4 spaces).
If auto correct a whole file, recheck it for unintended indentation (and fix), and functionality.
Only auto correct what you will verify and test ie not the whole app.
Commit auto corrects separately from fixes, so can see fixes in git diffs easier.
Your team should now have less conflicts from git, and a more consistent code base to maintain and grow.
-End of Document-
Thanks for reading
No comments:
Post a Comment