forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalTypeForParameter.php
More file actions
177 lines (148 loc) · 3.75 KB
/
Copy pathConditionalTypeForParameter.php
File metadata and controls
177 lines (148 loc) · 3.75 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
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 getIf(): Type
{
return $this->if;
}
public function getElse(): Type
{
return $this->else;
}
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 isSuperTypeOf(Type $type): IsSuperTypeOfResult
{
if ($type instanceof self) {
return $this->if->isSuperTypeOf($type->if)
->and($this->else->isSuperTypeOf($type->else));
}
return $this->isSuperTypeOfDefault($type);
}
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;
}
protected function getResult(): Type
{
return TypeCombinator::union($this->if, $this->else);
}
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 self($this->parameterName, $target, $if, $else, $this->negated);
}
public function traverseSimultaneously(Type $right, callable $cb): Type
{
if (!$right instanceof self) {
return $this;
}
$target = $cb($this->target, $right->target);
$if = $cb($this->if, $right->if);
$else = $cb($this->else, $right->else);
if ($this->target === $target && $this->if === $if && $this->else === $else) {
return $this;
}
return new self($this->parameterName, $target, $if, $else, $this->negated);
}
public function toPhpDocNode(): TypeNode
{
return new ConditionalTypeForParameterNode(
$this->parameterName,
$this->target->toPhpDocNode(),
$this->if->toPhpDocNode(),
$this->else->toPhpDocNode(),
$this->negated,
);
}
}