Fix #13596: False positive: first class callable protected by method_exists still reports callable.nonNativeMethod error.#4935
Open
phpstan-bot wants to merge 1 commit into2.1.xfrom
Open
Conversation
…s guard - Skip callable.nonNativeMethod error when method was resolved through HasMethodType (from method_exists/is_callable guard) - Detect this by checking if declaring class is stdClass with no such method, which indicates a DummyMethodReflection from HasMethodType - Applied same fix to both MethodCallableRule and StaticMethodCallableRule - Added regression test in tests/PHPStan/Rules/Methods/data/bug-13596.php
| return $errors; | ||
| } | ||
|
|
||
| if ($declaringClass->getName() === stdClass::class && !$declaringClass->hasMethod($methodNameName)) { |
Contributor
There was a problem hiding this comment.
There is isDummy() method on DummyPropertyReflection/ExtendedPropertyReflection
I feel like it's better to have a isDummy() method on DummyMethodReflection/ExtendedMethodReflection too rather than explicitly checking stdClass.
WDYT ?
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 creating a first-class callable guarded by
method_exists()oris_callable(), PHPStan incorrectly reportedcallable.nonNativeMethodeven though the method's existence was verified at runtime. This fix prevents the false positive by detecting when the method reflection was resolved throughHasMethodType(the accessory type added bymethod_exists()narrowing).Changes
src/Rules/Methods/MethodCallableRule.php: Added check to skipcallable.nonNativeMethoderror when the method's declaring class isstdClassand doesn't actually have the method — indicating aDummyMethodReflectionfromHasMethodTyperesolutionsrc/Rules/Methods/StaticMethodCallableRule.php: Applied the same fix for static method callablestests/PHPStan/Rules/Methods/MethodCallableRuleTest.php: AddedtestBug13596test methodtests/PHPStan/Rules/Methods/data/bug-13596.php: Regression test coveringmethod_exists()andis_callable()guards with first-class callable syntaxRoot cause
When
method_exists($this, 'myCallable')narrows the type,HasMethodType('myCallable')is added as an accessory type.HasMethodType::getMethod()returns aDummyMethodReflectionwhose declaring class isstdClass. TheMethodCallableRulethen checks$declaringClass->hasNativeMethod('myCallable')onstdClass, which returns false, triggering thecallable.nonNativeMethoderror. The fix detects this dummy case by checking if the declaring class isstdClasswithout the method, and skips the error since the method's existence was runtime-verified.Test
Added regression test
tests/PHPStan/Rules/Methods/data/bug-13596.phpwith a base class that usesmethod_exists()andis_callable()guards before creating first-class callables. The test expects no errors.Fixes phpstan/phpstan#13596