Repository navigation
Expand file tree
/
Copy pathInnerFunctionToPrivateMethodRector.php
More file actions
222 lines (185 loc) · 6.2 KB
/
Copy pathInnerFunctionToPrivateMethodRector.php
File metadata and controls
222 lines (185 loc) · 6.2 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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Class_;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Class_\InnerFunctionToPrivateMethodRector\InnerFunctionToPrivateMethodRectorTest
*/
final class InnerFunctionToPrivateMethodRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turn an inner named function declared inside a class method into a private method, as inner named functions are not supported by PHPStan',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(): void
{
function inner(): void
{
echo 'hello';
}
inner();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run(): void
{
$this->inner();
}
private function inner(): void
{
echo 'hello';
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$existingMethodNames = array_map(
fn (ClassMethod $classMethod): string => $classMethod->name->toString(),
$node->getMethods()
);
$newClassMethods = [];
foreach ($node->getMethods() as $classMethod) {
$innerFunctions = $this->extractInnerFunctions($classMethod, $existingMethodNames);
if ($innerFunctions === []) {
continue;
}
$isStatic = $classMethod->isStatic();
$innerFunctionNames = array_map(
fn (Function_ $innerFunction): string => $innerFunction->name->toString(),
$innerFunctions
);
foreach ($innerFunctions as $innerFunction) {
$newClassMethod = $this->createPrivateMethod($innerFunction, $isStatic);
$this->rewriteCallsToMethod($newClassMethod, $innerFunctionNames, $isStatic);
$newClassMethods[] = $newClassMethod;
}
$this->rewriteCallsToMethod($classMethod, $innerFunctionNames, $isStatic);
}
if ($newClassMethods === []) {
return null;
}
$node->stmts = array_merge($node->stmts, $newClassMethods);
return $node;
}
/**
* @param string[] $existingMethodNames
* @return Function_[]
*/
private function extractInnerFunctions(ClassMethod $classMethod, array &$existingMethodNames): array
{
$innerFunctions = [];
foreach ($classMethod->stmts ?? [] as $key => $stmt) {
if (! $stmt instanceof Function_) {
continue;
}
$functionName = $stmt->name->toString();
// avoid collision with an already existing method
if (in_array($functionName, $existingMethodNames, true)) {
continue;
}
// avoid breaking string callables, e.g. usort($items, 'functionName')
if ($this->isReferencedAsStringCallable($classMethod, $functionName)) {
continue;
}
$existingMethodNames[] = $functionName;
$innerFunctions[] = $stmt;
unset($classMethod->stmts[$key]);
}
if ($innerFunctions !== []) {
$classMethod->stmts = array_values((array) $classMethod->stmts);
}
return $innerFunctions;
}
private function isReferencedAsStringCallable(ClassMethod $classMethod, string $functionName): bool
{
$isReferenced = false;
$this->traverseNodesWithCallable($classMethod->stmts ?? [], function (Node $node) use (
$functionName,
&$isReferenced
): null {
if ($node instanceof String_ && $node->value === $functionName) {
$isReferenced = true;
}
return null;
});
return $isReferenced;
}
private function createPrivateMethod(Function_ $innerFunction, bool $isStatic): ClassMethod
{
$flags = Modifiers::PRIVATE;
if ($isStatic) {
$flags |= Modifiers::STATIC;
}
$classMethod = new ClassMethod($innerFunction->name, [
'flags' => $flags,
'byRef' => $innerFunction->byRef,
'params' => $innerFunction->params,
'returnType' => $innerFunction->returnType,
'stmts' => $innerFunction->stmts,
'attrGroups' => $innerFunction->attrGroups,
]);
$this->mirrorComments($classMethod, $innerFunction);
return $classMethod;
}
/**
* @param string[] $innerFunctionNames
*/
private function rewriteCallsToMethod(ClassMethod $classMethod, array $innerFunctionNames, bool $isStatic): void
{
$this->traverseNodesWithCallable($classMethod->stmts ?? [], function (Node $node) use (
$innerFunctionNames,
$isStatic
): MethodCall|StaticCall|null {
if (! $node instanceof FuncCall) {
return null;
}
if (! $node->name instanceof Name) {
return null;
}
$functionName = $node->name->toString();
if (! in_array($functionName, $innerFunctionNames, true)) {
return null;
}
if ($isStatic) {
return new StaticCall(new Name('self'), $functionName, $node->args);
}
return new MethodCall(new Variable('this'), $functionName, $node->args);
});
}
}