From f87b7dc3b69481ee0f8ffd81ae265c85609434b5 Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 30 Nov 2026 11:59:55 +0100 Subject: [PATCH] Keep the function call stack in scope transitions inside a function body --- src/Analyser/MutatingScope.php | 24 ++-- ...copeFunctionCallStackInTransitionsRule.php | 55 ++++++++ ...FunctionCallStackInTransitionsRuleTest.php | 122 ++++++++++++++++++ .../scope-function-call-stack-transitions.php | 84 ++++++++++++ 4 files changed, 273 insertions(+), 12 deletions(-) create mode 100644 tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRule.php create mode 100644 tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRuleTest.php create mode 100644 tests/PHPStan/Rules/data/scope-function-call-stack-transitions.php diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 9fe944d8557..d15d71713bb 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1507,7 +1507,7 @@ public function withRecordedStatementDelta(self $recordedEntry, self $recordedEx $conditionalExpressions, [], [], - [], + $this->inFunctionCallsStack, $this->inFirstLevelStatement, $recordedExit->afterExtractCall, ); @@ -3041,7 +3041,7 @@ public function enterExpressionAssign(Expr $expr, bool $isPlainWrite = true): se $this->isInFirstLevelStatement(), $currentlyAssignedExpressions, $this->currentlyAllowedUndefinedExpressions, - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, @@ -3072,7 +3072,7 @@ public function exitExpressionAssign(Expr $expr): self $this->isInFirstLevelStatement(), $currentlyAssignedExpressions, $this->currentlyAllowedUndefinedExpressions, - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, @@ -3133,7 +3133,7 @@ public function setAllowedUndefinedExpression(Expr $expr): self $this->isInFirstLevelStatement(), $this->currentlyAssignedExpressions, $currentlyAllowedUndefinedExpressions, - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, @@ -3164,7 +3164,7 @@ public function unsetAllowedUndefinedExpression(Expr $expr): self $this->isInFirstLevelStatement(), $this->currentlyAssignedExpressions, $currentlyAllowedUndefinedExpressions, - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, @@ -4180,7 +4180,7 @@ private function invalidateExpressionWithoutAliases(Expr $expressionToInvalidate $result[2], $this->currentlyAssignedExpressions, $this->currentlyAllowedUndefinedExpressions, - [], + $this->inFunctionCallsStack, $this->inFirstLevelStatement, $this->afterExtractCall, ); @@ -4224,7 +4224,7 @@ private function invalidateMethodsOnExpression(Expr $expressionToInvalidate): se $result[2], $this->currentlyAssignedExpressions, $this->currentlyAllowedUndefinedExpressions, - [], + $this->inFunctionCallsStack, $this->inFirstLevelStatement, $this->afterExtractCall, ); @@ -5079,7 +5079,7 @@ private function mergeWithVariableState(?self $otherScope, bool $preserveVacuous $conditionalExpressions, [], [], - [], + $this->inFunctionCallsStack, $this->inFirstLevelStatement, $afterExtractCall, ); @@ -5102,7 +5102,7 @@ private function mergeWithVariableState(?self $otherScope, bool $preserveVacuous $conditionalExpressions, [], [], - [], + $this->inFunctionCallsStack, $this->inFirstLevelStatement, $afterExtractCall, ); @@ -5502,7 +5502,7 @@ public function processFinallyScope(self $finallyScope, self $originalFinallySco $this->inFirstLevelStatement, [], [], - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, @@ -5651,7 +5651,7 @@ public function processAlwaysIterableForeachScopeWithoutPollute(self $finalScope $this->inFirstLevelStatement, [], [], - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, @@ -5806,7 +5806,7 @@ private function generalizeWithVariableState(self $otherScope, ?array $writableV $this->inFirstLevelStatement, [], [], - [], + $this->inFunctionCallsStack, $this->afterExtractCall, $this->parentScope, $this->nativeTypesPromoted, diff --git a/tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRule.php b/tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRule.php new file mode 100644 index 00000000000..756d921790a --- /dev/null +++ b/tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRule.php @@ -0,0 +1,55 @@ + */ +class ScopeFunctionCallStackInTransitionsRule implements Rule +{ + + public function getNodeType(): string + { + return Node\Expr::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if (!$node instanceof MethodCall && !$node instanceof PropertyFetch) { + return []; + } + if (!$node->name instanceof Node\Identifier) { + throw new ShouldNotHappenException(); + } + + $stack = []; + foreach ($scope->getFunctionCallStackWithParameters() as [$reflection, $parameter]) { + if ($parameter === null) { + throw new ShouldNotHappenException(); + } + if ($reflection instanceof FunctionReflection) { + $stack[] = sprintf('%s ($%s)', $reflection->getName(), $parameter->getName()); + continue; + } + + $stack[] = sprintf('%s::%s ($%s)', $reflection->getDeclaringClass()->getDisplayName(), $reflection->getName(), $parameter->getName()); + } + + return [ + RuleErrorBuilder::message(sprintf( + '%s: %s', + $node->name->name, + count($stack) === 0 ? 'empty' : implode(', ', $stack), + ))->identifier('dummy')->build(), + ]; + } + +} diff --git a/tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRuleTest.php b/tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRuleTest.php new file mode 100644 index 00000000000..b7090ecf344 --- /dev/null +++ b/tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRuleTest.php @@ -0,0 +1,122 @@ + + */ +class ScopeFunctionCallStackInTransitionsRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new ScopeFunctionCallStackInTransitionsRule(); + } + + #[RequiresPhp('>= 8.0.0')] + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/scope-function-call-stack-transitions.php'], [ + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 30, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 31, + ], + [ + 'p: ScopeFunctionCallStackTransitions\foo ($cb)', + 32, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 33, + ], + [ + 'x: ScopeFunctionCallStackTransitions\foo ($cb)', + 33, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 34, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 35, + ], + [ + 'p: ScopeFunctionCallStackTransitions\foo ($cb)', + 35, + ], + [ + 'p: ScopeFunctionCallStackTransitions\foo ($cb)', + 36, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb), trim ($string), ScopeFunctionCallStackTransitions\bar ($cb)', + 37, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 43, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 46, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 50, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 56, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 64, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 68, + ], + [ + 'm: ScopeFunctionCallStackTransitions\foo ($cb)', + 70, + ], + [ + 'm: trim ($string), ScopeFunctionCallStackTransitions\foo ($cb)', + 73, + ], + [ + 'm: empty', + 79, + ], + [ + 'm: empty', + 80, + ], + [ + 'p: empty', + 81, + ], + [ + 'm: empty', + 82, + ], + [ + 'm: empty', + 83, + ], + [ + 'p: empty', + 83, + ], + ]); + } + +} diff --git a/tests/PHPStan/Rules/data/scope-function-call-stack-transitions.php b/tests/PHPStan/Rules/data/scope-function-call-stack-transitions.php new file mode 100644 index 00000000000..47e18a29840 --- /dev/null +++ b/tests/PHPStan/Rules/data/scope-function-call-stack-transitions.php @@ -0,0 +1,84 @@ += 8.0 + +namespace ScopeFunctionCallStackTransitions; + +class Foo +{ + + public ?Foo $p = null; + + public ?Foo $x = null; + + public function m(): ?Foo + { + return null; + } + +} + +function foo(callable $cb): void +{ +} + +function bar(callable $cb): ?Foo +{ + return null; +} + +function arrowFunctions(Foo $o, ?Foo $a): void +{ + foo(fn () => $o->m()); + foo(fn () => $o->m() ?? null); + foo(fn () => $o->p ?? null); + foo(fn () => $o->m()->x ?? null); + foo(fn () => $a ?? $o->m()); + foo(fn () => isset($o->p) ? $o->m() : null); + foo(fn () => empty($o->p)); + foo(fn () => trim(bar(fn () => $o->m() ?? null))); +} + +function closures(Foo $o, bool $x): void +{ + foo(function () use ($o) { + return $o->m(); + }); + foo(function () use ($o) { + return $o->m() ?? null; + }); + foo(function () use ($o) { + $a = 1; + return $o->m(); + }); + foo(function () use ($o, $x) { + if ($x) { + $a = 1; + } + return $o->m(); + }); + foo(function () use ($o) { + try { + $a = 1; + } finally { + $a = 2; + } + return $o->m(); + }); + foo(function () use ($o) { + foreach ([1, 2] as $i) { + $o->m(); + } + return $o->m(); + }); + trim(foo(function () use ($o) { + return $o->m() ?? null; + })); +} + +function outsideAnyCall(Foo $o, ?Foo $a): void +{ + $o->m() ?? null; + $a = $o->m(); + $o->p ?? null; + $o->m(); + $x = isset($o->p) ? $o->m() : null; +}