-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathAliasUsesResolver.php
More file actions
71 lines (59 loc) · 1.73 KB
/
AliasUsesResolver.php
File metadata and controls
71 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\ClassNameImport;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use Rector\PhpParser\Node\FileNode;
final readonly class AliasUsesResolver
{
public function __construct(
private UseImportsTraverser $useImportsTraverser
) {
}
/**
* @param Stmt[] $stmts
* @return string[]
*/
public function resolveFromNode(Node $node, array $stmts): array
{
if (! $node instanceof Namespace_ && ! $node instanceof FileNode) {
/** @var Namespace_[]|FileNode[] $namespaces */
$namespaces = array_filter(
$stmts,
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileNode
);
if (count($namespaces) !== 1) {
return [];
}
$node = current($namespaces);
}
return $this->resolveFromStmts($node->stmts);
}
/**
* @param Stmt[] $stmts
* @return string[]
*/
public function resolveFromStmts(array $stmts): array
{
$aliasedUses = [];
/** @param Use_::TYPE_* $useType */
$this->useImportsTraverser->traverserStmts($stmts, static function (
int $useType,
UseItem $useItem,
string $name
) use (&$aliasedUses): void {
if ($useType !== Use_::TYPE_NORMAL) {
return;
}
if (! $useItem->alias instanceof Identifier) {
return;
}
$aliasedUses[] = $name;
});
return $aliasedUses;
}
}