-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
NodeFactory.php
632 lines (521 loc) · 19.3 KB
/
NodeFactory.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php
declare(strict_types=1);
namespace Rector\Core\PhpParser\Node;
use PhpParser\BuilderFactory;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Error;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeDecorator\PropertyTypeDecorator;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\Astral\ValueObject\NodeBuilder\MethodBuilder;
use Symplify\Astral\ValueObject\NodeBuilder\ParamBuilder;
use Symplify\Astral\ValueObject\NodeBuilder\PropertyBuilder;
/**
* @see \Rector\Core\Tests\PhpParser\Node\NodeFactoryTest
*/
final class NodeFactory
{
/**
* @var string
*/
private const THIS = 'this';
public function __construct(
private readonly BuilderFactory $builderFactory,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly NodeNameResolver $nodeNameResolver,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly CurrentNodeProvider $currentNodeProvider,
private readonly PropertyTypeDecorator $propertyTypeDecorator
) {
}
/**
* Creates "SomeClass::CONSTANT"
*/
public function createShortClassConstFetch(string $shortClassName, string $constantName): ClassConstFetch
{
$name = new Name($shortClassName);
return $this->createClassConstFetchFromName($name, $constantName);
}
/**
* @param string|ObjectReference::* $className
* Creates "\SomeClass::CONSTANT"
*/
public function createClassConstFetch(string $className, string $constantName): ClassConstFetch
{
$name = $this->createName($className);
return $this->createClassConstFetchFromName($name, $constantName);
}
/**
* @param string|ObjectReference::* $className
* Creates "\SomeClass::class"
*/
public function createClassConstReference(string $className): ClassConstFetch
{
return $this->createClassConstFetch($className, 'class');
}
/**
* Creates "['item', $variable]"
*
* @param mixed[] $items
*/
public function createArray(array $items): Array_
{
$arrayItems = [];
$defaultKey = 0;
foreach ($items as $key => $item) {
$customKey = $key !== $defaultKey ? $key : null;
$arrayItems[] = $this->createArrayItem($item, $customKey);
++$defaultKey;
}
return new Array_($arrayItems);
}
/**
* Creates "($args)"
*
* @param mixed[] $values
* @return Arg[]
*/
public function createArgs(array $values): array
{
foreach ($values as $value) {
$this->normalizeArgValue($value);
}
return $this->builderFactory->args($values);
}
/**
* Creates $this->property = $property;
*/
public function createPropertyAssignment(string $propertyName): Assign
{
$variable = new Variable($propertyName);
return $this->createPropertyAssignmentWithExpr($propertyName, $variable);
}
public function createPropertyAssignmentWithExpr(string $propertyName, Expr $expr): Assign
{
$propertyFetch = $this->createPropertyFetch(self::THIS, $propertyName);
return new Assign($propertyFetch, $expr);
}
public function createArg(mixed $argument): Arg
{
return new Arg(BuilderHelpers::normalizeValue($argument));
}
public function createPublicMethod(string $name): ClassMethod
{
$methodBuilder = new MethodBuilder($name);
$methodBuilder->makePublic();
return $methodBuilder->getNode();
}
public function createParamFromNameAndType(string $name, ?Type $type): Param
{
$paramBuilder = new ParamBuilder($name);
if ($type !== null) {
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM);
if ($typeNode !== null) {
$paramBuilder->setType($typeNode);
}
}
return $paramBuilder->getNode();
}
public function createPublicInjectPropertyFromNameAndType(string $name, ?Type $type): Property
{
$propertyBuilder = new PropertyBuilder($name);
$propertyBuilder->makePublic();
$property = $propertyBuilder->getNode();
$this->propertyTypeDecorator->decorate($property, $type);
// add @inject
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@inject', new GenericTagValueNode('')));
return $property;
}
public function createPrivatePropertyFromNameAndType(string $name, ?Type $type): Property
{
$propertyBuilder = new PropertyBuilder($name);
$propertyBuilder->makePrivate();
$property = $propertyBuilder->getNode();
$this->propertyTypeDecorator->decorate($property, $type);
return $property;
}
/**
* @param mixed[] $arguments
*/
public function createLocalMethodCall(string $method, array $arguments = []): MethodCall
{
$variable = new Variable('this');
return $this->createMethodCall($variable, $method, $arguments);
}
/**
* @param mixed[] $arguments
*/
public function createMethodCall(Expr|string $exprOrVariableName, string $method, array $arguments = []): MethodCall
{
$callerExpr = $this->createMethodCaller($exprOrVariableName);
return $this->builderFactory->methodCall($callerExpr, $method, $arguments);
}
public function createPropertyFetch(string | Expr $variableNameOrExpr, string $property): PropertyFetch
{
$fetcherExpr = is_string($variableNameOrExpr) ? new Variable($variableNameOrExpr) : $variableNameOrExpr;
return $this->builderFactory->propertyFetch($fetcherExpr, $property);
}
/**
* @param Param[] $params
*/
public function createParentConstructWithParams(array $params): StaticCall
{
return new StaticCall(
new Name(ObjectReference::PARENT),
new Identifier(MethodName::CONSTRUCT),
$this->createArgsFromParams($params)
);
}
public function createProperty(string $name): Property
{
$propertyBuilder = new PropertyBuilder($name);
$property = $propertyBuilder->getNode();
$this->phpDocInfoFactory->createFromNode($property);
return $property;
}
public function createPrivateProperty(string $name): Property
{
$propertyBuilder = new PropertyBuilder($name);
$propertyBuilder->makePrivate();
$property = $propertyBuilder->getNode();
$this->phpDocInfoFactory->createFromNode($property);
return $property;
}
public function createPublicProperty(string $name): Property
{
$propertyBuilder = new PropertyBuilder($name);
$propertyBuilder->makePublic();
$property = $propertyBuilder->getNode();
$this->phpDocInfoFactory->createFromNode($property);
return $property;
}
public function createGetterClassMethod(string $propertyName, Type $type): ClassMethod
{
$methodBuilder = new MethodBuilder('get' . ucfirst($propertyName));
$methodBuilder->makePublic();
$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
$return = new Return_($propertyFetch);
$methodBuilder->addStmt($return);
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::RETURN);
if ($typeNode !== null) {
$methodBuilder->setReturnType($typeNode);
}
return $methodBuilder->getNode();
}
public function createSetterClassMethod(string $propertyName, Type $type): ClassMethod
{
$methodBuilder = new MethodBuilder('set' . ucfirst($propertyName));
$methodBuilder->makePublic();
$variable = new Variable($propertyName);
$param = $this->createParamWithType($variable, $type);
$methodBuilder->addParam($param);
$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
$assign = new Assign($propertyFetch, $variable);
$methodBuilder->addStmt($assign);
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::VOID_TYPE)) {
$methodBuilder->setReturnType(new Name('void'));
}
return $methodBuilder->getNode();
}
/**
* @param Expr[] $exprs
*/
public function createConcat(array $exprs): ?Concat
{
if (count($exprs) < 2) {
return null;
}
$previousConcat = array_shift($exprs);
foreach ($exprs as $expr) {
$previousConcat = new Concat($previousConcat, $expr);
}
if (! $previousConcat instanceof Concat) {
throw new ShouldNotHappenException();
}
return $previousConcat;
}
public function createClosureFromClassMethod(ClassMethod $classMethod): Closure
{
$classMethodName = $this->nodeNameResolver->getName($classMethod);
$args = $this->createArgs($classMethod->params);
$methodCall = new MethodCall(new Variable(self::THIS), $classMethodName, $args);
$return = new Return_($methodCall);
return new Closure([
'params' => $classMethod->params,
'stmts' => [$return],
'returnType' => $classMethod->returnType,
]);
}
/**
* @param string[] $names
* @return Use_[]
*/
public function createUsesFromNames(array $names): array
{
$uses = [];
foreach ($names as $name) {
$useUse = new UseUse(new Name($name));
$uses[] = new Use_([$useUse]);
}
return $uses;
}
/**
* @param string|ObjectReference::* $class
* @param Node[] $args
*/
public function createStaticCall(string $class, string $method, array $args = []): StaticCall
{
$name = $this->createName($class);
$args = $this->createArgs($args);
return new StaticCall($name, $method, $args);
}
/**
* @param mixed[] $arguments
*/
public function createFuncCall(string $name, array $arguments = []): FuncCall
{
$arguments = $this->createArgs($arguments);
return new FuncCall(new Name($name), $arguments);
}
public function createSelfFetchConstant(string $constantName): ClassConstFetch
{
$name = new Name(ObjectReference::SELF);
return new ClassConstFetch($name, $constantName);
}
/**
* @param Param[] $params
* @return Arg[]
*/
public function createArgsFromParams(array $params): array
{
$args = [];
foreach ($params as $param) {
$args[] = new Arg($param->var);
}
return $args;
}
public function createNull(): ConstFetch
{
return new ConstFetch(new Name('null'));
}
public function createPromotedPropertyParam(PropertyMetadata $propertyMetadata): Param
{
$paramBuilder = new ParamBuilder($propertyMetadata->getName());
$propertyType = $propertyMetadata->getType();
if ($propertyType !== null) {
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
if ($typeNode !== null) {
$paramBuilder->setType($typeNode);
}
}
$param = $paramBuilder->getNode();
$propertyFlags = $propertyMetadata->getFlags();
$param->flags = $propertyFlags !== 0 ? $propertyFlags : Class_::MODIFIER_PRIVATE;
return $param;
}
public function createFalse(): ConstFetch
{
return new ConstFetch(new Name('false'));
}
public function createTrue(): ConstFetch
{
return new ConstFetch(new Name('true'));
}
/**
* @param string|ObjectReference::* $constantName
*/
public function createClassConstFetchFromName(Name $className, string $constantName): ClassConstFetch
{
$classConstFetch = $this->builderFactory->classConstFetch($className, $constantName);
$classNameString = $className->toString();
if (in_array($classNameString, [ObjectReference::SELF, ObjectReference::STATIC], true)) {
$currentNode = $this->currentNodeProvider->getNode();
if ($currentNode !== null) {
$classConstFetch->class->setAttribute(AttributeKey::RESOLVED_NAME, $className);
}
} else {
$classConstFetch->class->setAttribute(AttributeKey::RESOLVED_NAME, $classNameString);
}
return $classConstFetch;
}
/**
* @param array<NotIdentical|BooleanAnd> $newNodes
*/
public function createReturnBooleanAnd(array $newNodes): ?Expr
{
if ($newNodes === []) {
return null;
}
if (count($newNodes) === 1) {
return $newNodes[0];
}
return $this->createBooleanAndFromNodes($newNodes);
}
public function createClassConstant(string $name, Expr $expr, int $modifier): ClassConst
{
$expr = BuilderHelpers::normalizeValue($expr);
$const = new Const_($name, $expr);
$classConst = new ClassConst([$const]);
$classConst->flags |= $modifier;
// add @var type by default
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($expr);
if (! $staticType instanceof MixedType) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classConst);
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $staticType);
}
return $classConst;
}
private function createArrayItem(mixed $item, string | int | null $key = null): ArrayItem
{
$arrayItem = null;
if ($item instanceof Variable
|| $item instanceof MethodCall
|| $item instanceof StaticCall
|| $item instanceof FuncCall
|| $item instanceof Concat
|| $item instanceof Scalar
|| $item instanceof Cast
) {
$arrayItem = new ArrayItem($item);
} elseif ($item instanceof Identifier) {
$string = new String_($item->toString());
$arrayItem = new ArrayItem($string);
} elseif (is_scalar($item) || $item instanceof Array_) {
$itemValue = BuilderHelpers::normalizeValue($item);
$arrayItem = new ArrayItem($itemValue);
} elseif (is_array($item)) {
$arrayItem = new ArrayItem($this->createArray($item));
}
if ($item === null || $item instanceof ClassConstFetch) {
$itemValue = BuilderHelpers::normalizeValue($item);
$arrayItem = new ArrayItem($itemValue);
}
if ($item instanceof Arg) {
$arrayItem = new ArrayItem($item->value);
}
if ($arrayItem instanceof ArrayItem) {
$this->decorateArrayItemWithKey($key, $arrayItem);
return $arrayItem;
}
$nodeClass = is_object($item) ? $item::class : $item;
throw new NotImplementedYetException(sprintf(
'Not implemented yet. Go to "%s()" and add check for "%s" node.',
__METHOD__,
(string) $nodeClass
));
}
/**
* @return mixed|Error|Variable
*/
private function normalizeArgValue(mixed $value)
{
if ($value instanceof Param) {
return $value->var;
}
return $value;
}
private function decorateArrayItemWithKey(int | string | null $key, ArrayItem $arrayItem): void
{
if ($key !== null) {
$arrayItem->key = BuilderHelpers::normalizeValue($key);
}
}
/**
* @param NotIdentical[]|BooleanAnd[] $exprs
*/
private function createBooleanAndFromNodes(array $exprs): BooleanAnd
{
/** @var NotIdentical|BooleanAnd $booleanAnd */
$booleanAnd = array_shift($exprs);
foreach ($exprs as $expr) {
$booleanAnd = new BooleanAnd($booleanAnd, $expr);
}
/** @var BooleanAnd $booleanAnd */
return $booleanAnd;
}
private function createParamWithType(Variable $variable, Type $type): Param
{
$param = new Param($variable);
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM);
return $param;
}
/**
* @param string|ObjectReference::* $className
*/
private function createName(string $className): Name|FullyQualified
{
if (in_array($className, [ObjectReference::PARENT, ObjectReference::SELF, ObjectReference::STATIC], true)) {
return new Name($className);
}
return new FullyQualified($className);
}
private function createMethodCaller(
Expr|string $exprOrVariableName
): PropertyFetch|Variable|MethodCall|StaticPropertyFetch|Expr {
if (is_string($exprOrVariableName)) {
return new Variable($exprOrVariableName);
}
if ($exprOrVariableName instanceof PropertyFetch) {
return new PropertyFetch($exprOrVariableName->var, $exprOrVariableName->name);
}
if ($exprOrVariableName instanceof StaticPropertyFetch) {
return new StaticPropertyFetch($exprOrVariableName->class, $exprOrVariableName->name);
}
if ($exprOrVariableName instanceof MethodCall) {
return new MethodCall($exprOrVariableName->var, $exprOrVariableName->name, $exprOrVariableName->args);
}
return $exprOrVariableName;
}
}