Fix #9844: Calling a static method over non-generic class-string results in ErrorType#4964
Open
phpstan-bot wants to merge 2 commits into2.1.xfrom
Open
Fix #9844: Calling a static method over non-generic class-string results in ErrorType#4964phpstan-bot wants to merge 2 commits into2.1.xfrom
phpstan-bot wants to merge 2 commits into2.1.xfrom
Conversation
- When calling a static method on a non-generic class-string (e.g. $class::foo() where $class is class-string), the result was ErrorType instead of MixedType - Root cause: class-string converts to ObjectWithoutClassType via getObjectTypeOrClassStringObjectType(), which returns maybe for hasMethod(), causing filterTypeWithMethod() to reject it and methodCallReturnType() to return null, falling back to ErrorType - Fix checks if the static call is on an expression (not a class name), the resolved type has no known class names, and hasMethod is not definitively no, returning MixedType in that case - New regression test in tests/PHPStan/Analyser/nsrt/bug-9844.php Closes phpstan/phpstan#9844
Automated fix attempt 1 for CI failures.
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
Calling a static method on a non-generic
class-string(e.g.,$class::foo()where$classis typed asclass-string) resulted inErrorTypeinstead ofMixedType. This also affected static method calls onmixedvalues.Changes
src/Analyser/MutatingScope.php(getStaticCallTypemethod): Added a check so that whenmethodCallReturnType()returns null for a static call on an expression, and the resolved type has no known class names (i.e., it's something likeObjectWithoutClassType) andhasMethod()is not definitivelyno, the result isMixedTypeinstead ofErrorType.tests/PHPStan/Analyser/nsrt/bug-9844.phpRoot cause
When a static method is called on a
class-stringvariable, the type goes throughgetObjectTypeOrClassStringObjectType()which converts it toObjectWithoutClassType. This type returnsTrinaryLogic::createMaybe()forhasMethod(), meaning the method might exist but we don't know. However,filterTypeWithMethod()only accepts types wherehasMethod()returnsyes, so it rejectsObjectWithoutClassTypeand returns null. The calling code then fell back toErrorType.The fix distinguishes between:
class-stringormixed):ObjectWithoutClassTypewith no class names → returnMixedTypesince any method could exist$foo::method()where$foo: SomeClass): These have known class names even thoughhasMethod()may returnmaybefor non-final classes → correctly returnErrorTypewhen the method doesn't existTest
Added
tests/PHPStan/Analyser/nsrt/bug-9844.phpwhich tests that$class::foo()where$classisclass-stringreturnsmixedinstead of*ERROR*.Fixes phpstan/phpstan#9844