Skip to content

Commit

Permalink
[CodingStyle] Refactor UseImportsAdder to return [FileWithoutNamespac…
Browse files Browse the repository at this point in the history
…e] code on no namespaced code (#5254)

* [Core] Refactor AbstractRector to add afterTraverse() method to ensure it returns [FileWithoutNamespace] code on no namespaced code

* update UseImportsAdder
  • Loading branch information
samsonasik committed Nov 16, 2023
1 parent 60047ce commit 1595e34
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
8 changes: 1 addition & 7 deletions packages/PostRector/Rector/NameImportingPostRector.php
Expand Up @@ -94,15 +94,9 @@ private function processNodeName(Name $name, File $file): ?Node

$namespaces = array_filter(
$file->getNewStmts(),
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_
);

// handle overlapped resolve last new stmts
// @see https://github.com/rectorphp/rector-src/pull/5251
if ($namespaces === []) {
return null;
}

if (count($namespaces) > 1) {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions rules/CodingStyle/Application/UseImportsAdder.php
Expand Up @@ -58,7 +58,7 @@ public function addImportsToStmts(

$newUses = $this->createUses($useImportTypes, $constantUseImportTypes, $functionUseImportTypes, null);
if ($newUses === []) {
return $stmts;
return [$fileWithoutNamespace];
}

// place after declare strict_types
Expand All @@ -78,7 +78,7 @@ public function addImportsToStmts(
$fileWithoutNamespace->stmts = $stmts;
$fileWithoutNamespace->stmts = array_values($fileWithoutNamespace->stmts);

return $fileWithoutNamespace->stmts;
return [$fileWithoutNamespace];
}
}

Expand All @@ -88,7 +88,7 @@ public function addImportsToStmts(
$fileWithoutNamespace->stmts = array_merge($newUses, $stmts);
$fileWithoutNamespace->stmts = array_values($fileWithoutNamespace->stmts);

return $fileWithoutNamespace->stmts;
return [$fileWithoutNamespace];
}

/**
Expand Down

0 comments on commit 1595e34

Please sign in to comment.