Skip to content

Commit d6c2752

Browse files
committed
Fix defining variables in match condition
1 parent f6e65bd commit d6c2752

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/Analyser/MutatingScope.php

+3
Original file line numberDiff line numberDiff line change
@@ -3099,6 +3099,9 @@ public function enterMatch(Expr\Match_ $expr): self
30993099
if ($expr->cond instanceof Variable) {
31003100
return $this;
31013101
}
3102+
if ($expr->cond instanceof AlwaysRememberedExpr) {
3103+
return $this;
3104+
}
31023105

31033106
$type = $this->getType($expr->cond);
31043107
$nativeType = $this->getNativeType($expr->cond);

src/Analyser/NodeScopeResolver.php

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
use PHPStan\Node\ClosureReturnStatementsNode;
7373
use PHPStan\Node\DoWhileLoopConditionNode;
7474
use PHPStan\Node\ExecutionEndNode;
75+
use PHPStan\Node\Expr\AlwaysRememberedExpr;
7576
use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
7677
use PHPStan\Node\Expr\GetIterableValueTypeExpr;
7778
use PHPStan\Node\Expr\GetOffsetValueTypeExpr;
@@ -2772,6 +2773,11 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27722773
}
27732774

27742775
$nodeCallback(new MatchExpressionNode($expr->cond, $armNodes, $expr, $matchScope), $scope);
2776+
} elseif ($expr instanceof AlwaysRememberedExpr) {
2777+
$result = $this->processExprNode($expr->getExpr(), $scope, $nodeCallback, $context);
2778+
$hasYield = $result->hasYield();
2779+
$throwPoints = $result->getThrowPoints();
2780+
$scope = $result->getScope();
27752781
} elseif ($expr instanceof Expr\Throw_) {
27762782
$hasYield = false;
27772783
$result = $this->processExprNode($expr->expr, $scope, $nodeCallback, ExpressionContext::createDeep());

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -954,4 +954,13 @@ public function testBug393(): void
954954
$this->analyse([__DIR__ . '/data/bug-393.php'], []);
955955
}
956956

957+
public function testBug9474(): void
958+
{
959+
$this->cliArgumentsVariablesRegistered = true;
960+
$this->polluteScopeWithLoopInitialAssignments = true;
961+
$this->checkMaybeUndefinedVariables = true;
962+
$this->polluteScopeWithAlwaysIterableForeach = true;
963+
$this->analyse([__DIR__ . '/data/bug-9474.php'], []);
964+
}
965+
957966
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Bug9474;
4+
5+
class GlazedTerracotta{
6+
public function getColor() : int{ return 1; }
7+
}
8+
9+
class HelloWorld
10+
{
11+
public function sayHello(): void
12+
{
13+
var_dump((function(GlazedTerracotta $block) : int{
14+
$i = match($color = $block->getColor()){
15+
1 => 1,
16+
default => throw new \Exception("Unhandled dye colour " . $color)
17+
};
18+
echo $color;
19+
return $i;
20+
})(new GlazedTerracotta));
21+
}
22+
}

0 commit comments

Comments
 (0)