Skip to content

[Uid] Make AbstractUid implement Ds\Hashable if available #57313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractUid implements \JsonSerializable, \Stringable
abstract class AbstractUid implements \JsonSerializable, \Stringable, HashableInterface
{
/**
* The identifier in its canonic representation.
Expand Down Expand Up @@ -150,7 +150,7 @@
/**
* Returns whether the argument is an AbstractUid and contains the same value as the current instance.
*/
public function equals(mixed $other): bool

Check failure on line 153 in src/Symfony/Component/Uid/AbstractUid.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Uid/AbstractUid.php:153:34: ParamNameMismatch: Argument 1 of Symfony\Component\Uid\AbstractUid::equals has wrong name $other, expecting $object as defined by Ds\Hashable::equals (see https://psalm.dev/230)

Check failure on line 153 in src/Symfony/Component/Uid/AbstractUid.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Uid/AbstractUid.php:153:34: ParamNameMismatch: Argument 1 of Symfony\Component\Uid\AbstractUid::equals has wrong name $other, expecting $object as defined by Ds\Hashable::equals (see https://psalm.dev/230)
{
if (!$other instanceof self) {
return false;
Expand All @@ -159,6 +159,11 @@
return $this->uid === $other->uid;
}

public function hash(): string
{
return $this->uid;
}

public function compare(self $other): int
{
return (\strlen($this->uid) - \strlen($other->uid)) ?: ($this->uid <=> $other->uid);
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Make `AbstractUid` implement `Ds\Hashable` if available

7.1
---

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Uid/HashableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Uid;

if (interface_exists(\Ds\Hashable::class)) {
/**
* @internal
*/
interface HashableInterface extends \Ds\Hashable

Check failure on line 18 in src/Symfony/Component/Uid/HashableInterface.php

View workflow job for this annotation

GitHub Actions / Psalm

UnrecognizedStatement

src/Symfony/Component/Uid/HashableInterface.php:18:5: UnrecognizedStatement: Psalm does not understand _HumbugBox0c61de993aae\PhpParser\Node\Stmt\Interface_ (see https://psalm.dev/049)

Check failure on line 18 in src/Symfony/Component/Uid/HashableInterface.php

View workflow job for this annotation

GitHub Actions / Psalm

UnrecognizedStatement

src/Symfony/Component/Uid/HashableInterface.php:18:5: UnrecognizedStatement: Psalm does not understand _HumbugBox0c61de993aae\PhpParser\Node\Stmt\Interface_ (see https://psalm.dev/049)
{
public function hash(): string;
}
} else {
/**
* @internal
*/
interface HashableInterface
{
public function equals(mixed $other): bool;

public function hash(): string;
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,29 @@ public static function provideInvalidEqualType(): iterable
yield [new \stdClass()];
}

public function testHashable()
{
$uuid1 = new UuidV4(self::A_UUID_V4);
$uuid2 = new UuidV4(self::A_UUID_V4);

$this->assertSame($uuid1->hash(), $uuid2->hash());
}

/** @requires extension ds */
public function testDsCompatibility()
{
$uuid1 = new UuidV4(self::A_UUID_V4);
$uuid2 = new UuidV4(self::A_UUID_V4);

$set = new \Ds\Set();
$set->add($uuid1);
$set->add($uuid2);

$this->assertTrue($set->contains($uuid1));
$this->assertTrue($set->contains($uuid2));
$this->assertCount(1, $set);
}

public function testCompare()
{
$uuids = [];
Expand Down
Loading