LinuxCommandLibrary

phpstan

performs static analysis on PHP code, finding bugs without running the code

TLDR

Analyze source directory

$ phpstan analyse [src/]
copy
Analyze with specific level
$ phpstan analyse --level [5] [src/]
copy
Analyze using config file
$ phpstan analyse -c [phpstan.neon]
copy
Analyze with max level
$ phpstan analyse --level max [src/]
copy
Generate baseline
$ phpstan analyse --generate-baseline
copy
Clear cache
$ phpstan clear-result-cache
copy
Show version
$ phpstan --version
copy

SYNOPSIS

phpstan [analyse] [--level n] [-c config] [options] [paths]

DESCRIPTION

phpstan performs static analysis on PHP code, finding bugs without running the code. It catches type errors, undefined methods, and incorrect function calls.
Analysis levels range from 0 (basic) to 9 (strictest). Higher levels check more conditions but may find more false positives in legacy code. Start low and increase gradually.
Configuration files (phpstan.neon) define paths, excludes, and custom rules. They enable consistent analysis across team members and CI pipelines.
Baseline files ignore existing errors, enabling gradual adoption. New code must pass analysis while legacy issues can be fixed over time.
The tool integrates with IDEs through plugins, showing errors as you type. CI integration catches regressions before merge.
PHPStan supports PHPDoc annotations for type hints in older code. Modern PHP type declarations are preferred when possible.

PARAMETERS

analyse, analyze

Run analysis.
--level, -l N
Rule level (0-9 or max).
-c FILE
Configuration file.
--configuration FILE
Configuration file (long form).
--memory-limit SIZE
Memory limit.
--debug
Debug mode.
--generate-baseline
Generate error baseline.
-b FILE
Baseline file.
--error-format FMT
Output format (table, json, raw, etc.).
--no-progress
Disable progress bar.
clear-result-cache
Clear analysis cache.

CAVEATS

High levels may be impractical for legacy code. Some dynamic PHP patterns can't be analyzed. Extensions needed for frameworks. Memory usage scales with codebase size.

HISTORY

PHPStan was created by Ondřej Mirtes in 2016 to bring static typing benefits to PHP. It became one of the most popular PHP static analysis tools, competing with Psalm and Phan.

SEE ALSO

psalm(1), php(1), composer(1), phpcs(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community