Updated Rector to commit e43a1cec56310b6762eb0b56d18f60402d5fd652

e43a1cec56 [Renaming] Handle Rename no namespace to namespaced class with existing use statements (#5264)
This commit is contained in:
Tomas Votruba 2023-11-19 02:55:19 +00:00
parent 73bd769732
commit 318c07bf9f
3 changed files with 49 additions and 11 deletions

View File

@ -95,15 +95,6 @@ final class UseAddingPostRector extends \Rector\PostRector\Rector\AbstractPostRe
$this->useImportsAdder->addImportsToNamespace($namespace, $useImportTypes, $constantUseImportTypes, $functionUseImportTypes);
return $nodes;
}
// just renamed no-namepaced class to namespaced class
$namespaces = \array_filter($nodes, static function (Stmt $stmt) : bool {
return $stmt instanceof Namespace_;
});
if ($namespaces !== []) {
// then add, to prevent adding + removing false positive of same short use
$this->useImportsAdder->addImportsToNamespace(\current($namespaces), $useImportTypes, $constantUseImportTypes, $functionUseImportTypes);
return $nodes;
}
// B. no namespace? add in the top
$useImportTypes = $this->filterOutNonNamespacedNames($useImportTypes);
// then add, to prevent adding + removing false positive of same short use

View File

@ -7,12 +7,14 @@ use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
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_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Renaming\NodeManipulator\ClassRenamer;
@ -95,4 +97,49 @@ CODE_SAMPLE
Assert::allString(\array_keys($configuration));
$this->renamedClassesDataCollector->addOldToNewClasses($configuration);
}
/**
* @param Node[] $nodes
* @return null|Node[]
*/
public function afterTraverse(array $nodes) : ?array
{
foreach ($nodes as $node) {
if ($node instanceof Namespace_) {
return parent::afterTraverse($nodes);
}
if ($node instanceof FileWithoutNamespace) {
foreach ($node->stmts as $stmt) {
if ($stmt instanceof Namespace_) {
$this->restructureUnderNamespace($node);
return $node->stmts;
}
}
return parent::afterTraverse($nodes);
}
}
return parent::afterTraverse($nodes);
}
private function restructureUnderNamespace(FileWithoutNamespace $fileWithoutNamespace) : void
{
$stmts = \array_reverse($fileWithoutNamespace->stmts, \true);
$isBeforeNamespace = \false;
$stmtsBeforeNamespace = [];
$namepace = null;
foreach ($stmts as $key => $stmt) {
if ($stmt instanceof Namespace_) {
$isBeforeNamespace = \true;
$namepace = $stmt;
continue;
}
if ($isBeforeNamespace && !$stmt instanceof Declare_) {
$stmtsBeforeNamespace[] = $stmt;
unset($stmts[$key]);
}
}
if ($stmtsBeforeNamespace === [] || !$namepace instanceof Namespace_) {
return;
}
$namepace->stmts = \array_values(\array_merge(\is_array($stmtsBeforeNamespace) ? $stmtsBeforeNamespace : \iterator_to_array($stmtsBeforeNamespace), $namepace->stmts));
$fileWithoutNamespace->stmts = \array_values(\array_reverse($stmts, \true));
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '7fca71b08c86551c39f0a9f71c41a9806d80b154';
public const PACKAGE_VERSION = 'e43a1cec56310b6762eb0b56d18f60402d5fd652';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-11-19 00:26:45';
public const RELEASE_DATE = '2023-11-19 09:53:05';
/**
* @var int
*/