mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 07:22:43 +02:00
Updated Rector to commit be53faca08c2f5c3ebafb636e766788676f54d28
be53faca08
[Renaming] Remove namespace renames in ClassRename, as it should only rename references, not move to new namespace (#5487)
This commit is contained in:
parent
72af333641
commit
599ee7833e
@ -7,7 +7,6 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Declare_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
@ -15,7 +14,6 @@ use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Configuration\RenamedClassesDataCollector;
|
||||
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Renaming\NodeManipulator\ClassRenamer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
@ -36,24 +34,11 @@ final class RenameClassRector extends AbstractRector implements ConfigurableRect
|
||||
* @var \Rector\Renaming\NodeManipulator\ClassRenamer
|
||||
*/
|
||||
private $classRenamer;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isMayRequireRestructureNamespace = \false;
|
||||
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer)
|
||||
{
|
||||
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
|
||||
$this->classRenamer = $classRenamer;
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]|null
|
||||
*/
|
||||
public function beforeTraverse(array $nodes) : ?array
|
||||
{
|
||||
$this->isMayRequireRestructureNamespace = \false;
|
||||
return parent::beforeTraverse($nodes);
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Replaces defined classes by new ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
||||
@ -87,7 +72,7 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, Namespace_::class, If_::class];
|
||||
return [FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, If_::class];
|
||||
}
|
||||
/**
|
||||
* @param FunctionLike|FullyQualified|ClassLike|Expression|Namespace_|Property|If_ $node
|
||||
@ -97,11 +82,7 @@ CODE_SAMPLE
|
||||
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
|
||||
if ($oldToNewClasses !== []) {
|
||||
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
$renameNode = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
|
||||
if ($renameNode instanceof Namespace_) {
|
||||
$this->isMayRequireRestructureNamespace = \true;
|
||||
}
|
||||
return $renameNode;
|
||||
return $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -114,50 +95,4 @@ CODE_SAMPLE
|
||||
Assert::allString(\array_keys($configuration));
|
||||
$this->renamedClassesDataCollector->addOldToNewClasses($configuration);
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return null|Node[]
|
||||
*/
|
||||
public function afterTraverse(array $nodes) : ?array
|
||||
{
|
||||
if (!$this->isMayRequireRestructureNamespace) {
|
||||
return parent::afterTraverse($nodes);
|
||||
}
|
||||
foreach ($nodes as $node) {
|
||||
if ($node instanceof Namespace_) {
|
||||
return parent::afterTraverse($nodes);
|
||||
}
|
||||
if (!$node instanceof FileWithoutNamespace) {
|
||||
continue;
|
||||
}
|
||||
foreach ($node->stmts as $stmt) {
|
||||
if ($stmt instanceof Namespace_) {
|
||||
$this->restructureUnderNamespace($node);
|
||||
return $node->stmts;
|
||||
}
|
||||
}
|
||||
}
|
||||
return parent::afterTraverse($nodes);
|
||||
}
|
||||
private function restructureUnderNamespace(FileWithoutNamespace $fileWithoutNamespace) : void
|
||||
{
|
||||
$stmtsBeforeNamespace = [];
|
||||
foreach ($fileWithoutNamespace->stmts as $key => $stmt) {
|
||||
if ($stmt instanceof Namespace_) {
|
||||
if ($stmtsBeforeNamespace !== []) {
|
||||
$stmt->stmts = \array_values(\array_merge(\is_array($stmtsBeforeNamespace) ? $stmtsBeforeNamespace : \iterator_to_array($stmtsBeforeNamespace), $stmt->stmts));
|
||||
}
|
||||
break;
|
||||
}
|
||||
if ($stmt instanceof Declare_) {
|
||||
continue;
|
||||
}
|
||||
$stmtsBeforeNamespace[] = $stmt;
|
||||
unset($fileWithoutNamespace->stmts[$key]);
|
||||
}
|
||||
if ($stmtsBeforeNamespace === []) {
|
||||
return;
|
||||
}
|
||||
$fileWithoutNamespace->stmts = \array_values($fileWithoutNamespace->stmts);
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '3dac3a00d8a9e306bacc1ae2f9ded092dc95c212';
|
||||
public const PACKAGE_VERSION = 'be53faca08c2f5c3ebafb636e766788676f54d28';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-01-21 08:10:26';
|
||||
public const RELEASE_DATE = '2024-01-21 22:10:56';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user