forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHPUnitStaticToKernelTestCaseGetRector.php
329 lines (273 loc) · 9.93 KB
/
PHPUnitStaticToKernelTestCaseGetRector.php
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
declare(strict_types=1);
namespace Rector\RemovingStatic\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeManipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPUnit\NodeFactory\SetUpClassMethodFactory;
use Rector\RemovingStatic\NodeAnalyzer\SetUpClassMethodUpdater;
use Rector\RemovingStatic\NodeFactory\SelfContainerFactory;
use Rector\RemovingStatic\NodeFactory\SetUpFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector\PHPUnitStaticToKernelTestCaseGetRectorTest
*/
final class PHPUnitStaticToKernelTestCaseGetRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @api
* @var string
*/
public const STATIC_CLASS_TYPES = 'static_class_types';
/**
* @var ObjectType[]
*/
private $staticObjectTypes = [];
/**
* @var ObjectType[]
*/
private $newPropertyObjectTypes = [];
/**
* @var PropertyNaming
*/
private $propertyNaming;
/**
* @var ClassInsertManipulator
*/
private $classInsertManipulator;
/**
* @var SetUpClassMethodFactory
*/
private $setUpClassMethodFactory;
/**
* @var SetUpFactory
*/
private $setUpFactory;
/**
* @var SelfContainerFactory
*/
private $selfContainerFactory;
/**
* @var SetUpClassMethodUpdater
*/
private $setUpClassMethodUpdater;
public function __construct(
PropertyNaming $propertyNaming,
ClassInsertManipulator $classInsertManipulator,
SetUpClassMethodFactory $setUpClassMethodFactory,
SetUpFactory $setUpFactory,
SelfContainerFactory $selfContainerFactory,
SetUpClassMethodUpdater $setUpClassMethodUpdater
) {
$this->propertyNaming = $propertyNaming;
$this->classInsertManipulator = $classInsertManipulator;
$this->setUpClassMethodFactory = $setUpClassMethodFactory;
$this->setUpFactory = $setUpFactory;
$this->selfContainerFactory = $selfContainerFactory;
$this->setUpClassMethodUpdater = $setUpClassMethodUpdater;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Convert static calls in PHPUnit test cases, to get() from the container of KernelTestCase', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
<?php
use PHPUnit\Framework\TestCase;
final class SomeTestCase extends TestCase
{
public function test()
{
$product = EntityFactory::create('product');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
final class SomeTestCase extends KernelTestCase
{
/**
* @var EntityFactory
*/
private $entityFactory;
protected function setUp(): void
{
parent::setUp();
$this->entityFactory = $this->getService(EntityFactory::class);
}
public function test()
{
$product = $this->entityFactory->create('product');
}
}
CODE_SAMPLE
,
[
self::STATIC_CLASS_TYPES => ['EntityFactory'],
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class, Class_::class];
}
/**
* @param StaticCall|Class_ $node
*/
public function refactor(Node $node): ?Node
{
// skip yourself
$this->newPropertyObjectTypes = [];
if ($node instanceof Class_) {
if ($this->nodeTypeResolver->isObjectTypes($node, $this->staticObjectTypes)) {
return null;
}
return $this->processClass($node);
}
return $this->processStaticCall($node);
}
/**
* @param array<string, mixed> $configuration
*/
public function configure(array $configuration): void
{
$staticClassTypes = $configuration[self::STATIC_CLASS_TYPES] ?? [];
foreach ($staticClassTypes as $staticClassType) {
$this->staticObjectTypes[] = new ObjectType($staticClassType);
}
}
private function processClass(Class_ $class): ?Class_
{
if ($this->isObjectType($class, new ObjectType('PHPUnit\Framework\TestCase'))) {
return $this->processPHPUnitClass($class);
}
// add property with the object
$newPropertyObjectTypes = $this->collectNewPropertyObjectTypes($class);
if ($newPropertyObjectTypes === []) {
return null;
}
// add via constructor
foreach ($newPropertyObjectTypes as $newPropertyObjectType) {
$newPropertyName = $this->propertyNaming->fqnToVariableName($newPropertyObjectType);
$this->addConstructorDependencyToClass($class, $newPropertyObjectType, $newPropertyName);
}
return $class;
}
private function processStaticCall(StaticCall $staticCall): ?MethodCall
{
$classLike = $staticCall->getAttribute(AttributeKey::CLASS_NODE);
if (! $classLike instanceof Class_) {
return null;
}
foreach ($this->staticObjectTypes as $staticObjectType) {
if (! $this->isObjectType($staticCall->class, $staticObjectType)) {
continue;
}
return $this->convertStaticCallToPropertyMethodCall($staticCall, $staticObjectType);
}
return null;
}
private function processPHPUnitClass(Class_ $class): ?Class_
{
// add property with the object
$newPropertyTypes = $this->collectNewPropertyObjectTypes($class);
if ($newPropertyTypes === []) {
return null;
}
// add all properties to class
$class = $this->addNewPropertiesToClass($class, $newPropertyTypes);
$parentSetUpStaticCallExpression = $this->setUpFactory->createParentStaticCall();
foreach ($newPropertyTypes as $newPropertyType) {
// container fetch assign
$assign = $this->createContainerGetTypeToPropertyAssign($newPropertyType);
$setupClassMethod = $class->getMethod(MethodName::SET_UP);
// get setup or create a setup add add it there
if ($setupClassMethod !== null) {
$this->setUpClassMethodUpdater->updateSetUpMethod(
$setupClassMethod,
$parentSetUpStaticCallExpression,
$assign
);
} else {
$setUpMethod = $this->setUpClassMethodFactory->createSetUpMethod([$assign]);
$this->classInsertManipulator->addAsFirstMethod($class, $setUpMethod);
}
}
// update parent clsas if not already
if (! $this->isObjectType($class, new ObjectType('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'))) {
$class->extends = new FullyQualified('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase');
}
return $class;
}
/**
* @return ObjectType[]
*/
private function collectNewPropertyObjectTypes(Class_ $class): array
{
$this->newPropertyObjectTypes = [];
$this->traverseNodesWithCallable($class->stmts, function (Node $node): void {
if (! $node instanceof StaticCall) {
return;
}
foreach ($this->staticObjectTypes as $staticObjectType) {
if (! $this->isObjectType($node->class, $staticObjectType)) {
continue;
}
$this->newPropertyObjectTypes[] = $staticObjectType;
}
});
$this->newPropertyObjectTypes = array_unique($this->newPropertyObjectTypes);
return $this->newPropertyObjectTypes;
}
private function convertStaticCallToPropertyMethodCall(StaticCall $staticCall, ObjectType $objectType): MethodCall
{
// create "$this->someService" instead
$propertyName = $this->propertyNaming->fqnToVariableName($objectType);
$propertyFetch = new PropertyFetch(new Variable('this'), $propertyName);
// turn static call to method on property call
$methodCall = new MethodCall($propertyFetch, $staticCall->name);
$methodCall->args = $staticCall->args;
return $methodCall;
}
/**
* @param ObjectType[] $propertyTypes
*/
private function addNewPropertiesToClass(Class_ $class, array $propertyTypes): Class_
{
$properties = [];
foreach ($propertyTypes as $propertyType) {
$propertyName = $this->propertyNaming->fqnToVariableName($propertyType);
$properties[] = $this->nodeFactory->createPrivatePropertyFromNameAndType($propertyName, $propertyType);
}
// add property to the start of the class
$class->stmts = array_merge($properties, $class->stmts);
return $class;
}
private function createContainerGetTypeToPropertyAssign(ObjectType $objectType): Expression
{
$getMethodCall = $this->selfContainerFactory->createGetTypeMethodCall($objectType);
$propertyName = $this->propertyNaming->fqnToVariableName($objectType);
$propertyFetch = new PropertyFetch(new Variable('this'), $propertyName);
$assign = new Assign($propertyFetch, $getMethodCall);
return new Expression($assign);
}
}