/
TClassStringMap.php
228 lines (200 loc) · 5.95 KB
/
TClassStringMap.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
<?php
namespace Psalm\Type\Atomic;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\TemplateInferredTypeReplacer;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TemplateStandinTypeReplacer;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Union;
use function get_class;
/**
* Represents an array where the type of each value
* is a function of its string key value
*/
class TClassStringMap extends Atomic
{
/**
* @var string
*/
public $param_name;
/**
* @var ?TNamedObject
*/
public $as_type;
/**
* @var Union
*/
public $value_param;
/**
* Constructs a new instance of a list
*/
public function __construct(string $param_name, ?TNamedObject $as_type, Union $value_param)
{
$this->value_param = $value_param;
$this->param_name = $param_name;
$this->as_type = $as_type;
}
public function getId(bool $exact = true, bool $nested = false): string
{
/** @psalm-suppress MixedOperand */
return 'class-string-map'
. '<'
. $this->param_name
. ' as '
. ($this->as_type ? $this->as_type->getId($exact) : 'object')
. ', '
. $this->value_param->getId($exact)
. '>';
}
public function __clone()
{
$this->value_param = clone $this->value_param;
}
/**
* @param array<lowercase-string, string> $aliased_classes
*
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
if ($use_phpdoc_format) {
return (new TArray([Type::getString(), $this->value_param]))
->toNamespacedString(
$namespace,
$aliased_classes,
$this_class,
true
);
}
/** @psalm-suppress MixedOperand */
return 'class-string-map'
. '<'
. $this->param_name
. ($this->as_type ? ' as ' . $this->as_type : '')
. ', '
. $this->value_param->toNamespacedString(
$namespace,
$aliased_classes,
$this_class,
false
)
. '>';
}
/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toPhpString(
?string $namespace,
array $aliased_classes,
?string $this_class,
int $analysis_php_version_id
): string {
return 'array';
}
public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
}
public function getKey(bool $include_extra = true): string
{
return 'array';
}
public function replaceTemplateTypesWithStandins(
TemplateResult $template_result,
Codebase $codebase,
?StatementsAnalyzer $statements_analyzer = null,
?Atomic $input_type = null,
?int $input_arg_offset = null,
?string $calling_class = null,
?string $calling_function = null,
bool $replace = true,
bool $add_lower_bound = false,
int $depth = 0
): Atomic {
$map = clone $this;
foreach ([Type::getString(), $map->value_param] as $offset => $type_param) {
$input_type_param = null;
if (($input_type instanceof TGenericObject
|| $input_type instanceof TIterable
|| $input_type instanceof TArray)
&&
isset($input_type->type_params[$offset])
) {
$input_type_param = clone $input_type->type_params[$offset];
} elseif ($input_type instanceof TKeyedArray) {
if ($offset === 0) {
$input_type_param = $input_type->getGenericKeyType();
} else {
$input_type_param = $input_type->getGenericValueType();
}
} elseif ($input_type instanceof TList) {
if ($offset === 0) {
continue;
}
$input_type_param = clone $input_type->type_param;
}
$value_param = TemplateStandinTypeReplacer::replace(
$type_param,
$template_result,
$codebase,
$statements_analyzer,
$input_type_param,
$input_arg_offset,
$calling_class,
$calling_function,
$replace,
$add_lower_bound,
null,
$depth + 1
);
if ($offset === 1) {
$map->value_param = $value_param;
}
}
return $map;
}
public function replaceTemplateTypesWithArgTypes(
TemplateResult $template_result,
?Codebase $codebase
): void {
TemplateInferredTypeReplacer::replace(
$this->value_param,
$template_result,
$codebase
);
}
public function getChildNodes(): array
{
return [$this->value_param];
}
public function equals(Atomic $other_type, bool $ensure_source_equality): bool
{
if (get_class($other_type) !== static::class) {
return false;
}
if (!$this->value_param->equals($other_type->value_param, $ensure_source_equality)) {
return false;
}
return true;
}
public function getAssertionString(): string
{
return $this->getKey();
}
public function getStandinKeyParam(): Union
{
return new Union([
new TTemplateParamClass(
$this->param_name,
$this->as_type->value ?? 'object',
$this->as_type,
'class-string-map'
)
]);
}
}