forked from vimeo/psalm
/
YieldTypeCollector.php
75 lines (62 loc) · 1.87 KB
/
YieldTypeCollector.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
<?php
namespace Psalm\Internal\PhpVisitor;
use PhpParser\Node;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Expr\Yield_;
use PhpParser\NodeVisitorAbstract;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Type;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Union;
/**
* @internal
*/
class YieldTypeCollector extends NodeVisitorAbstract
{
/** @var list<Union> */
private array $yield_types = [];
private NodeDataProvider $nodes;
public function __construct(NodeDataProvider $nodes)
{
$this->nodes = $nodes;
}
public function enterNode(Node $stmt): ?Node
{
if ($stmt instanceof Yield_) {
$key_type = null;
if ($stmt->key && $stmt_key_type = $this->nodes->getType($stmt->key)) {
$key_type = $stmt_key_type;
}
if ($stmt->value
&& $value_type = $this->nodes->getType($stmt->value)
) {
$generator_type = new TGenericObject(
'Generator',
[
$key_type ? clone $key_type : Type::getInt(),
clone $value_type,
Type::getMixed(),
Type::getMixed()
]
);
$this->yield_types []= new Union([$generator_type]);
return null;
}
$this->yield_types []= Type::getMixed();
} elseif ($stmt instanceof YieldFrom) {
if ($stmt_expr_type = $this->nodes->getType($stmt->expr)) {
$this->yield_types []= $stmt_expr_type;
return null;
}
$this->yield_types []= Type::getMixed();
}
return null;
}
/**
* @return list<Union>
*/
public function getYieldTypes(): array
{
return $this->yield_types;
}
}