PHP CodeSniffer

From XennisWiki
Jump to: navigation, search

'PHP CodeSniffer checks PHP code.

Installation

Use Composer to install it

composer require "squizlabs/php_codesniffer=*"

Usage

Create a rule set

Create file ruleset.xml with the rules, for instance

<?xml version="1.0"?>
<ruleset name="MyStandard">
    <description>My custom coding standard.</description>
    <rule ref="PEAR">
        <exclude name="Generic.Commenting.DocComment.MissingShort"/>
        <exclude name="PEAR.Commenting.ClassComment.Missing"/>
        <exclude name="PEAR.Commenting.FileComment.Missing"/>
        <exclude name="PEAR.Commenting.FunctionComment.Missing"/>
        <exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
        <exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
        <exclude name="PEAR.NamingConventions.ValidFunctionName.PrivateNoUnderscore"/>
    </rule>
    <rule ref="PSR1">
        <exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
    </rule>
    <rule ref="PSR2">
        <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
    </rule>
    <rule ref="Zend">
         <exclude name="Generic.Files.LineLength"/>
    </rule>
    <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="4"/>
            <property name="tabIndent" value="true"/>
        </properties>
    </rule>
</ruleset>

Ignore errors inside a file

Add a comment

// phpcs:disable PEAR.Commenting.FileComment.WrongStyle
$somePhpCode = 'here';
// phpcs:enable

Run it

Run it on src directory

phpcs --standard=ruleset.xml -s src/

See also

External links