The phpstan component#
This page shows how to add the phpstan component to an app's .gitlab-ci.yml: it runs the app's own PHPStan and fails the pipeline on static analysis errors, writing a GitLab Code Quality report. It finds the loose bolts before the flight does; check the requirements first, then add the include.
Version 1, category Testing/Validation, tags php, static-analysis, phpstan.
Before you start#
The component does not install PHPStan itself. The app must:
- Require
phpstan/phpstanas a dev dependency. - Ship a
phpstan.neon, or point theconfiginput at its own file.
Include the component#
Add the include to the app's .gitlab-ci.yml, pinned to a commit SHA with the tag in a trailing comment:
include:
- component: $CI_SERVER_FQDN/enterprise-apps/preflight/phpstan@fb73b538f3980292119b5b306ff64383d760a214 # 0.9.0
inputs:
config: phpstan.neon
memory_limit: 1G
A tag or branch can move; a SHA cannot.
What the job does#
The job lands in the test stage by default, is interruptible: true, and has a timeout: 10m. It runs three steps:
-
Move to the directory given by
working_dir(default.). -
Install the app's dependencies:
composer install --no-interaction --no-progress --prefer-dist -
Run PHPStan against the app's own config:
vendor/bin/phpstan analyse --no-progress --error-format=gitlab --memory-limit=1G -c phpstan.neon > "$CI_PROJECT_DIR/phpstan-report.json"--memory-limittakes thememory_limitinput (default1G).-ctakes theconfiginput (defaultphpstan.neon).--error-format=gitlabwrites the findings as a GitLab Code Quality report to$CI_PROJECT_DIR/phpstan-report.json.
Report#
The job publishes the report as a Code Quality report:
artifacts:
when: always
reports:
codequality: phpstan-report.json
Inputs#
| Input | Type | Default | What it does |
|---|---|---|---|
stage |
string | test |
Pipeline stage for the job. |
job_name |
string | phpstan |
Name of the job. Set it when you include this component more than once. |
image |
string | composer:2.11.1@sha256:8de2184c6de224f4689e8f80ed5f5edaf0030b6b8b7839a22a2bcb6722ac87d0 |
Docker image for the job. |
image_prefix |
string | ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX} |
Registry path in front of image: the job pulls image_prefix/image. The default is the group's GitLab Dependency Proxy, so Docker Hub is asked once and GitLab serves the cached copy. Outside a group with the proxy, set docker.io. No trailing slash. |
config |
string | phpstan.neon |
Path to the app's PHPStan config file. It must be a relative path; GitLab refuses the pipeline at creation if it is not. |
memory_limit |
string | 1G |
Memory limit passed to PHPStan. A PHP size such as 1G, 512M or -1; GitLab refuses the pipeline at creation if it is not. |
working_dir |
string | . |
Directory to run PHPStan in, relative to the project root. |
job_name must match ^[A-Za-z0-9_][A-Za-z0-9_.-]*$, and working_dir must match ^(\.|[A-Za-z0-9_-][A-Za-z0-9._/-]*)$.
Verify#
Push the change. The components are ordinary jobs, so the phpstan job runs whenever the app's pipeline runs, in the test stage by default. A green job means no static analysis errors, and the Code Quality report carries the findings.
Related#
- The pint component: the style check that pairs with it.
- The proctor component: another check that publishes a Code Quality report.
- Include a component twice: the
job_nameinput when one project includes the same component more than once. - Reference: every component, with its inputs and defaults.