/
TemplateTypeVariance.php
187 lines (151 loc) · 3.71 KB
/
TemplateTypeVariance.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
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
178
179
180
181
182
183
184
185
186
187
<?php declare(strict_types = 1);
namespace PHPStan\Type\Generic;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
/** @api */
class TemplateTypeVariance
{
private const INVARIANT = 1;
private const COVARIANT = 2;
private const CONTRAVARIANT = 3;
private const STATIC = 4;
/** @var self[] */
private static array $registry;
private static bool $invarianceCompositionEnabled = false;
private function __construct(private int $value)
{
}
private static function create(int $value): self
{
self::$registry[$value] ??= new self($value);
return self::$registry[$value];
}
public static function createInvariant(): self
{
return self::create(self::INVARIANT);
}
public static function createCovariant(): self
{
return self::create(self::COVARIANT);
}
public static function createContravariant(): self
{
return self::create(self::CONTRAVARIANT);
}
public static function createStatic(): self
{
return self::create(self::STATIC);
}
public function invariant(): bool
{
return $this->value === self::INVARIANT;
}
public function covariant(): bool
{
return $this->value === self::COVARIANT;
}
public function contravariant(): bool
{
return $this->value === self::CONTRAVARIANT;
}
public function static(): bool
{
return $this->value === self::STATIC;
}
public function compose(self $other): self
{
if ($this->contravariant()) {
if ($other->contravariant()) {
return self::createCovariant();
}
if ($other->covariant()) {
return self::createContravariant();
}
return self::createInvariant();
}
if ($this->covariant()) {
if ($other->contravariant()) {
return self::createContravariant();
}
if ($other->covariant()) {
return self::createCovariant();
}
return self::createInvariant();
}
if (self::$invarianceCompositionEnabled && $this->invariant()) {
return self::createInvariant();
}
return $other;
}
public function isValidVariance(Type $a, Type $b): TrinaryLogic
{
if ($b instanceof NeverType) {
return TrinaryLogic::createYes();
}
if ($a instanceof MixedType && !$a instanceof TemplateType) {
return TrinaryLogic::createYes();
}
if ($a instanceof BenevolentUnionType) {
if (!$a->isSuperTypeOf($b)->no()) {
return TrinaryLogic::createYes();
}
}
if ($b instanceof BenevolentUnionType) {
if (!$b->isSuperTypeOf($a)->no()) {
return TrinaryLogic::createYes();
}
}
if ($b instanceof MixedType && !$b instanceof TemplateType) {
return TrinaryLogic::createYes();
}
if ($this->invariant()) {
return TrinaryLogic::createFromBoolean($a->equals($b));
}
if ($this->covariant()) {
return $a->isSuperTypeOf($b);
}
if ($this->contravariant()) {
return $b->isSuperTypeOf($a);
}
throw new ShouldNotHappenException();
}
public function equals(self $other): bool
{
return $other->value === $this->value;
}
public function validPosition(self $other): bool
{
return $other->value === $this->value
|| $other->invariant()
|| $this->static();
}
public function describe(): string
{
switch ($this->value) {
case self::INVARIANT:
return 'invariant';
case self::COVARIANT:
return 'covariant';
case self::CONTRAVARIANT:
return 'contravariant';
case self::STATIC:
return 'static';
}
throw new ShouldNotHappenException();
}
/**
* @param array{value: int} $properties
*/
public static function __set_state(array $properties): self
{
return new self($properties['value']);
}
public static function setInvarianceCompositionEnabled(bool $enabled): void
{
self::$invarianceCompositionEnabled = $enabled;
}
}