From 8fa223b1afb12e808ad1bc2a9cb1f94c3a07500c Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 9 Aug 2021 10:42:17 +0100 Subject: [PATCH 1/2] Fix soft BC break of Uuid::fromString() Uses an assertion to indicate the `$uuid` variable cannot be an empty string, satisfying docblock on `LazyUuidFromString`. --- src/Uuid.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Uuid.php b/src/Uuid.php index dd690e1..d80fd57 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -24,6 +24,7 @@ use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Integer as IntegerObject; +use function assert; use function bin2hex; use function preg_match; use function str_replace; @@ -436,7 +437,7 @@ class Uuid implements UuidInterface /** * Creates a UUID from the string standard representation * - * @param non-empty-string $uuid A hexadecimal string + * @param string $uuid A hexadecimal string * * @return UuidInterface A UuidInterface instance created from a hexadecimal * string representation @@ -452,6 +453,8 @@ class Uuid implements UuidInterface public static function fromString(string $uuid): UuidInterface { if (! self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) { + assert($uuid !== ''); + return new LazyUuidFromString(strtolower($uuid)); } From ef1eb74c82a4acbccce9e45b3795c94ff7e122c5 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 9 Aug 2021 12:55:31 +0100 Subject: [PATCH 2/2] Add test covering `Uuid::fromString()` being called with empty string --- tests/UuidTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 034189b..29f1761 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -124,6 +124,14 @@ class UuidTest extends TestCase $this->assertSame('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString()); } + public function testFromStringWithEmptyString(): void + { + $this->expectException(InvalidUuidStringException::class); + $this->expectExceptionMessage('Invalid UUID string: '); + + Uuid::fromString(''); + } + public function testGetBytes(): void { $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');