forked from vimeo/psalm
/
ReturnTypeCollector.php
318 lines (289 loc) · 10.7 KB
/
ReturnTypeCollector.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Psalm\Internal\Analyzer\FunctionLike;
use PhpParser;
use PhpParser\NodeTraverser;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
use Psalm\Internal\PhpVisitor\YieldTypeCollector;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TIterable;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;
use function array_merge;
/**
* A class for analysing a given method call's effects in relation to $this/self and also looking at return types
*
* @internal
*/
class ReturnTypeCollector
{
/**
* Gets the return types from a list of statements
*
* @param array<PhpParser\Node> $stmts
* @param list<Union> $yield_types
*
* @return list<Union> a list of return types
*
* @psalm-suppress ComplexMethod to be refactored
*/
public static function getReturnTypes(
Codebase $codebase,
NodeDataProvider $nodes,
array $stmts,
array &$yield_types,
bool $collapse_types = false
): array {
$return_types = [];
foreach ($stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Return_) {
if (!$stmt->expr) {
$return_types[] = Type::getVoid();
} elseif ($stmt_type = $nodes->getType($stmt)) {
$return_types[] = $stmt_type;
$yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($stmt->expr, $nodes));
} elseif ($stmt->expr instanceof PhpParser\Node\Scalar\String_) {
$return_types[] = Type::getString();
} elseif ($stmt->expr instanceof PhpParser\Node\Scalar\LNumber) {
$return_types[] = Type::getInt();
} elseif ($stmt->expr instanceof PhpParser\Node\Expr\ConstFetch) {
if ((string)$stmt->expr->name === 'true') {
$return_types[] = Type::getTrue();
} elseif ((string)$stmt->expr->name === 'false') {
$return_types[] = Type::getFalse();
} elseif ((string)$stmt->expr->name === 'null') {
$return_types[] = Type::getNull();
}
} else {
$return_types[] = Type::getMixed();
}
break;
}
if ($stmt instanceof PhpParser\Node\Stmt\Break_
|| $stmt instanceof PhpParser\Node\Stmt\Continue_
) {
break;
}
if ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
if ($collapse_types) {
$return_types[] = Type::getNever();
}
break;
}
if ($stmt instanceof PhpParser\Node\Stmt\Expression) {
if ($stmt->expr instanceof PhpParser\Node\Expr\Exit_) {
if ($collapse_types) {
$return_types[] = Type::getNever();
}
break;
}
if ($stmt->expr instanceof PhpParser\Node\Expr\Assign) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
[$stmt->expr->expr],
$yield_types
)
];
}
$yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($stmt->expr, $nodes));
} elseif ($stmt instanceof PhpParser\Node\Stmt\If_) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->stmts,
$yield_types
)
];
foreach ($stmt->elseifs as $elseif) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$elseif->stmts,
$yield_types
)
];
}
if ($stmt->else) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->else->stmts,
$yield_types
)
];
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->stmts,
$yield_types
)
];
foreach ($stmt->catches as $catch) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$catch->stmts,
$yield_types
)
];
}
if ($stmt->finally) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->finally->stmts,
$yield_types
)
];
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->stmts,
$yield_types
)
];
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->stmts,
$yield_types
)
];
} elseif ($stmt instanceof PhpParser\Node\Stmt\While_) {
$yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($stmt->cond, $nodes));
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->stmts,
$yield_types
)
];
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$stmt->stmts,
$yield_types
)
];
} elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
foreach ($stmt->cases as $case) {
$return_types = [
...$return_types,
...self::getReturnTypes(
$codebase,
$nodes,
$case->stmts,
$yield_types
)
];
}
}
}
// if we're at the top level and we're not ending in a return, make sure to add possible null
if ($collapse_types) {
// if it's a generator, boil everything down to a single generator return type
if ($yield_types) {
$yield_types = self::processYieldTypes($codebase, $return_types, $yield_types);
}
}
return $return_types;
}
/**
* @param list<Union> $return_types
* @param non-empty-list<Union> $yield_types
* @return non-empty-list<Union>
*/
private static function processYieldTypes(
Codebase $codebase,
array $return_types,
array $yield_types
): array {
$key_type = null;
$value_type = null;
$yield_type = Type::combineUnionTypeArray($yield_types, null);
foreach ($yield_type->getAtomicTypes() as $type) {
if ($type instanceof TKeyedArray) {
$type = $type->getGenericArrayType();
}
if ($type instanceof TList) {
$type = new TArray([Type::getInt(), $type->type_param]);
}
if ($type instanceof TArray) {
[$key_type_param, $value_type_param] = $type->type_params;
$key_type = Type::combineUnionTypes(clone $key_type_param, $key_type);
$value_type = Type::combineUnionTypes(clone $value_type_param, $value_type);
} elseif ($type instanceof TIterable
|| $type instanceof TNamedObject
) {
ForeachAnalyzer::getKeyValueParamsForTraversableObject(
$type,
$codebase,
$key_type,
$value_type
);
}
}
return [
new Union([
new TGenericObject(
'Generator',
[
$key_type ?? Type::getMixed(),
$value_type ?? Type::getMixed(),
Type::getMixed(),
$return_types ? Type::combineUnionTypeArray($return_types, null) : Type::getVoid()
]
),
])
];
}
/**
* @return list<Union>
*/
private static function getYieldTypeFromExpression(
PhpParser\Node\Expr $stmt,
NodeDataProvider $nodes
): array {
$collector = new YieldTypeCollector($nodes);
$traverser = new NodeTraverser();
$traverser->addVisitor(
$collector
);
$traverser->traverse([$stmt]);
return $collector->getYieldTypes();
}
}