Static analysis

PHPStan levels

PHPStan reads code without running it and reports types that cannot work: wrong arguments, missing returns, dead branches. Start at level 0 for a legacy folder and climb one level at a time until level 5 feels quiet. The full walkthrough lives in PHPStan and Psalm and the tool list is in PHP linters we use.

vendor/bin/phpstan analyse src --level=5

[OK] No errors in 12 files

Psalm

Psalm is a second pair of eyes with its own strengths: tainted input tracking, precise array shapes, unused code reports. Run it next to PHPStan on the same folder and fix the overlap first, since two tools agreeing usually means a real bug.

vendor/bin/psalm --show-info=false

No errors found in 12 files

Baselines

A large legacy folder reports hundreds of old warnings on day one. A baseline records the current list so new code must stay clean while old code gets fixed gradually. Regenerate the baseline weekly and watch the count fall toward zero.

function cartTotal(array $rows): int { $sum = 0; foreach ($rows as $row) { $sum += (int)$row; } return $sum; }

Run in CI

Static analysis only works when it blocks merges. Add one CI step that runs both tools with zero tolerance for new findings, so every pull request arrives clean and reviewers discuss design instead of typos.

composer analyse

phpstan: OK, psalm: OK

$errors = count($problems); if ($errors > 0) { echo "FAIL: " . $errors . " problems\n"; exit(1); } echo "OK\n";

Next: Refactoring

Автор статьи: Arthur Isaev