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.
This commit is contained in:
Choraimy Kroonstuiver
2025-05-26 07:09:20 +02:00
committed by GitHub
parent fd7c1c974c
commit c72360e015
+19
View File
@@ -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;
}
}