Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatchExpressionArmBody #1726

Merged
merged 3 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}

}