Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 46721c1

Browse filesBrowse files
hhamonnicolas-grekas
authored andcommitted
[Uid] make Uuid::equals() accept any types of argument for more flexibility
1 parent 14c95a9 commit 46721c1
Copy full SHA for 46721c1

2 files changed

+25-2Lines changed: 25 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UuidTest.php
+16Lines 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 = [];
Collapse file

‎src/Symfony/Component/Uid/Uuid.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Uuid.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(string $uuid = null)
4242
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
4343
}
4444

45-
$this->uuid = $uuid;
45+
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
4646
}
4747

4848
public static function v1(): self
@@ -85,8 +85,15 @@ public function isNull(): bool
8585
return uuid_is_null($this->uuid);
8686
}
8787

88-
public function equals(self $other): bool
88+
/**
89+
* Returns whether the argument is of class Uuid and contains the same value as the current instance.
90+
*/
91+
public function equals($other): bool
8992
{
93+
if (!$other instanceof self) {
94+
return false;
95+
}
96+
9097
return 0 === uuid_compare($this->uuid, $other->uuid);
9198
}
9299

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.