Skip to content

Instantly share code, notes, and snippets.

@jrencz
Last active August 20, 2018 10:13
Show Gist options
  • Save jrencz/691863718babf05dfec2 to your computer and use it in GitHub Desktop.
Save jrencz/691863718babf05dfec2 to your computer and use it in GitHub Desktop.
This tutorial describes how to set PHP_CodeSniffer as the local project dependency instead of the suggested global PEAR dependency.

Setting PHP_CodeSniffer as the project dependency

This tutorial describes how to set PHP_CodeSniffer as the local project dependency instead of the suggested global PEAR dependency.

Installing PHPCS

Follow the https://github.com/squizlabs/PHP_CodeSniffer/blob/master/README.markdown

Add the following configuration to your composer.json

{
    "require-dev": {
        "squizlabs/php_codesniffer": "1.*"
    }
}

in your composer bin-dir you'll find phpcs executable

Now run phpcs against your code (assuming the code is placed in the src subdirectory):

./bin/phpcs src

You're good to go.

Using non-bundled ruleset installed locally

PHPCS comes stuffed with many rulesets. But if you wish to use your own for any reason you can do so by passing a path containing ruleset.xml (or the file itself)

--standard=path/to/the/directory/or/the/file

Cou can create your own ruleset or you may use any non-bundled one. Some rulesets may be found on Composer such as the Symfony2 coding standard. Its maintained by opensky but it's more convenient to use instaclick fork published on Packagist:

{
    "require-dev": {
        "instaclick/symfony2-coding-standard": "dev-master"
    }
}

Creating custom ruleset

You may even refine locally installed ruleset to meet your own needs by creating your own ruleset.xml. An example of extending the Symfony2 standard below:

<?xml version="1.0"?>
<ruleset name="ProjectPHPCSRules">
    <description>Custom PHPCS config based on the Symfony2 coding standard.</description>

    <rule ref="../../vendor/instaclick/symfony2-coding-standard/Symfony2" />
</ruleset>

Note that all non-bundled standards have to be provided as the path relative to the place your ruleset.xml is placed. This way the config will work both in the CLI and the IDE. You may place it anywhere in your project. If you're using Symfony 2 Standard Edition that'll probably be something like

app/Resources/ruleset.xml

Now you may use your ruleset:

./bin/phpcs --standards=app/Resources/ruleset.xml src

Running PHPCS conveniently

After creating your own ruleset you may use it by calling it with --standard option and path to your code. It'll get more and more verbose the more configuration you may want to provide (like exclusions or severity). To avoid re-typing them each time (and provide your teammates with consistent rules) it's good to create a proxy script that will contain all the required configuration. something like bin/phpcs-project:

#!/bin/bash

bin/phpcs --standard=app/Resources/ruleset.xml src

Now running

./bin/phpcs-project

is even easier.

Configuring IDE (example of PHP Storm)

Running PHPCS in the CLI is nifty - anyone (including your CI server) may do it but no one (excluding you CI server) probably would do it frequently. To enforce the coding standard you should configure your IDE to use your projects ruleset. You may do so (example of PHP Storm):

  1. go to Project Settings > PHP > Code Sniffer and set your phpcs executable. You should use installed in bin-dir.
  2. go to Project Settings > Inspections > PHP > PHP Code Sniffer validation and set Custom coding standard.
  3. use path selection ("...") to find your ruleset (as you placed it earlier in the app/Resources/reuleset.xml or the one installed in vendor). Make sure your ruleset refs are resolvable (see "Creating custom ruleset")

Now you may enjoy the same ruleset both in you IDE, your CLI and your CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment