Skip to content

Latest commit

 

History

History
219 lines (150 loc) · 5.3 KB

phpcs.md

File metadata and controls

219 lines (150 loc) · 5.3 KB

Phpcs fixer

The Phpcs task will sniff your code for bad coding standards.

Composer

composer require --dev squizlabs/php_codesniffer

Config

The task lives under the phpcs namespace and has following configurable parameters:

# grumphp.yml
grumphp:
    tasks:
        phpcs:
            standard: []
            severity: ~
            error_severity: ~
            warning_severity: ~
            tab_width: ~
            report: full
            report_width: ~
            whitelist_patterns: []
            encoding: ~
            ignore_patterns: []
            sniffs: []
            triggered_by: [php]
            exclude: []
            

standard

Default: []

This parameter will describe which standard/s is being used to validate your code for bad coding standards. By default it is set to null so that the Phpcs defaults are being used. Phpcs will be using the PEAR or local phpcs.xml standard by default. You can configure this task to use any standard supported by the Phpcs CLI. For Example: PEAR, PHPCS, PSR1, PSR2, PSR12, Squiz and Zend

You can get a list of all installed phpcs standards with the command:

phpcs -i

severity

Default: null

Global severity level that should be used by phpcs (default is 5).

error_severity

Default: null

Error severity that should be used by phpcs (default is 5).

warning_severity

Default: null

Warning severity that should be used by phpcs (default is 5).

tab_width

Default: null

By default, the standard will specify the optimal tab-width of the code. If you want to overwrite this option, you can use this configuration option.

encoding

Default: null

The default encoding used by PHP_CodeSniffer (is ISO-8859-1).

report

Default: full

The report type output by PHP_CodeSniffer, put code to see a code snippet of the offending code. Consult the complete list for more formats.

report_width

Default: null

PHP_CodeSniffer will print all screen-based reports 80 characters wide. You may override this size so that long lines do not wrap.

whitelist_patterns

Default: []

This is a list of regex patterns that will filter files to validate. With this option you can skip files like tests. This option is used in relation with the parameter triggered_by. For exemple to validate only files in your src/App/ and src/AppBundle/ directories in a Symfony you can use

whitelist_patterns:
  - /^src\/App\/(.*)/
  - /^src\/AppBundle\/(.*)/

ignore_patterns

Default: []

This is a list of patterns that will be ignored by phpcs. With this option you can skip files like tests. Leave this option blank to run phpcs for every php file.

sniffs

Default: []

This is a list of sniffs that need to be executed. Leave this option blank to run all configured sniffs for the selected standard.

triggered_by

*Default: [php]:

This is a list of extensions to be sniffed. This list is also passed to phpcs using the --extensions parameter.

exclude

Default: []

A list of rules that should not be checked. Leave this option blank to run all configured rules for the selected standard.

Framework presets

Symfony 2

If you want to use Phpcs for your Symfony2 projects, you can require the leanpub phpcs repo.

composer require --dev leaphub/phpcs-symfony2-standard

Following this, you can add the path to your phpcs task.

# grumphp.yml
grumphp:
    tasks:
        phpcs:
            standard: "vendor/leaphub/phpcs-symfony2-standard/leaphub/phpcs/Symfony2/"

Magento

If you want to use Phpcs for your Magento projects, you can require the magento-ecg repo ( works with Magento 1 and Magento 2 ).

composer require --dev magento-ecg/coding-standard

Following this, you can add the path to your phpcs task.

# grumphp.yml
grumphp:
    tasks:
        phpcs:
            standard: "vendor/magento-ecg/coding-standard/Ecg/"
            warning_severity: 0

For Magento 2 projects, you can require the Magento Coding Standard repo.

composer require --dev magento/magento-coding-standard

Next, add the following to your composer.json file, and run composer run-script post-install-cmd.

"scripts": {
    "post-install-cmd": "vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/",
    "post-update-cmd": "vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/"
}

Following this, you can add the path to your phpcs task.

# gumphp.yml
grumphp:
    tasks:
        phpcs:
            standard: "Magento2"

Drupal

If you want to use Phpcs for your Drupal projects, you can require the Drupal Code Sniffer (Coder)

composer require --dev drupal/coder

Following this, you can add the path to your phpcs task.

# grumphp.yml
grumphp:
    tasks:
        phpcs:
            standard: 
              - vendor/drupal/coder/coder_sniffer/Drupal
              - vendor/drupal/coder/coder_sniffer/DrupalPractice
            ignore_patterns:
              - cfg/
              - libraries/
            triggered_by:
              - php
              - module
              - inc