phpstan
Analyze PHP code for errors
TLDR
Analyze one or more directories
Analyze a directory using a configuration file
Analyze using a specific rule level (0-10, higher is stricter)
Specify an autoload file to load before analyzing
Specify a memory limit during analysis
Display available options for analysis
SYNOPSIS
phpstan analyse [paths] [OPTIONS]
phpstan generate-baseline [file] [paths] [OPTIONS]
phpstan clear-result-cache
phpstan init
PARAMETERS
--configuration | -c <file>
Path to the phpstan.neon configuration file.
--level | -l <level>
Defines the rule level for analysis (0-9 or 'max'). Higher levels mean stricter checks.
--error-format <format>
Specifies the output format for errors (e.g., 'raw', 'pretty', 'checkstyle', 'json').
--memory-limit <limit>
Sets the PHP memory limit for the analysis process (e.g., '512M', '-1' for no limit).
--no-progress
Disables the progress bar during analysis, useful for CI/CD environments.
--clear-result-cache
Clears the analysis result cache before running analysis.
--generate-baseline <file>
Generates a baseline file to ignore existing errors, allowing new errors to be tracked for future code.
DESCRIPTION
phpstan is a static analysis tool for PHP that focuses on finding bugs in your code without actually running it. It works by analyzing the abstract syntax tree (AST) of your PHP files and performing deep type checking. This helps developers catch potential errors, such as incorrect type usage, undefined variables, or uncalled methods, early in the development cycle.
By integrating phpstan into a continuous integration (CI) pipeline, teams can significantly improve code quality, reduce runtime errors, and ensure better adherence to coding standards. It supports various levels of strictness, allowing projects to gradually adopt stricter analysis.
CAVEATS
phpstan can be memory and CPU intensive on large codebases. Its effectiveness depends on the quality of type hints and PHPDoc annotations in the code. Configuring phpstan for complex projects, especially with custom extensions or dynamic code, can sometimes be challenging and may require fine-tuning to avoid false positives or negatives.
LEVELS OF ANALYSIS
phpstan provides a system of analysis "levels" from 0 to 9 (and 'max'). Level 0 performs basic syntax checks, while level 9 (or 'max') enforces the strictest possible type checks and best practices. This allows projects to gradually increase their code quality checks without being overwhelmed by a large number of errors initially.
CONFIGURATION FILES
phpstan uses a configuration file, typically named phpstan.neon or phpstan.neon.dist, written in NEON format. This file allows users to define paths to analyze, exclude specific files, configure extensions, set analysis levels, and customize various other aspects of the analysis process.
HISTORY
phpstan was created by Ondrej Mirtes and first released in 2016. It quickly gained traction in the PHP community due to its comprehensive type analysis capabilities, which were more advanced than existing static analysis tools at the time. Its continuous development, community contributions, and the introduction of "levels" for gradual adoption have cemented its position as a leading tool for ensuring code quality in PHP projects.