PHP Code Sniffer configuration

Please note that PHP Code Sniffer is in beta version and not enabled by default. If you'd like to try it out, please contact us at support@codeac.io.

PHPCS requires configuration via a phpcs.xml configuration file as detailed in PHP Code Sniffer's documentation.

If you don't specify your custom configuration file we use our default below. We suggest to add your own configuration file into your repository root directory to be able to specify your coding and quality standards.

phpcs.xml

<ruleset name="coding-style">
    <description>Coding style description</description>

    <!-- 2. Files -->

    <!-- 2.1. PHP Tags -->

    <!-- PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations. -->
    <rule ref="Generic.PHP.DisallowShortOpenTag.EchoFound">
        <severity>0</severity>
    </rule>

    <!-- 2.2. Character Encoding -->

    <!-- PHP code MUST use only UTF-8 without BOM. -->
    <rule ref="Generic.Files.ByteOrderMark"/>

    <rule ref="Generic.PHP.DisallowShortOpenTag"/>
    <rule ref="Generic.Commenting.Todo"/>
    <rule ref="Generic.ControlStructures.InlineControlStructure"/>

    <!-- There MUST NOT be more than one statement per line. -->
    <rule ref="Generic.Formatting.DisallowMultipleStatements"/>
    <rule ref="Generic.Formatting.SpaceAfterCast"/>
    <rule ref="Generic.NamingConventions.ConstructorName"/>
    <rule ref="Generic.PHP.DeprecatedFunctions"/>
    <rule ref="Generic.Strings.UnnecessaryStringConcat"/>

    <!-- Class constants MUST be declared in all upper case with underscore separators. -->
    <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>

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

    <!-- Class names MUST be declared in StudlyCaps. -->
    <rule ref="Squiz.Classes.ValidClassName"/>

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

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

Ignoring files

Storing generated or third party code in your repository is in general not a good idea. However, sometimes you really need to do this and Codeac got you covered. Usually you don't want to analyze those files so you can place .phpcsignore file to the root folder of your repository. This file can contain regular expressions that are matched with paths in your project and excluded from the analysis. The syntax is identical with popular gitignore format.

Take a look on this simple example with Composer and Protocol Buffers:

.phpcsignore

# 3rd party libraries
vendor
composer.phar

# Protocol Buffers
app/protobuff

If you are dealing with any special type of project structure, please, let us know. We are happy to help you.