From 07995276abd80b766407534055f58592ffe1a1a5 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] Reset remembered match cond when entering match --- src/Analyser/MutatingScope.php | 10 ++++++---- .../Rules/Comparison/MatchExpressionRuleTest.php | 9 +++++++++ .../PHPStan/Rules/Comparison/data/bug-11246.php | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) 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..6950ded4e7 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -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); 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', + }; +}