Skip to main content

PHPStan in GitLab

Updated 1 min readMarkdown
Part of seriesLaravel in Gitlab
PHPStan in GitLab

Let's get PHPStan working with Gitlab and code quality.

In your .gitlab-ci.yml add a new section:

phpstan:
  stage: testing
  needs:
  - composer
  script:
  - php -d memory_limit=-1 vendor/bin/phpstan analyse -c phpstan.neon --error-format gitlab > phpstan-report.json
  artifacts:
    paths:
    - "phpstan-report.json"
    expire_in: 14 days
    when: always
    reports:
      codequality: "./phpstan-report.json"

If your phpstan config isn't located in phpstan.neon change the portion after the -c to be the correct file. You might also need to change the stage: testing to match with your stages setup.

One thing we've run into a few times with our main app was that PHPStan would fail on Gitlab but be passing on our local machines. While we still have no idea what causes this issue we added a manual job to run for these times:

phpstanmanual:
  stage: testing
  needs:
  - composer
  script:
  - php -d memory_limit=-1 vendor/bin/phpstan analyse -c phpstan.neon
  when: manual

After it is completed running, click on the specific job to view what the errors are.

Thanks, for reading, please let me know if you found this helpful or have questions over on Twitter: https://twitter.com/guywarner801

About the author

  • Guy Warner is a Salt Lake City software engineer with 20+ years building PHP/MySQL/AWS SaaS products, with 10+ years of Laravel in production. He was Chief Technology Officer (CTO) and VP of Development at Monitorbase in credit monitoring, handling 7M+ monthly queries, where he led 12+ developers and migrated to SingleStore. He founded Eco Moving Utah, a moving tote rental business, and builds Bin X, rental business software. He has a B.S. in Political Science, University of Utah.

Does it show the results per file in the gitlab UI? Is this working for regular commits or only for pull requests?

It does show the per file on requests:

image.png

It also runs for each commit but has no UI outside of a request which is why we added the manual run

1

More from this blog