forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagicClosureTwigExtensionToNativeMethodsRector.php
155 lines (130 loc) · 4.35 KB
/
MagicClosureTwigExtensionToNativeMethodsRector.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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony61\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\Symfony61\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector\MagicClosureTwigExtensionToNativeMethodsRectorTest
*
* @see PHP 8.1 way to handle functions/filters https://github.com/symfony/symfony/blob/e0ad2eead3513a558c09d8aa3ae9e867fb10b419/src/Symfony/Bridge/Twig/Extension/CodeExtension.php#L41-L52
*/
final class MagicClosureTwigExtensionToNativeMethodsRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change TwigExtension function/filter magic closures to inlined and clear callables',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class TerminologyExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('resolve', [$this, 'resolve']);
];
}
private function resolve($value)
{
return $value + 100;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class TerminologyExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('resolve', $this->resolve(...)),
];
}
private function resolve($value)
{
return $value + 100;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if (! $this->nodeTypeResolver->isObjectTypes($node, [
new ObjectType('Twig_ExtensionInterface'),
new ObjectType('Twig\Extension\ExtensionInterface'),
])) {
return null;
}
$hasFunctionsChanged = false;
$getFunctionsClassMethod = $node->getMethod('getFunctions');
if ($getFunctionsClassMethod instanceof ClassMethod) {
$hasFunctionsChanged = $this->refactorClassMethod($getFunctionsClassMethod, $scope);
}
$hasFiltersChanged = false;
$getFiltersClassMethod = $node->getMethod('getFilters');
if ($getFiltersClassMethod instanceof ClassMethod) {
$hasFiltersChanged = $this->refactorClassMethod($getFiltersClassMethod, $scope);
}
if ($hasFiltersChanged || $hasFunctionsChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_81;
}
private function refactorClassMethod(ClassMethod $classMethod, Scope $scope): bool
{
$hasChanged = false;
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$hasChanged, $scope): ?Node {
if (! $node instanceof Array_) {
return null;
}
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);
if (! $arrayCallable instanceof ArrayCallable) {
return null;
}
$hasChanged = true;
return new MethodCall($arrayCallable->getCallerExpr(), $arrayCallable->getMethod(), [
new VariadicPlaceholder(),
]);
});
return $hasChanged;
}
}