Skip to content

Commit 2b74aa8

Browse files
committed
Process match arm condition before analysing the body
1 parent 826819d commit 2b74aa8

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2884,7 +2884,8 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
28842884
);
28852885
}
28862886

2887-
$bodyScope = $matchScope->filterByTruthyValue($filteringExpr);
2887+
$bodyScope = $this->processExprNode($stmt, $filteringExpr, $matchScope, static function (): void {
2888+
}, $deepContext)->getTruthyScope();
28882889
$matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body);
28892890
$armNodes[] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getLine());
28902891

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,4 +1029,17 @@ public function testDiscussion10252(): void
10291029
$this->analyse([__DIR__ . '/data/discussion-10252.php'], []);
10301030
}
10311031

1032+
public function testBug10418(): void
1033+
{
1034+
if (PHP_VERSION_ID < 80100) {
1035+
$this->markTestSkipped('Test requires PHP 8.1.');
1036+
}
1037+
1038+
$this->cliArgumentsVariablesRegistered = true;
1039+
$this->polluteScopeWithLoopInitialAssignments = true;
1040+
$this->checkMaybeUndefinedVariables = true;
1041+
$this->polluteScopeWithAlwaysIterableForeach = true;
1042+
$this->analyse([__DIR__ . '/data/bug-10418.php'], []);
1043+
}
1044+
10321045
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10418;
4+
5+
function (): void {
6+
$text = '123';
7+
$result = match(1){
8+
preg_match('/(\d+)/', $text, $match) => 'matched number: ' . $match[1],
9+
preg_match('/(\w+)/', $text, $match) => 'matched word: ' . json_encode($match),
10+
default => 'no matches!'
11+
};
12+
};

0 commit comments

Comments
 (0)