/
ConditionalType.php
132 lines (110 loc) · 2.97 KB
/
ConditionalType.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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use function array_merge;
use function sprintf;
/** @api */
final class ConditionalType implements CompoundType, LateResolvableType
{
use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;
public function __construct(
private Type $subject,
private Type $target,
private Type $if,
private Type $else,
private bool $negated,
)
{
}
public function getSubject(): Type
{
return $this->subject;
}
public function getTarget(): Type
{
return $this->target;
}
public function isNegated(): bool
{
return $this->negated;
}
public function getReferencedClasses(): array
{
return array_merge(
$this->subject->getReferencedClasses(),
$this->target->getReferencedClasses(),
$this->if->getReferencedClasses(),
$this->else->getReferencedClasses(),
);
}
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return array_merge(
$this->subject->getReferencedTemplateTypes($positionVariance),
$this->target->getReferencedTemplateTypes($positionVariance),
$this->if->getReferencedTemplateTypes($positionVariance),
$this->else->getReferencedTemplateTypes($positionVariance),
);
}
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->subject->equals($type->subject)
&& $this->target->equals($type->target)
&& $this->if->equals($type->if)
&& $this->else->equals($type->else);
}
public function describe(VerbosityLevel $level): string
{
return sprintf(
'(%s %s %s ? %s : %s)',
$this->subject->describe($level),
$this->negated ? 'is not' : 'is',
$this->target->describe($level),
$this->if->describe($level),
$this->else->describe($level),
);
}
public function isResolvable(): bool
{
return !TypeUtils::containsTemplateType($this->subject) && !TypeUtils::containsTemplateType($this->target);
}
protected function getResult(): Type
{
$isSuperType = $this->target->isSuperTypeOf($this->subject);
if ($isSuperType->yes()) {
return !$this->negated ? $this->if : $this->else;
}
if ($isSuperType->no()) {
return !$this->negated ? $this->else : $this->if;
}
return TypeCombinator::union($this->if, $this->else);
}
public function traverse(callable $cb): Type
{
$subject = $cb($this->subject);
$target = $cb($this->target);
$if = $cb($this->if);
$else = $cb($this->else);
if ($this->subject === $subject && $this->target === $target && $this->if === $if && $this->else === $else) {
return $this;
}
return new self($subject, $target, $if, $else, $this->negated);
}
/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): Type
{
return new self(
$properties['subject'],
$properties['target'],
$properties['if'],
$properties['else'],
$properties['negated'],
);
}
}