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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"php": "^8.1",
"cakephp/console": "^5.0",
"nette/utils": "^4.0",
"rector/rector": "~2.2.9",
"rector/rector": "~2.3.0",
"symfony/string": "^6.0 || ^7.0"
},
"autoload": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -86,9 +86,11 @@ public function refactor(Node $node): ?Node

if ($node instanceof Namespace_) {
$node->stmts = array_merge($uses, $node->stmts);

return $node;
}

if ($node instanceof FileWithoutNamespace) {
if ($node instanceof FileNode) {
$this->refactorFile($node, $uses);
}

Expand Down Expand Up @@ -174,18 +176,22 @@ private function resolveNamesFromStaticCalls(array $staticCalls): array
}

/**
* @param array<\PhpParser\Node\Stmt\Use_> $fileWithoutNamespace
* @param array<\PhpParser\Node\Stmt\Use_> $uses
*/
private function refactorFile(FileWithoutNamespace $fileWithoutNamespace, array $uses): ?FileWithoutNamespace
private function refactorFile(FileNode $fileNode, array $uses): ?FileNode
{
$hasDeclare = $this->betterNodeFinder->findFirstInstanceOf($fileWithoutNamespace->stmts, Declare_::class);
if ($fileNode->isNamespaced()) {
return null;
}

$hasDeclare = $this->betterNodeFinder->findFirstInstanceOf($fileNode->stmts, Declare_::class);
if ($hasDeclare !== null) {
return $this->refactorFileWithDeclare($fileWithoutNamespace, $uses);
return $this->refactorFileWithDeclare($fileNode, $uses);
}

$fileWithoutNamespace->stmts = array_merge($uses, $fileWithoutNamespace->stmts);
$fileNode->stmts = array_merge($uses, $fileNode->stmts);

return $fileWithoutNamespace;
return $fileNode;
}

private function createFullyQualifiedNameFromAppUsesStaticCall(StaticCall $staticCall): string
Expand All @@ -203,20 +209,20 @@ private function createFullyQualifiedNameFromAppUsesStaticCall(StaticCall $stati
}

/**
* @param array<\PhpParser\Node\Stmt\Use_> $fileWithoutNamespace
* @param array<\PhpParser\Node\Stmt\Use_> $uses
*/
private function refactorFileWithDeclare(
FileWithoutNamespace $fileWithoutNamespace,
FileNode $fileNode,
array $uses,
): FileWithoutNamespace {
foreach ($fileWithoutNamespace->stmts as $key => $stmt) {
): FileNode {
foreach ($fileNode->stmts as $key => $stmt) {
if ($stmt instanceof Declare_) {
foreach ($uses as $use) {
array_splice($fileWithoutNamespace->stmts, $key + 1, 0, [$use]);
array_splice($fileNode->stmts, $key + 1, 0, [$use]);
}
}
}

return $fileWithoutNamespace;
return $fileNode;
}
}