|
5 | 5 | namespace Rector\CodeQuality\Rector\StmtsAwareInterface; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Name; |
9 | | -use PhpParser\Node\Stmt\Function_; |
10 | | -use PHPStan\Reflection\Native\NativeFunctionReflection; |
11 | | -use PHPStan\Reflection\ReflectionProvider; |
| 8 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 9 | +use Rector\Exception\ShouldNotHappenException; |
12 | 10 | use Rector\PhpParser\Enum\NodeGroup; |
13 | 11 | use Rector\Rector\AbstractRector; |
14 | 12 | use Rector\RuleDoc\CodeSample\CodeSample; |
15 | 13 | use Rector\RuleDoc\RuleDefinition; |
16 | 14 |
|
17 | 15 | /** |
18 | | - * @see \Rector\Tests\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector\MoveInnerFunctionToTopLevelRectorTest |
| 16 | + * @deprecated This rule is deprecated, as it handles a niche inner-function case that is not part of the PHP upgrade path. Use a custom rule instead. |
19 | 17 | */ |
20 | | -final class MoveInnerFunctionToTopLevelRector extends AbstractRector |
| 18 | +final class MoveInnerFunctionToTopLevelRector extends AbstractRector implements DeprecatedInterface |
21 | 19 | { |
22 | | - public function __construct( |
23 | | - private readonly ReflectionProvider $reflectionProvider |
24 | | - ) { |
25 | | - |
26 | | - } |
27 | | - |
28 | 20 | public function getRuleDefinition(): RuleDefinition |
29 | 21 | { |
30 | 22 | return new RuleDefinition( |
@@ -72,87 +64,9 @@ public function getNodeTypes(): array |
72 | 64 | */ |
73 | 65 | public function refactor(Node $node): ?Node |
74 | 66 | { |
75 | | - $stmts = (array) $node->stmts; |
76 | | - |
77 | | - $newStmts = []; |
78 | | - $hasChanged = false; |
79 | | - |
80 | | - foreach ($stmts as $stmt) { |
81 | | - if (! $stmt instanceof Function_) { |
82 | | - $newStmts[] = $stmt; |
83 | | - continue; |
84 | | - } |
85 | | - |
86 | | - $innerFunctions = $this->extractInnerFunctions($stmt, $stmts); |
87 | | - foreach ($innerFunctions as $innerFunction) { |
88 | | - $newStmts[] = $innerFunction; |
89 | | - $hasChanged = true; |
90 | | - } |
91 | | - |
92 | | - $newStmts[] = $stmt; |
93 | | - } |
94 | | - |
95 | | - if (! $hasChanged) { |
96 | | - return null; |
97 | | - } |
98 | | - |
99 | | - $node->stmts = $newStmts; |
100 | | - |
101 | | - return $node; |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * @param Node\Stmt[] $siblingStmts |
106 | | - * @return Function_[] |
107 | | - */ |
108 | | - private function extractInnerFunctions(Function_ $outerFunction, array $siblingStmts): array |
109 | | - { |
110 | | - $innerFunctions = []; |
111 | | - |
112 | | - foreach ($outerFunction->stmts as $key => $innerStmt) { |
113 | | - if (! $innerStmt instanceof Function_) { |
114 | | - continue; |
115 | | - } |
116 | | - |
117 | | - // avoid fatal error on name collision with an existing top-level function |
118 | | - if ($this->hasSiblingFunctionOfSameName($innerStmt, $siblingStmts)) { |
119 | | - continue; |
120 | | - } |
121 | | - |
122 | | - $innerFunctions[] = $innerStmt; |
123 | | - unset($outerFunction->stmts[$key]); |
124 | | - } |
125 | | - |
126 | | - if ($innerFunctions !== []) { |
127 | | - $outerFunction->stmts = array_values($outerFunction->stmts); |
128 | | - } |
129 | | - |
130 | | - return $innerFunctions; |
131 | | - } |
132 | | - |
133 | | - /** |
134 | | - * @param Node\Stmt[] $siblingStmts |
135 | | - */ |
136 | | - private function hasSiblingFunctionOfSameName(Function_ $innerFunction, array $siblingStmts): bool |
137 | | - { |
138 | | - $innerFunctionName = new Name($innerFunction->name->toString()); |
139 | | - foreach ($siblingStmts as $siblingStmt) { |
140 | | - if (! $siblingStmt instanceof Function_) { |
141 | | - continue; |
142 | | - } |
143 | | - |
144 | | - if ($this->nodeNameResolver->areNamesEqual($siblingStmt, $innerFunction)) { |
145 | | - return true; |
146 | | - } |
147 | | - |
148 | | - if ($this->reflectionProvider->hasFunction($innerFunctionName, null)) { |
149 | | - $functionReflection = $this->reflectionProvider->getFunction($innerFunctionName, null); |
150 | | - if ($functionReflection instanceof NativeFunctionReflection) { |
151 | | - return true; |
152 | | - } |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - return false; |
| 67 | + throw new ShouldNotHappenException(sprintf( |
| 68 | + '"%s" rule is deprecated, as it handles a niche inner-function case that is not part of the PHP upgrade path', |
| 69 | + self::class |
| 70 | + )); |
157 | 71 | } |
158 | 72 | } |
0 commit comments