Repository navigation
Expand file tree
/
Copy pathAddStubIntersectionVarToStubPropertyRector.php
More file actions
233 lines (198 loc) · 7.37 KB
/
Copy pathAddStubIntersectionVarToStubPropertyRector.php
File metadata and controls
233 lines (198 loc) · 7.37 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?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\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
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\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\MethodName;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddStubIntersectionVarToStubPropertyRector\AddStubIntersectionVarToStubPropertyRectorTest
*/
final class AddStubIntersectionVarToStubPropertyRector extends AbstractRector implements ComposerPackageConstraintInterface
{
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);
$propertyNamesToCreateStubCalls = $setUpClassMethod instanceof ClassMethod
? $this->mockObjectPropertyDetector->collectFromClassMethod($setUpClassMethod, 'createStub')
: [];
$hasChanged = false;
foreach ($node->getProperties() as $property) {
// only properties typed as a native Stub
if (! $this->mockObjectPropertyDetector->detect($property, PHPUnitClassName::STUB)) {
continue;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
// already has an intersection @var, skip
if ($varTagValueNode instanceof VarTagValueNode && $varTagValueNode->type instanceof IntersectionTypeNode) {
continue;
}
$intersectionTypeNode = $this->resolveStubIntersection(
$property,
$propertyNamesToCreateStubCalls,
$varTagValueNode
);
if (! $intersectionTypeNode instanceof BracketsAwareIntersectionTypeNode) {
continue;
}
$this->phpDocTypeChanger->changeVarTypeNode($property, $phpDocInfo, $intersectionTypeNode);
$hasChanged = true;
}
if (! $hasChanged) {
return null;
}
return $node;
}
public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
return new ComposerPackageConstraint('phpunit/phpunit', '>=11.0');
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add a Stub intersection @var docblock with the stubbed class to a native Stub property',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
protected function setUp(): void
{
$this->someServiceStub = $this->createStub(SomeService::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @var \PHPUnit\Framework\MockObject\Stub&\SomeService
*/
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
protected function setUp(): void
{
$this->someServiceStub = $this->createStub(SomeService::class);
}
}
CODE_SAMPLE
),
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @var SomeService
*/
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @var \PHPUnit\Framework\MockObject\Stub&SomeService
*/
private \PHPUnit\Framework\MockObject\Stub $someServiceStub;
}
CODE_SAMPLE
),
]
);
}
/**
* @param array<string, MethodCall|StaticCall> $propertyNamesToCreateStubCalls
*/
private function resolveStubIntersection(
Property $property,
array $propertyNamesToCreateStubCalls,
?VarTagValueNode $varTagValueNode
): ?BracketsAwareIntersectionTypeNode {
$propertyName = $property->props[0]->name->toString();
// 1. prefer the stubbed class from a setUp() createStub() call
$createStubCall = $propertyNamesToCreateStubCalls[$propertyName] ?? null;
if ($createStubCall !== null) {
$stubbedClass = $this->resolveStubbedClass($createStubCall);
if ($stubbedClass !== null) {
return new BracketsAwareIntersectionTypeNode([
new IdentifierTypeNode('\\' . PHPUnitClassName::STUB),
new IdentifierTypeNode('\\' . $stubbedClass),
]);
}
}
// 2. fall back to a bare single-class @var docblock
if ($varTagValueNode instanceof VarTagValueNode && $varTagValueNode->type instanceof IdentifierTypeNode) {
// skip Stub/MockObject themselves, only real stubbed class types
if (in_array($this->resolveShortName($varTagValueNode->type->name), ['Stub', 'MockObject'], true)) {
return null;
}
return new BracketsAwareIntersectionTypeNode([
new IdentifierTypeNode('\\' . PHPUnitClassName::STUB),
$varTagValueNode->type,
]);
}
return null;
}
private function resolveShortName(string $name): string
{
$lastBackslashPosition = strrpos($name, '\\');
return $lastBackslashPosition === false ? $name : substr($name, $lastBackslashPosition + 1);
}
private function resolveStubbedClass(MethodCall|StaticCall $createStubCall): ?string
{
$firstArg = $createStubCall->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;
}
}