Skip to content

Commit fd4f6d0

Browse files
authored
[AutoImport] Skip auto import conflict aliased on no namespace (#6266)
* [AutoImport] Skip auto import conflict aliased on no namespace * Fixed 🎉 * cs fix * cs fix
1 parent 07cfc27 commit fd4f6d0

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

rules/CodingStyle/ClassNameImport/AliasUsesResolver.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Stmt\Namespace_;
1111
use PhpParser\Node\Stmt\Use_;
1212
use PhpParser\Node\Stmt\UseUse;
13+
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
1314

1415
final readonly class AliasUsesResolver
1516
{
@@ -24,9 +25,12 @@ public function __construct(
2425
*/
2526
public function resolveFromNode(Node $node, array $stmts): array
2627
{
27-
if (! $node instanceof Namespace_) {
28-
/** @var Namespace_[] $namespaces */
29-
$namespaces = array_filter($stmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);
28+
if (! $node instanceof Namespace_ && ! $node instanceof FileWithoutNamespace) {
29+
/** @var Namespace_[]|FileWithoutNamespace[] $namespaces */
30+
$namespaces = array_filter(
31+
$stmts,
32+
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
33+
);
3034
if (count($namespaces) !== 1) {
3135
return [];
3236
}

rules/Naming/Naming/UseImportsResolver.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,17 @@ private function resolveNamespace(): Namespace_|FileWithoutNamespace|null
7171
return null;
7272
}
7373

74-
$namespaces = array_filter($newStmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);
74+
/** @var Namespace_[]|FileWithoutNamespace[] $namespaces */
75+
$namespaces = array_filter(
76+
$newStmts,
77+
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
78+
);
7579

7680
// multiple namespaces is not supported
77-
if (count($namespaces) > 1) {
78-
return null;
79-
}
80-
81-
$currentNamespace = current($namespaces);
82-
if ($currentNamespace instanceof Namespace_) {
83-
return $currentNamespace;
84-
}
85-
86-
$currentStmt = current($newStmts);
87-
if (! $currentStmt instanceof FileWithoutNamespace) {
81+
if (count($namespaces) !== 1) {
8882
return null;
8983
}
9084

91-
return $currentStmt;
85+
return current($namespaces);
9286
}
9387
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Some\Class_ as ClassOrig;
4+
use PhpParser\Node;
5+
6+
final class SkipConflictLastNameAliasedUsed
7+
{
8+
/**
9+
* @param Node\Stmt\Class_ $param
10+
*/
11+
public function run($param): void
12+
{
13+
new ClassOrig();
14+
}
15+
}

0 commit comments

Comments
 (0)