Skip to content

[CodeQuality] Add CallbackSingleAssertToSimplerRector to simplify single-assert callback matchers in with()#714

Merged
TomasVotruba merged 4 commits into
mainfrom
simpler-with-equal-to
Jul 12, 2026
Merged

[CodeQuality] Add CallbackSingleAssertToSimplerRector to simplify single-assert callback matchers in with()#714
TomasVotruba merged 4 commits into
mainfrom
simpler-with-equal-to

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 12, 2026

Copy link
Copy Markdown
Member

Adds CallbackSingleAssertToSimplerRector.

A ->with() callback matcher that only does a single assertSame() + return true; is just a verbose equalTo(). This rule collapses it to the native matcher.

 $builder->expects($this->exactly(2))
     ->method('add')
-    ->with($this->callback(function ($type): bool {
-        $this->assertSame(TextType::class, $type);
-
-        return true;
-    }));
+    ->with($this->equalTo(TextType::class));

Handles multiple with() args independently.

Skips when the callback does more than assertSame() + return true; (a different assert, extra statements, or a non-true return), so matcher behavior is preserved.

Registered in the phpunit-code-quality set.


Also loops all with() args in the sibling SimplerWithIsInstanceOfRector (was first-arg only), so assertInstanceOf callbacks convert in any position:

-->with($this->callback(function ($event): bool {
-    $this->assertInstanceOf(UpdateColumnEvent::class, $event);
-    return true;
-}))
+->with($this->isInstanceOf(UpdateColumnEvent::class))

@TomasVotruba TomasVotruba enabled auto-merge (squash) July 12, 2026 10:10
@TomasVotruba TomasVotruba changed the title [CodeQuality] Add SimplerWithEqualToRector to collapse assertSame callback in with() to equalTo() matcher [CodeQuality] Add CallbackSingleAssertToSimplerRector to simplify single-assert callback matchers in with() Jul 12, 2026
@TomasVotruba TomasVotruba merged commit 08758f4 into main Jul 12, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the simpler-with-equal-to branch July 12, 2026 10:10
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