Read class-constant @var type without triggering @extends resolution#5502
Merged
phpstan-bot merged 4 commits into2.1.xfrom Apr 21, 2026
Merged
Read class-constant @var type without triggering @extends resolution#5502phpstan-bot merged 4 commits into2.1.xfrom
@var type without triggering @extends resolution#5502phpstan-bot merged 4 commits into2.1.xfrom
Conversation
…CONST>>` Resolving a class-constant fetch called `ClassReflection::getConstant()` solely to read `getPhpDocType()`. That pulls in the ancestor walk (`getAncestors()` → `getParentClass()` → `getFirstExtendsTag()`), which eagerly resolves `@extends` type nodes. When `@extends` contains `value-of<self::CONST>`, the resolution re-enters constant resolution, hits the recursion guard, and the `MixedType` it returns gets baked into the cached `ResolvedPhpDocBlock::$extendsTags`. Introduce `ClassReflection::getConstantPhpDocType()`, which reads just the constant's own `@var` tag without walking ancestors or applying PHPDoc inheritance. Use it from both `getClassConstFetchTypeByReflection` call sites. Extract `resolveConstantVarPhpDocType()` to share the @var tag extraction with `getConstant()`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The fixture uses typed class constants (`const string`, `const array`), which are a PHP 8.3 feature. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Describe what it does, not what it resembles. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Deduplicate the "stub → docComment → fileTypeMapper" fetch shared by getConstant() and getConstantPhpDocType(). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
mixed in @extends<value-of<self::CONST>>@var type without triggering @extends resolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes phpstan/phpstan#14501, a regression introduced in 2.1.48 by #5457. Resolving a class-constant fetch called
ClassReflection::getConstant()solely to readgetPhpDocType(). That pulls ingetAncestors()→getParentClass()→getFirstExtendsTag(), which eagerly resolves@extendstype nodes. When the tag containsvalue-of<self::CONST>, resolution re-enters constant resolution, hits thecurrentlyResolvingClassConstantguard, and theMixedTypeit returns gets cached insideResolvedPhpDocBlock::$extendsTags. The flakiness the reporter saw is cache-ordering: whichever code path reachesgetExtendsTags()first wins, so the error shows up on different classes across runs.