Fix #9592: Call to an undefined static method after method_exists()#5083
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #9592: Call to an undefined static method after method_exists()#5083phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…alls - When method_exists() receives a string literal class name (not ClassName::class), the type narrowing was only applied to the string expression, not to the equivalent ClassConstFetch expression that StaticMethodCallCheck uses - Added logic in MethodExistsTypeSpecifyingExtension to also narrow the ClassConstFetch expression when the first argument is a string literal that is a class-string - New regression tests in tests/PHPStan/Rules/Methods/data/bug-9592.php and tests/PHPStan/Analyser/nsrt/bug-9592.php Closes phpstan/phpstan#9592
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
method_exists()was called with a string literal class name (e.g.,method_exists('ClassName', 'methodName')), PHPStan did not properly narrow the type for subsequent static method calls on that class. This caused false positive "Call to an undefined static method" errors even though the method's existence was verified.Changes
src/Type/Php/MethodExistsTypeSpecifyingExtension.phpto also create aClassConstFetchtype narrowing when the first argument is a string literal (not aClassConstFetchexpression likeClassName::class)tests/PHPStan/Rules/Methods/data/bug-9592.phpcovering ternary, early return, and if-block patternstests/PHPStan/Analyser/nsrt/bug-9592.phpverifying theClassConstFetchexpression is narrowed withhasMethod()Root cause
MethodExistsTypeSpecifyingExtensionnarrowed the type of the first argument expression ($args[0]->value). When the argument wasClassName::class(aClassConstFetchnode),StaticMethodCallCheckcould find the narrowing because it looks up$scope->getType(new ClassConstFetch($class, 'class'))which matches the same expression key.However, when the argument was a string literal like
'ClassName', the narrowing was stored under the string literal's expression key (e.g.,'ClassName'), not under theClassConstFetchexpression key (e.g.,\ClassName::class) thatStaticMethodCallChecklooks up. The fix bridges this gap by also creating a narrowing for the equivalentClassConstFetchexpression when a string literal is used.Test
The regression test covers three patterns:
method_exists('Class', 'method') ? Class::method() : ''if (!method_exists('Class', 'method')) { return; } Class::method();if (method_exists('Class', 'method')) { Class::method(); }All should produce no errors (previously all three produced false positive "undefined static method" errors).
Fixes phpstan/phpstan#9592