diff --git a/packages/PostRector/Rector/UseAddingPostRector.php b/packages/PostRector/Rector/UseAddingPostRector.php index 2a822174e00..d1793af0bd3 100644 --- a/packages/PostRector/Rector/UseAddingPostRector.php +++ b/packages/PostRector/Rector/UseAddingPostRector.php @@ -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 diff --git a/rules/Renaming/Rector/Name/RenameClassRector.php b/rules/Renaming/Rector/Name/RenameClassRector.php index 102a05ef1b2..36ee54569f2 100644 --- a/rules/Renaming/Rector/Name/RenameClassRector.php +++ b/rules/Renaming/Rector/Name/RenameClassRector.php @@ -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)); + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index e5403a953f0..956e77ebc69 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */