Skip to content

Commit

Permalink
Process match arm condition before analysing the body
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 11, 2024
1 parent 826819d commit 2b74aa8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2884,7 +2884,8 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
);
}

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

Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,4 +1029,17 @@ public function testDiscussion10252(): void
$this->analyse([__DIR__ . '/data/discussion-10252.php'], []);
}

public function testBug10418(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-10418.php'], []);
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-10418.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Bug10418;

function (): void {
$text = '123';
$result = match(1){
preg_match('/(\d+)/', $text, $match) => 'matched number: ' . $match[1],
preg_match('/(\w+)/', $text, $match) => 'matched word: ' . json_encode($match),
default => 'no matches!'
};
};

0 comments on commit 2b74aa8

Please sign in to comment.