forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeAliasResolver.php
More file actions
164 lines (123 loc) · 4.67 KB
/
Copy pathTypeAliasResolver.php
File metadata and controls
164 lines (123 loc) · 4.67 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
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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\Reflection\ReflectionProvider;
use function array_key_exists;
class TypeAliasResolver
{
/** @var array<string, string> */
private array $globalTypeAliases;
private TypeStringResolver $typeStringResolver;
private TypeNodeResolver $typeNodeResolver;
private ReflectionProvider $reflectionProvider;
/** @var array<string, Type> */
private array $resolvedGlobalTypeAliases = [];
/** @var array<string, Type> */
private array $resolvedLocalTypeAliases = [];
/** @var array<string, true> */
private array $resolvingClassTypeAliases = [];
/** @var array<string, true> */
private array $inProcess = [];
/**
* @param array<string, string> $globalTypeAliases
*/
public function __construct(
array $globalTypeAliases,
TypeStringResolver $typeStringResolver,
TypeNodeResolver $typeNodeResolver,
ReflectionProvider $reflectionProvider
)
{
$this->globalTypeAliases = $globalTypeAliases;
$this->typeStringResolver = $typeStringResolver;
$this->typeNodeResolver = $typeNodeResolver;
$this->reflectionProvider = $reflectionProvider;
}
public function hasTypeAlias(string $aliasName, ?string $classNameScope): bool
{
$hasGlobalTypeAlias = array_key_exists($aliasName, $this->globalTypeAliases);
if ($hasGlobalTypeAlias) {
return true;
}
if ($classNameScope === null || !$this->reflectionProvider->hasClass($classNameScope)) {
return false;
}
$classReflection = $this->reflectionProvider->getClass($classNameScope);
$localTypeAliases = $classReflection->getTypeAliases();
return array_key_exists($aliasName, $localTypeAliases);
}
public function resolveTypeAlias(string $aliasName, NameScope $nameScope): ?Type
{
return $this->resolveLocalTypeAlias($aliasName, $nameScope)
?? $this->resolveGlobalTypeAlias($aliasName, $nameScope);
}
private function resolveLocalTypeAlias(string $aliasName, NameScope $nameScope): ?Type
{
if (array_key_exists($aliasName, $this->globalTypeAliases)) {
return null;
}
if (!$nameScope->hasTypeAlias($aliasName)) {
return null;
}
$className = $nameScope->getClassName();
if ($className === null) {
return null;
}
$aliasNameInClassScope = $className . '::' . $aliasName;
if (array_key_exists($aliasNameInClassScope, $this->resolvedLocalTypeAliases)) {
return $this->resolvedLocalTypeAliases[$aliasNameInClassScope];
}
// prevent infinite recursion
if (array_key_exists($className, $this->resolvingClassTypeAliases)) {
return null;
}
$this->resolvingClassTypeAliases[$className] = true;
if (!$this->reflectionProvider->hasClass($className)) {
unset($this->resolvingClassTypeAliases[$className]);
return null;
}
$classReflection = $this->reflectionProvider->getClass($className);
$localTypeAliases = $classReflection->getTypeAliases();
unset($this->resolvingClassTypeAliases[$className]);
if (!array_key_exists($aliasName, $localTypeAliases)) {
return null;
}
if (array_key_exists($aliasNameInClassScope, $this->inProcess)) {
// resolve circular reference as ErrorType to make it easier to detect
throw new \PHPStan\Type\CircularTypeAliasDefinitionException();
}
$this->inProcess[$aliasNameInClassScope] = true;
try {
$unresolvedAlias = $localTypeAliases[$aliasName];
$resolvedAliasType = $unresolvedAlias->resolve($this->typeNodeResolver);
} catch (\PHPStan\Type\CircularTypeAliasDefinitionException $e) {
$resolvedAliasType = new ErrorType();
}
$this->resolvedLocalTypeAliases[$aliasNameInClassScope] = $resolvedAliasType;
unset($this->inProcess[$aliasNameInClassScope]);
return $resolvedAliasType;
}
private function resolveGlobalTypeAlias(string $aliasName, NameScope $nameScope): ?Type
{
if (!array_key_exists($aliasName, $this->globalTypeAliases)) {
return null;
}
if (array_key_exists($aliasName, $this->resolvedGlobalTypeAliases)) {
return $this->resolvedGlobalTypeAliases[$aliasName];
}
if ($this->reflectionProvider->hasClass($nameScope->resolveStringName($aliasName))) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Type alias %s already exists as a class.', $aliasName));
}
if (array_key_exists($aliasName, $this->inProcess)) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Circular definition for type alias %s.', $aliasName));
}
$this->inProcess[$aliasName] = true;
$aliasTypeString = $this->globalTypeAliases[$aliasName];
$aliasType = $this->typeStringResolver->resolve($aliasTypeString);
$this->resolvedGlobalTypeAliases[$aliasName] = $aliasType;
unset($this->inProcess[$aliasName]);
return $aliasType;
}
}