Skip to main content

PHPStan - PHP static analysis tool

https://phpstan.org/user-guide/getting-started

PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.

Vortex comes with a pre-configured PHPStan ruleset for Drupal projects.

Usage

ahoy lint-be
note

PHPStan does not fix code. It only reports errors. To fix errors, use Rector.

➡️ See Rector

Configuration

See configuration reference.

All global configuration takes place in the phpstan.neon file.

The analysis runs at level 7 against the PHP version pinned in the composer.json key config.platform.php, and checks with the Drupal context in mind thanks to mglaman/phpstan-drupal.

Targets include custom modules and themes, settings, tests and scripts.

Adding or removing targets:

parameters:
paths:
- path/to/dir_or_file
excludePaths:
- path/to/exclude/all_dir_files/*
- path/to/exclude/a_file.php

Ignoring

Ignoring rules globally takes place in the phpstan.neon file:

parameters:
ignoreErrors:
- # Comment about why this rule is excluded.
# 'message' is a regular expression with `#` as begin and end delimiters.
message: '#.*no value type specified in iterable type array.#'
paths:
- path/to/exclude/all_dir_files/*
- path/to/exclude/a_file.php

PHPStan does not support ignoring all rules within a file, ignoring a specific rule within a file, or ignoring code blocks - inline suppression is per-line only.

To ignore only the next line:

// @phpstan-ignore-next-line
$a = 1;

Continuous integration

PHPStan runs in the lint job of the continuous integration pipeline and fails the build on violations.

Ignoring failures

Set the VORTEX_CI_PHPSTAN_IGNORE_FAILURE environment variable to 1. The tool still runs and reports violations.

➡️ See Continuous integration > Ignore tool failures