-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathSuffixMockObjectPropertyRector.php
More file actions
146 lines (122 loc) · 3.91 KB
/
SuffixMockObjectPropertyRector.php
File metadata and controls
146 lines (122 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\VarLikeIdentifier;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SuffixMockObjectPropertyRector\SuffixMockObjectPropertyRectorTest
*/
final class SuffixMockObjectPropertyRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MockObjectPropertyDetector $mockObjectPropertyDetector
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Suffix mock object property names with "Mock" to clearly separate from real objects later on',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class MockingEntity extends TestCase
{
private MockObject $simpleObject;
protected function setUp(): void
{
$this->simpleObject = $this->createMock(SimpleObject::class);
}
public function test()
{
$this->simpleObject->method('someMethod')->willReturn('someValue');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class MockingEntity extends TestCase
{
private MockObject $simpleObjectMock;
protected function setUp(): void
{
$this->simpleObjectMock = $this->createMock(SimpleObject::class);
}
public function test()
{
$this->simpleObjectMock->method('someMethod')->willReturn('someValue');
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$hasChanged = false;
foreach ($node->getProperties() as $property) {
if (! $this->mockObjectPropertyDetector->detect($property)) {
continue;
}
$propertyName = $this->getName($property);
if (str_ends_with($propertyName, 'Mock')) {
continue;
}
$newPropertyName = $propertyName . 'Mock';
$property->props[0]->name = new VarLikeIdentifier($newPropertyName);
$this->renamePropertyUsagesInClass($node, $propertyName, $newPropertyName);
$hasChanged = true;
}
if (! $hasChanged) {
return null;
}
return $node;
}
private function renamePropertyUsagesInClass(Class_ $class, string $oldPropertyName, string $newPropertyName): void
{
$this->traverseNodesWithCallable($class, function (Node $node) use (
$oldPropertyName,
$newPropertyName
): ?Node {
if (! $node instanceof PropertyFetch) {
return null;
}
// is local property?
if (! $node->var instanceof Variable && ! $this->isName($node->var, 'this')) {
return null;
}
if (! $this->isName($node->name, $oldPropertyName)) {
return null;
}
$node->name = new Identifier($newPropertyName);
return $node;
});
}
}