From ffecbea590c02eefe6206396bd61497007e0f2a8 Mon Sep 17 00:00:00 2001 From: Yevhen Sidelnyk Date: Sat, 5 Apr 2025 14:14:36 +0300 Subject: [PATCH 1/3] feat: use more concrete uid exception classes --- src/Symfony/Component/Uid/CHANGELOG.md | 6 ++++ .../Component/Uid/InvalidUlidException.php | 26 +++++++++++++++ .../Component/Uid/InvalidUuidException.php | 32 +++++++++++++++++++ src/Symfony/Component/Uid/Tests/UlidTest.php | 3 +- src/Symfony/Component/Uid/Ulid.php | 2 +- src/Symfony/Component/Uid/Uuid.php | 4 +-- 6 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/Uid/InvalidUlidException.php create mode 100644 src/Symfony/Component/Uid/InvalidUuidException.php diff --git a/src/Symfony/Component/Uid/CHANGELOG.md b/src/Symfony/Component/Uid/CHANGELOG.md index f53899b6061c2..3f879c2182fb8 100644 --- a/src/Symfony/Component/Uid/CHANGELOG.md +++ b/src/Symfony/Component/Uid/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +7.3 +--- + + * Make `Uuid` throw more concrete `InvalidUuidException` + * Make `Ulid` throw more concrete `InvalidUlidException` + 7.2 --- diff --git a/src/Symfony/Component/Uid/InvalidUlidException.php b/src/Symfony/Component/Uid/InvalidUlidException.php new file mode 100644 index 0000000000000..ca7dfb0832f60 --- /dev/null +++ b/src/Symfony/Component/Uid/InvalidUlidException.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Uid; + +final class InvalidUlidException extends \InvalidArgumentException +{ + public function __construct( + private readonly string $value, + ) { + parent::__construct(\sprintf('Invalid ULID: "%s".', $this->value)); + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/src/Symfony/Component/Uid/InvalidUuidException.php b/src/Symfony/Component/Uid/InvalidUuidException.php new file mode 100644 index 0000000000000..705cd8d70e4d6 --- /dev/null +++ b/src/Symfony/Component/Uid/InvalidUuidException.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Uid; + +final class InvalidUuidException extends \InvalidArgumentException +{ + public function __construct( + private readonly int $type, + private readonly string $value, + ) { + parent::__construct(\sprintf('Invalid UUID%s: "%s".', $this->type ? 'v'.$this->type : '', $this->value)); + } + + public function getType(): int + { + return $this->type; + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/src/Symfony/Component/Uid/Tests/UlidTest.php b/src/Symfony/Component/Uid/Tests/UlidTest.php index 338b699159a77..e5b521db4e3ff 100644 --- a/src/Symfony/Component/Uid/Tests/UlidTest.php +++ b/src/Symfony/Component/Uid/Tests/UlidTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Uid\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\Uid\InvalidUlidException; use Symfony\Component\Uid\MaxUlid; use Symfony\Component\Uid\NilUlid; use Symfony\Component\Uid\Tests\Fixtures\CustomUlid; @@ -41,7 +42,7 @@ public function testGenerate() public function testWithInvalidUlid() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidUlidException::class); $this->expectExceptionMessage('Invalid ULID: "this is not a ulid".'); new Ulid('this is not a ulid'); diff --git a/src/Symfony/Component/Uid/Ulid.php b/src/Symfony/Component/Uid/Ulid.php index 1240b019e28e2..3e208e8f2d65b 100644 --- a/src/Symfony/Component/Uid/Ulid.php +++ b/src/Symfony/Component/Uid/Ulid.php @@ -36,7 +36,7 @@ public function __construct(?string $ulid = null) $this->uid = $ulid; } else { if (!self::isValid($ulid)) { - throw new \InvalidArgumentException(\sprintf('Invalid ULID: "%s".', $ulid)); + throw new InvalidUlidException($ulid); } $this->uid = strtoupper($ulid); diff --git a/src/Symfony/Component/Uid/Uuid.php b/src/Symfony/Component/Uid/Uuid.php index c956156a3d580..8604ff3e38163 100644 --- a/src/Symfony/Component/Uid/Uuid.php +++ b/src/Symfony/Component/Uid/Uuid.php @@ -39,13 +39,13 @@ public function __construct(string $uuid, bool $checkVariant = false) $type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false; if (false === $type || (static::TYPE ?: $type) !== $type) { - throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid)); + throw new InvalidUuidException(static::TYPE, $uuid); } $this->uid = strtolower($uuid); if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) { - throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid)); + throw new InvalidUuidException(static::TYPE, $uuid); } } From 3802394175d0ac9ba1eee171192956f92bfd89fa Mon Sep 17 00:00:00 2001 From: Yevhen Sidelnyk <41589422+rela589n@users.noreply.github.com> Date: Mon, 7 Apr 2025 09:07:45 +0300 Subject: [PATCH 2/3] Apply suggestions from code review chore: tweak CHANGELOG Co-authored-by: Fabien Potencier --- src/Symfony/Component/Uid/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Uid/CHANGELOG.md b/src/Symfony/Component/Uid/CHANGELOG.md index 3f879c2182fb8..91fc15701fe4a 100644 --- a/src/Symfony/Component/Uid/CHANGELOG.md +++ b/src/Symfony/Component/Uid/CHANGELOG.md @@ -4,8 +4,8 @@ CHANGELOG 7.3 --- - * Make `Uuid` throw more concrete `InvalidUuidException` - * Make `Ulid` throw more concrete `InvalidUlidException` + * Add `Symfony\Component\Uid\InvalidUuidException` + * Add `Symfony\Component\Uid\InvalidUlidException` 7.2 --- From d163d4fb9df590c5de5326e92b87c3eee37d2959 Mon Sep 17 00:00:00 2001 From: Yevhen Sidelnyk Date: Thu, 10 Apr 2025 17:53:07 +0300 Subject: [PATCH 3/3] feat: add InvalidUidException --- .../Component/Uid/InvalidUidException.php | 27 +++++++++++++++++++ .../Component/Uid/InvalidUlidException.php | 12 +++------ .../Component/Uid/InvalidUuidException.php | 18 +++---------- 3 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 src/Symfony/Component/Uid/InvalidUidException.php diff --git a/src/Symfony/Component/Uid/InvalidUidException.php b/src/Symfony/Component/Uid/InvalidUidException.php new file mode 100644 index 0000000000000..9b1a02be15ef9 --- /dev/null +++ b/src/Symfony/Component/Uid/InvalidUidException.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Uid; + +abstract class InvalidUidException extends \InvalidArgumentException +{ + public function __construct( + private readonly string $value, + string $message, + ) { + parent::__construct($message); + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/src/Symfony/Component/Uid/InvalidUlidException.php b/src/Symfony/Component/Uid/InvalidUlidException.php index ca7dfb0832f60..5512c5c75d4d3 100644 --- a/src/Symfony/Component/Uid/InvalidUlidException.php +++ b/src/Symfony/Component/Uid/InvalidUlidException.php @@ -11,16 +11,10 @@ namespace Symfony\Component\Uid; -final class InvalidUlidException extends \InvalidArgumentException +final class InvalidUlidException extends InvalidUidException { - public function __construct( - private readonly string $value, - ) { - parent::__construct(\sprintf('Invalid ULID: "%s".', $this->value)); - } - - public function getValue(): string + public function __construct(string $value) { - return $this->value; + parent::__construct($value, \sprintf('Invalid ULID: "%s".', $value)); } } diff --git a/src/Symfony/Component/Uid/InvalidUuidException.php b/src/Symfony/Component/Uid/InvalidUuidException.php index 705cd8d70e4d6..316245ed280e3 100644 --- a/src/Symfony/Component/Uid/InvalidUuidException.php +++ b/src/Symfony/Component/Uid/InvalidUuidException.php @@ -11,22 +11,10 @@ namespace Symfony\Component\Uid; -final class InvalidUuidException extends \InvalidArgumentException +final class InvalidUuidException extends InvalidUidException { - public function __construct( - private readonly int $type, - private readonly string $value, - ) { - parent::__construct(\sprintf('Invalid UUID%s: "%s".', $this->type ? 'v'.$this->type : '', $this->value)); - } - - public function getType(): int - { - return $this->type; - } - - public function getValue(): string + public function __construct(int $type, string $value) { - return $this->value; + parent::__construct($value, \sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value)); } }