-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathGetMethodToAsTwigAttributeTransformer.php
More file actions
136 lines (110 loc) · 4.4 KB
/
GetMethodToAsTwigAttributeTransformer.php
File metadata and controls
136 lines (110 loc) · 4.4 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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony73;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Symfony\Enum\TwigClass;
use Rector\Symfony\Symfony73\NodeAnalyzer\LocalArrayMethodCallableMatcher;
use Rector\Symfony\Symfony73\NodeRemover\ReturnEmptyArrayMethodRemover;
/**
* @see https://symfony.com/blog/new-in-symfony-7-3-twig-extension-attributes
*/
final readonly class GetMethodToAsTwigAttributeTransformer
{
public function __construct(
private LocalArrayMethodCallableMatcher $localArrayMethodCallableMatcher,
private ReturnEmptyArrayMethodRemover $returnEmptyArrayMethodRemover,
private ReflectionProvider $reflectionProvider,
private VisibilityManipulator $visibilityManipulator
) {
}
public function transformClassGetMethodToAttributeMarker(
Class_ $class,
string $methodName,
string $attributeClass,
ObjectType $objectType
): bool {
// check if attribute even exists
if (! $this->reflectionProvider->hasClass($attributeClass)) {
return false;
}
$getMethod = $class->getMethod($methodName);
if (! $getMethod instanceof ClassMethod) {
return false;
}
$originalMethod = clone $getMethod;
$hasChanged = false;
foreach ((array) $getMethod->stmts as $stmt) {
// handle return array simple case
if (! $stmt instanceof Return_) {
continue;
}
if (! $stmt->expr instanceof Array_) {
continue;
}
$returnArray = $stmt->expr;
foreach ($returnArray->items as $key => $arrayItem) {
if (! $arrayItem->value instanceof New_) {
continue;
}
if ($arrayItem->value->isFirstClassCallable()) {
continue;
}
$new = $arrayItem->value;
if (count($new->getArgs()) !== 2) {
continue;
}
$nameArg = $new->getArgs()[0];
if (! $nameArg->value instanceof String_) {
continue;
}
$secondArg = $new->getArgs()[1];
if ($this->isLocalCallable($secondArg->value)) {
$localMethodName = $this->localArrayMethodCallableMatcher->match($secondArg->value, $objectType);
if (! is_string($localMethodName)) {
$getMethod = $originalMethod;
return false;
}
$localMethod = $class->getMethod($localMethodName);
if (! $localMethod instanceof ClassMethod) {
continue;
}
$this->decorateMethodWithAttribute($localMethod, $attributeClass, $nameArg);
$this->visibilityManipulator->makePublic($localMethod);
// remove old new function instance
unset($returnArray->items[$key]);
$hasChanged = true;
}
}
$this->returnEmptyArrayMethodRemover->removeClassMethodIfArrayEmpty($class, $returnArray, $methodName);
}
if ($hasChanged && $class->extends instanceof FullyQualified && $class->extends->toString() === TwigClass::TWIG_EXTENSION) {
$class->extends = null;
}
return $hasChanged;
}
private function decorateMethodWithAttribute(ClassMethod $classMethod, string $attributeClass, Arg $arg): void
{
$classMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified($attributeClass), [$arg])]);
}
private function isLocalCallable(Expr $expr): bool
{
if ($expr instanceof MethodCall && $expr->isFirstClassCallable()) {
return true;
}
return $expr instanceof Array_ && count($expr->items) === 2;
}
}