Fix phpstan/phpstan#14001: loosing type in foreach analysis#5269
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#14001: loosing type in foreach analysis#5269phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…esult::getScope() - Changed StatementResult, StatementExitPoint, and ReturnStatement to store and return MutatingScope instead of Scope, since mergeWith() is only on MutatingScope and calling it on Scope produced ErrorType, poisoning the variable type across foreach loop iterations - Added toMutatingScope() calls in NodeScopeResolver where ReturnStatement is constructed from a Scope-typed callback parameter - Fixed a null check on getClassReflection() in NodeScopeResolver that was exposed by the more precise type tracking - Removed now-redundant toMutatingScope() calls on ReturnStatement::getScope() - New regression test in tests/PHPStan/Analyser/nsrt/bug-14001.php
c64dfba to
deaafc7
Compare
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
Fixes a bug where variable types were lost to
mixedor*ERROR*insideforeachloops when usingStatementResult::getScope()and callingmergeWith()on the result. The root cause was thatStatementResult::getScope()returned theScopeinterface, which doesn't havemergeWith()— onlyMutatingScopedoes. This caused the method call to returnErrorType, which poisoned the variable type across loop iterations.Changes
src/Analyser/StatementResult.php: Changed constructor parameter andgetScope()return type fromScopetoMutatingScopesrc/Analyser/StatementExitPoint.php: Same change —Scope→MutatingScopefor constructor andgetScope()src/Node/ReturnStatement.php: Same change —Scope→MutatingScopefor constructor andgetScope()src/Analyser/NodeScopeResolver.php:->toMutatingScope()on 4ReturnStatementconstructor calls where$scopecomes from aScope-typed callback parameter->toMutatingScope()calls onReturnStatement::getScope()(lines ~845/849)$scope->getClassReflection()at line ~858, which was exposed by more precise type trackingRoot cause
StatementResult,StatementExitPoint, andReturnStatementare public API classes (@api) that internally always holdMutatingScopeinstances but declared their types asScope(the interface). SincemergeWith()is only defined onMutatingScope, calling it on theScopereturn type producedErrorType. In a foreach loop, thisErrorType(which extendsMixedType) contaminated the variable type across convergence iterations, causing it to degrade tomixedor*ERROR*.The fix narrows the stored/returned type to
MutatingScope, which is a covariant (backwards-compatible) change for callers.Test
Added
tests/PHPStan/Analyser/nsrt/bug-14001.php— an NSRT test with two cases:isAlwaysTerminating()+continueguard before the null check (matches the original issue's first playground sample)Both assert that the variable type after the loop is
MutatingScope|nullinstead ofmixedor*ERROR*.Fixes phpstan/phpstan#14001