Skip to content

[PHPUnit100] Add willReturnOnConsecutiveCalls() variables to closure use() in WithConsecutiveRector - #821

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-will-return-on-consecutive-calls-use-variables
Nov 9, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-will-return-on-consecutive-calls-use-variables

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9962

WithConsecutiveRector built the willReturnCallback() closure's use() list from the withConsecutive() args and the single return statement only. The willReturnOnConsecutiveCalls() return values are injected into the if-branches afterwards by WillReturnIfNodeDecorator, so variables used there (e.g. $title) were never captured, producing a closure that fails with "Undefined variable".

Now the willReturnOnConsecutiveCalls() call is passed into UsedVariablesResolver so its args' variables are added to use() too.

Before:

function (...$parameters) use ($matcher) {
    if ($matcher->numberOfInvocations() === 1) {
        $this->assertSame('first', $parameters[0]);
        return ['title' => $title]; // $title not captured
    }
}

After:

function (...$parameters) use ($matcher, $title) {

Added a fixture covering the reported case.

@TomasVotruba
TomasVotruba merged commit db4740b into main Nov 9, 2026
8 checks passed
@TomasVotruba
TomasVotruba deleted the fix-will-return-on-consecutive-calls-use-variables branch November 9, 2026 16:18
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.

Incorrect behavior of WithConsecutiveRector: variables in willReturnOnConsecutiveCalls() not added to closure use()

1 participant