Adopt stricter PHPStan checks incrementally, using precise PHPDoc collection types and CI checks instead of ignoring warnings.
What It Does
Start with a reachable analysis level. A non-empty-list annotation expresses a stronger input contract.
Requirements and Compatibility
Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.
Syntax, Inputs and Results
The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.
Example 1: Basic Use
Start with a reachable analysis level.
composer require --dev phpstan/phpstan
vendor/bin/phpstan analyse src --level=6
vendor/bin/phpstan analyse --level=max
Example 2: Another Case
A non-empty-list annotation expresses a stronger input contract.
<?php
/** @param non-empty-list<int> $prices */
function maxPrice(array $prices): int { return max($prices); }
echo maxPrice([10,20,15]);
Practical Example
Move toward strict rules and track pre-existing findings explicitly.
vendor/bin/phpstan analyse --level=max
vendor/bin/phpstan analyse --generate-baseline
vendor/bin/phpstan analyse --level=max
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
A baseline should document known debt, not become a permanent substitute for correcting type errors.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| Static analysis vs runtime tests | PHPStan checks source types without executing business behavior; tests confirm actual outcomes. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring unknown or mixed types | Add valid PHPDoc element types or proper native declarations, then re-run the analyzer. |
| Skipping the prerequisite of this specific workflow | Install or configure the named tool and confirm the example can actually run. |
| Ignoring edge conditions | A baseline should document known debt, not become a permanent substitute for correcting type errors. |
Key Takeaways
- Adopt stricter PHPStan checks incrementally, using precise PHPDoc collection types and CI checks instead of ignoring warnings.
- A baseline should document known debt, not become a permanent substitute for correcting type errors.
- Keep application configuration, source code and validation tests under version control.
Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.