Skip to content

[Naming] Fix partial rename in RenameVariableToMatchMethodCallReturnTypeRector with same-named nested arrow param - #8428

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-rename-variable-usage-same-named-arrow-param
Sep 2, 2026
Merged

[Naming] Fix partial rename in RenameVariableToMatchMethodCallReturnTypeRector with same-named nested arrow param#8428
TomasVotruba merged 1 commit into
mainfrom
fix-rename-variable-usage-same-named-arrow-param

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9872

Problem

RenameVariableToMatchMethodCallReturnTypeRector renamed a variable at its assignment but left later usages untouched when a nested arrow function declared a parameter of the same name:

$query = QueryBuilder::for(PartMapping::class)
    ->allowedSorts(fn (Builder $query, bool $descending): Builder => $query->orderBy('part_type'))
    ->defaultSort('part_type');

return $this->apiPaginatedResponse($query->jsonPaginate());

resulted in:

$queryBuilder = QueryBuilder::for(PartMapping::class)
    ->allowedSorts(fn (Builder $query, bool $descending): Builder => $query->orderBy('part_type'))
    ->defaultSort('part_type');

// still $query, now undefined
return $this->apiPaginatedResponse($query->jsonPaginate());

Cause

VariableRenamer::renameVariableInFunctionLike() tracks the innermost $currentFunctionLike while traversing, but that tracker is only updated on enter and never reset on leave (the traverser is enter-only). Once traversal left the arrow function, $currentFunctionLike still pointed at it, so isParamInParentFunction() matched the later $query usage against the arrow function param of the same name and skipped it.

Fix

Ignore the tracked function-like when the variable sits after its body (token position), so params of an already-left arrow function no longer block the rename.

…ypeRector when a nested arrow function has a param of the same name

The tracked $currentFunctionLike is only updated on enter, so it lingers
past its own body. A variable located after a nested arrow function was
matched against that stale function-like params and skipped, renaming the
assignment but not its later usage.

Closes rectorphp/rector#9872
@TomasVotruba
TomasVotruba merged commit ee2e8d0 into main Sep 2, 2026
44 checks passed
@TomasVotruba
TomasVotruba deleted the fix-rename-variable-usage-same-named-arrow-param branch September 2, 2026 09:00
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.

RenameVariableToMatchMethodCallReturnTypeRector rename a variable only partialy

1 participant