-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathstatic-late-binding.php
More file actions
151 lines (132 loc) · 4.56 KB
/
static-late-binding.php
File metadata and controls
151 lines (132 loc) · 4.56 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
<?php
namespace StaticLateBinding;
use function PHPStan\Testing\assertType;
class A
{
public static function retStaticConst(): int
{
return 1;
}
/**
* @return static
*/
public static function retStatic()
{
return new static();
}
/**
* @return static
*/
public function retNonStatic()
{
return new static();
}
/**
* @param-out int $out
*/
public static function outStaticConst(&$out): int
{
$out = 1;
}
}
class B extends A
{
/**
* @return 2
*/
public static function retStaticConst(): int
{
return 2;
}
/**
* @param-out 2 $out
*/
public static function outStaticConst(&$out): int
{
$out = 2;
}
public function foo(): void
{
$clUnioned = mt_rand() === 0
? A::class
: X::class;
assertType('int', A::retStaticConst());
assertType('2', B::retStaticConst());
assertType('2', self::retStaticConst());
assertType('2', static::retStaticConst());
assertType('int', parent::retStaticConst());
assertType('2', $this->retStaticConst());
assertType('bool', X::retStaticConst());
assertType('*ERROR*', $clUnioned->retStaticConst()); // should be bool|int https://github.com/phpstan/phpstan/issues/11687
assertType('int', A::retStaticConst(...)());
assertType('2', B::retStaticConst(...)());
assertType('2', self::retStaticConst(...)());
assertType('2', static::retStaticConst(...)());
assertType('int', parent::retStaticConst(...)());
assertType('2', $this->retStaticConst(...)());
assertType('bool', X::retStaticConst(...)());
assertType('mixed', $clUnioned->retStaticConst(...)()); // should be bool|int https://github.com/phpstan/phpstan/issues/11687
assertType('StaticLateBinding\A', A::retStatic());
assertType('StaticLateBinding\B', B::retStatic());
assertType('static(StaticLateBinding\B)', self::retStatic());
assertType('static(StaticLateBinding\B)', static::retStatic());
assertType('static(StaticLateBinding\B)', parent::retStatic());
assertType('static(StaticLateBinding\B)', $this->retStatic());
assertType('bool', X::retStatic());
assertType('bool|StaticLateBinding\A', $clUnioned::retStatic());
assertType('StaticLateBinding\A', A::retStatic(...)());
assertType('StaticLateBinding\B', B::retStatic(...)());
assertType('static(StaticLateBinding\B)', self::retStatic(...)());
assertType('static(StaticLateBinding\B)', static::retStatic(...)());
assertType('static(StaticLateBinding\A)', parent::retStatic(...)()); // could be static(StaticLateBinding\B)
assertType('static(StaticLateBinding\B)', $this->retStatic(...)());
assertType('bool', X::retStatic(...)());
assertType('mixed', $clUnioned::retStatic(...)()); // should be bool|StaticLateBinding\A https://github.com/phpstan/phpstan/issues/11687
assertType('static(StaticLateBinding\B)', A::retNonStatic());
assertType('static(StaticLateBinding\B)', B::retNonStatic());
assertType('static(StaticLateBinding\B)', self::retNonStatic());
assertType('static(StaticLateBinding\B)', static::retNonStatic());
assertType('static(StaticLateBinding\B)', parent::retNonStatic());
assertType('static(StaticLateBinding\B)', $this->retNonStatic());
assertType('bool', X::retNonStatic());
assertType('*ERROR*', $clUnioned->retNonStatic()); // should be bool|static(StaticLateBinding\B) https://github.com/phpstan/phpstan/issues/11687
A::outStaticConst($v);
assertType('int', $v);
B::outStaticConst($v);
assertType('2', $v);
self::outStaticConst($v);
assertType('2', $v);
static::outStaticConst($v);
assertType('2', $v);
parent::outStaticConst($v);
assertType('int', $v);
$this->outStaticConst($v);
assertType('2', $v);
X::outStaticConst($v);
assertType('bool', $v);
$clUnioned->outStaticConst($v);
assertType('bool', $v); // should be bool|int
}
}
class X
{
public static function retStaticConst(): bool
{
return false;
}
/**
* @param-out bool $out
*/
public static function outStaticConst(&$out): void
{
$out = false;
}
public static function retStatic(): bool
{
return false;
}
public function retNonStatic(): bool
{
return false;
}
}