3.12: model `@method` docblocks now override the builder return type, yielding `Model|Builder<Model>`
Summary
Upgrading larastan from 3.10.0 to 3.12.x changes the inferred return type of Model::query() / Model::newQuery() from Builder<Model> to the union Model|Builder<Model> when the model carries the @method docblocks that barryvdh/laravel-ide-helper generates.
In 3.10 larastan's own return type extension won over the docblock. In 3.12 the docblock wins, so the union leaks into every call site and produces return.type / argument.type errors at level 10 in code that was previously clean.
Reproduction
A model with stock ide-helper output — note the union in the generated annotations, which is what ide-helper:models has always written:
/**
* @method static \Illuminate\Database\Eloquent\Builder<static>|Foo newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Foo newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Foo query()
* @mixin \Eloquent
*/
class Foo extends Model {}Then:
\PHPStan\dumpType(Foo::query());
\PHPStan\dumpType((new Foo)->newQuery());Result
Same code, same PHPStan (2.2.14) in both runs — only larastan differs:
| larastan | Dumped type |
|---|---|
| 3.10.0 | Illuminate\Database\Eloquent\Builder<Foo> |
| 3.12.2 | Foo|Illuminate\Database\Eloquent\Builder<Foo> |
Expected
Builder<Foo>, as in 3.10 — query() and newQuery() return a builder, never the model, so the union is not a type either method can actually produce.
Impact
On a level-10 codebase with no baseline this surfaced 21 errors across ordinary query helpers, all of the shape:
Method X::population() should return Builder<Foo> but returns Foo|Builder<Foo>.
Anonymous function should return Builder<Foo> but returns Foo|Builder<Foo>.Chaining recovers (newQuery()->where(...) is Builder<Foo> again), so it only bites where a query() result is returned or passed directly.
Scope annotations are affected the same way, since ide-helper writes them in the same union form (@method static Builder<static>|Foo someScope()), so regenerating the docblocks is not a workaround.
Versions
- larastan 3.12.2 (also reproduces on 3.12.0/3.12.1)
- phpstan 2.2.14
- Laravel 13.32.0, PHP 8.4
- barryvdh/laravel-ide-helper 3.7 (default config)
Source: larastan/larastan