Skip to content

Commit 8c599b4

Browse files
committed
[CodeQuality] Deprecate MoveInnerFunctionToTopLevelRector as niche rule, not part of PHP upgrade path
1 parent 3f34e5c commit 8c599b4

8 files changed

Lines changed: 8 additions & 213 deletions

File tree

‎rules-tests/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector/Fixture/inner_function.php.inc‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector/Fixture/skip_name_collision.php.inc‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector/Fixture/skip_redefine_native_function.php.inc‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector/Fixture/skip_top_level_function.php.inc‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector/MoveInnerFunctionToTopLevelRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector/config/configured_rule.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules/CodeQuality/Rector/StmtsAwareInterface/MoveInnerFunctionToTopLevelRector.php‎

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@
55
namespace Rector\CodeQuality\Rector\StmtsAwareInterface;
66

77
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;
1210
use Rector\PhpParser\Enum\NodeGroup;
1311
use Rector\Rector\AbstractRector;
1412
use Rector\RuleDoc\CodeSample\CodeSample;
1513
use Rector\RuleDoc\RuleDefinition;
1614

1715
/**
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.
1917
*/
20-
final class MoveInnerFunctionToTopLevelRector extends AbstractRector
18+
final class MoveInnerFunctionToTopLevelRector extends AbstractRector implements DeprecatedInterface
2119
{
22-
public function __construct(
23-
private readonly ReflectionProvider $reflectionProvider
24-
) {
25-
26-
}
27-
2820
public function getRuleDefinition(): RuleDefinition
2921
{
3022
return new RuleDefinition(
@@ -72,87 +64,9 @@ public function getNodeTypes(): array
7264
*/
7365
public function refactor(Node $node): ?Node
7466
{
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+
));
15771
}
15872
}

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector;
6464
use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
6565
use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector;
66-
use Rector\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector;
6766
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
6867
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
6968
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
@@ -113,7 +112,6 @@ final class CodeQualityLevel
113112
ForeachToInArrayRector::class,
114113
RepeatedOrEqualToInArrayRector::class,
115114
RepeatedAndNotEqualToNotInArrayRector::class,
116-
MoveInnerFunctionToTopLevelRector::class,
117115
InnerFunctionToPrivateMethodRector::class,
118116
SimplifyForeachToCoalescingRector::class,
119117
SimplifyFuncGetArgsCountRector::class,

0 commit comments

Comments
 (0)