The coding standard ruleset
This specification extends and expands PSR-12, the extended coding style guide and requires adherence to PSR-1, the basic coding standard.
-
Install the module via composer by running:
$ composer require --dev ground/coding-standard
-
Add composer scripts into your
composer.json
:"scripts": { "cs-check": "phpcs", "cs-fix": "phpcbf" }
-
Create
phpcs.xml
file on base path of your repository:<?xml version="1.0"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> <arg name="basepath" value="."/> <arg name="cache" value=".phpcs-cache"/> <arg name="colors"/> <arg name="extensions" value="php"/> <arg name="parallel" value="80"/> <!-- Show progress --> <arg value="p"/> <!-- Paths to check --> <file>config</file> <file>src</file> <file>test</file> <!-- Include all rules from the Coding Standard --> <rule ref="CodingStandard"/> </ruleset>
You can add or exclude some locations in that file. For a reference please see: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset
-
To run checks only:
$ composer cs-check
-
To automatically fix many CS issues:
$ composer cs-fix
Disable parts of a file:
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable
Disable a specific rule:
// phpcs:disable Generic.Commenting.Todo.Found
$xmlPackage = new XMLPackage;
$xmlPackage['error_code'] = get_default_error_code_value();
// TODO: Add an error message here.
$xmlPackage->send();
// phpcs:enable
Ignore a specific violation:
$xmlPackage = new XMLPackage;
$xmlPackage['error_code'] = get_default_error_code_value();
// phpcs:ignore Generic.Commenting.Todo.Found
// TODO: Add an error message here.
$xmlPackage->send();
Rules can be added, excluded or tweaked locally, depending on your preferences. More information on how to do this can be found here: