PHP Code Sniffer

PHP_CodeSniffer tokenises PHP, Javascript and CSS files and detects violations of a defined set of coding standards.

http://pear.php.net/package/PHP_CodeSniffer

Configuring PHP Code Sniffer

If you have a configuration file phpcs.xml or phpcs.xml.dist in your repository, you can easily configure PHP_CodeSniffer in your configuration as follows:

build:
  nodes:
    analysis:
      tests:
        override:
        - phpcs-run

        # If you have a phpcs.xml in a sub-folder
        - phpcs-run --standard=build/sub/folder/phpcs.xml build/sub/folder

phpcs-run is a built-in wrapper for PHP_CodeSniffer which takes care of results and output format, however all the options of PHP_CodeSniffer are supported as well.

Using a Specific PHP Code Sniffer Version

If you would like to use a specific PHP CodeSniffer version, you can add it as a dev dependency to your project, the phpcs-run wrapper will automatically start using the bundled version.

Alternatively, you can also install the version as part of the build process:

build:
  nodes:
    analysis:
      dependencies:
        after:
        - composer require --dev "squizlabs/php_codesniffer=3.2.2"

Configuration via the Website

For PHP CodeSniffer, we offer two configuration modes:

  1. via a phpcs.xml configuration file in the root folder of your project (default).
  2. Alternatively, via our website config editor.

If you would like to use the website-based configuration, it has to be enabled in your configuration file like this:

build:
  nodes:
    analysis:
      tests:
        override:
          - command: phpcs-run
            use_website_config: true              # use configuration from website editor

Framework-Specific Standards

Generally, you can use any PHP CodeSniffer Coding Standard. If you require a custom standard, we recommend to use a phpcs.xml file and set use_website_config: false in your configuration.

You can install your own coding standards for example as part of your composer's dev dependencies.

Problems with Drupal Coding Standard

The Drupal coding standard does not yet support the newest version of PHP CodeSniffer.

As a result, you need to explicitly configure the 2.9 branch of PHP CodeSniffer:

build:
  nodes:
    analysis:
      dependencies:
        after:
          - composer require --dev squizlabs/php_codesniffer:2.9

      tests:
        override:
          -
            command: phpcs-run
            use_website_config: false