Fix #11705: Value of a variable is forgotten in case where it ought to be known with certainty#5075
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
- When an expression type holder exists for $arr[$var] (e.g. from array_key_exists), and $var is later narrowed (e.g. in a switch/case), recompute the offset value type using the narrowed variable type and use it if it's more specific - New regression test in tests/PHPStan/Analyser/nsrt/bug-11705.php - The root cause was that array_key_exists creates an expression type holder for $arr[$tag] based on the broad value type, which overrides fresh computation even after $tag is narrowed to a specific constant in a switch case Closes phpstan/phpstan#11705
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 accessing an array with a variable key like
$theInput[$tag]afterarray_key_exists($tag, $theInput), and the variable$tagis later narrowed (e.g., in aswitch/case), PHPStan would still use the original broad value type instead of the narrowed one. This caused false positives like "Parameter #1 (array|string) of echo cannot be converted to string" even though the narrowed key type resolves to a specific string value.Changes
src/Analyser/MutatingScope.phpinresolveType(): when anArrayDimFetchnode has an expression type holder and its dimension is aVariable, recompute the offset value type using the current (possibly narrowed) variable type. If the recomputed type is more specific (the expression type is a supertype of it), return the recomputed type instead of the stale expression type holder.tests/PHPStan/Analyser/nsrt/bug-11705.phpRoot cause
The
ArrayKeyExistsFunctionTypeSpecifyingExtensioncreates an expression type holder for$arr[$key]with the array's full iterable value type when the key is non-constant. This expression type holder persists in the scope and takes priority over fresh type computation inresolveType(). When$keyis later narrowed to a more specific type (e.g.,$tagnarrowed fromstringto'name'in acase 'name':branch), the stale expression type holder still returns the broad union type, preventing the type system from using the narrowed key to resolve a more specific value type.The fix detects this situation by checking whether the freshly computed
getOffsetValueType()(using the current narrowed dim type) is more specific than the stored expression type. If so, the fresh computation's result is used.Test
Added
tests/PHPStan/Analyser/nsrt/bug-11705.phpwhich reproduces the original issue: a typed arrayarray{'name': string, 'owners': array<int, string>}accessed with a variable key$taginside aswitch($tag) { case 'name': }block. The test verifies that$theInput[$tag]correctly resolves tostring(notarray<int, string>|string) both before and after an always-trueif ($tag === 'name')check.Fixes phpstan/phpstan#11705