Skip to content

Commit 3d8dbb2

Browse files
committed
update removing use imports to FileNode
1 parent 4f720cc commit 3d8dbb2

File tree

5 files changed

+92
-42
lines changed

5 files changed

+92
-42
lines changed

rules/Naming/Naming/UseImportsResolver.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
namespace Rector\Naming\Naming;
66

7-
use PhpParser\Node;
8-
use PhpParser\Node\Stmt;
97
use PhpParser\Node\Stmt\GroupUse;
10-
use PhpParser\Node\Stmt\Namespace_;
118
use PhpParser\Node\Stmt\Use_;
129
use Rector\Application\Provider\CurrentFileProvider;
13-
use Rector\PhpParser\Node\FileNode;
1410
use Rector\ValueObject\Application\File;
1511

1612
final readonly class UseImportsResolver
@@ -25,15 +21,13 @@ public function __construct(
2521
*/
2622
public function resolve(): array
2723
{
28-
$namespace = $this->resolveNamespace();
29-
if (! $namespace instanceof Node) {
24+
$file = $this->currentFileProvider->getFile();
25+
if (! $file instanceof File) {
3026
return [];
3127
}
3228

33-
return array_filter(
34-
$namespace->stmts,
35-
static fn (Stmt $stmt): bool => $stmt instanceof Use_ || $stmt instanceof GroupUse
36-
);
29+
$fileNode = $file->getFileNode();
30+
return $fileNode->getUsesAndGroupUses();
3731
}
3832

3933
/**
@@ -42,12 +36,14 @@ public function resolve(): array
4236
*/
4337
public function resolveBareUses(): array
4438
{
45-
$namespace = $this->resolveNamespace();
46-
if (! $namespace instanceof Node) {
39+
$file = $this->currentFileProvider->getFile();
40+
41+
if (! $file instanceof File) {
4742
return [];
4843
}
4944

50-
return array_filter($namespace->stmts, static fn (Stmt $stmt): bool => $stmt instanceof Use_);
45+
$fileNode = $file->getFileNode();
46+
return $fileNode->getUses();
5147
}
5248

5349
public function resolvePrefix(Use_|GroupUse $use): string
@@ -57,24 +53,24 @@ public function resolvePrefix(Use_|GroupUse $use): string
5753
: '';
5854
}
5955

60-
private function resolveNamespace(): Namespace_|null
61-
{
62-
/** @var File|null $file */
63-
$file = $this->currentFileProvider->getFile();
64-
if (! $file instanceof File) {
65-
return null;
66-
}
67-
68-
$newStmts = $file->getNewStmts();
69-
if ($newStmts === []) {
70-
return null;
71-
}
72-
73-
if ($newStmts[0] instanceof FileNode) {
74-
$fileNode = $newStmts[0];
75-
return $fileNode->getNamespace();
76-
}
77-
78-
return null;
79-
}
56+
// private function resolveNamespace(): Namespace_|null
57+
// {
58+
// /** @var File|null $file */
59+
// $file = $this->currentFileProvider->getFile();
60+
// if (! $file instanceof File) {
61+
// return null;
62+
// }
63+
//
64+
// $newStmts = $file->getNewStmts();
65+
// if ($newStmts === []) {
66+
// return null;
67+
// }
68+
//
69+
// if ($newStmts[0] instanceof FileNode) {
70+
// $fileNode = $newStmts[0];
71+
// return $fileNode->getNamespace();
72+
// }
73+
//
74+
// return null;
75+
// }
8076
}

src/PhpParser/Node/CustomNode/FileWithoutNamespace.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
99

1010
/**
11+
* @deprecated Use @see \Rector\PhpParser\Node\FileNode instead
12+
*
1113
* Inspired by https://github.com/phpstan/phpstan-src/commit/ed81c3ad0b9877e6122c79b4afda9d10f3994092
1214
*/
1315
final class FileWithoutNamespace extends Stmt implements StmtsAwareInterface

src/PhpParser/Node/FileNode.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
namespace Rector\PhpParser\Node;
66

77
use PhpParser\Node\Stmt;
8+
use PhpParser\Node\Stmt\GroupUse;
89
use PhpParser\Node\Stmt\Namespace_;
10+
use PhpParser\Node\Stmt\Use_;
911

12+
/**
13+
* Inspired by https://github.com/phpstan/phpstan-src/commit/ed81c3ad0b9877e6122c79b4afda9d10f3994092
14+
*/
1015
final class FileNode extends Stmt
1116
{
1217
/**
@@ -15,7 +20,11 @@ final class FileNode extends Stmt
1520
public function __construct(
1621
public array $stmts
1722
) {
23+
$firstStmt = $stmts[0] ?? null;
24+
parent::__construct($firstStmt instanceof \PhpParser\Node ? $firstStmt->getAttributes() : []);
25+
1826
parent::__construct();
27+
1928
}
2029

2130
public function getType(): string
@@ -50,4 +59,33 @@ public function getNamespace(): ?Stmt\Namespace_
5059

5160
return null;
5261
}
62+
63+
/**
64+
* @return array<Use_|GroupUse>
65+
*/
66+
public function getUsesAndGroupUses(): array
67+
{
68+
$rootNode = $this->getNamespace();
69+
if (! $rootNode instanceof Namespace_) {
70+
$rootNode = $this;
71+
}
72+
73+
return array_filter(
74+
$rootNode->stmts,
75+
static fn (Stmt $stmt): bool => $stmt instanceof Use_ || $stmt instanceof GroupUse
76+
);
77+
}
78+
79+
/**
80+
* @return Use_[]
81+
*/
82+
public function getUses(): array
83+
{
84+
$rootNode = $this->getNamespace();
85+
if (! $rootNode instanceof Namespace_) {
86+
$rootNode = $this;
87+
}
88+
89+
return array_filter($rootNode->stmts, static fn (Stmt $stmt): bool => $stmt instanceof Use_);
90+
}
5391
}

src/PostRector/Rector/UnusedImportRemovingPostRector.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2121
use Rector\NodeTypeResolver\Node\AttributeKey;
2222
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
23-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
23+
use Rector\PhpParser\Node\FileNode;
2424

2525
final class UnusedImportRemovingPostRector extends AbstractPostRector
2626
{
@@ -32,7 +32,7 @@ public function __construct(
3232

3333
public function enterNode(Node $node): ?Node
3434
{
35-
if (! $node instanceof Namespace_ && ! $node instanceof FileWithoutNamespace) {
35+
if (! $node instanceof Namespace_ && ! $node instanceof FileNode) {
3636
return null;
3737
}
3838

@@ -101,11 +101,11 @@ public function enterNode(Node $node): ?Node
101101
/**
102102
* @return string[]
103103
*/
104-
private function findNonUseImportNames(Namespace_|FileWithoutNamespace $namespace): array
104+
private function findNonUseImportNames(Namespace_|FileNode $fileNode): array
105105
{
106106
$names = [];
107107

108-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace->stmts, static function (Node $node) use (
108+
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($fileNode->stmts, static function (Node $node) use (
109109
&$names
110110
): int|null|Name {
111111
if ($node instanceof Use_) {
@@ -136,11 +136,11 @@ private function findNonUseImportNames(Namespace_|FileWithoutNamespace $namespac
136136
/**
137137
* @return string[]
138138
*/
139-
private function findNamesInDocBlocks(Namespace_|FileWithoutNamespace $namespace): array
139+
private function findNamesInDocBlocks(Namespace_|FileNode $rootNode): array
140140
{
141141
$names = [];
142142

143-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace, function (Node $node) use (
143+
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($rootNode, function (Node $node) use (
144144
&$names
145145
) {
146146
$comments = $node->getComments();
@@ -180,10 +180,10 @@ private function findNamesInDocBlocks(Namespace_|FileWithoutNamespace $namespace
180180
/**
181181
* @return string[]
182182
*/
183-
private function resolveUsedPhpAndDocNames(Namespace_|FileWithoutNamespace $namespace): array
183+
private function resolveUsedPhpAndDocNames(Namespace_|FileNode $rootNode): array
184184
{
185-
$phpNames = $this->findNonUseImportNames($namespace);
186-
$docBlockNames = $this->findNamesInDocBlocks($namespace);
185+
$phpNames = $this->findNonUseImportNames($rootNode);
186+
$docBlockNames = $this->findNamesInDocBlocks($rootNode);
187187

188188
$names = [...$phpNames, ...$docBlockNames];
189189
return array_unique($names);

src/ValueObject/Application/File.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Token;
1212
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
1313
use Rector\Exception\ShouldNotHappenException;
14+
use Rector\PhpParser\Node\FileNode;
1415
use Rector\ValueObject\Reporting\FileDiff;
1516

1617
final class File
@@ -170,4 +171,17 @@ public function containsHTML(): bool
170171
$this->containsHtml = (bool) $nodeFinder->findFirstInstanceOf($this->oldStmts, InlineHTML::class);
171172
return $this->containsHtml;
172173
}
174+
175+
public function getFileNode(): ?FileNode
176+
{
177+
if ($this->newStmts === []) {
178+
return null;
179+
}
180+
181+
if ($this->newStmts[0] instanceof FileNode) {
182+
return $this->newStmts[0];
183+
}
184+
185+
return null;
186+
}
173187
}

0 commit comments

Comments
 (0)