Skip to content

Add a rule detecting Options captured by closures in OptionsResolver callbacks - #533

Open
stof wants to merge 1 commit into
phpstan:2.1.xfrom
stof:options-resolver-captured-options
Open

stof wants to merge 1 commit into
phpstan:2.1.xfrom
stof:options-resolver-captured-options

Conversation

@stof

@stof stof commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Closes #454

Lazy defaults and normalizers receive an Options object whose ArrayAccess reads detect circular dependencies between options. That detection only works while the callback runs. A closure that captures $options and reads it later bypasses it.

This rule reports closures and arrow functions that capture a variable of type Symfony\Component\OptionsResolver\Options. Variables of type OptionsResolver are not reported, because nested options callbacks receive a real resolver.

$resolver->setDefault('foo', function (Options $options) {
    return fn () => $options['bar']; // reported
});

$resolver->setDefault('foo', function (Options $options) {
    $bar = $options['bar'];

    return fn () => $bar; // fine
});

Identifier: symfonyOptionsResolver.optionsCaptured

What is reported

  • Variables imported with use ($options) or use (&$options).
  • Variables that an arrow function captures implicitly, including variables needed by closures nested inside it.

Closures that run right away

These are allowed:

  • (function () use ($options) { ... })() and (fn () => ...)()
  • Closures passed directly as an argument to a parameter that PHPStan treats as immediately invoked. This uses the same logic as NodeScopeResolver::callCallbackImmediately(): callable parameters of functions such as array_map() count by default, and method parameters count only when annotated @param-immediately-invoked-callable.

PHPStan provides the parameter receiving the closure through Scope::getFunctionCallStackWithParameters(). The call stack is also set for closures nested deeper inside an argument, such as foo([fn () => ...]). A small parser visitor registered in rules.neon therefore marks closures that are direct call arguments. The same visitor marks (fn)() calls, so the rule does not depend on PHPStan's internal ImmediatelyInvokedClosureVisitor.

Other changes

  • symfony/options-resolver added to require-dev. It was previously installed only through symfony/form.
  • README feature list updated.

🤖 Generated with Claude Code

…callbacks

Reading options from a closure invoked after the lazy default or normalizer
has returned bypasses the circular dependency detection of the
OptionsResolver. The rule reports closures and arrow functions capturing a
variable of type Symfony\Component\OptionsResolver\Options (but not
OptionsResolver itself), unless the closure is invoked immediately.

Closes phpstan#454

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stof

stof commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes the lowest deps jobs are failing because they install nikic/php-parser 4.x even though phpstan 2.3 only supports version 5.x. Is this expected ? What is the right way to fix it ?

@stof

stof commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

it looks like nikic/php-parser is installed as a phpunit dependency and wins over the version bundled in the phpstan phar. Maybe phpstan needs a conflict rule to prevent installing incompatible nikic/php-parser versions.

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.

Rule idea: prevent broken capturing of lazy options in OptionsResolver smart defaults

1 participant