Skip to content

Commit

Permalink
Fix GenericClassStringType being equal when it's not
Browse files Browse the repository at this point in the history
  • Loading branch information
jlherren authored and ondrejmirtes committed Oct 14, 2020
1 parent a093f92 commit 4a74d7c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ public function getReferencedTemplateTypes(TemplateTypeVariance $positionVarianc
return $this->type->getReferencedTemplateTypes($variance);
}

public function equals(Type $type): bool
{
if (!$type instanceof self) {
return false;
}

if (!parent::equals($type)) {
return false;
}

if (!$this->type->equals($type->type)) {
return false;
}

return true;
}

/**
* @param mixed[] $properties
* @return Type
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Type/Generic/GenericClassStringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,54 @@ public function testAccepts(
);
}

public function dataEquals(): array
{
return [
[
new GenericClassStringType(new ObjectType(\Exception::class)),
new GenericClassStringType(new ObjectType(\Exception::class)),
true,
],
[
new GenericClassStringType(new ObjectType(\Exception::class)),
new GenericClassStringType(new ObjectType(\stdClass::class)),
false,
],
[
new GenericClassStringType(new StaticType(\Exception::class)),
new GenericClassStringType(new StaticType(\Exception::class)),
true,
],
[
new GenericClassStringType(new StaticType(\Exception::class)),
new GenericClassStringType(new StaticType(\stdClass::class)),
false,
],
];
}

/**
* @dataProvider dataEquals
*/
public function testEquals(GenericClassStringType $type, Type $otherType, bool $expected): void
{
$verbosityLevel = VerbosityLevel::precise();
$typeDescription = $type->describe($verbosityLevel);
$otherTypeDescription = $otherType->describe($verbosityLevel);

$actual = $type->equals($otherType);
$this->assertSame(
$expected,
$actual,
sprintf('%s -> equals(%s)', $typeDescription, $otherTypeDescription)
);

$actual = $otherType->equals($type);
$this->assertSame(
$expected,
$actual,
sprintf('%s -> equals(%s)', $otherTypeDescription, $typeDescription)
);
}

}

0 comments on commit 4a74d7c

Please sign in to comment.