diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 10285dd54f..2d39f77423 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -90,6 +90,7 @@ use PHPStan\Node\LiteralArrayItem; use PHPStan\Node\LiteralArrayNode; use PHPStan\Node\MatchExpressionArm; +use PHPStan\Node\MatchExpressionArmBody; use PHPStan\Node\MatchExpressionArmCondition; use PHPStan\Node\MatchExpressionNode; use PHPStan\Node\MethodCallableNode; @@ -2628,12 +2629,13 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void { foreach ($expr->arms as $arm) { if ($arm->conds === null) { $hasDefaultCond = true; + $matchArmBody = new MatchExpressionArmBody($matchScope, $arm->body); + $armNodes[] = new MatchExpressionArm($matchArmBody, [], $arm->getLine()); $armResult = $this->processExprNode($arm->body, $matchScope, $nodeCallback, ExpressionContext::createTopLevel()); $matchScope = $armResult->getScope(); $hasYield = $hasYield || $armResult->hasYield(); $throwPoints = array_merge($throwPoints, $armResult->getThrowPoints()); $scope = $scope->mergeWith($matchScope); - $armNodes[] = new MatchExpressionArm([], $arm->getLine()); continue; } @@ -2663,11 +2665,13 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void { $filteringExpr = new BinaryOp\BooleanOr($filteringExpr, $armCondExpr); } - $armNodes[] = new MatchExpressionArm($condNodes, $arm->getLine()); + $bodyScope = $matchScope->filterByTruthyValue($filteringExpr); + $matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body); + $armNodes[] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getLine()); $armResult = $this->processExprNode( $arm->body, - $matchScope->filterByTruthyValue($filteringExpr), + $bodyScope, $nodeCallback, ExpressionContext::createTopLevel(), ); diff --git a/src/Node/MatchExpressionArm.php b/src/Node/MatchExpressionArm.php index 53832b1bcb..bc8643af45 100644 --- a/src/Node/MatchExpressionArm.php +++ b/src/Node/MatchExpressionArm.php @@ -9,10 +9,15 @@ class MatchExpressionArm /** * @param MatchExpressionArmCondition[] $conditions */ - public function __construct(private array $conditions, private int $line) + public function __construct(private MatchExpressionArmBody $body, private array $conditions, private int $line) { } + public function getBody(): MatchExpressionArmBody + { + return $this->body; + } + /** * @return MatchExpressionArmCondition[] */ diff --git a/src/Node/MatchExpressionArmBody.php b/src/Node/MatchExpressionArmBody.php new file mode 100644 index 0000000000..544d6237de --- /dev/null +++ b/src/Node/MatchExpressionArmBody.php @@ -0,0 +1,26 @@ +scope; + } + + public function getBody(): Expr + { + return $this->body; + } + +}