Skip to content

Commit

Permalink
Fix infinite recursion when asking isSuperTypeOf() between template u…
Browse files Browse the repository at this point in the history
…nion types
  • Loading branch information
ondrejmirtes committed Mar 7, 2021
1 parent bf0ba16 commit 9f51f8e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
public function isSubTypeOf(Type $type): TrinaryLogic
{
$boundClass = get_class($this->getBound());
if (!$type instanceof $boundClass && ($type instanceof UnionType || $type instanceof IntersectionType)) {
if (
!$type instanceof $boundClass
&& !$type instanceof TemplateType
&& ($type instanceof UnionType || $type instanceof IntersectionType)
) {
return $type->isSuperTypeOf($this);
}

Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Type/TemplateTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,48 @@ public function dataIsSuperTypeOf(): array
TrinaryLogic::createMaybe(),
TrinaryLogic::createMaybe(),
],
[
$templateType('T', new MixedType(true)),
$templateType('U', new UnionType([new IntegerType(), new StringType()])),
TrinaryLogic::createMaybe(),
TrinaryLogic::createMaybe(),
],
[
$templateType('T', new MixedType(true)),
$templateType('U', new BenevolentUnionType([new IntegerType(), new StringType()])),
TrinaryLogic::createMaybe(),
TrinaryLogic::createMaybe(),
],
[
$templateType('T', new ObjectType(\stdClass::class)),
$templateType('U', new BenevolentUnionType([new IntegerType(), new StringType()])),
TrinaryLogic::createNo(),
TrinaryLogic::createNo(),
],
[
$templateType('T', new BenevolentUnionType([new IntegerType(), new StringType()])),
$templateType('T', new BenevolentUnionType([new IntegerType(), new StringType()])),
TrinaryLogic::createYes(),
TrinaryLogic::createYes(),
],
[
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
TrinaryLogic::createYes(),
TrinaryLogic::createYes(),
],
[
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
$templateType('T', new BenevolentUnionType([new IntegerType(), new StringType()])),
TrinaryLogic::createMaybe(),
TrinaryLogic::createMaybe(),
],
[
$templateType('T', new UnionType([new IntegerType(), new StringType()])),
$templateType('T', new IntegerType()),
TrinaryLogic::createMaybe(),
TrinaryLogic::createMaybe(),
],
];
}

Expand Down

0 comments on commit 9f51f8e

Please sign in to comment.