PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

Installation

Install PHP_CodeSniffer system-wide with the following command:

composer global require squizlabs/php_codesniffer

Include a dependency for squizlabs/php_codesniffer in your composer.json file.

composer require --dev squizlabs/php_codesniffer

Configuration

<?xml version="1.0"?>
<ruleset name="Custom Standard">
    <description>The PSR-2 coding standard.</description>
    <arg name="tab-width" value="4"/>

    <!-- 2. General -->

    <!-- 2.1 Basic Coding Standard -->

    <!-- Include the whole PSR-2 standard -->
    <rule ref="PSR2" />

    <!-- 2.3 Lines -->

    <!-- The soft limit on line length MUST be 120 characters; automated style checkers MUST warn but MUST NOT error at the soft limit. -->
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="0"/>
        </properties>
    </rule>
</ruleset>

Dependencies

Ignore Line exceeds maximum limit of characters.

<?xml version="1.0"?>
<ruleset name="Custom Standard">
    <rule ref="Generic.Files.LineLength">
        <exclude name="Generic.Files.LineLength"/>
    </rule>
</ruleset>

Integration with visual studio code

Install through vscode extensions. Search for phpcs.

settings.json

{
    "php.validate.enable": false,
    "php.suggest.basic": false,
    
    "phpcs.enable": true,
    "phpcs.standard": "PSR2",
}

Enable/disable built-in PHP validation.

{
    "php.validate.enable": false,
}

Controls whether the built-in PHP language suggestions are enabled. The support suggests PHP globals and variables.

{
    "php.suggest.basic": false,
}

Control whether phpcs is enabled for PHP files or not.

{
    "phpcs.enable": true,
}

Optional. The name or path of the coding standard to use. Defaults to the one set in phpcs global config.

{
    "phpcs.standard": "PSR2",
}

Automatically search for any phpcs.xml, phpcs.xml.dist, phpcs.ruleset.xml or ruleset.xml file to use as configuration. Overrides custom standards defined above.

{
    "phpcs.autoConfigSearch": true,
}