Fix phpstan/phpstan#14421: Incorrect type narrowing of superglobal with dependent types#5377
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#5377phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Extended conditional expression skip logic in MutatingScope to handle superglobal variables not present in the other branch's expression types - Added exprContainsSuperGlobal() helper method to detect superglobal expressions for reuse across merge logic - New regression test in tests/PHPStan/Rules/Variables/data/bug-14421.php - Root cause: superglobals always exist but aren't tracked in expressionTypes unless narrowed, so the dependent type skip check failed to recognize them
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 using
isset()on a superglobal array offset (e.g.,$_SESSION['a']) with dependent type narrowing, PHPStan incorrectly reported that the offset always exists and is not nullable, even when the code path could bypass theisset()branch.Changes
src/Analyser/MutatingScope.phpto handle superglobal variables that aren't present in the other branch's expression typesexprContainsSuperGlobal()helper method to detect whether an expression involves a superglobal variabletests/PHPStan/Rules/Variables/data/bug-14421.phpand test method intests/PHPStan/Rules/Variables/IssetRuleTest.phpRoot cause
The fix for phpstan/phpstan#14411 added a check to skip creating conditional expressions when both the expression and its guard exist with certainty in the other branch. However, superglobal variables like
$_SESSIONare always defined but aren't tracked inexpressionTypesunless they've been narrowed by a type specification (e.g., fromisset()). When$_SESSIONwas narrowed in the if-branch but not present in the else-branch's expression types, the skip check failed, causing incorrect conditional expressions to be created. These conditional expressions then incorrectly linked$b's type to$_SESSION's narrowed type, so narrowing$b !== nullalso incorrectly narrowed$_SESSION.The fix treats superglobal expressions as always existing with certainty, even when not explicitly tracked in the other branch's expression types.
Test
Added a regression test reproducing the exact scenario from the issue:
isset($_SESSION['a'])with a fallback assignment in the else branch, followed by a null check that should not cause theisset()to be flagged as always true.Fixes phpstan/phpstan#14421