diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 1ccdde7ba1..65e5780a9e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -24,6 +24,7 @@ use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\PropertyHook; +use PhpParser\Node\Scalar; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; @@ -3946,6 +3947,9 @@ public function enterMatch(Expr\Match_ $expr): self } else { $cond = $expr->cond; } + if ($cond instanceof Scalar) { + return $this; + } $type = $this->getType($cond); $nativeType = $this->getNativeType($cond); @@ -4265,6 +4269,10 @@ private function unsetExpression(Expr $expr): self public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType, TrinaryLogic $certainty): self { + if ($expr instanceof Scalar) { + return $this; + } + if ($expr instanceof ConstFetch) { $loweredConstName = strtolower($expr->name->toString()); if (in_array($loweredConstName, ['true', 'false', 'null'], true)) { diff --git a/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php b/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php index 90a5dd508c..f1bcd81285 100644 --- a/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php +++ b/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php @@ -53,4 +53,17 @@ public function testRuleInPhpStanNamespace(): void ]); } + public function testPr4663(): void + { + $this->analyse([__DIR__ . '/data/pr-4663.php'], [ + [ + implode("\n", [ + "\$result (Yes): 'no matches!'", + "native \$result (Yes): 'no matches!'", + ]), + 11, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Debug/data/pr-4663.php b/tests/PHPStan/Rules/Debug/data/pr-4663.php new file mode 100644 index 0000000000..dc0f88eb50 --- /dev/null +++ b/tests/PHPStan/Rules/Debug/data/pr-4663.php @@ -0,0 +1,12 @@ += 8.1 + +namespace PR4663; + +use function PHPStan\debugScope; + +function (): void { + $result = match(1){ + default => 'no matches!' + }; + debugScope(); +};