3.12: `builder-of<static>` resolves to `never` on a `Model & <interface with @phpstan-require-extends Model>` receiver
- Larastan Version: 3.12.2 (
builder-of<static>landed in 3.12.0; fine on 3.10.0) - Laravel Version: 13.32.0
- PHPStan Version: 2.2.14, PHP 8.5.4
Description
Since 3.12, the model query factories (newQuery(), newModelQuery(), newQueryWithoutScopes(), …) are typed builder-of<static> in stubs/common/Model.stub. When the receiver is an intersection of Model with an interface that carries @phpstan-require-extends Model, the call resolves to *NEVER*. Everything chained off it degrades to *ERROR* and then mixed, so the first report is a misleading Cannot call method toBase() on mixed two calls later — and every builder call in between goes unchecked.
An intersection with a plain marker interface (no @phpstan-require-extends) resolves correctly, so the tag is the trigger. My guess at the mechanism, stated as an inference: PHPStan grants the interface the parent's methods through the tag, so both halves of the intersection have newModelQuery(), and intersecting the two late-resolved builder-of<static> results collapses to never.
On 3.10.0 the same code resolved to Builder<Model> via the (since removed) NewModelQueryDynamicMethodReturnTypeExtension, which iterated the receiver's class reflections and skipped the non-Model half — imprecise, but it kept the chain type-checked.
Real-world shape: a SoftDeletableModel interface (@phpstan-require-extends Model) that a cascade-delete service accepts as Model&SoftDeletableModel and then runs $model->newModelQuery()->whereKey($model->getKey())->toBase()->update([...]) on.
Laravel code where the issue was found
<?php
use Illuminate\Database\Eloquent\Model;
interface PlainMarker {}
/** @phpstan-require-extends Model */
interface RequireExtendsMarker {}
function repro(Model&PlainMarker $plain, Model&RequireExtendsMarker $tagged): void
{
\PHPStan\dumpType($plain->newModelQuery()); // Illuminate\Database\Eloquent\Builder<PlainMarker&Illuminate\Database\Eloquent\Model>
\PHPStan\dumpType($tagged->newModelQuery()); // *NEVER*
\PHPStan\dumpType($tagged->newQuery()); // *NEVER*
\PHPStan\dumpType($tagged->newModelQuery()->whereKey(1)); // *ERROR*
$tagged->newModelQuery()->whereKey(1)->toBase()->update(['deleted' => 1]);
// Cannot call method toBase() on mixed.
// Cannot call method update() on mixed.
}Expected: Builder<Model&RequireExtendsMarker> (what the untagged intersection gets), or at minimum Builder<Model> as on 3.10.
Possibly related (same 3.12 change of query()/newQuery() typing, different symptom): #2554.
Source: larastan/larastan