-
Notifications
You must be signed in to change notification settings - Fork 431
/
conditional-types.php
202 lines (163 loc) · 4.79 KB
/
conditional-types.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace ConditionalTypes;
use function PHPStan\Testing\assertType;
abstract class Test
{
/**
* @template TKey of array-key
* @template TArray of array<TKey, mixed>
*
* @param TArray $array
*
* @return (TArray is non-empty-array ? non-empty-list<TKey> : list<TKey>)
*/
abstract public function arrayKeys(array $array);
/**
* @param array $array
* @param non-empty-array $nonEmptyArray
*
* @param array<int, int> $intArray
* @param non-empty-array<int, int> $nonEmptyIntArray
*
* @param array{} $emptyArray
*/
public function testArrayKeys(array $array, array $nonEmptyArray, array $intArray, array $nonEmptyIntArray, array $emptyArray): void
{
assertType('array<int, (int|string)>', $this->arrayKeys($array));
assertType('array<int, int>', $this->arrayKeys($intArray));
assertType('non-empty-array<int, (int|string)>', $this->arrayKeys($nonEmptyArray));
assertType('non-empty-array<int, int>', $this->arrayKeys($nonEmptyIntArray));
assertType('array<int, *NEVER*>', $this->arrayKeys($emptyArray));
}
/**
* @return ($array is non-empty-array ? true : false)
*/
abstract public function accessory(array $array): bool;
/**
* @param array $array
* @param non-empty-array $nonEmptyArray
* @param array{} $emptyArray
*/
public function testAccessory(array $array, array $nonEmptyArray, array $emptyArray): void
{
assertType('bool', $this->accessory($array));
assertType('true', $this->accessory($nonEmptyArray));
assertType('false', $this->accessory($emptyArray));
assertType('bool', $this->accessory($_GET['array']));
}
/**
* @return ($as_float is true ? float : string)
*/
abstract public function microtime(bool $as_float);
public function testMicrotime(): void
{
assertType('float', $this->microtime(true));
assertType('string', $this->microtime(false));
assertType('float|string', $this->microtime($_GET['as_float']));
}
/**
* @return ($version is 8 ? true : ($version is 10 ? true : false))
*/
abstract public function versionIsEightOrTen(int $version);
public function testVersionIsEightOrTen(): void
{
assertType('false', $this->versionIsEightOrTen(6));
assertType('false', $this->versionIsEightOrTen(7));
assertType('true', $this->versionIsEightOrTen(8));
assertType('false', $this->versionIsEightOrTen(9));
assertType('true', $this->versionIsEightOrTen(10));
assertType('false', $this->versionIsEightOrTen(11));
assertType('false', $this->versionIsEightOrTen(12));
assertType('bool', $this->versionIsEightOrTen($_GET['version']));
}
/**
* @return ($parameter is true ? int : string)
*/
abstract public function missingParameter();
public function testMissingParameter(): void
{
assertType('int|string', $this->missingParameter());
}
/**
* @return (5 is int ? true : false)
*/
abstract public function deterministicReturnValue();
public function testDeterministicReturnValue(): void
{
assertType('true', $this->deterministicReturnValue());
}
/**
* @param (true is true ? string : bool) $foo
* @param (5 is int<4, 6> ? string : bool) $bar
* @param (5 is not int<0, 4> ? (4 is bool ? float : string) : bool) $baz
*/
public function testDeterministicParameter($foo, $bar, $baz): void
{
assertType('string', $foo);
assertType('string', $bar);
assertType('string', $baz);
}
/**
* @template TInt of int
* @param TInt $foo
* @param (TInt is 5 ? int<0, 10> : int<10, 100>) $bar
*/
public function testConditionalInParameter(int $foo, int $bar): void
{
assertType('TInt of int (method ConditionalTypes\Test::testConditionalInParameter(), argument)', $foo);
assertType('int<0, 100>', $bar);
}
/**
* @return ($input is null ? null : string)
*/
abstract public function retainNullable(?bool $input): ?string;
public function testRetainNullable(?bool $input): void
{
assertType('string|null', $this->retainNullable($input));
if ($input === null) {
assertType('null', $this->retainNullable($input));
} else {
assertType('string', $this->retainNullable($input));
}
}
/**
* @return ($option is 1 ? never : void)
*/
abstract public function maybeNever(int $option): void;
public function testMaybeNever(): void
{
assertType('void', $this->maybeNever(0));
assertType('*NEVER*', $this->maybeNever(1));
assertType('void', $this->maybeNever(2));
}
}
class ParentClassToInherit
{
/**
* @param mixed $p
* @return ($p is int ? int : string)
*/
public function doFoo($p)
{
}
}
class ChildClass extends ParentClassToInherit
{
public function doFoo($p)
{
}
}
function (ChildClass $c): void {
assertType('int', $c->doFoo(1));
assertType('string', $c->doFoo('foo'));
};
class ChildClass2 extends ParentClassToInherit
{
public function doFoo($x)
{
}
}
function (ChildClass2 $c): void {
assertType('int', $c->doFoo(1));
assertType('string', $c->doFoo('foo'));
};