Repository navigation
Expand file tree
/
Copy pathClassAnnotationMatcher.php
More file actions
121 lines (99 loc) · 3.5 KB
/
Copy pathClassAnnotationMatcher.php
File metadata and controls
121 lines (99 loc) · 3.5 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
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocParser;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Use_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher;
use Rector\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* Matches "@ORM\Entity" to FQN names based on use imports in the file
*/
final class ClassAnnotationMatcher
{
/**
* @var array<non-empty-string, non-empty-string>
*/
private array $fullyQualifiedNameByHash = [];
public function __construct(
private readonly UseImportNameMatcher $useImportNameMatcher,
private readonly UseImportsResolver $useImportsResolver,
private readonly ReflectionProvider $reflectionProvider
) {
}
/**
* @return non-empty-string
*/
public function resolveTagFullyQualifiedName(string $tag, Node $node): string
{
$uniqueId = $tag . spl_object_id($node);
if (isset($this->fullyQualifiedNameByHash[$uniqueId])) {
return $this->fullyQualifiedNameByHash[$uniqueId];
}
$tag = ltrim($tag, '@');
if ($tag === '') {
throw new ShouldNotHappenException();
}
$uses = $this->useImportsResolver->resolve();
$fullyQualifiedClass = $this->resolveFullyQualifiedClass($uses, $node, $tag);
$fullyQualifiedClass ??= $tag;
$this->fullyQualifiedNameByHash[$uniqueId] = $fullyQualifiedClass;
return $fullyQualifiedClass;
}
/**
* @param array<Use_|GroupUse> $uses
* @return non-empty-string|null
*/
private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag): ?string
{
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope) {
$namespace = $scope->getNamespace();
if ($namespace !== null) {
$namespacedTag = $namespace . '\\' . $tag;
if ($this->reflectionProvider->hasClass($namespacedTag)) {
return $namespacedTag;
}
if (! str_contains($tag, '\\')) {
return $this->resolveAsAliased($uses, $tag);
}
if ($this->isPreslashedExistingClass($tag)) {
// Global or absolute Class
return $tag;
}
}
}
return $this->useImportNameMatcher->matchNameWithUses($tag, $uses);
}
/**
* @param array<Use_|GroupUse> $uses
* @return non-empty-string|null
*/
private function resolveAsAliased(array $uses, string $tag): ?string
{
foreach ($uses as $use) {
$prefix = $this->useImportsResolver->resolvePrefix($use);
foreach ($use->uses as $useUse) {
if (! $useUse->alias instanceof Identifier) {
continue;
}
if ($useUse->alias->toString() === $tag) {
return $prefix . $useUse->name->toString();
}
}
}
return $this->useImportNameMatcher->matchNameWithUses($tag, $uses);
}
private function isPreslashedExistingClass(string $tag): bool
{
if (! str_starts_with($tag, '\\')) {
return false;
}
return $this->reflectionProvider->hasClass($tag);
}
}