Skip to content

[CodeQuality] Skip by-ref use() closures in CallbackSingleAssertToSimplerRector#715

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-by-ref-use-callback-single-assert
Jul 12, 2026
Merged

[CodeQuality] Skip by-ref use() closures in CallbackSingleAssertToSimplerRector#715
TomasVotruba merged 1 commit into
mainfrom
skip-by-ref-use-callback-single-assert

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

CallbackSingleAssertToSimplerRector wrongly rewrote callbacks that capture a variable by reference in use. Such a variable is typically mutated in an earlier callback, so freezing it into an equalTo() matcher captures the wrong value.

Now the rule skips any callback closure with a by-reference use.

$download = null;

$this->entityManager->expects($this->once())
    ->method('persist')
    ->with($this->callback(function ($downloadPersist) use (&$download): bool {
        $download = $downloadPersist;
        $this->assertInstanceOf(Download::class, $download);

        return true;
    }));

// referenced value modified above, must NOT be collapsed
$this->entityManager->expects($this->once())
    ->method('detach')
    ->with($this->callback(function ($downloadDetach) use (&$download): bool {
        $this->assertSame($downloadDetach, $download);

        return true;
    }));

Previously the second callback was rewritten to ->with($this->equalTo($downloadDetach)), which is wrong. Now it is left untouched.

@TomasVotruba TomasVotruba enabled auto-merge (squash) July 12, 2026 10:37
@TomasVotruba TomasVotruba merged commit 7cc60e8 into main Jul 12, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the skip-by-ref-use-callback-single-assert branch July 12, 2026 10:37
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