Repository navigation
Expand file tree
/
Copy pathClassNameImportContext.php
More file actions
125 lines (103 loc) · 3.35 KB
/
Copy pathClassNameImportContext.php
File metadata and controls
125 lines (103 loc) · 3.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\ClassNameImport;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use Rector\PhpParser\Node\FileNode;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;
/**
* Per-file import facts shared across skip voters. Aliased uses and used import types are
* file-global, so they are resolved once per file stmts version and reused for every imported name.
*/
final class ClassNameImportContext
{
private ?File $cachedFile = null;
private int $cachedVersion = -1;
/**
* @var string[]
*/
private array $aliasedUses = [];
/**
* @var array<AliasedObjectType|FullyQualifiedObjectType>
*/
private array $usedImportTypes = [];
public function __construct(
private readonly AliasUsesResolver $aliasUsesResolver,
private readonly ShortNameResolver $shortNameResolver
) {
}
/**
* Short names actually used in the file (class-likes, names, docblock types) mapped to their FQN
*
* @return array<string, string>
*/
public function resolveShortNamesToFullyQualifiedNames(File $file): array
{
return $this->shortNameResolver->resolveFromFile($file);
}
/**
* Short names of class-likes (class, interface, trait) declared in the file
*
* @return string[]
*/
public function resolveShortClassLikeNames(File $file): array
{
return $this->shortNameResolver->resolveShortClassLikeNames($file);
}
/**
* @return string[]
*/
public function resolveAliasedUses(File $file): array
{
$this->ensureResolved($file);
return $this->aliasedUses;
}
/**
* @return array<AliasedObjectType|FullyQualifiedObjectType>
*/
public function resolveUsedImportTypes(File $file): array
{
$this->ensureResolved($file);
return $this->usedImportTypes;
}
private function ensureResolved(File $file): void
{
$newStmtsVersion = $file->getNewStmtsVersion();
if ($file === $this->cachedFile && $newStmtsVersion === $this->cachedVersion) {
return;
}
$this->cachedFile = $file;
$this->cachedVersion = $newStmtsVersion;
$this->aliasedUses = $this->resolveAliasedUsesFromFile($file);
$this->usedImportTypes = $this->resolveUsedImportTypesFromFile($file);
}
/**
* @return string[]
*/
private function resolveAliasedUsesFromFile(File $file): array
{
/** @var Namespace_[]|FileNode[] $namespaces */
$namespaces = array_filter(
$file->getNewStmts(),
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileNode
);
if (count($namespaces) !== 1) {
return [];
}
$namespace = current($namespaces);
return $this->aliasUsesResolver->resolveFromStmts($namespace->stmts);
}
/**
* @return array<AliasedObjectType|FullyQualifiedObjectType>
*/
private function resolveUsedImportTypesFromFile(File $file): array
{
$fileNode = $file->getFileNode();
if (! $fileNode instanceof FileNode) {
return [];
}
return $fileNode->resolveUsedImportTypes();
}
}