Skip to content

Commit 7c6f6c4

Browse files
committed
[code-quality] Add RemoveNeverUsedMockPropertyRector
1 parent 1993f6b commit 7c6f6c4

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
EntityDocumentCreateMockToDirectNewRector::class,
149149
ReplaceAtMethodWithDesiredMatcherRector::class,
150150
BareCreateMockAssignToDirectUseRector::class,
151+
\Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector::class,
151152

152153
// readability
153154
NoSetupWithParentCallOverrideRector::class,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class SomeTest extends TestCase
9+
{
10+
private MockObject $mockProperty;
11+
12+
protected function setUp(): void
13+
{
14+
$this->mockProperty = $this->createMock(\stdClass::class);
15+
$this->mockProperty->expects($this->once())
16+
->method('someMethod')
17+
->willReturn('someValue');
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector\Fixture;
26+
27+
use PHPUnit\Framework\MockObject\MockObject;
28+
use PHPUnit\Framework\TestCase;
29+
30+
final class SomeTest extends TestCase
31+
{
32+
protected function setUp(): void
33+
{
34+
}
35+
}
36+
37+
?>
Lines changed: 28 additions & 0 deletions
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_\RemoveNeverUsedMockPropertyRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveNeverUsedMockPropertyRectorTest 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->rule(\Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector::class);
9+
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
11+
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder;
12+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
13+
use Rector\Rector\AbstractRector;
14+
use Rector\ValueObject\MethodName;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector\RemoveNeverUsedMockPropertyRectorTest
20+
*/
21+
final class RemoveNeverUsedMockPropertyRector extends AbstractRector
22+
{
23+
public function __construct(
24+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
25+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
26+
private readonly AttrinationFinder $attrinationFinder
27+
) {
28+
}
29+
30+
public function getRuleDefinition(): RuleDefinition
31+
{
32+
return new RuleDefinition(
33+
'Remove never used property mock, only to set expectations',
34+
[
35+
new CodeSample(
36+
<<<'CODE_SAMPLE'
37+
use PHPUnit\Framework\TestCase;
38+
39+
final class SomeTest extends TestCase
40+
{
41+
private $mockProperty = null;
42+
43+
protected function setUp(): void
44+
{
45+
$this->mockProperty = $this->createMock(SomeClass::class);
46+
$this->mockProperty->expects($this->once())
47+
->method('someMethod')
48+
->willReturn('someValue');
49+
}
50+
}
51+
CODE_SAMPLE
52+
53+
,
54+
<<<'CODE_SAMPLE'
55+
use PHPUnit\Framework\TestCase;
56+
57+
final class SomeTest extends TestCase
58+
{
59+
protected function setUp(): void
60+
{
61+
}
62+
}
63+
CODE_SAMPLE
64+
),
65+
]
66+
);
67+
}
68+
69+
/**
70+
* @return array<class-string<Node>>
71+
*/
72+
public function getNodeTypes(): array
73+
{
74+
return [Class_::class];
75+
}
76+
77+
/**
78+
* @param Class_ $node
79+
*/
80+
public function refactor(Node $node): ?Node
81+
{
82+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
83+
return null;
84+
}
85+
86+
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
87+
if (! $setUpClassMethod instanceof ClassMethod) {
88+
return null;
89+
}
90+
91+
return $node;
92+
}
93+
}

0 commit comments

Comments
 (0)