-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathbug-12444b.php
More file actions
158 lines (139 loc) · 2.69 KB
/
bug-12444b.php
File metadata and controls
158 lines (139 loc) · 2.69 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
<?php
declare(strict_types = 1);
namespace Bug12444b;
use function PHPStan\Testing\assertType;
/**
* @template-contravariant T
*/
interface Contra {}
/**
* @template T
* @extends Contra<T>
*/
interface Inv extends Contra {}
/**
* @template T of object
* @param class-string<T> $class
* @return Contra<T>
*/
function contra(string $class): Contra
{
throw new \Exception();
}
/**
* @template T of object
* @param class-string<T> $class
* @return Inv<T>
*/
function inv(string $class): Inv
{
throw new \Exception();
}
// Non-variadic: two separate contravariant params
/**
* @template T
* @param T $value
* @param Contra<T> $a
* @param Contra<T> $b
* @return T
*/
function testTwoParams(mixed $value, Contra $a, Contra $b): mixed
{
return $value;
}
// Non-variadic with direct Contra
$r1 = testTwoParams(
new \RuntimeException(),
contra(\Throwable::class),
contra(\Exception::class),
);
assertType('RuntimeException', $r1);
// Non-variadic with Inv (extending Contra)
$r2 = testTwoParams(
new \RuntimeException(),
inv(\Throwable::class),
inv(\Exception::class),
);
assertType('RuntimeException', $r2);
// Mixed variance: function with both covariant and contravariant template params
/**
* @template-covariant Out
* @template-contravariant In
*/
interface Func
{
}
/**
* @template Out
* @template In
* @extends Func<Out, In>
*/
interface InvFunc extends Func {}
/**
* @template T
* @param Func<T, T> $fn
* @param T $value
* @return T
*/
function applyFunc(Func $fn, mixed $value): mixed
{
return $value;
}
/**
* @param Func<\Exception, \Throwable> $fn
*/
function testMixedVariance(Func $fn): void
{
$r = applyFunc($fn, new \RuntimeException());
assertType('Exception', $r);
}
/**
* @param InvFunc<\Exception, \Throwable> $fn
*/
function testMixedVarianceWithInv(InvFunc $fn): void
{
$r = applyFunc($fn, new \RuntimeException());
assertType('Exception', $r);
}
// Method on a class (vs function)
class Container
{
/**
* @template T
* @param T $value
* @param Contra<T> ...$contras
* @return T
*/
public function test(mixed $value, Contra ...$contras): mixed
{
return $value;
}
/**
* @template T
* @param T $value
* @param Contra<T> ...$contras
* @return T
*/
public static function testStatic(mixed $value, Contra ...$contras): mixed
{
return $value;
}
}
function testMethod(): void
{
$c = new Container();
// Method with Inv args
$r = $c->test(
new \RuntimeException(),
inv(\Throwable::class),
inv(\Exception::class),
);
assertType('RuntimeException', $r);
// Static method with Inv args
$r2 = Container::testStatic(
new \RuntimeException(),
inv(\Throwable::class),
inv(\Exception::class),
);
assertType('RuntimeException', $r2);
}