Skip to content

[TypeDeclaration] Add AddClosureParamTypeFromVariableCallRector#8142

Merged
TomasVotruba merged 3 commits into
mainfrom
add-closure-param-type-from-variable-call
Jul 4, 2026
Merged

[TypeDeclaration] Add AddClosureParamTypeFromVariableCallRector#8142
TomasVotruba merged 3 commits into
mainfrom
add-closure-param-type-from-variable-call

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 4, 2026

Copy link
Copy Markdown
Member

Complements #8141. Where #8141 stops the wrong array type on recursive closures walking an ArrayAccess tree, this new rule adds the correct object type from the call site.

What it does

When a closure is assigned to a variable and that variable is invoked with a concrete object argument, the object type is added to the matching closure param:

$printItem = function ($item) {   // =>  function (Item $item)
    echo $item->name;
};

$printItem(new Item());

Real-world case (Symfony FormView):

-$findThemes = function ($formView) use (&$findThemes) {
+$findThemes = function (FormView $formView) use (&$findThemes) { 
    $child = $formView['child'];
    $findThemes($child);
};

$findThemes($formView); // $formView : FormView

Scope (conservative)

  • Only concrete object types are added; scalar / array / conflicting-object arguments are skipped (arrays are already handled by StrictArrayParamDimFetchRector).
  • Nested scopes are not descended into, so a recursive self-call (whose argument is derived inside the closure and thus untyped) does not pollute the inferred type.
  • Params already typed or variadic are left untouched.

Covered by fixtures: object param added, recursive FormView, skip already-typed, skip scalar arg, skip conflicting types.

Infer a closure param's object type from the argument the assigned closure
variable is invoked with, e.g. $cb($formView) where $formView is a FormView
adds a FormView type hint to the closure param. Only concrete object types are
added; scalar/array/conflicting args are skipped.
…mDimFetchRector

Infer the closure param object type before the array-dim-fetch rule runs, so a typed param is already present and the array fallback is skipped.
@TomasVotruba TomasVotruba enabled auto-merge (squash) July 4, 2026 14:49
@TomasVotruba TomasVotruba merged commit eb3eb21 into main Jul 4, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the add-closure-param-type-from-variable-call branch July 4, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant