Skip to content

Commit 9f51f8e

Browse files
committed
Fix infinite recursion when asking isSuperTypeOf() between template union types
1 parent bf0ba16 commit 9f51f8e

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Type/Generic/TemplateTypeTrait.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
118118
public function isSubTypeOf(Type $type): TrinaryLogic
119119
{
120120
$boundClass = get_class($this->getBound());
121-
if (!$type instanceof $boundClass && ($type instanceof UnionType || $type instanceof IntersectionType)) {
121+
if (
122+
!$type instanceof $boundClass
123+
&& !$type instanceof TemplateType
124+
&& ($type instanceof UnionType || $type instanceof IntersectionType)
125+
) {
122126
return $type->isSuperTypeOf($this);
123127
}
124128

tests/PHPStan/Type/TemplateTypeTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,48 @@ public function dataIsSuperTypeOf(): array
206206
TrinaryLogic::createMaybe(),
207207
TrinaryLogic::createMaybe(),
208208
],
209+
[
210+
$templateType('T', new MixedType(true)),
211+
$templateType('U', new UnionType([new IntegerType(), new StringType()])),
212+
TrinaryLogic::createMaybe(),
213+
TrinaryLogic::createMaybe(),
214+
],
215+
[
216+
$templateType('T', new MixedType(true)),
217+
$templateType('U', new BenevolentUnionType([new IntegerType(), new StringType()])),
218+
TrinaryLogic::createMaybe(),
219+
TrinaryLogic::createMaybe(),
220+
],
221+
[
222+
$templateType('T', new ObjectType(\stdClass::class)),
223+
$templateType('U', new BenevolentUnionType([new IntegerType(), new StringType()])),
224+
TrinaryLogic::createNo(),
225+
TrinaryLogic::createNo(),
226+
],
227+
[
228+
$templateType('T', new BenevolentUnionType([new IntegerType(), new StringType()])),
229+
$templateType('T', new BenevolentUnionType([new IntegerType(), new StringType()])),
230+
TrinaryLogic::createYes(),
231+
TrinaryLogic::createYes(),
232+
],
233+
[
234+
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
235+
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
236+
TrinaryLogic::createYes(),
237+
TrinaryLogic::createYes(),
238+
],
239+
[
240+
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
241+
$templateType('T', new BenevolentUnionType([new IntegerType(), new StringType()])),
242+
TrinaryLogic::createMaybe(),
243+
TrinaryLogic::createMaybe(),
244+
],
245+
[
246+
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
247+
$templateType('T', new IntegerType()),
248+
TrinaryLogic::createMaybe(),
249+
TrinaryLogic::createMaybe(),
250+
],
209251
];
210252
}
211253

0 commit comments

Comments
 (0)