Skip to main content

PHPStan is now running for all Drupal modules on GitLab CI!

Published on

The Drupal Association has been working on a monumental effort to migrate away from our bespoke DrupalCI continuous integration system to GitLab CI as part of the GitLab Acceleration Initiative. Drupal core's test runs are five times faster using GitLab CI. I have loosely followed the progress as Drupal moves from our custom-built infrastructure onto GitLab. But someone shared with me a little feature I missed: adding a PHPStan job to the default GitLab CI templates!

Fran Garcia-Linares (fjgarlin) is the engineer from the Drupal Association who has been working on the GitLab CI templates. GitLab supports templates to allow reusing configuration for continuous integration workflows. The new phpstan job does a handful of things, and I love its approach.

  • Allows modules to commit a phpstan.neon file to provide customized PHPStan configuration, such as level: 9 or specific errors to be ignored.
  • Exports errors as a JUnit report, GitLab quality report for the user interface, and terminal output for users to manually review.
  • Generates a baseline file uploaded as an artifact that can be included with the project so they can start using PHPStan and accept all existing errors to be fixed later on!

What I found very creative was the way each report has been generated. PHPStan uses a result cache to make subsequent scans faster. The phpstan job uses this to create multiple reports from the results. It runs PHPStan three times with different outputs, capturing the exit code after each job run and generating the baseline.

With most of my open source work being on phpstan-drupal or Retrofit, I haven't worked on Drupal modules that often recently. I haven't had a chance to try out GitLab CI on Drupal.org yet. I need to set aside some time to check it out!

Is your module using GitLab CI yet? If not, check out the extensive documentation: https://www.drupal.org/node/3356364/