-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
ShortNameResolver.php
242 lines (200 loc) · 7.88 KB
/
ShortNameResolver.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\ClassNameImport;
use Nette\Utils\Reflection;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionClass;
use Symfony\Contracts\Service\Attribute\Required;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
/**
* @see \Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\ShortNameResolverTest
*/
final class ShortNameResolver
{
/**
* @var string
* @see https://regex101.com/r/KphLd2/1
*/
private const BIG_LETTER_START_REGEX = '#^[A-Z]#';
/**
* @var array<string, string[]>
*/
private array $shortNamesByFilePath = [];
private PhpDocInfoFactory $phpDocInfoFactory;
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly NodeNameResolver $nodeNameResolver,
private readonly ReflectionProvider $reflectionProvider,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly UseImportNameMatcher $useImportNameMatcher,
) {
}
// Avoids circular reference
#[Required]
public function autowire(PhpDocInfoFactory $phpDocInfoFactory): void
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
}
/**
* @return array<string, string>
*/
public function resolveFromFile(File $file): array
{
$filePath = $file->getFilePath();
if (isset($this->shortNamesByFilePath[$filePath])) {
return $this->shortNamesByFilePath[$filePath];
}
$shortNamesToFullyQualifiedNames = $this->resolveForStmts($file->getNewStmts());
$this->shortNamesByFilePath[$filePath] = $shortNamesToFullyQualifiedNames;
return $shortNamesToFullyQualifiedNames;
}
/**
* Collects all "class <SomeClass>", "trait <SomeTrait>" and "interface <SomeInterface>"
* @return string[]
*/
public function resolveShortClassLikeNamesForNode(Node $node): array
{
$namespace = $this->betterNodeFinder->findParentType($node, Namespace_::class);
if (! $namespace instanceof Namespace_) {
// only handle namespace nodes
return [];
}
/** @var ClassLike[] $classLikes */
$classLikes = $this->betterNodeFinder->findInstanceOf($namespace, ClassLike::class);
$shortClassLikeNames = [];
foreach ($classLikes as $classLike) {
$shortClassLikeNames[] = $this->nodeNameResolver->getShortName($classLike);
}
return array_unique($shortClassLikeNames);
}
/**
* @param Stmt[] $stmts
* @return array<string, string>
*/
private function resolveForStmts(array $stmts): array
{
$shortNamesToFullyQualifiedNames = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
&$shortNamesToFullyQualifiedNames
) {
// class name is used!
if ($node instanceof ClassLike && $node->name instanceof Identifier) {
$fullyQualifiedName = $this->nodeNameResolver->getName($node);
if ($fullyQualifiedName === null) {
return null;
}
$shortNamesToFullyQualifiedNames[$node->name->toString()] = $fullyQualifiedName;
return null;
}
if (! $node instanceof Name) {
return null;
}
$originalName = $node->getAttribute(AttributeKey::ORIGINAL_NAME);
if (! $originalName instanceof Name) {
return null;
}
// already short
if (\str_contains($originalName->toString(), '\\')) {
return null;
}
$shortNamesToFullyQualifiedNames[$originalName->toString()] = $this->nodeNameResolver->getName($node);
return null;
});
$docBlockShortNamesToFullyQualifiedNames = $this->resolveFromStmtsDocBlocks($stmts);
/** @var array<string, string> $result */
$result = [...$shortNamesToFullyQualifiedNames, ...$docBlockShortNamesToFullyQualifiedNames];
return $result;
}
/**
* @param Stmt[] $stmts
* @return array<string, string>
*/
private function resolveFromStmtsDocBlocks(array $stmts): array
{
$reflectionClass = $this->resolveNativeClassReflection($stmts);
$shortNames = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
&$shortNames
) {
// speed up for nodes that are
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable(
$phpDocInfo->getPhpDocNode(),
'',
static function ($node) use (&$shortNames) {
if ($node instanceof PhpDocTagNode) {
$shortName = trim($node->name, '@');
if (StringUtils::isMatch($shortName, self::BIG_LETTER_START_REGEX)) {
$shortNames[] = $shortName;
}
return null;
}
if ($node instanceof IdentifierTypeNode) {
$shortNames[] = $node->name;
}
return null;
}
);
return null;
});
return $this->fqnizeShortNames($shortNames, $reflectionClass, $stmts);
}
/**
* @param Node[] $stmts
*/
private function resolveNativeClassReflection(array $stmts): ?ReflectionClass
{
$firstClassLike = $this->betterNodeFinder->findFirstInstanceOf($stmts, ClassLike::class);
if (! $firstClassLike instanceof ClassLike) {
return null;
}
$className = (string) $this->nodeNameResolver->getName($firstClassLike);
if (! $this->reflectionProvider->hasClass($className)) {
return null;
}
$classReflection = $this->reflectionProvider->getClass($className);
return $classReflection->getNativeReflection();
}
/**
* @param string[] $shortNames
* @param Stmt[] $stmts
* @return array<string, string>
*/
private function fqnizeShortNames(array $shortNames, ?ReflectionClass $reflectionClass, array $stmts): array
{
$shortNamesToFullyQualifiedNames = [];
foreach ($shortNames as $shortName) {
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts);
if ($reflectionClass instanceof ReflectionClass) {
$fullyQualifiedName = Reflection::expandClassName($shortName, $reflectionClass);
} elseif (is_string($stmtsMatchedName)) {
$fullyQualifiedName = $stmtsMatchedName;
} else {
$fullyQualifiedName = $shortName;
}
$shortNamesToFullyQualifiedNames[$shortName] = $fullyQualifiedName;
}
return $shortNamesToFullyQualifiedNames;
}
}