Fix #13981: Assignment inside match statement no longer recognized#5021
Closed
phpstan-bot wants to merge 1 commit into2.1.xfrom
Closed
Fix #13981: Assignment inside match statement no longer recognized#5021phpstan-bot wants to merge 1 commit into2.1.xfrom
phpstan-bot wants to merge 1 commit into2.1.xfrom
Conversation
- Variable assignments in match arm conditions (e.g. `is_dir($baseDir = ...)`) were lost after commit 3beb8c6 replaced processExprNode with filterByTruthyValue - filterByTruthyValue only narrows types without walking the AST, so assignments within condition expressions were not discovered for the arm body scope - Fix transfers newly-defined variables from the condition processing scope to the body scope after applying truthiness filtering - New regression test in tests/PHPStan/Rules/Variables/data/bug-13981.php Closes phpstan/phpstan#13981
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
Assignment inside match arm conditions (e.g.
is_dir($baseDir = dirname(__DIR__).'/lang') => $baseDir) stopped being recognized in 2.1.34, causing false positive "Variable $baseDir might not be defined" errors. This was a regression from commit 3beb8c6 ("Fix match analysis").Changes
src/Analyser/NodeScopeResolver.php: After computing the match arm body scope viafilterByTruthyValue, transfer newly-defined variables from the condition processing scope ($armCondResultScope) to the body scope. This ensures variables assigned within match arm conditions are visible in the arm body.tests/PHPStan/Rules/Variables/data/bug-13981.php: New test data file reproducing the issue withmatch(true)and an assignment inside anis_dir()condition.tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php: NewtestBug13981()test method.Root cause
Commit 3beb8c6 changed the match arm body scope computation from:
to:
The old code called
processExprNodewhich walked the filtering expression's AST tree, discovering any assignments within the condition sub-expressions (like$baseDir = dirname(__DIR__).'/lang'). The new code only applies type narrowing viafilterByTruthyValuewithout walking the AST, so assignments in conditions were lost.The fix iterates over variables defined during condition processing (tracked in
$armCondResultScope) and transfers any that are new (not already in$matchScope) to the body scope. This preserves correct type narrowing fromfilterByTruthyValuewhile restoring variable visibility.Test
Added
tests/PHPStan/Rules/Variables/data/bug-13981.phpwith amatch(true)expression that assigns a variable inside the condition of a match arm. The test expects no errors (the variable should be recognized as defined). Verified the test fails without the fix and passes with it.Fixes phpstan/phpstan#13981