mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-13 15:46:53 +03:00
Assert non-empty-string when UUID is valid (#410)
* Assert, for psalm, the string is non-empty if validation passes * Adds a test for SA asserting that valid string are also non-empty * Suppress PHPStan error for unsupported conditional type assertion * Check SA tools don't complain about non-empty-string as input parameter to `Uuid::isValid()`
This commit is contained in:
@@ -26,3 +26,7 @@ parameters:
|
||||
- ./tests/Guid/*Test.php
|
||||
- ./tests/Nonstandard/*Test.php
|
||||
- ./tests/Rfc4122/*Test.php
|
||||
-
|
||||
message: "#^Method Ramsey\\\\Uuid\\\\.+ should return non-empty-string but returns string\\.$#"
|
||||
paths:
|
||||
- ./tests/static-analysis/ValidUuidIsNonEmpty.php
|
||||
|
||||
@@ -549,6 +549,8 @@ class Uuid implements UuidInterface
|
||||
*
|
||||
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
|
||||
* but under constant factory setups, this method operates in functionally pure manners
|
||||
*
|
||||
* @psalm-assert-if-true non-empty-string $uuid
|
||||
*/
|
||||
public static function isValid(string $uuid): bool
|
||||
{
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\StaticAnalysis;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
final class ValidUuidIsNonEmpty
|
||||
{
|
||||
/** @return non-empty-string */
|
||||
public function validUuidsAreNotEmpty(string $input): string
|
||||
{
|
||||
if (Uuid::isValid($input)) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Not a UUID');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string $input
|
||||
*
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function givenNonEmptyInputAssertionRemainsValid(string $input): string
|
||||
{
|
||||
if (Uuid::isValid($input)) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Not a UUID');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user