Static analysis

A static analyzer reads PHP without running it and reports the calls and returns that cannot work. Two tools cover the field, and the examples below show how the gate behaves: the two commands that need a project with its packages installed are kept as they were written and are not run here.

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.

<?php function cartTotal(array $rows): int { $sum = 0; foreach ($rows as $row) { $sum += (int) $row; } return $sum; } echo cartTotal([10, 20, 30]), PHP_EOL;

php cart.php

60

The function is the shape an analyzer likes: a declared parameter, a declared return, and one path that returns the sum. A baseline entry would name the line of this function only while the old warning is still there.

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.

Both tools sit behind one Composer script in composer.json:

{ "scripts": { "analyse": [ "phpstan analyse src --level=5", "psalm --show-info=false" ] } }

composer analyse

In the project this page was written for, the script answers phpstan: OK, psalm: OK. The line is quoted here rather than in an output block, because the two tools need a folder with its packages installed and this machine has none.

The gate that decides the status of the step is a plain script:

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

php gate.php

OK

The gate passes on an empty list. Give it one entry and the same block prints FAIL: 1 problems and leaves with status 1, which is the status a CI step reads as a failure. The command above it belongs to a project with its packages installed, so its output is not from this machine.

Exercises

  1. Add a second problem to the gate list and read the status the run leaves.
  2. Write a function with a return type and a path that returns nothing, then describe what the analyzer reports.
  3. Explain why a baseline has to be regenerated instead of copied between projects.
  4. Run php -l and a static analyzer on the same file and name the kind of bug only the analyzer finds.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What makes static analysis different from a test run?

    Answer

    It reads the code without executing it

    The analyzer builds a model of the types and follows the code, so a path nobody runs is still checked.

  2. Why start PHPStan at a low level and climb?

    Answer

    A high level reports hundreds of findings at once on legacy code

    One level at a time keeps the list small enough to fix, and each step proves the folder improved.

  3. What does a baseline do?

    Answer

    It records the current findings so new ones stand out

    The old list is accepted once, and the gate then fails only on something new.

  4. Why does the CI step run with zero tolerance?

    Answer

    Because a warning nobody must fix is a warning nobody reads

    The gate is only honest when a new finding stops the merge.

  5. What does this program print? <?php $problems = ["a", "b"]; $errors = count($problems); if ($errors > 0) { echo "FAIL: " . $errors . " problems\n"; exit(1); } echo "OK\n";

    Answer

    FAIL: 2 problems

    Two entries are counted, and the branch prints them and exits before the OK line.

  6. What does this program print? <?php function cartTotal(array $rows): int { $sum = 0; foreach ($rows as $row) { $sum += (int) $row; } return $sum; } echo cartTotal([10, 20, 30]);

    Answer

    60

    The cast makes each value an integer and the loop adds them to sixty.

  7. What does this program print? <?php $problems = []; $errors = count($problems); if ($errors > 0) { echo "FAIL\n"; exit(1); } echo "OK\n";

    Answer

    OK

    An empty list counts zero, so the branch is skipped and the last line runs.

  8. What does this program print? <?php function total(array $rows): int { $sum = 0; foreach ($rows as $row) { $sum += (int) $row; } } echo "done";

    Answer

    done

    PHP only complains at the moment the function is called and its return value is used; a plain call still prints done.

  9. A pull request adds a new warning and the baseline is regenerated in the same commit. What is lost?

    Answer

    The new warning, which the baseline now accepts as old

    A baseline must be regenerated from the code before the change, never from a branch that carries the new finding.

  10. Which bug does a static analyzer find that php -l cannot?

    Answer

    A function that returns a string where the caller expects an int array

    The syntax is valid, and only the types of the values show that the call will fail.

The PHP blocks on this page were run with PHP 8.5.13 (php -v). The phpstan, psalm and composer analyse commands need a project with its packages installed, which this machine does not have, so their outputs are kept as the page had them and are not run here.

Article author: Arthur Isaev