Skip to content

How to install and setup PHP CS Fixer

Tom de Wit edited this page Feb 9, 2021 · 3 revisions

PHP-CS-Fixer

https://cs.symfony.com or https://github.com/FriendsOfPHP/PHP-CS-Fixer

Installation

composer require friendsofphp/php-cs-fixer --dev

Important note

According to https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5352#issuecomment-743149029 PHP-CS-Fixer's GitLab format doesn't respect the lines, so they are always set to 0.

CLI usage

./vendor/bin/php-cs-fixer fix --allow-risky=yes --format=gitlab --dry-run > php-cs-fixer.json

We run PHP-CS-Fixer according to it's .php_cs file. We output the GitLab formatted output to a JSON-file called php-cs-fixer.json. We use the --dry-run option so no files are changed, only highlighted.

GitLab CI/CD configuration

PHP-CS-Fixer:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/php-cs-fixer fix --allow-risky=yes --format=gitlab --dry-run > php-cs-fixer.json
  artifacts:
    paths:
      - php-cs-fixer.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-CS-Fixer 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 phpstan.json.