Repository navigation
Expand file tree
/
Copy pathMoveInnerFunctionToTopLevelRector.php
More file actions
158 lines (131 loc) · 3.96 KB
/
Copy pathMoveInnerFunctionToTopLevelRector.php
File metadata and controls
158 lines (131 loc) · 3.96 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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\StmtsAwareInterface;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector\MoveInnerFunctionToTopLevelRectorTest
*/
final class MoveInnerFunctionToTopLevelRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Move an inner named function to the top level, as inner named functions are not supported by PHPStan',
[
new CodeSample(
<<<'CODE_SAMPLE'
function outer(): void
{
function inner(): void
{
echo 'hello';
}
inner();
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function inner(): void
{
echo 'hello';
}
function outer(): void
{
inner();
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return NodeGroup::STMTS_AWARE;
}
/**
* @param StmtsAware $node
*/
public function refactor(Node $node): ?Node
{
$stmts = (array) $node->stmts;
$newStmts = [];
$hasChanged = false;
foreach ($stmts as $stmt) {
if (! $stmt instanceof Function_) {
$newStmts[] = $stmt;
continue;
}
$innerFunctions = $this->extractInnerFunctions($stmt, $stmts);
foreach ($innerFunctions as $innerFunction) {
$newStmts[] = $innerFunction;
$hasChanged = true;
}
$newStmts[] = $stmt;
}
if (! $hasChanged) {
return null;
}
$node->stmts = $newStmts;
return $node;
}
/**
* @param Node\Stmt[] $siblingStmts
* @return Function_[]
*/
private function extractInnerFunctions(Function_ $outerFunction, array $siblingStmts): array
{
$innerFunctions = [];
foreach ($outerFunction->stmts as $key => $innerStmt) {
if (! $innerStmt instanceof Function_) {
continue;
}
// avoid fatal error on name collision with an existing top-level function
if ($this->hasSiblingFunctionOfSameName($innerStmt, $siblingStmts)) {
continue;
}
$innerFunctions[] = $innerStmt;
unset($outerFunction->stmts[$key]);
}
if ($innerFunctions !== []) {
$outerFunction->stmts = array_values($outerFunction->stmts);
}
return $innerFunctions;
}
/**
* @param Node\Stmt[] $siblingStmts
*/
private function hasSiblingFunctionOfSameName(Function_ $innerFunction, array $siblingStmts): bool
{
$innerFunctionName = new Name($innerFunction->name->toString());
foreach ($siblingStmts as $siblingStmt) {
if (! $siblingStmt instanceof Function_) {
continue;
}
if ($this->nodeNameResolver->areNamesEqual($siblingStmt, $innerFunction)) {
return true;
}
if ($this->reflectionProvider->hasFunction($innerFunctionName, null)) {
$functionReflection = $this->reflectionProvider->getFunction($innerFunctionName, null);
if ($functionReflection instanceof NativeFunctionReflection) {
return true;
}
}
}
return false;
}
}