From 8587937162ee988d65b59af19413ec00481074bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Fri, 28 Jun 2024 22:56:26 +0200 Subject: [PATCH] Avoid using AlwaysRememberedExpr when process match expression --- src/Analyser/MutatingScope.php | 3 +++ .../Rules/Comparison/MatchExpressionRuleTest.php | 9 +++++++++ .../PHPStan/Rules/Comparison/data/bug-11246.php | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/PHPStan/Rules/Comparison/data/bug-11246.php diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 361ad5299b..c2ef7ddf5c 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -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); diff --git a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php index b267bfc3e1..0b47552de7 100644 --- a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php @@ -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'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-11246.php b/tests/PHPStan/Rules/Comparison/data/bug-11246.php new file mode 100644 index 0000000000..3c718c00ec --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-11246.php @@ -0,0 +1,16 @@ += 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', + }; +}