Skip to content

Commit 7e13bfb

Browse files
authored
[CodeQuality] Flip void-method with(callback) to willReturnCallback, rename rule (#724)
* [CodeQuality] Rename and refocus rule to flip void-method with(callback) to willReturnCallback * Type flipped callback as void, matching the mocked void method * Also handle plain willReturnCallback closures on void methods, not only with(callback) * Handle only with(callback) form; leave plain willReturnCallback untouched
1 parent 6082812 commit 7e13bfb

13 files changed

Lines changed: 106 additions & 153 deletions

File tree

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector;
1313
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
1414
use Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector;
15-
use Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector;
1615
use Rector\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector;
1716
use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector;
1817
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;
18+
use Rector\PHPUnit\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector;
1919
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
2020
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector;
2121
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
@@ -94,7 +94,7 @@
9494

9595
// type declarations
9696
TypeWillReturnCallableArrowFunctionRector::class,
97-
RemoveReturnFromVoidMethodMockCallbackRector::class,
97+
VoidMethodWithCallbackToWillReturnCallbackRector::class,
9898
StringCastAssertStringContainsStringRector::class,
9999
AddParamTypeFromDependsRector::class,
100100
AddReturnTypeToDependedRector::class,

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/Fixture/return_throw_and_null.php.inc

Lines changed: 0 additions & 53 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/Fixture/will_return_callback_void.php.inc

Lines changed: 0 additions & 43 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/config/configured_rule.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/Fixture/skip_already_void.php.inc renamed to rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/skip_already_void.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
6-
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
77

88
final class SkipAlreadyVoid extends TestCase
99
{

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/Fixture/skip_non_void_method.php.inc renamed to rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/skip_non_void_method.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
6-
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
77

88
final class SkipNonVoidMethod extends TestCase
99
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
7+
8+
final class SkipWillReturnCallback extends TestCase
9+
{
10+
public function test($value): void
11+
{
12+
$this->createMock(SomeEntityManager::class)
13+
->method('persist')
14+
->willReturnCallback(function ($entity) {
15+
echo $entity;
16+
17+
return true;
18+
});
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
7+
8+
final class SkipWillReturnCallbackThrow extends TestCase
9+
{
10+
public function test($matcher): void
11+
{
12+
$this->createMock(SomeEntityManager::class)
13+
->method('persist')
14+
->willReturnCallback(function ($entity) use ($matcher) {
15+
if (1 === $matcher->numberOfInvocations()) {
16+
return null;
17+
}
18+
19+
if (2 === $matcher->numberOfInvocations()) {
20+
return throw new \RuntimeException('boom');
21+
}
22+
});
23+
}
24+
}

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/Fixture/with_callback_void_setup.php.inc renamed to rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Fixture/with_callback_void_setup.php.inc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
44

55
use PHPUnit\Framework\MockObject\MockObject;
66
use PHPUnit\Framework\TestCase;
7-
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
7+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
88

99
final class WithCallbackVoidSetup extends TestCase
1010
{
@@ -19,7 +19,7 @@ final class WithCallbackVoidSetup extends TestCase
1919
{
2020
$this->entityManager
2121
->method('persist')
22-
->with($this->callback(function ($entity): bool {
22+
->with($this->callback(function ($entity) {
2323
$this->assertInstanceOf(\stdClass::class, $entity);
2424

2525
return true;
@@ -31,11 +31,11 @@ final class WithCallbackVoidSetup extends TestCase
3131
-----
3232
<?php
3333

34-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
34+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Fixture;
3535

3636
use PHPUnit\Framework\MockObject\MockObject;
3737
use PHPUnit\Framework\TestCase;
38-
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
38+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source\SomeEntityManager;
3939

4040
final class WithCallbackVoidSetup extends TestCase
4141
{
@@ -50,9 +50,9 @@ final class WithCallbackVoidSetup extends TestCase
5050
{
5151
$this->entityManager
5252
->method('persist')
53-
->with($this->callback(function ($entity): void {
53+
->willReturnCallback(function ($entity): void {
5454
$this->assertInstanceOf(\stdClass::class, $entity);
55-
}));
55+
});
5656
}
5757
}
5858

rules-tests/CodeQuality/Rector/Class_/RemoveReturnFromVoidMethodMockCallbackRector/Source/SomeEntityManager.php renamed to rules-tests/CodeQuality/Rector/Class_/VoidMethodWithCallbackToWillReturnCallbackRector/Source/SomeEntityManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source;
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\VoidMethodWithCallbackToWillReturnCallbackRector\Source;
44

55
// non final on purpose so PHPStan can analyze it
66
class SomeEntityManager

0 commit comments

Comments
 (0)