Fix phpstan/phpstan#13828: Reference to static const behaves as self const#5351
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#13828: Reference to static const behaves as self const#5351phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ic binding - Added ClassConstantAccessType as a LateResolvableType that wraps a class type and constant name - Modified TypeNodeResolver::resolveConstTypeNode to return ClassConstantAccessType for static::CONST - When the type gets resolved (e.g., via transformStaticType), StaticType is replaced with the actual class, and the constant is looked up from the correct subclass - New regression test in tests/PHPStan/Analyser/nsrt/bug-13828.php
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
When using
@return static::CONSTin a PHPDoc, PHPStan was resolving the constant value from the declaring class (likeself::CONST), ignoring subclass overrides. This fix introduces late static binding for class constant references in PHPDoc types.Changes
src/Type/ClassConstantAccessType.php— a newLateResolvableTypethat wraps a class type and constant name, deferring resolution until the actual class is knownsrc/PhpDoc/TypeNodeResolver.php—resolveConstTypeNode()now returns aClassConstantAccessType(StaticType, constantName)forstatic::CONSTon non-final classes, instead of immediately resolving the constant valueRoot cause
In
TypeNodeResolver::resolveConstTypeNode(), bothstaticandselfwere mapped to$nameScope->getClassName(), and the constant value was resolved immediately. This lost the late static binding semantics ofstatic::. The fix preserves theStaticTypereference inside aClassConstantAccessType, which gets resolved when the method's return type is transformed byCalledOnTypeUnresolvedMethodPrototypeReflection::transformStaticType()— at that point, theStaticTypeis replaced with the actual called class type, and the constant is looked up from the correct class.Test
Added
tests/PHPStan/Analyser/nsrt/bug-13828.phpwith a parent class declaring@return static::FOO_BARand two subclasses overriding the constant. The test verifies that callingtest()on each subclass returns the correct constant value.Fixes phpstan/phpstan#13828