Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@
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\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector\MoveInnerFunctionToTopLevelRectorTest
* @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.
*/
final class MoveInnerFunctionToTopLevelRector extends AbstractRector
final class MoveInnerFunctionToTopLevelRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {

}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -72,87 +64,9 @@ public function getNodeTypes(): array
*/
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;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as it handles a niche inner-function case that is not part of the PHP upgrade path',
self::class
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector;
use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector;
use Rector\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector;
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
Expand Down Expand Up @@ -113,7 +112,6 @@ final class CodeQualityLevel
ForeachToInArrayRector::class,
RepeatedOrEqualToInArrayRector::class,
RepeatedAndNotEqualToNotInArrayRector::class,
MoveInnerFunctionToTopLevelRector::class,
InnerFunctionToPrivateMethodRector::class,
SimplifyForeachToCoalescingRector::class,
SimplifyFuncGetArgsCountRector::class,
Expand Down
Loading