levelmax World 3 PHPStan to Max
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. B
L
Level 3.B ~30 min Target: PHPStan max

Boss: take it to max

This returns service passes PHPStan at max. On paper it's finished. Then you read phpstan.neon:

  • a baseline of eight entries, one of which no longer matches anything
  • reportUnmatchedIgnoredErrors: false, added the day a fix turned the build red, so nobody has heard about a fixed error since
  • no strict rules

Meanwhile, an unknown order number crashes the returns page, "7" is accepted as an order ID until it reaches a typed return, and SKU 0100 is refused as a final sale item because SKU 100 is one.

A green build is only as honest as its configuration. Your job is to make this one honest.

Your checklist

Level Ask of the project
3.1 Living with a baseline Does every baseline entry hide a real error? Is PHPStan allowed to say when one doesn't? Can you get it to zero?
3.2 mixed, the final enemy Is the request checked once, where the JSON arrives, so nothing after it sees mixed?
3.3 Extensions Is anything in phpstan.neon hiding errors instead of explaining them?
3.4 Strict rules With strict rules on, is every comparison strict and every condition a boolean?
3.5 Custom rules Is there a convention here worth a rule? Not for this exercise, but worth asking of your own project.

Start by removing reportUnmatchedIgnoredErrors and running Analyse to find the stale entry. When the baseline is empty, leave the file with ignoreErrors: [] and add strict rules to includes. The tests check the behaviour and the configuration.

CHALLENGE

The returns service is green at level max, but only because of its configuration. Its baseline hides two bugs, one entry no longer matches anything, and the check that would say so is switched off.

Make it honestly clean: no baseline entries, unmatched ignores reported, and strict rules on. Fix ReturnRequest and ReturnPolicy as you go:

  • ReturnRequest::fromJson() checks the JSON once, and throws an UnexpectedValueException for a bad request. reason() returns a ReturnReason, and a missing or unknown reason counts as ChangedMind
  • ReturnPolicy refuses an unknown order with ReturnRefused::unknownOrder(), and compares SKUs exactly
REFERENCE

An empty baseline, as --generate-baseline --allow-empty-baseline writes it:

parameters:
    ignoreErrors: []

Strict rules are installed, and a project includes them like this:

includes:
    - vendor/phpstan/phpstan-strict-rules/rules.neon
Running on PHP 8.3…

Run the tests, run PHPStan, or submit for both. Ctrl/⌘ + Enter submits from the editor.