Skip to content

Commit

Permalink
Avoid using AlwaysRememberedExpr when process match expression
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool committed Jun 28, 2024
1 parent 877ff0a commit 8587937
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3509,6 +3509,9 @@ public function enterMatch(Expr\Match_ $expr): self
if ($expr->cond instanceof AlwaysRememberedExpr) {
return $this;
}
if ($expr->cond instanceof BinaryOp) {
return $this;
}

$type = $this->getType($expr->cond);
$nativeType = $this->getNativeType($expr->cond);
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 8587937

Please sign in to comment.