8000 feature #57313 [Uid] Make `AbstractUid` implement `Ds\Hashable` if av… · symfony/symfony@63ef48f · GitHub
[go: up one dir, main page]

Skip to content

Commit 63ef48f

Browse files
committed
feature #57313 [Uid] Make AbstractUid implement Ds\Hashable if available (jahudka)
This PR was merged into the 7.2 branch. Discussion ---------- [Uid] Make `AbstractUid` implement `Ds\Hashable` if available | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT This PR makes UUIDs usable with the PHP DS extension's `Map` and `Set` classes. It's a new feature and shouldn't cause any BC breaks. The `Map` and `Set` classes in the DS extension have extended support for objects implementing the `Ds\Hashable` interface (as keys in `Map` and values in `Set`). By default, values are made unique by testing for strict equality, which will consider two `Uuid` instances to be distinct even though they represent the same UUID; with this PR merged, `Uuid` instances would behave as expected with these classes. The current implementation of `AbstractUid` already includes the `equals()` method defined in the interface; the PR adds the missing `hash()` method and a shim for the interface in case the extension isn't loaded. I've also added a test for the new `hash()` method, checking both the behaviour of the method itself and the correctness of that behaviour when used with the `Ds\Set` class (that part of the test only runs if the `ds` extension is loaded). I've read through the contributing docs, but it's still my first time contributing to Symfony, I'm sure I've made mistakes - please don't be too harsh on me! 🙏 ❤️ Commits ------- 1a0d9b0 [Uid] Make `AbstractUid` implement `Ds\Hashable` if available
2 parents ee63ef5 + 1a0d9b0 commit 63ef48f

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/Symfony/Component/Uid/AbstractUid.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @author Nicolas Grekas <p@tchwork.com>
1616
*/
17-
abstract class AbstractUid implements \JsonSerializable, \Stringable
17+
abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface
1818
{
1919
/**
2020
* The identifier in its canonic representation.
@@ -159,6 +159,11 @@ public function equals(mixed $other): bool
159159
return $this->uid === $other->uid;
160160
}
161161

162+
public function hash(): string
163+
{
164+
return $this->uid;
165+
}
166+
162167
public function compare(self $other): int
163168
{
164169
return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Make `AbstractUid` implement `Ds\Hashable` if available
8+
49
7.1
510
---
611

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Uid;
13+
14+
if (interface_exists(\Ds\Hashable::class)) {
15+
/**
16+
* @internal
17+
*/
18+
interface HashableInterface extends \Ds\Hashable
19+
{
20+
public function hash(): string;
21+
}
22+
} else {
23+
/**
24+
* @internal
25+
*/
26+
interface HashableInterface
27+
{
28+
public function equals(mixed $other): bool;
29+
30+
public function hash(): string;
31+
}
32+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,29 @@ public static function provideInvalidEqualType(): iterable
248248
yield [new \stdClass()];
249249
}
250250

251+
public function testHashable()
252+
{
253+
$uuid1 = new UuidV4(self::A_UUID_V4);
254+
$uuid2 = new UuidV4(self::A_UUID_V4);
255+
256+
$this->assertSame($uuid1->hash(), $uuid2->hash());
257+
}
258+
259+
/** @requires extension ds */
260+
public function testDsCompatibility()
261+
{
262+
$uuid1 = new UuidV4(self::A_UUID_V4);
263+
$uuid2 = new UuidV4(self::A_UUID_V4);
264+
265+
$set = new \Ds\Set();
266+
$set->add($uuid1);
267+
$set->add($uuid2);
268+
269+
$this->assertTrue($set->contains($uuid1));
270+
$this->assertTrue($set->contains($uuid2));
271+
$this->assertCount(1, $set);
272+
}
273+
251274
public function testCompare()
252275
{
253276
$uuids = [];

0 commit comments

Comments
 (0)
0