Skip to content

Commit 4ba83e8

Browse files
hrachondrejmirtes
authored andcommitted
template type: correctly resolve isSuperTypeOf
Uncertainty of instanceof $object is already handled in MutatingScope (fixed by phpstan/phpstan-src#211).
1 parent 7738778 commit 4ba83e8

13 files changed

Lines changed: 124 additions & 95 deletions

src/Type/Generic/TemplateMixedType.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public function isSubTypeOf(Type $type): TrinaryLogic
106106
}
107107

108108
if (!$type instanceof TemplateType) {
109-
return $type->isSuperTypeOf($this->getBound())
110-
->and(TrinaryLogic::createMaybe());
109+
return $type->isSuperTypeOf($this->getBound());
111110
}
112111

113112
if ($this->equals($type)) {

src/Type/Generic/TemplateObjectType.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ public function isSubTypeOf(Type $type): TrinaryLogic
110110
}
111111

112112
if (!$type instanceof TemplateType) {
113-
if ($this->getBound()->equals($type)) {
114-
return TrinaryLogic::createYes();
115-
}
116-
117-
return $type->isSuperTypeOf($this->getBound())
118-
->and(TrinaryLogic::createMaybe());
113+
return $type->isSuperTypeOf($this->getBound());
119114
}
120115

121116
if ($this->equals($type)) {

src/Type/Generic/TemplateObjectWithoutClassType.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,8 @@ public function isSubTypeOf(Type $type): TrinaryLogic
135135
return $type->isSuperTypeOf($this);
136136
}
137137

138-
if ($type instanceof ObjectWithoutClassType && !$type instanceof self) {
139-
return TrinaryLogic::createYes();
140-
}
141-
142138
if (!$type instanceof TemplateType) {
143-
return $type->isSuperTypeOf($this->getBound())
144-
->and(TrinaryLogic::createMaybe());
139+
return $type->isSuperTypeOf($this->getBound());
145140
}
146141

147142
if ($this->equals($type)) {

tests/PHPStan/Levels/data/acceptTypes-5.json

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,142 +61,142 @@
6161
},
6262
{
6363
"message": "Parameter #1 $var of function count expects array|Countable, string given.",
64-
"line": 166,
64+
"line": 170,
6565
"ignorable": true
6666
},
6767
{
6868
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface, mixed, mixed): Levels\\AcceptTypes\\FooInterface given.",
69-
"line": 217,
69+
"line": 226,
7070
"ignorable": true
7171
},
7272
{
7373
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface, mixed, mixed): Levels\\AcceptTypes\\FooInterface given.",
74-
"line": 218,
74+
"line": 227,
7575
"ignorable": true
7676
},
7777
{
7878
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooImpl): Levels\\AcceptTypes\\FooInterface given.",
79-
"line": 229,
79+
"line": 238,
8080
"ignorable": true
8181
},
8282
{
8383
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooImpl): Levels\\AcceptTypes\\FooInterface given.",
84-
"line": 230,
84+
"line": 239,
8585
"ignorable": true
8686
},
8787
{
8888
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
89-
"line": 241,
89+
"line": 250,
9090
"ignorable": true
9191
},
9292
{
9393
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
94-
"line": 242,
94+
"line": 251,
9595
"ignorable": true
9696
},
9797
{
9898
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface, mixed, mixed): Levels\\AcceptTypes\\FooInterface given.",
99-
"line": 274,
99+
"line": 283,
100100
"ignorable": true
101101
},
102102
{
103103
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface, mixed, mixed): Levels\\AcceptTypes\\FooInterface given.",
104-
"line": 275,
104+
"line": 284,
105105
"ignorable": true
106106
},
107107
{
108108
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooImpl): Levels\\AcceptTypes\\FooInterface given.",
109-
"line": 292,
109+
"line": 301,
110110
"ignorable": true
111111
},
112112
{
113113
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooImpl): Levels\\AcceptTypes\\FooInterface given.",
114-
"line": 293,
114+
"line": 302,
115115
"ignorable": true
116116
},
117117
{
118118
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
119-
"line": 310,
119+
"line": 319,
120120
"ignorable": true
121121
},
122122
{
123123
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
124-
"line": 311,
124+
"line": 320,
125125
"ignorable": true
126126
},
127127
{
128128
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, float given.",
129-
"line": 403,
129+
"line": 412,
130130
"ignorable": true
131131
},
132132
{
133133
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, float|string given.",
134-
"line": 404,
134+
"line": 413,
135135
"ignorable": true
136136
},
137137
{
138138
"message": "Parameter #1 $a of method Levels\\AcceptTypes\\Baz::doLorem() expects int|resource, float|string given.",
139-
"line": 416,
139+
"line": 425,
140140
"ignorable": true
141141
},
142142
{
143143
"message": "Parameter #1 $a of method Levels\\AcceptTypes\\Baz::doLorem() expects int|resource, float|string|null given.",
144-
"line": 417,
144+
"line": 426,
145145
"ignorable": true
146146
},
147147
{
148148
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, null given.",
149-
"line": 422,
149+
"line": 431,
150150
"ignorable": true
151151
},
152152
{
153153
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBarArray() expects array<int>, array<float> given.",
154-
"line": 484,
154+
"line": 493,
155155
"ignorable": true
156156
},
157157
{
158158
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBarArray() expects array<int>, array<float|string> given.",
159-
"line": 485,
159+
"line": 494,
160160
"ignorable": true
161161
},
162162
{
163163
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array<int> given.",
164-
"line": 570,
164+
"line": 579,
165165
"ignorable": true
166166
},
167167
{
168168
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array() given.",
169-
"line": 571,
169+
"line": 580,
170170
"ignorable": true
171171
},
172172
{
173173
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array('foo' => 1) given.",
174-
"line": 573,
174+
"line": 582,
175175
"ignorable": true
176176
},
177177
{
178178
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array('foo' => 'nonexistent') given.",
179-
"line": 575,
179+
"line": 584,
180180
"ignorable": true
181181
},
182182
{
183183
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array('bar' => 'date') given.",
184-
"line": 576,
184+
"line": 585,
185185
"ignorable": true
186186
},
187187
{
188188
"message": "Parameter #1 $min (0) of function random_int expects lower number than parameter #2 $max (-1).",
189-
"line": 662,
189+
"line": 671,
190190
"ignorable": true
191191
},
192192
{
193193
"message": "Parameter #1 $min (int<11, max>) of function random_int expects lower number than parameter #2 $max (10).",
194-
"line": 669,
194+
"line": 678,
195195
"ignorable": true
196196
},
197197
{
198198
"message": "Parameter #1 $min (340) of function random_int expects lower number than parameter #2 $max (int<min, 339>).",
199-
"line": 676,
199+
"line": 685,
200200
"ignorable": true
201201
}
202202
]

tests/PHPStan/Levels/data/acceptTypes-6.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,97 +56,97 @@
5656
},
5757
{
5858
"message": "Method Levels\\AcceptTypes\\Foo::iterableCountable() has no return typehint specified.",
59-
"line": 158,
59+
"line": 160,
6060
"ignorable": true
6161
},
6262
{
6363
"message": "Method Levels\\AcceptTypes\\Foo::benevolentUnionNotReported() has no return typehint specified.",
64-
"line": 172,
64+
"line": 176,
6565
"ignorable": true
6666
},
6767
{
6868
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doFoo() has no return typehint specified.",
69-
"line": 199,
69+
"line": 208,
7070
"ignorable": true
7171
},
7272
{
7373
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doFooUnionClosures() has no return typehint specified.",
74-
"line": 245,
74+
"line": 254,
7575
"ignorable": true
7676
},
7777
{
7878
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doBar() has no return typehint specified.",
79-
"line": 323,
79+
"line": 332,
8080
"ignorable": true
8181
},
8282
{
8383
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doBaz() has no return typehint specified.",
84-
"line": 333,
84+
"line": 342,
8585
"ignorable": true
8686
},
8787
{
8888
"message": "Method Levels\\AcceptTypes\\Baz::doFoo() has no return typehint specified.",
89-
"line": 400,
89+
"line": 409,
9090
"ignorable": true
9191
},
9292
{
9393
"message": "Method Levels\\AcceptTypes\\Baz::doBar() has no return typehint specified.",
94-
"line": 409,
94+
"line": 418,
9595
"ignorable": true
9696
},
9797
{
9898
"message": "Method Levels\\AcceptTypes\\Baz::doBaz() has no return typehint specified.",
99-
"line": 414,
99+
"line": 423,
100100
"ignorable": true
101101
},
102102
{
103103
"message": "Method Levels\\AcceptTypes\\Baz::doLorem() has no return typehint specified.",
104-
"line": 428,
104+
"line": 437,
105105
"ignorable": true
106106
},
107107
{
108108
"message": "Method Levels\\AcceptTypes\\Baz::doIpsum() has no return typehint specified.",
109-
"line": 436,
109+
"line": 445,
110110
"ignorable": true
111111
},
112112
{
113113
"message": "Method Levels\\AcceptTypes\\Baz::doFooArray() has no return typehint specified.",
114-
"line": 481,
114+
"line": 490,
115115
"ignorable": true
116116
},
117117
{
118118
"message": "Method Levels\\AcceptTypes\\Baz::doBarArray() has no return typehint specified.",
119-
"line": 493,
119+
"line": 502,
120120
"ignorable": true
121121
},
122122
{
123123
"message": "Method Levels\\AcceptTypes\\Baz::testUnions() has no return typehint specified.",
124-
"line": 515,
124+
"line": 524,
125125
"ignorable": true
126126
},
127127
{
128128
"message": "Method Levels\\AcceptTypes\\Baz::testUnions2() has no return typehint specified.",
129-
"line": 526,
129+
"line": 535,
130130
"ignorable": true
131131
},
132132
{
133133
"message": "Method Levels\\AcceptTypes\\Baz::requireArray() has no return typehint specified.",
134-
"line": 540,
134+
"line": 549,
135135
"ignorable": true
136136
},
137137
{
138138
"message": "Method Levels\\AcceptTypes\\Baz::requireFoo() has no return typehint specified.",
139-
"line": 545,
139+
"line": 554,
140140
"ignorable": true
141141
},
142142
{
143143
"message": "Method Levels\\AcceptTypes\\ArrayShapes::doFoo() has no return typehint specified.",
144-
"line": 561,
144+
"line": 570,
145145
"ignorable": true
146146
},
147147
{
148148
"message": "Method Levels\\AcceptTypes\\ArrayShapes::doBar() has no return typehint specified.",
149-
"line": 594,
149+
"line": 603,
150150
"ignorable": true
151151
}
152152
]

0 commit comments

Comments
 (0)