/
TypeHintResolver.php
144 lines (118 loc) · 4.13 KB
/
TypeHintResolver.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
<?php
namespace Psalm\Internal\PhpVisitor\Reflector;
use PhpParser;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType;
use Psalm\Aliases;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FileStorage;
use Psalm\Type;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Union;
use UnexpectedValueException;
use function implode;
use function strtolower;
class TypeHintResolver
{
/**
* @param Identifier|IntersectionType|Name|NullableType|UnionType $hint
*/
public static function resolve(
PhpParser\NodeAbstract $hint,
Codebase $codebase,
FileStorage $file_storage,
?ClassLikeStorage $classlike_storage,
Aliases $aliases,
int $php_major_version,
int $php_minor_version
): Union {
if ($hint instanceof PhpParser\Node\UnionType) {
$type = null;
if (!$hint->types) {
throw new UnexpectedValueException('bad');
}
foreach ($hint->types as $atomic_typehint) {
$resolved_type = self::resolve(
$atomic_typehint,
$codebase,
$file_storage,
$classlike_storage,
$aliases,
$php_major_version,
$php_minor_version
);
$type = Type::combineUnionTypes($resolved_type, $type);
}
return $type;
}
if ($hint instanceof PhpParser\Node\IntersectionType) {
$type = null;
if (!$hint->types) {
throw new UnexpectedValueException('bad');
}
foreach ($hint->types as $atomic_typehint) {
$resolved_type = self::resolve(
$atomic_typehint,
$codebase,
$file_storage,
$classlike_storage,
$aliases,
$php_major_version,
$php_minor_version
);
$type = Type::intersectUnionTypes($resolved_type, $type, $codebase);
}
if ($type === null) {
throw new UnexpectedValueException('bad');
}
return $type;
}
$is_nullable = false;
if ($hint instanceof PhpParser\Node\NullableType) {
$is_nullable = true;
$hint = $hint->type;
}
$type_string = null;
if ($hint instanceof PhpParser\Node\Identifier) {
$fq_type_string = $hint->name;
} elseif ($hint instanceof PhpParser\Node\Name\FullyQualified) {
$fq_type_string = (string)$hint;
$codebase->scanner->queueClassLikeForScanning($fq_type_string);
$file_storage->referenced_classlikes[strtolower($fq_type_string)] = $fq_type_string;
} else {
$lower_hint = strtolower($hint->parts[0]);
if ($classlike_storage
&& ($lower_hint === 'self' || $lower_hint === 'static')
&& !$classlike_storage->is_trait
) {
$fq_type_string = $classlike_storage->name;
if ($lower_hint === 'static') {
$fq_type_string .= '&static';
}
} else {
$type_string = implode('\\', $hint->parts);
$fq_type_string = ClassLikeAnalyzer::getFQCLNFromNameObject($hint, $aliases);
$codebase->scanner->queueClassLikeForScanning($fq_type_string);
$file_storage->referenced_classlikes[strtolower($fq_type_string)] = $fq_type_string;
}
}
$type = Type::parseString(
$fq_type_string,
[$php_major_version, $php_minor_version],
[]
);
if ($type_string) {
$atomic_type = $type->getSingleAtomic();
$atomic_type->text = $type_string;
}
if ($is_nullable) {
$type->addType(new TNull);
}
return $type;
}
}