Fix phpstan/phpstan#11565: False positive: Conditional return type takes the wrong branch#5362
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
… when variable is reassigned - When processing `$items = iteratorToList($items)` as a statement, the TypeSpecifier evaluated the conditional return type using the post-assignment scope where `$items` was already `list<string>`, causing the condition `$iterable is list` to be true and the return type to resolve to `never` - Added `removeExpr` method to SpecifiedTypes to filter out entries by expression key - In TypeSpecifier, when processing an Assign in null context, remove specifiedTypes that target the assigned variable to prevent the post-assignment type from being incorrectly narrowed by the conditional return type evaluation - New regression test in tests/PHPStan/Analyser/nsrt/bug-11565.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 variable is reassigned from a function with a conditional return type (e.g.,
$items = iteratorToList($items)), PHPStan incorrectly resolved the conditional return type toneverinstead oflist<T>. This happened because the TypeSpecifier used the post-assignment scope to evaluate the conditional, causing the variable's new type (the function's return type) to be used as the input, creating a circular evaluation.Changes
src/Analyser/TypeSpecifier.php: In the null-context handling ofAssignexpressions, remove any specifiedTypes that target the assigned variable. This prevents the post-assignment type from incorrectly influencing the conditional return type evaluation.src/Analyser/SpecifiedTypes.php: AddedremoveExpr(string $exprString)method to allow filtering out entries from bothsureTypesandsureNotTypesby expression key.tests/PHPStan/Analyser/nsrt/bug-11565.php: Regression test covering both the buggy case (same variable) and the working case (different variable).Root cause
After processing
$items = iteratorToList($items), the scope has$itemstyped aslist<string>. The statement processing inNodeScopeResolverthen callsspecifyTypesInConditionwith this post-assignment scope to extract additional type narrowing. For an Assign in null context, the TypeSpecifier recurses into the RHS function call.ParametersAcceptorSelector::selectFromArgsevaluates the argument$itemsusing the post-assignment scope, gettinglist<string>instead of the originaliterable<string, string>. Sincelist<string>IS a list, the conditional($iterable is list ? never : list<T>)resolves tonever, and the TypeSpecifier creates a "not list" narrowing for$items. When applied tolist<string>, this produces*NEVER*.The fix removes specifiedTypes targeting the assigned variable in null context, since after reassignment the variable no longer represents the function's input.
Test
Added
tests/PHPStan/Analyser/nsrt/bug-11565.phpwhich tests that$items = iteratorToList($items)correctly resolves tolist<string>when$itemsstarts asiterable<string, string>. Also verifies the existing working case with different variables.Fixes phpstan/phpstan#11565