Suppress property.nonObject and method.nonObject errors when property_exists()/method_exists() guard is present#5729
Open
phpstan-bot wants to merge 1 commit into
Conversation
…perty_exists()`/`method_exists()` guard is present - Add synthetic `property_exists()` call check in `AccessPropertiesCheck` before the `canAccessProperties()` error, so `mixed` narrowed to `class-string|(object&hasProperty(…))` no longer triggers a false positive - Extend the existing `property_exists()` guard check for undefined properties to also handle `Identifier` property names (previously only handled `Expr`) - Apply the same fix to `MethodCallCheck` for `method_exists()` guards - Update `PropertyExistsTypeSpecifyingExtension` and `MethodExistsTypeSpecifyingExtension` to also store the function call result as `true` in the `!isObject()->yes()` branch, so synthetic call checks can find the stored specification - Remove the now-incorrect assertion in `CallMethodsRuleTest` that expected the false positive "Cannot call method foo() on class-string|object"
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
property_exists($mixed, 'prop')is used as a guard and then$mixed->propis accessed, PHPStan incorrectly reported "Cannot access property $prop on class-string|object." This happened becauseproperty_exists()narrowsmixedtoclass-string|(object&hasProperty(prop)), and theclass-stringmember of the union causescanAccessProperties()to returnmaybe(), triggering theproperty.nonObjecterror. The same issue existed formethod_exists()withcanCallMethods().PR #5544 added
property_exists()guard handling inAccessStaticPropertiesCheckbut missed the instance property access case inAccessPropertiesCheck, and also missed the analogousmethod_exists()case inMethodCallCheck.Changes
src/Type/Php/PropertyExistsTypeSpecifyingExtension.php: In the!isObject()->yes()branch, also store theproperty_exists()call result astrue(viacreateFuncCallSpec) in addition to narrowing the argument type. This enables synthetic call checks to find the stored specification.src/Type/Php/MethodExistsTypeSpecifyingExtension.php: Same fix for the generic (non-string) branch.src/Rules/Properties/AccessPropertiesCheck.php: Add a syntheticproperty_exists()call check before thecanAccessProperties()error (line 122). Also extend the existingproperty_exists()guard check for undefined properties (previously only handledExprnames) to also handleIdentifiernames by constructing aString_node.src/Rules/Methods/MethodCallCheck.php: Add a syntheticmethod_exists()call check before thecanCallMethods()error. Also extend the existingmethod_exists()guard check for undefined methods to handleIdentifiernames.tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php: Remove the assertion for the now-suppressed false positive "Cannot call method foo() on class-string|object."Analogous cases probed
AccessStaticPropertiesCheck): Already fixed by PR Suppress undefined static property error whenproperty_exists()guard is present #5544. Verified no regression.AccessPropertiesInAssignRule): UsesAccessPropertiesCheck, automatically covered. Added dedicated test.object&hasProperty(prop)(noclass-stringunion), socanAccessProperties()returnsyes()and no false positive occurs. No fix needed.CallStaticMethodsRule): Not affected — static method calls on class-strings are valid PHP and handled differently.Root cause
Two independent issues combined to produce the false positive:
PropertyExistsTypeSpecifyingExtension(andMethodExistsTypeSpecifyingExtension) narrowed the argument type formixedtoclass-string|(object&hasProperty(…))but did NOT store the function call result astrue. This meant syntheticproperty_exists()/method_exists()call checks in rule code could not detect the guard.AccessPropertiesCheck(andMethodCallCheck) did not check forproperty_exists()/method_exists()guards before reporting thecanAccessProperties()/canCallMethods()error. The existing guard checks only applied to the "undefined property/method" errors and only for dynamic (Expr) names.Test
tests/PHPStan/Rules/Properties/data/bug-14667.php— Tests instance property access afterproperty_exists()withmixed, explicitmixed,object|string,object,$this,self, and chained conditions.tests/PHPStan/Rules/Properties/data/bug-14667-assign.php— Tests property assignment afterproperty_exists()withmixed, explicitmixed, andobject|string.tests/PHPStan/Analyser/nsrt/bug-14667.php— Verifies type narrowing producesclass-string|(object&hasProperty(prop))formixed.tests/PHPStan/Rules/Methods/data/bug-14667.php— Tests method call aftermethod_exists()withmixed, explicitmixed,object|string,object, and chained conditions.Fixes phpstan/phpstan#14667