Skip to content

Fix phpstan/phpstan#13981: Assignment inside match statement no longer recognized#5120

Merged
VincentLanglet merged 3 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-hynebfv
Mar 5, 2026
Merged

Fix phpstan/phpstan#13981: Assignment inside match statement no longer recognized#5120
VincentLanglet merged 3 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-hynebfv

Conversation

@phpstan-bot
Copy link
Collaborator

@phpstan-bot phpstan-bot commented Mar 2, 2026

staabm and others added 3 commits March 2, 2026 06:32
- 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
@VincentLanglet VincentLanglet self-assigned this Mar 3, 2026
@VincentLanglet VincentLanglet requested a review from staabm March 4, 2026 17:19
@VincentLanglet VincentLanglet removed their assignment Mar 4, 2026
Comment on lines +4406 to +4411
$armCondScope = $armCondResultScope->filterByFalseyValue($armCondExpr);
if ($bodyScope === null) {
$bodyScope = $armCondResultScope->filterByTruthyValue($armCondExpr);
} else {
$bodyScope = $bodyScope->mergeWith($armCondResultScope->filterByTruthyValue($armCondExpr));
}
Copy link
Contributor

@staabm staabm Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seeing this I feared this would slow down match/match-arm analysis.

running vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php in contrast reveals though, that it now runs in 20-21 seconds while it took 21-22 seconds before -> so we actually got a bit faster :)

@VincentLanglet VincentLanglet merged commit afd310d into phpstan:2.1.x Mar 5, 2026
634 of 647 checks passed
@staabm staabm deleted the create-pull-request/patch-hynebfv branch March 5, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants