Skip to content

Commit 0799527

Browse files
jdecoolondrejmirtes
authored andcommitted
Reset remembered match cond when entering match
1 parent 271766e commit 0799527

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,12 +3507,14 @@ public function enterMatch(Expr\Match_ $expr): self
35073507
return $this;
35083508
}
35093509
if ($expr->cond instanceof AlwaysRememberedExpr) {
3510-
return $this;
3510+
$cond = $expr->cond->expr;
3511+
} else {
3512+
$cond = $expr->cond;
35113513
}
35123514

3513-
$type = $this->getType($expr->cond);
3514-
$nativeType = $this->getNativeType($expr->cond);
3515-
$condExpr = new AlwaysRememberedExpr($expr->cond, $type, $nativeType);
3515+
$type = $this->getType($cond);
3516+
$nativeType = $this->getNativeType($cond);
3517+
$condExpr = new AlwaysRememberedExpr($cond, $type, $nativeType);
35163518
$expr->cond = $condExpr;
35173519

35183520
return $this->assignExpression($condExpr, $type, $nativeType);

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,13 @@ public function testBugUnhandledTrueWithComplexCondition(): void
543543
$this->analyse([__DIR__ . '/data/bug-unhandled-true-with-complex-condition.php'], []);
544544
}
545545

546+
public function testBug11246(): void
547+
{
548+
if (PHP_VERSION_ID < 80100) {
549+
$this->markTestSkipped('Test requires PHP 8.1.');
550+
}
551+
552+
$this->analyse([__DIR__ . '/data/bug-11246.php'], []);
553+
}
554+
546555
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1); // lint >= 8.1
2+
3+
namespace Bug11246;
4+
5+
$var = 0;
6+
foreach ([1, 2, 3, 4, 5] as $index) {
7+
$var++;
8+
9+
match ($var % 5) {
10+
1 => 'c27ba0',
11+
2 => '5b9bd5',
12+
3 => 'ed7d31',
13+
4 => 'ffc000',
14+
default => '674ea7',
15+
};
16+
}

0 commit comments

Comments
 (0)