PHPCS is a great tool to help you make sure the correct coding style is being followed within a project. The problem is that some times the build in standards don’t cover all your needs so it is necessary to build custom rules. I will walk through the process I followed to create my own coding standard based on the build-in Zend standard.

Creating your work environment

In Ubuntu all PHPCS standards are stored in /usr/share/php/PHP/CodeSniffer/Standards/. There is a folder for each standard named after the standard name. I will call my standard SF1, so I will create a folder with that name.

cd /usr/share/php/PHP/CodeSniffer/Standards/
sudo mkdir Soonick
sudo chmod 777 Soonick

Now we can start working.

Extending a standard

Create a ruleset.xml file inside your standard folder. I want my standard to extend the Zend standard so to get started I will do just that.

<?xml version="1.0"?>
<ruleset name="SF1">
    <description>Coding standard based on PSR1 with some additions from PSR2 and indent rules of 2 spaces</description>

     <!-- Include the whole PSR1 standard -->
    <rule ref="PSR1"/>

    <!-- All PHP files MUST use the Unix LF (linefeed) line ending. -->
    <rule ref="Generic.Files.LineEndings">
        <properties>
            <property name="eolChar" value="\n"/>
        </properties>
    </rule>

    <!-- Code MUST use an indent of 2 spaces, and MUST NOT use tabs for indenting. -->
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="2"/>
        </properties>
    </rule>
    <rule ref="Generic.WhiteSpace.DisallowTabIndent"/>

    <!-- All PHP files MUST end with a single blank line. -->
    <!-- checked in Files/EndFileNewlineSniff -->

    <!-- The closing ?> tag MUST be omitted from files containing only PHP. -->
    <rule ref="Zend.Files.ClosingTag"/>

    <!-- There MUST NOT be trailing whitespace at the end of non-blank lines. -->
    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
        <properties>
            <property name="ignoreBlankLines" value="true"/>
        </properties>
    </rule>
    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile">
        <severity>0</severity>
    </rule>
    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndFile">
        <severity>0</severity>
    </rule>
    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
        <severity>0</severity>
    </rule>

    <!-- There MUST NOT be more than one statement per line. -->
    <rule ref="Generic.Formatting.DisallowMultipleStatements"/>

    <!-- 2.5 Keywords and True/False/Null -->

    <!-- PHP keywords MUST be in lower case. -->
    <rule ref="Generic.PHP.LowerCaseKeyword"/>

    <!-- The PHP constants true, false, and null MUST be in lower case. -->
    <rule ref="Generic.PHP.LowerCaseConstant"/>

    <!-- 4.3 Methods -->

    <!-- Visibility MUST be declared on all methods. -->
    <rule ref="Squiz.Scope.MethodScope"/>
    <rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>

    <!-- 4.4 Method Arguments -->

    <!-- In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. -->
    <rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
        <properties>
            <property name="equalsSpacing" value="1"/>
        </properties>
    </rule>
    <rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint">
        <severity>0</severity>
    </rule>

    <!-- Method arguments with default values MUST go at the end of the argument list. -->
    <rule ref="PEAR.Functions.ValidDefaultValue"/>
</ruleset>

Pour les projets SF1, PFS :

Indentation de 2 espaces Bracket à la ligne tout le temps