From 4592b99174f65c7d37c069df1c7a112150f5565a Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 27 Oct 2024 00:36:50 +0200 Subject: [PATCH] Fix too early inference --- src/Analyser/MutatingScope.php | 3 --- .../Rules/Comparison/MatchExpressionRuleTest.php | 9 +++++++++ tests/PHPStan/Rules/Comparison/data/bug-11852.php | 13 +++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/PHPStan/Rules/Comparison/data/bug-11852.php diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 90b3fdd465..899021bb85 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1129,9 +1129,6 @@ private function resolveType(string $exprString, Expr $node): Type } $resultType = $this->initializerExprTypeResolver->resolveConcatType($resultType, $partType); - if (count($resultType->getConstantStrings()) === 0) { - return $resultType; - } } return $resultType ?? new ConstantStringType(''); diff --git a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php index b727ba76e2..dd583aa13b 100644 --- a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php @@ -599,4 +599,13 @@ public function testBug9436(): void $this->analyse([__DIR__ . '/data/bug-9436.php'], []); } + public function testBug11852(): void + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Test requires PHP 8.0.'); + } + + $this->analyse([__DIR__ . '/data/bug-11852.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-11852.php b/tests/PHPStan/Rules/Comparison/data/bug-11852.php new file mode 100644 index 0000000000..690def3bc4 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-11852.php @@ -0,0 +1,13 @@ += 8.0 + +namespace Bug11852; + +function sayHello(int $type, string $activity): int +{ + return match("$type:$activity") { + '159:Work' => 12, + '159:education' => 19, + + default => throw new \InvalidArgumentException("unknown values $type:$activity"), + }; +}