Fix phpstan/phpstan#7317: Return type of SimpleXMLElement::current() is not tentative#5363
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#7317: Return type of SimpleXMLElement::current() is not tentative#5363phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…n type not checked - The tentative return type check in OverridingMethodRule only checked the deepest prototype (e.g., Iterator::current() with tentative type mixed), missing more specific tentative types from intermediate parent classes (e.g., SimpleXMLElement::current() with tentative type SimpleXMLElement) - Added getParentMethodTentativeReturnType() to also check the direct parent class method's tentative return type and use the more specific one - Updated Bug9615 test expectation to reflect the more accurate parent class reference - New regression test in tests/PHPStan/Rules/Methods/data/bug-7317.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 a class extends
SimpleXMLElementand overridescurrent()with an incompatible return type, PHPStan failed to report a tentative return type error. It correctly reported the error forvalid()but not forcurrent(), even though both methods have tentative return types in PHP 8.1+.Changes
src/Rules/Methods/OverridingMethodRule.phpto also check the direct parent class method's tentative return type, not just the deepest prototype'sgetParentMethodTentativeReturnType()helper method to resolve the parent method's tentative return type via BetterReflection$reportReturnTypelogic to use the computed$hasTentativeReturnTypeflagTypeandTypehintHelperimportsClassReflectionimportRecursiveFilterIterator::getChildren()instead ofRecursiveIterator::getChildren())MethodPrototypeReflection::getName()dead method (no longer called from the rule but may be used externally)Root cause
The
OverridingMethodRuleused$method->getPrototype()to get the tentative return type for comparison. PHP'sgetPrototype()returns the deepest ancestor (interface method), so forMySimpleXMLElement::current(), it returnedIterator::current()with tentative typemixed. Sinceboolis covariant withmixed, no error was reported.However,
SimpleXMLElement::current()has its own more specific tentative return typeSimpleXMLElement. The fix computes the tentative return type from both the deepest prototype AND the direct parent class method, using the more specific one for the check.Test
Added
tests/PHPStan/Rules/Methods/data/bug-7317.php- a regression test that extendsSimpleXMLElementand overridescurrent()with return typeboolandvalid()with return typeint. Both should produce tentative return type errors on PHP 8.1+.Fixes phpstan/phpstan#7317