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 pre-configured PHPStan ruleset for Drupal projects.

Usage

vendor/bin/phpstan

or

ahoy lint-be
note

PHPStan does not fix code. It only reports errors. To fix errors, use Rector. See Rector for more information.

Configuration

See configuration reference.

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

By default, PHPStan will check with the Drupal context in mind thanks to mglaman/phpstan-drupal.

Targets include custom modules and themes, settings and tests.

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 rules 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 of all PHPStan rules within a file.

PHPStan does not support ignoring of a specific rule within a file.

PHPStan does not support ignoring of the code blocks.

To ignore only the current and the next line:

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

Ignoring fail in CI

This tool runs in CI by default and fails the build if there are any violations.

Set VORTEX_CI_PHPSTAN_IGNORE_FAILURE environment variable to 1 to ignore failures. The tool will still run and report violations, if any.