-
Notifications
You must be signed in to change notification settings - Fork 653
/
YieldFromAnalyzer.php
82 lines (68 loc) · 2.69 KB
/
YieldFromAnalyzer.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
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TKeyedArray;
use function strtolower;
class YieldFromAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\YieldFrom $stmt,
Context $context
): bool {
$was_inside_call = $context->inside_call;
$context->inside_call = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_call = $was_inside_call;
return false;
}
if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
$key_type = null;
$value_type = null;
$always_non_empty_array = true;
if (ForeachAnalyzer::checkIteratorType(
$statements_analyzer,
$stmt,
$stmt->expr,
$stmt_expr_type,
$statements_analyzer->getCodebase(),
$context,
$key_type,
$value_type,
$always_non_empty_array
) === false
) {
$context->inside_call = $was_inside_call;
return false;
}
$yield_from_type = null;
foreach ($stmt_expr_type->getAtomicTypes() as $atomic_type) {
if ($yield_from_type === null) {
if ($atomic_type instanceof TGenericObject
&& strtolower($atomic_type->value) === 'generator'
&& isset($atomic_type->type_params[3])
) {
$yield_from_type = clone $atomic_type->type_params[3];
} elseif ($atomic_type instanceof TArray) {
$yield_from_type = clone $atomic_type->type_params[1];
} elseif ($atomic_type instanceof TKeyedArray) {
$yield_from_type = $atomic_type->getGenericValueType();
}
} else {
$yield_from_type = Type::getMixed();
}
}
// this should be whatever the generator above returns, but *not* the return type
$statements_analyzer->node_data->setType($stmt, $yield_from_type ?: Type::getMixed());
}
$context->inside_call = $was_inside_call;
return true;
}
}