Skip to content

Check @var against native types with template types resolved to bounds and stop descending into iterable values once the outer type already accepts - #7152

Open
phpstan-bot wants to merge 2 commits into
phpstan:2.4.xfrom
phpstan-bot:create-pull-request/patch-dgepn1r
Open

phpstan-bot wants to merge 2 commits into
phpstan:2.4.xfrom
phpstan-bot:create-pull-request/patch-dgepn1r

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

In a generic class implementing IteratorAggregate<int, T>, an inline /** @var static<string> $names */ on the result of a static-returning factory was reported as PHPDoc 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 @var on a foreach over such a factory result (@var string vs native T).

Changes

  • src/Rules/PhpDoc/VarTagTypeRuleHelper.php
    • isValidSuperType(): when both types are iterable and the native type is already a supertype of the @var type, 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).
  • I checked the @return/@param/property PHPDoc-vs-native checks (IncompatiblePhpDocTypeCheck, IncompatiblePropertyPhpDocTypeRule). They don't descend into iterable value types, and static can't be a native parameter or property type, so they aren't affected.

Root cause

Commit 0fb1068 gave GenericStaticType a class reflection that carries its type arguments, so static<string> now correctly iterates string. The native type of a static-returning call is a plain StaticType whose iterable value type is still the class template T. The @var widening check:

  1. checked the outer types (static(Items<T>) ⊇ static(Items<string>): yes),
  2. then, because both types are iterable, compared only their value types: T ⊇ string gives maybe, which counts as a failure.

Before that commit both sides iterated T, which is why the bug only appeared with IteratorAggregate. The outer check being yes already proves the @var narrows 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/$this and mean "whatever this object holds". Narrowing them with @var is allowed, so they are compared by their bound. A bound like T of int still rejects @var string.

Test

tests/PHPStan/Rules/PhpDoc/data/bug-15796.php runs through WrongVariableNameInVarTagRuleTest (with and without checkTypeAgainstPhpDocType):

  • the reporter's example (@var static<string> on a static<U> factory): no error
  • the self<U> factory variant: no error
  • @var string on a foreach over the factory result: no error (this failed before the fix)
  • a real mismatch (@var Other<string> on static): still reported
  • @var string on a foreach over a T of int collection: still reported

Fixes phpstan/phpstan#15796

…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 VincentLanglet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@phpstan-bot

Copy link
Copy Markdown
Collaborator Author

The error was real even though dumpType() shows the right type. The @var rule never compares the outer types on their own: when both sides are iterable, it goes on to compare the values they iterate, and that's where it failed. No logic change was needed; I added a code comment and committed it, not pushed.

Why it was reported

You're right about both dumps. dumpType() gives static(Items<string>) and dumpNativeType() gives static(Items<T>). The difference is in what each side iterates:

$x = $this->make(['Alice']);
foreach ($x as $v) {
    \PHPStan\dumpType($v);       // string
    \PHPStan\dumpNativeType($v); // T (class Items, argument)
}

For /** @var static<string> */ against the native static(Items<T>), VarTagTypeRuleHelper::isValidSuperType() did this:

  1. Outer types: static(Items<T>) against static(Items<string>). Generic arguments are made covariant here, so this passes. The code only checked that it wasn't "no".
  2. Iterated values: both types are iterable, so it then compared what they iterate: T against string. A template type is only "maybe" a supertype of string, and anything short of "yes" is reported. That's the error you saw.

Why it started in 2.3.3

Commit 0fb1068 made static<string> iterate string. Before that, both sides iterated T, so step 2 compared T with T and passed. That also explains why it only happens with IteratorAggregate: without an iterable class, step 2 never runs.

What the PR changes

  • Stop after a passing outer check. If the native type is already a full supertype of the @var type, step 2 is skipped. Step 2 can't add anything in that case except false positives like this one. This is the fix for the issue as reported.
  • Compare template types in the native type by their bound. This covers /** @var string $name */ on a foreach over the factory result, where the native value type is a plain T. The first change doesn't help there because no outer collection type is compared.

I checked that both changes are needed: with the bound resolution reverted, the test reports PHPDoc tag @var with type string is not subtype of native type T. on line 64 (the foreach). Genuine mismatches are still caught: @var Other<string> and @var string over a T of int collection are both still reported.

This round

The only new commit adds a comment above the skip in src/Rules/PhpDoc/VarTagTypeRuleHelper.php explaining the static(Foo<T>) case. make tests passes (24000 tests, 117 skipped) and make phpstan passes.

@VincentLanglet VincentLanglet self-assigned this Nov 25, 2026
@ondrejmirtes
ondrejmirtes changed the base branch from 2.3.x to 2.4.x December 3, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHPStan rejects @var static<string> on a factory result when the class implements IteratorAggregate

2 participants