Skip to content

Commit cc73b1e

Browse files
minor #36058 [Uid] make Uuid::equals method accept any types of argument for more flexibility (hhamon)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Uid] make `Uuid::equals` method accept any types of argument for more flexibility | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | ~ | License | MIT | Doc PR | ~ I suggest to weaken the `Uuid:equals` method argument type to accept any types of value to compare against. This makes one able to compare the `Uuid` instance with any values. Commits ------- 46721c1 [Uid] make `Uuid::equals()` accept any types of argument for more flexibility
2 parents 08bb79b + 46721c1 commit cc73b1e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Symfony/Component/Uid/Tests/UuidTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ public function testEquals()
9494
$this->assertFalse($uuid1->equals($uuid2));
9595
}
9696

97+
/**
98+
* @dataProvider provideInvalidEqualType
99+
*/
100+
public function testEqualsAgainstOtherType($other)
101+
{
102+
$this->assertFalse((new Uuid(self::A_UUID_V4))->equals($other));
103+
}
104+
105+
public function provideInvalidEqualType()
106+
{
107+
yield [null];
108+
yield [self::A_UUID_V1];
109+
yield [self::A_UUID_V4];
110+
yield [new \stdClass()];
111+
}
112+
97113
public function testCompare()
98114
{
99115
$uuids = [];

src/Symfony/Component/Uid/Uuid.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(string $uuid = null)
4848
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
4949
}
5050

51-
$this->uuid = $uuid;
51+
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
5252
}
5353

5454
public static function v1(): self
@@ -91,8 +91,15 @@ public function isNull(): bool
9191
return uuid_is_null($this->uuid);
9292
}
9393

94-
public function equals(self $other): bool
94+
/**
95+
* Returns whether the argument is of class Uuid and contains the same value as the current instance.
96+
*/
97+
public function equals($other): bool
9598
{
99+
if (!$other instanceof self) {
100+
return false;
101+
}
102+
96103
return 0 === uuid_compare($this->uuid, $other->uuid);
97104
}
98105

0 commit comments

Comments
 (0)