Skip to content

Commit

Permalink
Reset remembered match cond when entering match
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool authored and ondrejmirtes committed Jun 30, 2024
1 parent 271766e commit 0799527
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3507,12 +3507,14 @@ public function enterMatch(Expr\Match_ $expr): self
return $this;
}
if ($expr->cond instanceof AlwaysRememberedExpr) {
return $this;
$cond = $expr->cond->expr;
} else {
$cond = $expr->cond;
}

$type = $this->getType($expr->cond);
$nativeType = $this->getNativeType($expr->cond);
$condExpr = new AlwaysRememberedExpr($expr->cond, $type, $nativeType);
$type = $this->getType($cond);
$nativeType = $this->getNativeType($cond);
$condExpr = new AlwaysRememberedExpr($cond, $type, $nativeType);
$expr->cond = $condExpr;

return $this->assignExpression($condExpr, $type, $nativeType);
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,13 @@ public function testBugUnhandledTrueWithComplexCondition(): void
$this->analyse([__DIR__ . '/data/bug-unhandled-true-with-complex-condition.php'], []);
}

public function testBug11246(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-11246.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11246.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Bug11246;

$var = 0;
foreach ([1, 2, 3, 4, 5] as $index) {
$var++;

match ($var % 5) {
1 => 'c27ba0',
2 => '5b9bd5',
3 => 'ed7d31',
4 => 'ffc000',
default => '674ea7',
};
}

0 comments on commit 0799527

Please sign in to comment.