-
Notifications
You must be signed in to change notification settings - Fork 651
/
ReturnAnalyzer.php
658 lines (577 loc) · 27.7 KB
/
ReturnAnalyzer.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<?php
namespace Psalm\Internal\Analyzer\Statements;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\CodeLocation\DocblockTypeLocation;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Exception\DocblockParseException;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\ClosureAnalyzer;
use Psalm\Internal\Analyzer\CommentAnalyzer;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\ClassTemplateParamCollector;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\Codebase\TaintFlowGraph;
use Psalm\Internal\Codebase\VariableUseGraph;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\Internal\MethodIdentifier;
use Psalm\Internal\Type\Comparator\TypeComparisonResult;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Type\TemplateInferredTypeReplacer;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Issue\FalsableReturnStatement;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidReturnStatement;
use Psalm\Issue\LessSpecificReturnStatement;
use Psalm\Issue\MixedReturnStatement;
use Psalm\Issue\MixedReturnTypeCoercion;
use Psalm\Issue\NoValue;
use Psalm\Issue\NullableReturnStatement;
use Psalm\IssueBuffer;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Storage\MethodStorage;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TClosure;
use Psalm\Type\Union;
use function array_merge;
use function count;
use function explode;
use function reset;
use function strtolower;
/**
* @internal
*/
class ReturnAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Stmt\Return_ $stmt,
Context $context
): void {
$doc_comment = $stmt->getDocComment();
$var_comments = [];
$var_comment_type = null;
$source = $statements_analyzer->getSource();
$codebase = $statements_analyzer->getCodebase();
if ($doc_comment && ($parsed_docblock = $statements_analyzer->getParsedDocblock())) {
$file_storage_provider = $codebase->file_storage_provider;
$file_storage = $file_storage_provider->get($statements_analyzer->getFilePath());
try {
$var_comments = $codebase->config->disable_var_parsing
? []
: CommentAnalyzer::arrayToDocblocks(
$doc_comment,
$parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$statements_analyzer->getTemplateTypeMap(),
$file_storage->type_aliases
);
} catch (DocblockParseException $e) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
$e->getMessage(),
new CodeLocation($source, $stmt)
)
);
}
foreach ($var_comments as $var_comment) {
if (!$var_comment->type) {
continue;
}
$comment_type = TypeExpander::expandUnion(
$codebase,
$var_comment->type,
$context->self,
$context->self,
$statements_analyzer->getParentFQCLN()
);
if ($codebase->alter_code
&& $var_comment->type_start
&& $var_comment->type_end
&& $var_comment->line_number
) {
$type_location = new DocblockTypeLocation(
$statements_analyzer,
$var_comment->type_start,
$var_comment->type_end,
$var_comment->line_number
);
$codebase->classlikes->handleDocblockTypeInMigration(
$codebase,
$statements_analyzer,
$comment_type,
$type_location,
$context->calling_method_id
);
}
if (!$var_comment->var_id) {
$var_comment_type = $comment_type;
continue;
}
if (isset($context->vars_in_scope[$var_comment->var_id])) {
$comment_type->parent_nodes = $context->vars_in_scope[$var_comment->var_id]->parent_nodes;
}
$context->vars_in_scope[$var_comment->var_id] = $comment_type;
}
}
if ($stmt->expr) {
$context->inside_return = true;
if ($stmt->expr instanceof PhpParser\Node\Expr\Closure
|| $stmt->expr instanceof PhpParser\Node\Expr\ArrowFunction
) {
self::potentiallyInferTypesOnClosureFromParentReturnType(
$statements_analyzer,
$stmt->expr,
$context
);
}
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_return = false;
return;
}
$stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr);
if ($var_comment_type) {
$stmt_type = $var_comment_type;
if ($stmt_expr_type && $stmt_expr_type->parent_nodes) {
$stmt_type->parent_nodes = $stmt_expr_type->parent_nodes;
}
$statements_analyzer->node_data->setType($stmt, $var_comment_type);
} elseif ($stmt_expr_type) {
$stmt_type = $stmt_expr_type;
if ($stmt_type->isNever()) {
IssueBuffer::maybeAdd(
new NoValue(
'This function or method call never returns output',
new CodeLocation($source, $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
$stmt_type = Type::getNever();
}
if ($stmt_type->isVoid()) {
$stmt_type = Type::getNull();
}
} else {
$stmt_type = Type::getMixed();
}
$context->inside_return = false;
} else {
$stmt_type = Type::getVoid();
}
$statements_analyzer->node_data->setType($stmt, $stmt_type);
if ($context->finally_scope) {
foreach ($context->vars_in_scope as $var_id => $type) {
if (isset($context->finally_scope->vars_in_scope[$var_id])) {
$context->finally_scope->vars_in_scope[$var_id] = Type::combineUnionTypes(
$context->finally_scope->vars_in_scope[$var_id],
$type,
$statements_analyzer->getCodebase()
);
} else {
$context->finally_scope->vars_in_scope[$var_id] = $type;
$type->possibly_undefined = true;
$type->possibly_undefined_from_try = true;
}
}
}
if ($source instanceof FunctionLikeAnalyzer
&& !($source->getSource() instanceof TraitAnalyzer)
) {
$source->addReturnTypes($context);
$source->examineParamTypes($statements_analyzer, $context, $codebase, $stmt);
$storage = $source->getFunctionLikeStorage($statements_analyzer);
$cased_method_id = $source->getCorrectlyCasedMethodId();
if ($stmt->expr && $storage->location) {
$inferred_type = TypeExpander::expandUnion(
$codebase,
$stmt_type,
$source->getFQCLN(),
$source->getFQCLN(),
$source->getParentFQCLN()
);
if ($statements_analyzer->data_flow_graph instanceof TaintFlowGraph) {
self::handleTaints(
$statements_analyzer,
$stmt,
$cased_method_id,
$inferred_type,
$storage
);
}
if ($storage instanceof MethodStorage && $context->self) {
$self_class = $context->self;
$declared_return_type = $codebase->methods->getMethodReturnType(
MethodIdentifier::wrap($cased_method_id),
$self_class,
$statements_analyzer,
null
);
} else {
$declared_return_type = $storage->return_type;
}
if ($declared_return_type && !$declared_return_type->hasMixed()) {
$local_return_type = $source->getLocalReturnType(
$declared_return_type,
$storage instanceof MethodStorage && $storage->final
);
if ($storage instanceof MethodStorage) {
[$fq_class_name, $method_name] = explode('::', $cased_method_id);
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
$found_generic_params = ClassTemplateParamCollector::collect(
$codebase,
$class_storage,
$class_storage,
strtolower($method_name),
null,
true
);
if ($found_generic_params) {
foreach ($found_generic_params as $template_name => $_) {
unset($found_generic_params[$template_name][$fq_class_name]);
}
$local_return_type = clone $local_return_type;
TemplateInferredTypeReplacer::replace(
$local_return_type,
new TemplateResult([], $found_generic_params),
$codebase
);
}
}
if ($local_return_type->isGenerator() && $storage->has_yield) {
return;
}
if ($stmt_type->hasMixed()) {
if ($local_return_type->isVoid() || $local_return_type->isNever()) {
if (IssueBuffer::accepts(
new InvalidReturnStatement(
'No return values are expected for ' . $cased_method_id,
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
)) {
return;
}
}
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
&& !($source->getSource() instanceof TraitAnalyzer)
) {
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
}
if ($stmt_type->isMixed()) {
$origin_locations = [];
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
foreach ($stmt_type->parent_nodes as $parent_node) {
$origin_locations = array_merge(
$origin_locations,
$statements_analyzer->data_flow_graph->getOriginLocations($parent_node)
);
}
}
$origin_location = count($origin_locations) === 1 ? reset($origin_locations) : null;
$return_location = new CodeLocation($source, $stmt->expr);
if ($origin_location && $origin_location->getHash() === $return_location->getHash()) {
$origin_location = null;
}
IssueBuffer::maybeAdd(
new MixedReturnStatement(
'Could not infer a return type',
$return_location,
$origin_location
),
$statements_analyzer->getSuppressedIssues()
);
return;
}
IssueBuffer::maybeAdd(
new MixedReturnStatement(
'Possibly-mixed return value',
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
}
if ($local_return_type->isMixed()) {
return;
}
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
&& !($source->getSource() instanceof TraitAnalyzer)
) {
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
}
if ($local_return_type->isVoid()) {
if (IssueBuffer::accepts(
new InvalidReturnStatement(
'No return values are expected for ' . $cased_method_id,
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
)) {
return;
}
return;
}
$union_comparison_results = new TypeComparisonResult();
if (!UnionTypeComparator::isContainedBy(
$codebase,
$inferred_type,
$local_return_type,
true,
true,
$union_comparison_results
)
) {
// is the declared return type more specific than the inferred one?
if ($union_comparison_results->type_coerced) {
if ($union_comparison_results->type_coerced_from_mixed) {
if (!$union_comparison_results->type_coerced_from_as_mixed) {
if ($inferred_type->hasMixed()) {
IssueBuffer::maybeAdd(
new MixedReturnStatement(
'Could not infer a return type',
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
} else {
IssueBuffer::maybeAdd(
new MixedReturnTypeCoercion(
'The type \'' . $stmt_type->getId() . '\' is more general than the'
. ' declared return type \'' . $local_return_type->getId() . '\''
. ' for ' . $cased_method_id,
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
}
}
} else {
IssueBuffer::maybeAdd(
new LessSpecificReturnStatement(
'The type \'' . $stmt_type->getId() . '\' is more general than the'
. ' declared return type \'' . $local_return_type->getId() . '\''
. ' for ' . $cased_method_id,
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
}
foreach ($local_return_type->getAtomicTypes() as $local_type_part) {
if ($local_type_part instanceof TClassString
&& $stmt->expr instanceof PhpParser\Node\Scalar\String_
) {
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$stmt->expr->value,
new CodeLocation($source, $stmt->expr),
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true)
) === false
) {
return;
}
} elseif ($local_type_part instanceof TArray
&& $stmt->expr instanceof PhpParser\Node\Expr\Array_
) {
$value_param = $local_type_part->type_params[1];
foreach ($value_param->getAtomicTypes() as $local_array_type_part) {
if ($local_array_type_part instanceof TClassString) {
foreach ($stmt->expr->items as $item) {
if ($item && $item->value instanceof PhpParser\Node\Scalar\String_) {
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$item->value->value,
new CodeLocation($source, $item->value),
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true)
) === false
) {
return;
}
}
}
}
}
}
}
} else {
IssueBuffer::maybeAdd(
new InvalidReturnStatement(
'The inferred type \'' . $inferred_type->getId()
. '\' does not match the declared return '
. 'type \'' . $local_return_type->getId() . '\' for ' . $cased_method_id,
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
}
}
if (!$stmt_type->ignore_nullable_issues
&& $inferred_type->isNullable()
&& !$local_return_type->isNullable()
&& !$local_return_type->hasTemplate()
) {
IssueBuffer::maybeAdd(
new NullableReturnStatement(
'The declared return type \'' . $local_return_type->getId() . '\' for '
. $cased_method_id . ' is not nullable, but the function returns \''
. $inferred_type->getId() . '\'',
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
}
if (!$stmt_type->ignore_falsable_issues
&& $inferred_type->isFalsable()
&& !$local_return_type->isFalsable()
&& (!$local_return_type->hasBool() || $local_return_type->isTrue())
&& !$local_return_type->hasScalar()
) {
IssueBuffer::maybeAdd(
new FalsableReturnStatement(
'The declared return type \'' . $local_return_type . '\' for '
. $cased_method_id . ' does not allow false, but the function returns \''
. $inferred_type . '\'',
new CodeLocation($source, $stmt->expr)
),
$statements_analyzer->getSuppressedIssues()
);
}
}
} else {
if ($storage->signature_return_type
&& !$storage->signature_return_type->isVoid()
&& !$storage->has_yield
) {
IssueBuffer::maybeAdd(
new InvalidReturnStatement(
'Empty return statement is not expected in ' . $cased_method_id,
new CodeLocation($source, $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
}
}
}
}
private static function handleTaints(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Stmt\Return_ $stmt,
string $cased_method_id,
Union $inferred_type,
FunctionLikeStorage $storage
): void {
if (!$statements_analyzer->data_flow_graph instanceof TaintFlowGraph
|| !$stmt->expr
|| !$storage->location
) {
return;
}
$method_node = DataFlowNode::getForMethodReturn(
strtolower($cased_method_id),
$cased_method_id,
$storage->signature_return_type_location ?: $storage->location
);
$statements_analyzer->data_flow_graph->addNode($method_node);
if ($inferred_type->parent_nodes) {
foreach ($inferred_type->parent_nodes as $parent_node) {
$statements_analyzer->data_flow_graph->addPath(
$parent_node,
$method_node,
'return',
$storage->added_taints,
$storage->removed_taints
);
}
}
}
/**
* If a function returns a closure, we try to infer the param/return types of
* the inner closure.
* @see \Psalm\Tests\ReturnTypeTest:756
* @param PhpParser\Node\Expr\Closure|PhpParser\Node\Expr\ArrowFunction $expr
*/
private static function potentiallyInferTypesOnClosureFromParentReturnType(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\FunctionLike $expr,
Context $context
): void {
// if not returning from inside of a function, return
if (!$context->calling_method_id && !$context->calling_function_id) {
return;
}
$closure_id = (new ClosureAnalyzer($expr, $statements_analyzer))->getClosureId();
$closure_storage = $statements_analyzer
->getCodebase()
->getFunctionLikeStorage($statements_analyzer, $closure_id);
$parent_fn_storage = $statements_analyzer
->getCodebase()
->getFunctionLikeStorage(
$statements_analyzer,
$context->calling_function_id ?: $context->calling_method_id
);
if ($parent_fn_storage->return_type === null) {
return;
}
// can't infer returned closure if the parent doesn't have a callable return type
if (!$parent_fn_storage->return_type->hasCallableType()) {
return;
}
// cannot infer if we have union/intersection types
if (!$parent_fn_storage->return_type->isSingle()) {
return;
}
/** @var TClosure|TCallable $parent_callable_return_type */
$parent_callable_return_type = $parent_fn_storage->return_type->getSingleAtomic();
if ($parent_callable_return_type->params === null && $parent_callable_return_type->return_type === null) {
return;
}
foreach ($closure_storage->params as $key => $param) {
$parent_param = $parent_callable_return_type->params[$key] ?? null;
$param->type = self::inferInnerClosureTypeFromParent(
$statements_analyzer->getCodebase(),
$param->type,
$parent_param->type ?? null
);
}
$closure_storage->return_type = self::inferInnerClosureTypeFromParent(
$statements_analyzer->getCodebase(),
$closure_storage->return_type,
$parent_callable_return_type->return_type
);
}
/**
* - If non parent type, do nothing
* - If no return type, infer from parent
* - If parent return type is more specific, infer from parent
* - else, do nothing
*/
private static function inferInnerClosureTypeFromParent(
Codebase $codebase,
?Union $return_type,
?Union $parent_return_type
): ?Union {
if (!$parent_return_type) {
return $return_type;
}
if (!$return_type || UnionTypeComparator::isContainedBy($codebase, $parent_return_type, $return_type)) {
return $parent_return_type;
}
return $return_type;
}
}