Skip to content

Commit 6b72511

Browse files
committed
[code-quality] Add RemoveStandaloneCreateMockRector
1 parent 9cae00e commit 6b72511

File tree

6 files changed

+175
-1
lines changed

6 files changed

+175
-1
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\EntityDocumentCreateMockToDirectNewRector;
2222
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector;
2323
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
24+
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector;
2425
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
2526
use Rector\PHPUnit\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector;
2627
use Rector\PHPUnit\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector;
@@ -149,7 +150,10 @@
149150
EntityDocumentCreateMockToDirectNewRector::class,
150151
ReplaceAtMethodWithDesiredMatcherRector::class,
151152
BareCreateMockAssignToDirectUseRector::class,
153+
154+
// dead code
152155
RemoveNeverUsedMockPropertyRector::class,
156+
RemoveStandaloneCreateMockRector::class,
153157

154158
// readability
155159
NoSetupWithParentCallOverrideRector::class,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\Fixture;
6+
7+
final class SomeTest extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testSomething(): void
10+
{
11+
$this->createMock('someClass');
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\Fixture;
22+
23+
final class SomeTest extends \PHPUnit\Framework\TestCase
24+
{
25+
public function testSomething(): void
26+
{
27+
}
28+
}
29+
30+
?>
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\ClassMethod\RemoveStandaloneCreateMockRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveStandaloneCreateMockRectorTest 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: 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\ClassMethod\RemoveStandaloneCreateMockRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(RemoveStandaloneCreateMockRector::class);
10+
};
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PhpParser\Node\Stmt\Expression;
11+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\RemoveStandaloneCreateMockRectorTest
18+
*/
19+
final class RemoveStandaloneCreateMockRector extends AbstractRector
20+
{
21+
public function __construct(
22+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
23+
) {
24+
}
25+
26+
public function getRuleDefinition(): RuleDefinition
27+
{
28+
return new RuleDefinition(
29+
'Remove standalone method calls with no effect',
30+
[
31+
new CodeSample(
32+
<<<'CODE_SAMPLE'
33+
use PHPUnit\Framework\TestCase;
34+
35+
final class SomeTest extends TestCase
36+
{
37+
public function test()
38+
{
39+
$this->createMock('SomeClass');
40+
}
41+
}
42+
CODE_SAMPLE
43+
44+
,
45+
<<<'CODE_SAMPLE'
46+
use PHPUnit\Framework\TestCase;
47+
48+
final class SomeTest extends TestCase
49+
{
50+
public function test()
51+
{
52+
}
53+
}
54+
CODE_SAMPLE
55+
),
56+
]
57+
);
58+
}
59+
60+
/**
61+
* @return array<class-string<Node>>
62+
*/
63+
public function getNodeTypes(): array
64+
{
65+
return [ClassMethod::class];
66+
}
67+
68+
/**
69+
* @param ClassMethod $node
70+
*/
71+
public function refactor(Node $node): ?Node
72+
{
73+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
74+
return null;
75+
}
76+
77+
$hasChanged = false;
78+
foreach ((array) $node->stmts as $key => $stmt) {
79+
if (! $stmt instanceof Expression) {
80+
continue;
81+
}
82+
83+
if (! $stmt->expr instanceof MethodCall) {
84+
continue;
85+
}
86+
87+
$methodCall = $stmt->expr;
88+
if (! $this->isName($methodCall->name, 'createMock')) {
89+
continue;
90+
}
91+
92+
unset($node->stmts[$key]);
93+
$hasChanged = true;
94+
}
95+
96+
if ($hasChanged) {
97+
return $node;
98+
}
99+
100+
return null;
101+
}
102+
}

rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Rector\PHPUnit\PHPUnit120\Rector\MethodCall;
66

7-
use PhpParser\Node\Stmt\ClassMethod;
87
use PhpParser\Node;
98
use PhpParser\Node\Arg;
109
use PhpParser\Node\Expr\MethodCall;
1110
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Stmt\ClassMethod;
1212
use PHPStan\Type\ObjectType;
1313
use Rector\PHPUnit\Enum\PHPUnitClassName;
1414
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;

0 commit comments

Comments
 (0)