Fix phpstan/phpstan#14421: Incorrect type narrowing of superglobal with dependent types#5376
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14421: Incorrect type narrowing of superglobal with dependent types#5376phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Superglobal-containing expressions are now excluded from being guarded in conditional expressions during scope merging - The root cause was that createConditionalExpressions() would create type dependencies linking superglobal array offsets to assigned variables, causing narrowing of the variable to incorrectly propagate back to the superglobal - New regression test in tests/PHPStan/Rules/Variables/data/bug-14421.php Closes phpstan/phpstan#14421
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 was assigned from a superglobal array offset in one branch and from a function return in another, narrowing that variable (e.g.,
$b !== null) would incorrectly propagate back to the superglobal, causing PHPStan to think the offset always exists.Changes
MutatingScope::createConditionalExpressions()insrc/Analyser/MutatingScope.phpto skip superglobal-containing expressions from being guarded in conditional expressionsNodeFinder+isGlobalVariablepattern already used inmergeVariableHolders()) to both loops that create conditional expression holderstests/PHPStan/Rules/Variables/IssetRuleTest.phpandtests/PHPStan/Rules/Variables/data/bug-14421.phpRoot cause
createConditionalExpressions()creates type guards that link expressions across merged scopes. When$_SESSION['a']was checked withisset()in one branch and$bwas assigned from it, the method created a conditional expression saying "when$bhas type X, then$_SESSIONhas narrowed type Y". Later, when$b !== nullwas checked, this conditional triggered and incorrectly narrowed$_SESSIONto always have offset'a'set.The fix extends the superglobal protection already present in
mergeVariableHolders()to also apply increateConditionalExpressions(), preventing superglobal expressions from being targets of conditional type narrowing.Test
Added a regression test reproducing the exact scenario from the issue:
isset($_SESSION['a'])in an if/else with$bassigned from the superglobal in one branch andget_optional_int()in the other, followed by$b !== nullcheck and inner!isset($_SESSION['a']). The test expects no errors (the false positive about offset 'a' always existing is gone).Fixes phpstan/phpstan#14421