From c72360e015d4acde1169971ebb24dc48e31b0c4d Mon Sep 17 00:00:00 2001 From: Choraimy Kroonstuiver <3661474+axlon@users.noreply.github.com> Date: Mon, 26 May 2025 07:09:20 +0200 Subject: [PATCH] Hexadecimal is never an empty string (#593) Similarly to UUIDs, a hexadecimal can never be empty, this PR documents it as such. More and more libraries are starting to enforce stricter typing, I was dealing with a library that required a `non-empty-string` which hexadecimal isn't (is, but isn't documented that it is) which requires either baselining false positives are adding redundant assertions. --- src/Type/Hexadecimal.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Type/Hexadecimal.php b/src/Type/Hexadecimal.php index 132b4e0..2676291 100644 --- a/src/Type/Hexadecimal.php +++ b/src/Type/Hexadecimal.php @@ -32,6 +32,9 @@ use function substr; */ final class Hexadecimal implements TypeInterface { + /** + * @var non-empty-string + */ private string $value; /** @@ -42,21 +45,33 @@ final class Hexadecimal implements TypeInterface $this->value = $value instanceof self ? (string) $value : $this->prepareValue($value); } + /** + * @psalm-return non-empty-string + */ public function toString(): string { return $this->value; } + /** + * @psalm-return non-empty-string + */ public function __toString(): string { return $this->toString(); } + /** + * @psalm-return non-empty-string + */ public function jsonSerialize(): string { return $this->toString(); } + /** + * @psalm-return non-empty-string + */ public function serialize(): string { return $this->toString(); @@ -94,6 +109,9 @@ final class Hexadecimal implements TypeInterface $this->unserialize($data['string']); } + /** + * @psalm-return non-empty-string + */ private function prepareValue(string $value): string { $value = strtolower($value); @@ -108,6 +126,7 @@ final class Hexadecimal implements TypeInterface ); } + /** @var non-empty-string */ return $value; } }