Skip to main content

Command Palette

Search for a command to run...

PHPStan in GitLab

Published
1 min read
PHPStan in GitLab
G

I am a self-taught developer currently in Salt Lake City. Currently, work in Laravel but have experience in plain PHP, PERL, and CakePHP.

Entrepreneurial VP of tech and CTO with a strong grasp of both the business and engineering. PHP developer for 10 years with experience in CakePHP and Laravel

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

L

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

G

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

Laravel & PHP by Guy Warner

18 posts

Self taught programmer -> Freelancer -> Senior Developer -> CTO

Laravel, PHP and all that comes with it.