Skip to content

How to install and setup PHPStan

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

PHPStan

https://phpstan.org or https://github.com/phpstan/phpstan

Installation

composer require phpstan/phpstan --dev

CLI usage

./vendor/bin/phpstan analyse app --level max --error-format=gitlab > phpstan.json

We run PHPStan in the app (Laravel example) folder of the project. We output the GitLab formatted output to a JSON-file called phpstan.json.

GitLab CI/CD configuration

PHPStan:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/phpstan analyse app --level max --error-format=gitlab > phpstan.json
  artifacts:
    paths:
      - phpstan.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 PHPStan 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.

Clone this wiki locally