Coding standards guide the programmers to write code that can be easily read by other developers to understand every part of it effortlessly. So WordPress invent their own coding standard to make consistent between all developer’s code. In this article, we will discuss PHP Coding Standards and how we can verify our codes using PHPCS.
Install PHPCS
We need three composer package to run the coding standard tests. Here is how my composer.json
file looks like.
{
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "*",
"wp-coding-standards/wpcs": "*",
"phpcompatibility/phpcompatibility-wp": "*"
}
}
After adding composer.json
file we need to run composer update
command to install packages in our local directory. When composer packages installed then we need to run the following command.
vendor/bin/phpcs -i
If this command output like below then everything properly installed.
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, WordPress, WordPress-Extra, WordPress-Docs, WordPress-Core, PHPCompatibilityParagonieRandomCompat, PHPCompatibilityParagonieSodiumCompat, PHPCompatibilityWP and PHPCompatibility
Now add .phpcs.xml
file to your project root directory, this file will set our standard when we will run vendor/bin/phpcs
.
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards based custom ruleset for your plugin">
<description>Generally-applicable sniffs for WordPress plugins.</description>
<!-- What to scan -->
<file>.</file>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/node_modules/</exclude-pattern>
<!-- How to scan -->
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. -->
<!-- Rules: Check PHP version compatibility -->
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.3-"/>
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP"/>
<!-- Rules: WordPress Coding Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="4.6"/>
<rule ref="WordPress"/>
<rule ref="WordPress.WP.I18n">
<properties>
<!-- Value: replace the text domain used. -->
<property name="text_domain" type="array" value="my-plugin"/>
</properties>
</rule>
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">
<properties>
<property name="blank_line_check" value="true"/>
</properties>
</rule>
</ruleset>
Now run coding test using the PHPCS command.
vendor/bin/phpcs
Various Tweaks
Ignore a single line:
You can ignore a single line using the phpcs:ignore
comment. This comment will ignore the line that the comment is on.
// phpcs:ignore
$foo = [1,2,3];
bar($foo, false);
$foo = [1,2,3]; // phpcs:ignore
bar($foo, false);
Ignoring parts of a file:
Sometimes have to break coding standard, To stop PHPCS generating errors for our code, we can wrap it in special comments.
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable
Exclude any rule globally:
Let’s change a rule for WordPress standard globally through .phpcs.xml
<rule ref="WordPress">
<exclude name="WordPress.Files.FileName" />
</rule>