Skip to content

How to install and setup PHP_CodeSniffer

Tom de Wit edited this page Dec 11, 2020 · 2 revisions

PHP_CodeSniffer

https://github.com/squizlabs/PHP_CodeSniffer

Installation

composer require squizlabs/php_codesniffer --dev

CLI usage

./vendor/bin/phpcs app --basepath=. --report=json > phpcs.json

We run PHP_CodeSniffer from the project root and set the base path to .. We output the result to a JSON-file called phpcs.json. Setting the --basepath option helps with improved filenames and their readability.

GitLab CI/CD configuration

PHP_CodeSniffer:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/phpcs app --basepath=. --report=json > phpcs.json
  artifacts:
    paths:
      - phpcs.json
    when: always

Earlier in the configuration file you've defined the "Code quality" stage. Since quality assurance tools can fail we allow this job to fail to signal there are PHP_CodeSniffer errors. We then run the aforementioned script and save the output. Since GitLab, by default, doesn't store it's artifacts on failure, we overrule it's setting by always uploading phpcs.json.

Clone this wiki locally