From b2c64c1de00443c5bb83fe5e0798b0413234fe5f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 25 Dec 2025 13:04:17 +0100 Subject: [PATCH 1/3] Prevent Scalar expressions to be specified --- src/Analyser/MutatingScope.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 1ccdde7ba1..4f8715592a 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_; @@ -4265,6 +4266,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)) { From 076c0585724cf18d89c5978bf93b879acea0188a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 25 Dec 2025 13:17:56 +0100 Subject: [PATCH 2/3] added test --- tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php | 15 +++++++++++++++ tests/PHPStan/Rules/Debug/data/pr-4663.php | 12 ++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/PHPStan/Rules/Debug/data/pr-4663.php diff --git a/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php b/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php index 90a5dd508c..d6d902fa20 100644 --- a/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php +++ b/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php @@ -53,4 +53,19 @@ public function testRuleInPhpStanNamespace(): void ]); } + public function testPr4663(): void + { + $this->analyse([__DIR__ . '/data/pr-4663.php'], [ + [ + implode("\n", [ + '__phpstanRemembered(1) (Maybe): 1', + "\$result (Yes): 'no matches!'", + 'native __phpstanRemembered(1) (Maybe): 1', + "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(); +}; From 8dced28f22372c6e99dadbf283fcba7bc78ec8b4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 25 Dec 2025 13:27:34 +0100 Subject: [PATCH 3/3] Don't remember Scalar as AlwaysRememberedExpr --- src/Analyser/MutatingScope.php | 3 +++ tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 4f8715592a..65e5780a9e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3947,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); diff --git a/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php b/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php index d6d902fa20..f1bcd81285 100644 --- a/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php +++ b/tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php @@ -58,9 +58,7 @@ public function testPr4663(): void $this->analyse([__DIR__ . '/data/pr-4663.php'], [ [ implode("\n", [ - '__phpstanRemembered(1) (Maybe): 1', "\$result (Yes): 'no matches!'", - 'native __phpstanRemembered(1) (Maybe): 1', "native \$result (Yes): 'no matches!'", ]), 11,