Skip to content

Commit

Permalink
minor #36058 [Uid] make Uuid::equals method accept any types of arg…
Browse files Browse the repository at this point in the history
…ument 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
  • Loading branch information
nicolas-grekas committed Mar 13, 2020
2 parents 08bb79b + 46721c1 commit cc73b1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public function testEquals()
$this->assertFalse($uuid1->equals($uuid2));
}

/**
* @dataProvider provideInvalidEqualType
*/
public function testEqualsAgainstOtherType($other)
{
$this->assertFalse((new Uuid(self::A_UUID_V4))->equals($other));
}

public function provideInvalidEqualType()
{
yield [null];
yield [self::A_UUID_V1];
yield [self::A_UUID_V4];
yield [new \stdClass()];
}

public function testCompare()
{
$uuids = [];
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(string $uuid = null)
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
}

$this->uuid = $uuid;
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
}

public static function v1(): self
Expand Down Expand Up @@ -91,8 +91,15 @@ public function isNull(): bool
return uuid_is_null($this->uuid);
}

public function equals(self $other): bool
/**
* Returns whether the argument is of class Uuid and contains the same value as the current instance.
*/
public function equals($other): bool
{
if (!$other instanceof self) {
return false;
}

return 0 === uuid_compare($this->uuid, $other->uuid);
}

Expand Down

0 comments on commit cc73b1e

Please sign in to comment.