Repository navigation
Expand file tree
/
Copy pathAddIntersectionVarToMockObjectPropertyRector.php
More file actions
159 lines (133 loc) · 4.74 KB
/
Copy pathAddIntersectionVarToMockObjectPropertyRector.php
File metadata and controls
159 lines (133 loc) · 4.74 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
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddIntersectionVarToMockObjectPropertyRector\AddIntersectionVarToMockObjectPropertyRectorTest
*/
final class AddIntersectionVarToMockObjectPropertyRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MockObjectPropertyDetector $mockObjectPropertyDetector,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
) {
}
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
return null;
}
$propertyNamesToCreateMockMethodCalls = $this->mockObjectPropertyDetector->collectFromClassMethod(
$setUpClassMethod
);
if ($propertyNamesToCreateMockMethodCalls === []) {
return null;
}
$hasChanged = false;
foreach ($propertyNamesToCreateMockMethodCalls as $propertyName => $createMockMethodCall) {
$property = $node->getProperty($propertyName);
if (! $property instanceof Property) {
continue;
}
// only properties typed as a bare native MockObject
if (! $this->mockObjectPropertyDetector->detect($property)) {
continue;
}
$mockedClass = $this->resolveMockedClass($createMockMethodCall);
if ($mockedClass === null) {
continue;
}
$intersectionTypeNode = new BracketsAwareIntersectionTypeNode([
new IdentifierTypeNode('\\' . PHPUnitClassName::MOCK_OBJECT),
new IdentifierTypeNode('\\' . $mockedClass),
]);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$this->phpDocTypeChanger->changeVarTypeNode($property, $phpDocInfo, $intersectionTypeNode);
$hasChanged = true;
}
if (! $hasChanged) {
return null;
}
return $node;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add a MockObject intersection @var docblock with the mocked class to a native MockObject property',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
protected function setUp(): void
{
$this->someServiceMock = $this->createMock(SomeService::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @var \PHPUnit\Framework\MockObject\MockObject&\SomeService
*/
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
protected function setUp(): void
{
$this->someServiceMock = $this->createMock(SomeService::class);
}
}
CODE_SAMPLE
),
]
);
}
private function resolveMockedClass(MethodCall $methodCall): ?string
{
$firstArg = $methodCall->getArgs()[0] ?? null;
if ($firstArg === null) {
return null;
}
if (! $firstArg->value instanceof ClassConstFetch) {
return null;
}
$className = $this->getName($firstArg->value->class);
if (! is_string($className)) {
return null;
}
return $className;
}
}