forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConditionalType.php
147 lines (124 loc) · 3.37 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\ConditionalTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use function array_merge;
use function sprintf;
/** @api */
final class ConditionalType implements CompoundType
{
use ConditionalTypeTrait;
use NonGeneralizableTypeTrait;
private function __construct(
private Type $subject,
private Type $target,
Type $if,
Type $else,
private bool $negated,
)
{
$this->if = $if;
$this->else = $else;
}
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 static function create(
Type $subject,
Type $target,
Type $if,
Type $else,
bool $negated,
): Type
{
return (new self($subject, $target, $if, $else, $negated))->resolve();
}
private function resolve(): Type
{
$isSuperType = $this->target->isSuperTypeOf($this->subject);
if ($isSuperType->yes()) {
return !$this->negated ? $this->if : $this->else;
} elseif ($isSuperType->no()) {
return !$this->negated ? $this->else : $this->if;
}
if ($this->isResolved()) {
return $this->getResult();
}
return $this;
}
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 self::create($subject, $target, $if, $else, $this->negated);
}
private function isResolved(): bool
{
return !$this->containsTemplate($this->subject) && !$this->containsTemplate($this->target);
}
private function containsTemplate(Type $type): bool
{
$containsTemplate = false;
TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$containsTemplate): Type {
if ($type instanceof TemplateType) {
$containsTemplate = true;
}
return $containsTemplate ? $type : $traverse($type);
});
return $containsTemplate;
}
/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): Type
{
return new self(
$properties['subject'],
$properties['target'],
$properties['if'],
$properties['else'],
$properties['negated'],
);
}
}