Skip to content

Commit 7efc219

Browse files
committed
[CodeQuality] Add RemoveReturnFromVoidMethodMockCallbackRector to type void mock callbacks and drop their value return
1 parent 44c5166 commit 7efc219

9 files changed

Lines changed: 543 additions & 0 deletions

File tree

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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;
1516
use Rector\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector;
1617
use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector;
1718
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;
@@ -92,6 +93,7 @@
9293

9394
// type declarations
9495
TypeWillReturnCallableArrowFunctionRector::class,
96+
RemoveReturnFromVoidMethodMockCallbackRector::class,
9597
StringCastAssertStringContainsStringRector::class,
9698
AddParamTypeFromDependsRector::class,
9799
AddReturnTypeToDependedRector::class,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
7+
8+
final class SkipAlreadyVoid extends TestCase
9+
{
10+
public function test(): void
11+
{
12+
$this->createMock(SomeEntityManager::class)
13+
->method('persist')
14+
->willReturnCallback(function ($entity): void {
15+
echo $entity;
16+
});
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
7+
8+
final class SkipNonVoidMethod extends TestCase
9+
{
10+
public function test(): void
11+
{
12+
$this->createMock(SomeEntityManager::class)
13+
->method('count')
14+
->willReturnCallback(function ($name) {
15+
return 100;
16+
});
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
7+
8+
final class WillReturnCallbackVoid 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+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
27+
28+
use PHPUnit\Framework\TestCase;
29+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
30+
31+
final class WillReturnCallbackVoid extends TestCase
32+
{
33+
public function test($value): void
34+
{
35+
$this->createMock(SomeEntityManager::class)
36+
->method('persist')
37+
->willReturnCallback(function ($entity): void {
38+
echo $entity;
39+
});
40+
}
41+
}
42+
43+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\TestCase;
7+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
8+
9+
final class WithCallbackVoidSetup extends TestCase
10+
{
11+
private MockObject $entityManager;
12+
13+
protected function setUp(): void
14+
{
15+
$this->entityManager = $this->createMock(SomeEntityManager::class);
16+
}
17+
18+
public function test(): void
19+
{
20+
$this->entityManager
21+
->method('persist')
22+
->with($this->callback(function ($entity): bool {
23+
$this->assertInstanceOf(\stdClass::class, $entity);
24+
25+
return true;
26+
}));
27+
}
28+
}
29+
30+
?>
31+
-----
32+
<?php
33+
34+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Fixture;
35+
36+
use PHPUnit\Framework\MockObject\MockObject;
37+
use PHPUnit\Framework\TestCase;
38+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source\SomeEntityManager;
39+
40+
final class WithCallbackVoidSetup extends TestCase
41+
{
42+
private MockObject $entityManager;
43+
44+
protected function setUp(): void
45+
{
46+
$this->entityManager = $this->createMock(SomeEntityManager::class);
47+
}
48+
49+
public function test(): void
50+
{
51+
$this->entityManager
52+
->method('persist')
53+
->with($this->callback(function ($entity): void {
54+
$this->assertInstanceOf(\stdClass::class, $entity);
55+
}));
56+
}
57+
}
58+
59+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveReturnFromVoidMethodMockCallbackRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector\Source;
4+
5+
// non final on purpose so PHPStan can analyze it
6+
class SomeEntityManager
7+
{
8+
public function persist(object $entity): void
9+
{
10+
}
11+
12+
public function count(string $name): int
13+
{
14+
return 100;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveReturnFromVoidMethodMockCallbackRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(RemoveReturnFromVoidMethodMockCallbackRector::class);
10+
};

0 commit comments

Comments
 (0)