Skip to content

Navigation Menu

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 abadae2

Browse filesBrowse files
rela589nnicolas-grekas
authored andcommitted
[Uid] Add component-specific exception classes
1 parent 1088f53 commit abadae2
Copy full SHA for abadae2

17 files changed

+134
-33
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/AbstractUid.php
+11-9Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Uid;
1313

14+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @author Nicolas Grekas <p@tchwork.com>
1618
*/
@@ -29,41 +31,41 @@ abstract public static function isValid(string $uid): bool;
2931
/**
3032
* Creates an AbstractUid from an identifier represented in any of the supported formats.
3133
*
32-
* @throws \InvalidArgumentException When the passed value is not valid
34+
* @throws InvalidArgumentException When the passed value is not valid
3335
*/
3436
abstract public static function fromString(string $uid): static;
3537

3638
/**
37-
* @throws \InvalidArgumentException When the passed value is not valid
39+
* @throws InvalidArgumentException When the passed value is not valid
3840
*/
3941
public static function fromBinary(string $uid): static
4042
{
4143
if (16 !== \strlen($uid)) {
42-
throw new \InvalidArgumentException('Invalid binary uid provided.');
44+
throw new InvalidArgumentException('Invalid binary uid provided.');
4345
}
4446

4547
return static::fromString($uid);
4648
}
4749

4850
/**
49-
* @throws \InvalidArgumentException When the passed value is not valid
51+
* @throws InvalidArgumentException When the passed value is not valid
5052
*/
5153
public static function fromBase58(string $uid): static
5254
{
5355
if (22 !== \strlen($uid)) {
54-
throw new \InvalidArgumentException('Invalid base-58 uid provided.');
56+
throw new InvalidArgumentException('Invalid base-58 uid provided.');
5557
}
5658

5759
return static::fromString($uid);
5860
}
5961

6062
/**
61-
* @throws \InvalidArgumentException When the passed value is not valid
63+
* @throws InvalidArgumentException When the passed value is not valid
6264
*/
6365
public static function fromBase32(string $uid): static
6466
{
6567
if (26 !== \strlen($uid)) {
66-
throw new \InvalidArgumentException('Invalid base-32 uid provided.');
68+
throw new InvalidArgumentException('Invalid base-32 uid provided.');
6769
}
6870

6971
return static::fromString($uid);
@@ -72,12 +74,12 @@ public static function fromBase32(string $uid): static
7274
/**
7375
* @param string $uid A valid RFC 9562/4122 uid
7476
*
75-
* @throws \InvalidArgumentException When the passed value is not valid
77+
* @throws InvalidArgumentException When the passed value is not valid
7678
*/
7779
public static function fromRfc4122(string $uid): static
7880
{
7981
if (36 !== \strlen($uid)) {
80-
throw new \InvalidArgumentException('Invalid RFC4122 uid provided.');
82+
throw new InvalidArgumentException('Invalid RFC4122 uid provided.');
8183
}
8284

8385
return static::fromString($uid);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/BinaryUtil.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Uid;
1313

14+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @internal
1618
*
@@ -162,7 +164,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
162164
{
163165
if (\PHP_INT_SIZE >= 8) {
164166
if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) {
165-
throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
167+
throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
166168
}
167169

168170
return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT);
@@ -171,7 +173,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
171173
$time = $time->format('Uu0');
172174
$negative = '-' === $time[0];
173175
if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) {
174-
throw new \InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
176+
throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
175177
}
176178
$time = self::fromBase($time, self::BASE10);
177179
$time = str_pad($time, 8, "\0", \STR_PAD_LEFT);

‎src/Symfony/Component/Uid/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add component-specific exception hierarchy
8+
49
7.2
510
---
611

‎src/Symfony/Component/Uid/Command/GenerateUuidCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Command/GenerateUuidCommand.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\Uid\Exception\LogicException;
2324
use Symfony\Component\Uid\Factory\UuidFactory;
2425
use Symfony\Component\Uid\Uuid;
2526

@@ -146,7 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
146147
$create = function () use ($namespace, $name): Uuid {
147148
try {
148149
$factory = $this->factory->nameBased($namespace);
149-
} catch (\LogicException) {
150+
} catch (LogicException) {
150151
throw new \InvalidArgumentException('Missing namespace: use the "--namespace" option or configure a default namespace in the underlying factory.');
151152
}
152153

+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exception;
13+
14+
class InvalidArgumentException extends \InvalidArgumentException
15+
{
16+
}
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\Exception;
13+
14+
final class InvalidUlidException extends InvalidArgumentException
15+
{
16+
public function __construct(string $value)
17+
{
18+
parent::__construct(\sprintf('Invalid ULID: "%s".', $value));
19+
}
20+
}
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\Exception;
13+
14+
class InvalidUuidException extends InvalidArgumentException
15+
{
16+
public function __construct(
17+
public readonly int $type,
18+
string $value,
19+
) {
20+
parent::__construct(\sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value));
21+
}
22+
}
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Exception;
13+
14+
class LogicException extends \LogicException
15+
{
16+
}

‎src/Symfony/Component/Uid/Factory/UuidFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Factory/UuidFactory.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Uid\Factory;
1313

14+
use Symfony\Component\Uid\Exception\LogicException;
1415
use Symfony\Component\Uid\Uuid;
1516
use Symfony\Component\Uid\UuidV1;
1617
use Symfony\Component\Uid\UuidV4;
@@ -67,12 +68,15 @@ public function timeBased(Uuid|string|null $node = null): TimeBasedUuidFactory
6768
return new TimeBasedUuidFactory($this->timeBasedClass, $node);
6869
}
6970

71+
/**
72+
* @throws LogicException When no namespace is defined
73+
*/
7074
public function nameBased(Uuid|string|null $namespace = null): NameBasedUuidFactory
7175
{
7276
$namespace ??= $this->nameBasedNamespace;
7377

7478
if (null === $namespace) {
75-
throw new \LogicException(\sprintf('A namespace should be defined when using "%s()".', __METHOD__));
79+
throw new LogicException(\sprintf('A namespace should be defined when using "%s()".', __METHOD__));
7680
}
7781

7882
return new NameBasedUuidFactory($this->nameBasedClass, $this->getNamespace($namespace));

‎src/Symfony/Component/Uid/Tests/Factory/UlidFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/Factory/UlidFactoryTest.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Uid\Tests\Factory;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
1516
use Symfony\Component\Uid\Factory\UlidFactory;
1617

1718
final class UlidFactoryTest extends TestCase
@@ -36,7 +37,7 @@ public function testCreate()
3637

3738
public function testCreateWithInvalidTimestamp()
3839
{
39-
$this->expectException(\InvalidArgumentException::class);
40+
$this->expectException(InvalidArgumentException::class);
4041
$this->expectExceptionMessage('The timestamp must be positive.');
4142

4243
(new UlidFactory())->create(new \DateTimeImmutable('@-1000'));

‎src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Uid\Tests\Factory;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
1516
use Symfony\Component\Uid\Factory\UuidFactory;
1617
use Symfony\Component\Uid\NilUuid;
1718
use Symfony\Component\Uid\Uuid;
@@ -81,7 +82,7 @@ public function testCreateTimed()
8182

8283
public function testInvalidCreateTimed()
8384
{
84-
$this->expectException(\InvalidArgumentException::class);
85+
$this->expectException(InvalidArgumentException::class);
8586
$this->expectExceptionMessage('The given UUID date cannot be earlier than 1582-10-15.');
8687

8788
(new UuidFactory())->timeBased()->create(new \DateTimeImmutable('@-12219292800.001000'));

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UlidTest.php
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Uid\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
16+
use Symfony\Component\Uid\Exception\InvalidUlidException;
1517
use Symfony\Component\Uid\MaxUlid;
1618
use Symfony\Component\Uid\NilUlid;
1719
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
@@ -41,7 +43,7 @@ public function testGenerate()
4143

4244
public function testWithInvalidUlid()
4345
{
44-
$this->expectException(\InvalidArgumentException::class);
46+
$this->expectException(InvalidUlidException::class);
4547
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');
4648

4749
new Ulid('this is not a ulid');
@@ -151,7 +153,7 @@ public function testFromBinary()
151153
*/
152154
public function testFromBinaryInvalidFormat(string $ulid)
153155
{
154-
$this->expectException(\InvalidArgumentException::class);
156+
$this->expectException(InvalidArgumentException::class);
155157

156158
Ulid::fromBinary($ulid);
157159
}
@@ -178,7 +180,7 @@ public function testFromBase58()
178180
*/
179181
public function testFromBase58InvalidFormat(string $ulid)
180182
{
181-
$this->expectException(\InvalidArgumentException::class);
183+
$this->expectException(InvalidArgumentException::class);
182184

183185
Ulid::fromBase58($ulid);
184186
}
@@ -205,7 +207,7 @@ public function testFromBase32()
205207
*/
206208
public function testFromBase32InvalidFormat(string $ulid)
207209
{
208-
$this->expectException(\InvalidArgumentException::class);
210+
$this->expectException(InvalidArgumentException::class);
209211

210212
Ulid::fromBase32($ulid);
211213
}
@@ -232,7 +234,7 @@ public function testFromRfc4122()
232234
*/
233235
public function testFromRfc4122InvalidFormat(string $ulid)
234236
{
235-
$this->expectException(\InvalidArgumentException::class);
237+
$this->expectException(InvalidArgumentException::class);
236238

237239
Ulid::fromRfc4122($ulid);
238240
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UuidTest.php
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Uid\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Exception\InvalidArgumentException;
1516
use Symfony\Component\Uid\MaxUuid;
1617
use Symfony\Component\Uid\NilUuid;
1718
use Symfony\Component\Uid\Tests\Fixtures\CustomUuid;
@@ -35,7 +36,7 @@ class UuidTest extends TestCase
3536
*/
3637
public function testConstructorWithInvalidUuid(string $uuid)
3738
{
38-
$this->expectException(\InvalidArgumentException::class);
39+
$this->expectException(InvalidArgumentException::class);
3940
$this->expectExceptionMessage('Invalid UUID: "'.$uuid.'".');
4041

4142
Uuid::fromString($uuid);
@@ -58,7 +59,7 @@ public function testInvalidVariant(string $uuid)
5859
$uuid = (string) $uuid;
5960
$class = Uuid::class.'V'.$uuid[14];
6061

61-
$this->expectException(\InvalidArgumentException::class);
62+
$this->expectException(InvalidArgumentException::class);
6263
$this->expectExceptionMessage('Invalid UUIDv'.$uuid[14].': "'.$uuid.'".');
6364

6465
new $class($uuid);
@@ -381,7 +382,7 @@ public function testFromBinary()
381382
*/
382383
public function testFromBinaryInvalidFormat(string $ulid)
383384
{
384-
$this->expectException(\InvalidArgumentException::class);
385+
$this->expectException(InvalidArgumentException::class);
385386

386387
Uuid::fromBinary($ulid);
387388
}
@@ -408,7 +409,7 @@ public function testFromBase58()
408409
*/
409410
public function testFromBase58InvalidFormat(string $ulid)
410411
{
411-
$this->expectException(\InvalidArgumentException::class);
412+
$this->expectException(InvalidArgumentException::class);
412413

413414
Uuid::fromBase58($ulid);
414415
}
@@ -435,7 +436,7 @@ public function testFromBase32()
435436
*/
436437
public function testFromBase32InvalidFormat(string $ulid)
437438
{
438-
$this->expectException(\InvalidArgumentException::class);
439+
$this->expectException(InvalidArgumentException::class);
439440

440441
Uuid::fromBase32($ulid);
441442
}
@@ -462,7 +463,7 @@ public function testFromRfc4122()
462463
*/
463464
public function testFromRfc4122InvalidFormat(string $ulid)
464465
{
465-
$this->expectException(\InvalidArgumentException::class);
466+
$this->expectException(InvalidArgumentException::class);
466467

467468
Uuid::fromRfc4122($ulid);
468469
}
@@ -509,7 +510,7 @@ public function testV1ToV6()
509510

510511
public function testV1ToV7BeforeUnixEpochThrows()
511512
{
512-
$this->expectException(\InvalidArgumentException::class);
513+
$this->expectException(InvalidArgumentException::class);
513514
$this->expectExceptionMessage('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
514515

515516
(new UuidV1('9aba8000-ff00-11b0-b3db-3b3fc83afdfc'))->toV7(); // Timestamp is 1969-01-01 00:00:00.0000000

0 commit comments

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