-
Notifications
You must be signed in to change notification settings - Fork 431
/
ConditionalTypeForParameter.php
144 lines (123 loc) · 2.94 KB
/
ConditionalTypeForParameter.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
<?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 ConditionalTypeForParameter implements CompoundType, LateResolvableType
{
use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;
public function __construct(
private string $parameterName,
private Type $target,
private Type $if,
private Type $else,
private bool $negated,
)
{
}
public function getParameterName(): string
{
return $this->parameterName;
}
public function getTarget(): Type
{
return $this->target;
}
public function isNegated(): bool
{
return $this->negated;
}
public function changeParameterName(string $parameterName): self
{
return new self(
$parameterName,
$this->target,
$this->if,
$this->else,
$this->negated,
);
}
public function toConditional(Type $subject): Type
{
return new ConditionalType(
$subject,
$this->target,
$this->if,
$this->else,
$this->negated,
);
}
public function getReferencedClasses(): array
{
return array_merge(
$this->target->getReferencedClasses(),
$this->if->getReferencedClasses(),
$this->else->getReferencedClasses(),
);
}
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return array_merge(
$this->target->getReferencedTemplateTypes($positionVariance),
$this->if->getReferencedTemplateTypes($positionVariance),
$this->else->getReferencedTemplateTypes($positionVariance),
);
}
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->parameterName === $type->parameterName
&& $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->parameterName,
$this->negated ? 'is not' : 'is',
$this->target->describe($level),
$this->if->describe($level),
$this->else->describe($level),
);
}
public function isResolvable(): bool
{
return false;
}
private function getResult(): Type
{
return TypeCombinator::union($this->if, $this->else);
}
/**
* @param callable(Type): Type $cb
*/
public function traverse(callable $cb): Type
{
$target = $cb($this->target);
$if = $cb($this->if);
$else = $cb($this->else);
if ($this->target === $target && $this->if === $if && $this->else === $else) {
return $this;
}
return new ConditionalTypeForParameter($this->parameterName, $target, $if, $else, $this->negated);
}
/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): Type
{
return new self(
$properties['parameterName'],
$properties['target'],
$properties['if'],
$properties['else'],
$properties['negated'],
);
}
}