Skip to content

Commit 5593300

Browse files
committed
Unified Type::equals()
1 parent 7747262 commit 5593300

5 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/Type/ArrayType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
102102
public function equals(Type $type): bool
103103
{
104104
return $type instanceof self
105+
&& !$type instanceof ConstantArrayType
105106
&& $this->getItemType()->equals($type->getItemType())
106107
&& $this->keyType->equals($type->keyType);
107108
}

src/Type/Generic/GenericObjectType.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ public function describe(VerbosityLevel $level): string
4949
);
5050
}
5151

52+
public function equals(Type $type): bool
53+
{
54+
if (!$type instanceof self) {
55+
return false;
56+
}
57+
58+
if (!parent::equals($type)) {
59+
return false;
60+
}
61+
62+
if (count($this->types) !== count($type->types)) {
63+
return false;
64+
}
65+
66+
foreach ($this->types as $i => $genericType) {
67+
$otherGenericType = $type->types[$i];
68+
if (!$genericType->equals($otherGenericType)) {
69+
return false;
70+
}
71+
}
72+
73+
return true;
74+
}
75+
5276
/**
5377
* @return string[]
5478
*/

src/Type/Generic/TemplateMixedType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public function equals(Type $type): bool
7575
{
7676
return $type instanceof self
7777
&& $type->scope->equals($this->scope)
78-
&& $type->name === $this->name;
78+
&& $type->name === $this->name
79+
&& parent::equals($type);
7980
}
8081

8182
public function getBound(): Type

src/Type/Generic/TemplateObjectWithoutClassType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public function equals(Type $type): bool
149149
{
150150
return $type instanceof self
151151
&& $type->scope->equals($this->scope)
152-
&& $type->name === $this->name;
152+
&& $type->name === $this->name
153+
&& parent::equals($type);
153154
}
154155

155156
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic

src/Type/JustNullableTypeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
4343

4444
public function equals(Type $type): bool
4545
{
46-
return $type instanceof self;
46+
return get_class($type) === self::class;
4747
}
4848

4949
public function traverse(callable $cb): Type

0 commit comments

Comments
 (0)