Repository navigation
Check @var against native types with template types resolved to bounds and stop descending into iterable values once the outer type already accepts - #7152
Conversation
…nds and stop descending into iterable values once the outer type already accepts - VarTagTypeRuleHelper::isValidSuperType(): when both types are iterable and the native type is already a supertype of the @var type, return yes instead of comparing the iterable value types. Since GenericStaticType resolves its members against its type arguments, `static<string>` iterates `string`, while the native `static` of a generic class still iterates the class template `T`, so `@var static<string>` on a `static`-returning factory was rejected for classes implementing IteratorAggregate. - VarTagTypeRuleHelper::checkExprType(): resolve template types in the native type to their bounds before the native-type check. Templates only reach a native type through `static`/`$this` and stand for whatever the object holds, so `/** @var string $v */ foreach ($this->make(['a']) as $v)` is a narrowing of native `T`, not a widening. A bounded template (`T of int`) still reports `@var string`. - Probed `@return`/`@param`/property PHPDoc checks: they don't descend into iterable value types and natives can't be `static` there, so they are not affected.
VincentLanglet
left a comment
There was a problem hiding this comment.
I don't understand the issue
PHPDoc tag @var with type static(Items<string>) is not subtype of native type static(Items<T>).
was initially reported but
\PHPStan\dumpType($this->make(['Alice']));
reports
Dumped type: static(Items<string>)
while
\PHPStan\dumpNativeType($this->make(['Alice']));
reports
Dumped type: static(Items<T (class Items, argument)>)
… once the outer type accepts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The error was real even though Why it was reported You're right about both dumps. $x = $this->make(['Alice']);
foreach ($x as $v) {
\PHPStan\dumpType($v); // string
\PHPStan\dumpNativeType($v); // T (class Items, argument)
}For
Why it started in 2.3.3 Commit 0fb1068 made What the PR changes
I checked that both changes are needed: with the bound resolution reverted, the test reports This round The only new commit adds a comment above the skip in |
Summary
In a generic class implementing
IteratorAggregate<int, T>, an inline/** @var static<string> $names */on the result of astatic-returning factory was reported asPHPDoc tag @var with type static(Items<string>) is not subtype of native type static(Items<T>).This started in 2.3.3. The same false positive showed up for a@varon aforeachover such a factory result (@var stringvs nativeT).Changes
src/Rules/PhpDoc/VarTagTypeRuleHelper.phpisValidSuperType(): when both types are iterable and the native type is already a supertype of the@vartype, return yes without comparing iterable value types.checkExprType(): before the native-type check, resolve template types in the native type to their bounds (the reported message still shows the original native type).@return/@param/property PHPDoc-vs-native checks (IncompatiblePhpDocTypeCheck,IncompatiblePropertyPhpDocTypeRule). They don't descend into iterable value types, andstaticcan't be a native parameter or property type, so they aren't affected.Root cause
Commit 0fb1068 gave
GenericStaticTypea class reflection that carries its type arguments, sostatic<string>now correctly iteratesstring. The native type of astatic-returning call is a plainStaticTypewhose iterable value type is still the class templateT. The@varwidening check:static(Items<T>)⊇static(Items<string>): yes),T⊇stringgives maybe, which counts as a failure.Before that commit both sides iterated
T, which is why the bug only appeared withIteratorAggregate. The outer check beingyesalready proves the@varnarrows the native type, so the descent is now skipped in that case.The same pattern shows up wherever a native type exposes a class template: those only come from
static/$thisand mean "whatever this object holds". Narrowing them with@varis allowed, so they are compared by their bound. A bound likeT of intstill rejects@var string.Test
tests/PHPStan/Rules/PhpDoc/data/bug-15796.phpruns throughWrongVariableNameInVarTagRuleTest(with and withoutcheckTypeAgainstPhpDocType):@var static<string>on astatic<U>factory): no errorself<U>factory variant: no error@var stringon aforeachover the factory result: no error (this failed before the fix)@var Other<string>onstatic): still reported@var stringon aforeachover aT of intcollection: still reportedFixes phpstan/phpstan#15796