Fix #7317: Return type of SimpleXMLElement::current() is not tentative#5086
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #7317: Return type of SimpleXMLElement::current() is not tentative#5086phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ate prototypes - OverridingMethodRule now also checks the parent class method's own tentative return type, not just the deepest prototype's - This fixes the case where SimpleXMLElement::current() has tentative return type ?static but the check only saw Iterator::current() with tentative type mixed - Updated $reportReturnType logic to suppress regular return type errors when the parent class method has a tentative return type - Added regression test for phpstan/phpstan#7317 Closes phpstan/phpstan#7317
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
SimpleXMLElement::current()has a tentative return type (?static) in PHP 8.1+, but PHPStan was not reporting it as a tentative return type violation when a subclass overrides it with an incompatible return type. Instead, no error was reported at all, whileSimpleXMLElement::valid()correctly showed the tentative return type error.Changes
src/Rules/Methods/OverridingMethodRule.phpto also check the parent class method's own tentative return type, not just the deepest prototype in the chain$reportReturnTypelogic to suppress regular return type errors when either the deepest prototype or the parent class method has a tentative return typeTypehintHelperimport for converting reflection types to PHPStan typestests/PHPStan/Rules/Methods/data/bug-7317.phpandtests/PHPStan/Rules/Methods/OverridingMethodRuleTest.phpRoot cause
When checking tentative return types,
OverridingMethodRuleuses$method->getPrototype()which follows BetterReflection's prototype chain to the deepest ancestor. ForMySimpleXMLElement::current(), this resolves toIterator::current()which has a tentative return type ofmixed. Sinceboolis covariant withmixed, the tentative check passes and no error is reported.However, the intermediate class
SimpleXMLElement::current()has its own tentative return type of?static(i.e.,SimpleXMLElement|null), whichboolis NOT covariant with. The fix adds an additional check that examines the parent class method's tentative return type via$prototypeDeclaringClass->getNativeReflection()->getMethod(), catching cases where an intermediate prototype in the chain has a more restrictive tentative return type than the deepest prototype.Test
Added
tests/PHPStan/Rules/Methods/data/bug-7317.phpwith aMySimpleXMLElementclass that extendsSimpleXMLElementand overrides bothcurrent()(returningbool) andvalid()(returningint). The test verifies that both methods now correctly produce tentative return type errors on PHP 8.1+.Fixes phpstan/phpstan#7317