phpstan-type-trace
Ask PHPStan where a type came from, instead of guessing from the code.
🌐 Live examples → read 5 real chains in 10 seconds, no install.
PHPStan says:
app/Services/InvoiceService.php:11 Cannot access property $currency on App\Models\Customer|null.
The line is:
return $order->customer->currency;
Reading the code, nothing here says where the null comes from. The usual agent fix is $order->customer?->currency or an assert(). The error goes away, nobody checks whether an order can really lack a customer, and every other place that reads $order->customer gets the same patch.
Asking PHPStan:
$ phpstan-trace inspect app/Services/InvoiceService.php:11 'order->customer'
L11 read App\Models\Customer|null @property-read App\Models\Order::$customer at app/Models/Order.php:8
The |null is written on one line, Order.php:8. If every order has a customer (customer_id is NOT NULL), that declaration is the bug: fix it once and every error it caused goes away. If not, the null is real, and you handle it on purpose.
| Reading the code | With phpstan-trace |
|---|
| Where the type comes from | A guess | PHPStan's own answer, with file and line |
| Typical fix | ?-> or assert() at each call site | The one line that's actually wrong |
What a chain tells you
Each event is a point where PHPStan's type for the variable was set or changed, with the type at that point:
- Where it was set:
param, assign, assign-op, foreach, array-write, assign-ref.
- Where it was narrowed, and by what:
narrow with the guard, e.g. $x !== null, $x instanceof Foo, Assert::notNull($x).
- Where the type is declared:
decl points at the @property-read line, native property or method signature that produced a Foo|null.
- Which extension shaped it:
via names a larastan, webmozart/assert or other extension behind a type you didn't expect.
- When a
@var overrode inference: phpdoc-var.
- What a call returned:
call, for an argument-less call that is dereferenced, like the $request->user() in $request->user()->id.

Install
The CLI needs nothing in your project, only PHPStan 2.x already set up there:
composer global require kayw-geek/phpstan-type-trace
Put Composer's global bin directory on your PATH (composer global config bin-dir --absolute prints it). In a project, phpstan-trace runs the project's own vendor/bin/phpstan with its config, and loads the extension from the global install.
Then teach your agent to use it (next section).
Use it with AI agents
The bundled skill, skills/phpstan-trace/SKILL.md, tells an agent when a trace is worth running, how to read the chain, and how to explain a whole baseline in one run.
-
Agents that read Agent Skills (Claude Code, Codex, Cursor and the others the skills CLI supports):
npx skills add kayw-geek/phpstan-type-trace
-
Claude Code, as a plugin:
/plugin marketplace add kayw-geek/phpstan-type-trace
/plugin install phpstan-type-trace@kayw-geek
-
Any other agent: add this to its instructions file (AGENTS.md, CLAUDE.md, ...):
For a PHPStan type error whose cause the source doesn't show, run
`phpstan-trace inspect <file>:<line> <var> --json` and fix where the chain shows
the wrong type entering. For many errors (e.g. a baseline), run
`phpstan-trace explain <paths> --identifier=<regex> --no-ignore --json`.
CLI
One variable at one line:
phpstan-trace inspect app/Services/InvoiceService.php:11 'order->customer'
phpstan-trace inspect app/Services/InvoiceService.php:11 # no variable: picks the only one, or lists them
The variable is optional: if only one is tracked at that line it's picked, otherwise the candidates are listed.
Every error in one phpstan run (e.g. to work through a baseline):
phpstan-trace explain app/Http --identifier=nonObject --no-ignore
For each error it prints the chain of the variable whose type the message names (the Customer|null above). --no-ignore drops ignoreErrors and baselines, so errors a baseline hides are explained too.
| Option | |
|---|
--json | Machine-readable output for agents and tools. Versioned schema: docs/json-api.md. |
--identifier=REGEX | explain only: keep errors whose identifier matches. |
--no-ignore | explain only: ignore the config's ignoreErrors and baselines. |
--memory-limit=2G | Passed to phpstan. |
--configuration=PATH | Project phpstan config, if not phpstan.neon(.dist) / phpstan.dist.neon. |
--phpstan=PATH | phpstan binary, if not vendor/bin/phpstan. |