Skip to content

[CodeQuality] Add RemoveReturnFromVoidMethodMockCallbackRector#711

Merged
TomasVotruba merged 1 commit into
mainfrom
void-mock-callback-return
Jul 11, 2026
Merged

[CodeQuality] Add RemoveReturnFromVoidMethodMockCallbackRector#711
TomasVotruba merged 1 commit into
mainfrom
void-mock-callback-return

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds VoidMockCallbackRector.

When a mocked method returns void, its callback closure has no reason to return true; (or any value). This rule resolves the mocked method via reflection, and when it is void, drops the value return and types the closure as : void.

Covers both ->willReturnCallback(...) and ->with($this->callback(...)).

willReturnCallback()

 $this->createMock(SomeEntityManager::class)
     ->method('persist')
-    ->willReturnCallback(function ($entity) {
+    ->willReturnCallback(function ($entity): void {
         echo $entity;
-
-        return true;
     });

with($this->callback()), mock assigned in setUp()

 $this->entityManager
     ->method('persist')
-    ->with($this->callback(function ($entity): bool {
+    ->with($this->callback(function ($entity): void {
         $this->assertInstanceOf(\stdClass::class, $entity);
-
-        return true;
     }));

persist() is void, so the callback becomes void too.

Skipped

  • non-void mocked methods (return value kept)
  • closures already typed void
  • returns of computed/side-effectful expressions (only scalar/const/variable values are dropped)

…e void mock callbacks and drop their value return
@TomasVotruba TomasVotruba force-pushed the void-mock-callback-return branch from 7c52afb to 7efc219 Compare July 11, 2026 18:11
@TomasVotruba TomasVotruba changed the title [CodeQuality] Add VoidMockCallbackRector to void mock callbacks of void methods [CodeQuality] Add RemoveReturnFromVoidMethodMockCallbackRector Jul 11, 2026
@TomasVotruba TomasVotruba merged commit c682804 into main Jul 11, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the void-mock-callback-return branch July 11, 2026 18:27
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