Skip to content

Commit

Permalink
MatchExpressionArmBody
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Oct 19, 2022
1 parent 600e9e1 commit 6ddf21b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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(),
);
Expand Down
7 changes: 6 additions & 1 deletion src/Node/MatchExpressionArm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down
26 changes: 26 additions & 0 deletions src/Node/MatchExpressionArmBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;

/** @api */
class MatchExpressionArmBody
{

public function __construct(private Scope $scope, private Expr $body)
{
}

public function getScope(): Scope
{
return $this->scope;
}

public function getBody(): Expr
{
return $this->body;
}

}

0 comments on commit 6ddf21b

Please sign in to comment.