Skip to content

Keep the function call stack in scope transitions inside a function body - #7267

Open
bbrala wants to merge 1 commit into
phpstan:2.3.xfrom
bbrala:keep-function-call-stack-in-scope
Open

bbrala wants to merge 1 commit into
phpstan:2.3.xfrom
bbrala:keep-function-call-stack-in-scope

Conversation

@bbrala

@bbrala bbrala commented Nov 30, 2026

Copy link
Copy Markdown

Scope::getFunctionCallStack() and getFunctionCallStackWithParameters() lose their frames inside a closure passed as an argument as soon as certain scope transitions happen. enterAnonymousFunction() and enterArrowFunction() hand the stack to the closure body, but the next ??, isset(), empty(), if, try/finally or foreach rebuilds the scope with [] and the stack is gone.

foo(fn () => $o->m());          // stack: foo ($cb)
foo(fn () => $o->m() ?? null);  // stack: empty
foo(function () use ($o, $x) {
    if ($x) {
        $a = 1;
    }
    return $o->m();             // stack: empty
});

Why it matters

Extensions use the stack to tell where they are. mglaman/phpstan-drupal's DeprecationHelperScope (a DeprecatedScopeResolver for phpstan-deprecation-rules) uses it to skip deprecations inside the $deprecatedCallable of Drupal's DeprecationHelper::backwardsCompatibleCall():

DeprecationHelper::backwardsCompatibleCall(
    \Drupal::VERSION,
    '11.1.0',
    fn () => $entity->getOriginal(),
    fn () => $entity->original ?? NULL, // still reported as deprecated
);

drupal-rector reads the same stack to avoid wrapping code twice and runs into the same problem. Real-world hits include search_api, which has five $entity->original ?? NULL calls inside such wrappers.

Fix

There are twelve transitions that stay within the same function body but passed [] for the stack. They now pass $this->inFunctionCallsStack:

  • enterExpressionAssign() / exitExpressionAssign()
  • setAllowedUndefinedExpression() / unsetAllowedUndefinedExpression() (??, isset(), empty())
  • invalidateExpressionWithoutAliases() / invalidateMethodsOnExpression()
  • both scopeWith() calls in mergeWithVariableState()
  • processFinallyScope(), processAlwaysIterableForeachScopeWithoutPollute(), generalizeWithVariableState()
  • withRecordedStatementDelta()

Transitions that start a new context still reset the stack: entering a file, namespace, class, trait or function. I didn't find anything in git history suggesting the [] was deliberate. Most of these lines date from 17bde93, which only added the parentScope argument after it, and the allowed-undefined pair was copied from enterExpressionAssign() in 356250a.

Left alone, and open to feedback:

  • enterClosureBind(), restoreOriginalScopeAfterClosureBind() and enterClosureCall() drop the stack too, along with the assigned/allowed-undefined expressions and parentScope. It looks like the same bug class, but I kept this PR to plain function bodies.
  • enterAnonymousFunctionWithoutReflection() and enterArrowFunctionWithoutReflection() are only used by ClosureTypeResolver for inference walks, which don't run rules. The scope used for rules is rebuilt with the stack right after.

getClosureScopeCacheKey() and ClosureCallContextMatcher both look at the stack. Keeping it consistent with the closure's position should only make them more accurate, and the full suite passes.

Tests

ScopeFunctionCallStackInTransitionsRuleTest reports the stack at every method call and property fetch in a fixture. The fixture covers arrow functions and closures with ?? (left and right), isset() ternaries, empty(), assignments, if, try/finally, foreach, nested calls (trim(bar(fn () => ...))), and a control outside any call. Without the src/ change, lines 31–37, 46, 56, 64, 68, 70 and 73 report an empty stack.

Related, same area but a separate bug: #7011.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EY2u2vs7fU42ARkw6JV7Na

@bbrala
bbrala marked this pull request as ready for review November 30, 2026 11:29
@bbrala

bbrala commented Nov 30, 2026

Copy link
Copy Markdown
Author

Hmm, seems main is broken. Not gonna chase the CI right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant