/
TIterable.php
202 lines (179 loc) · 5.49 KB
/
TIterable.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
<?php
namespace Psalm\Type\Atomic;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Union;
use function array_merge;
use function array_values;
use function count;
use function implode;
use function substr;
/**
* denotes the `iterable` type(which can also result from an `is_iterable` check).
*/
final class TIterable extends Atomic
{
use HasIntersectionTrait;
/**
* @use GenericTrait<array{Union, Union}>
*/
use GenericTrait;
/**
* @var string
*/
public $value = 'iterable';
/**
* @var bool
*/
public $has_docblock_params = false;
/**
* @param array{Union, Union}|array<never, never> $type_params
* @param array<string, TNamedObject|TTemplateParam|TIterable|TObjectWithProperties> $extra_types
*/
public function __construct(array $type_params = [], array $extra_types = [])
{
if (isset($type_params[0], $type_params[1])) {
$this->has_docblock_params = true;
$this->type_params = $type_params;
} else {
$this->type_params = [Type::getMixed(), Type::getMixed()];
}
$this->extra_types = $extra_types;
}
public function getKey(bool $include_extra = true): string
{
if ($include_extra && $this->extra_types) {
// do nothing
}
return 'iterable';
}
public function getAssertionString(): string
{
return 'iterable';
}
public function getId(bool $exact = true, bool $nested = false): string
{
$s = '';
foreach ($this->type_params as $type_param) {
$s .= $type_param->getId($exact) . ', ';
}
$extra_types = '';
if ($this->extra_types) {
$extra_types = '&' . implode('&', $this->extra_types);
}
return $this->value . '<' . substr($s, 0, -2) . '>' . $extra_types;
}
/**
* @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 $analysis_php_version_id >= 7_01_00 ? 'iterable' : null;
}
public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return $this->type_params[0]->isMixed() && $this->type_params[1]->isMixed();
}
public function equals(Atomic $other_type, bool $ensure_source_equality): bool
{
if (!$other_type instanceof self) {
return false;
}
if (count($this->type_params) !== count($other_type->type_params)) {
return false;
}
foreach ($this->type_params as $i => $type_param) {
if (!$type_param->equals($other_type->type_params[$i], $ensure_source_equality)) {
return false;
}
}
return true;
}
public function getChildNodes(): array
{
return array_merge($this->type_params, array_values($this->extra_types));
}
/**
* @return static
*/
public function replaceClassLike(string $old, string $new): self
{
$type_params = $this->replaceTypeParamsClassLike(
$old,
$new
);
$intersection = $this->replaceIntersectionClassLike(
$old,
$new
);
if (!$type_params && !$intersection) {
return $this;
}
return new static(
$type_params ?? $this->type_params,
$intersection ?? $this->extra_types
);
}
/**
* @return static
*/
public function replaceTemplateTypesWithArgTypes(TemplateResult $template_result, ?Codebase $codebase): self
{
$type_params = $this->replaceTypeParamsTemplateTypesWithArgTypes(
$template_result,
$codebase
);
$intersection = $this->replaceIntersectionTemplateTypesWithArgTypes(
$template_result,
$codebase
);
return new static(
$type_params ?? $this->type_params,
$intersection ?? $this->extra_types
);
}
/**
* @return static
*/
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): self
{
$types = $this->replaceTypeParamsTemplateTypesWithStandins(
$template_result,
$codebase,
$statements_analyzer,
$input_type,
$input_arg_offset,
$calling_class,
$calling_function,
$replace,
$add_lower_bound,
$depth
);
$intersection = $this->replaceIntersectionTemplateTypesWithStandins(
$template_result,
$codebase,
$statements_analyzer,
$input_type,
$input_arg_offset,
$calling_class,
$calling_function,
$replace,
$add_lower_bound,
$depth
);
if (!$types && !$intersection) {
return $this;
}
return new static(
$types ?? $this->type_params,
$intersection ?? $this->extra_types
);
}
}