-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathUsefulTypeAliasResolver.php
More file actions
100 lines (78 loc) · 2.84 KB
/
Copy pathUsefulTypeAliasResolver.php
File metadata and controls
100 lines (78 loc) · 2.84 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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Analyser\NameScope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use function array_key_exists;
use function sprintf;
#[AutowiredService(as: TypeAliasResolver::class)]
final class UsefulTypeAliasResolver implements TypeAliasResolver
{
/** @var array<string, Type> */
private array $resolvedGlobalTypeAliases = [];
/** @var array<string, true> */
private array $inProcess = [];
/**
* @param array<string, string> $globalTypeAliases
*/
public function __construct(
#[AutowiredParameter(ref: '%typeAliases%')]
private array $globalTypeAliases,
private TypeStringResolver $typeStringResolver,
private 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;
}
return $nameScope->getTypeAlias($aliasName);
}
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 ShouldNotHappenException(sprintf('Type alias %s already exists as a class.', $aliasName));
}
if (array_key_exists($aliasName, $this->inProcess)) {
throw new 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;
}
}