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:
George Steel
2022-08-09 16:04:36 +01:00
committed by GitHub
parent bc6cd7d888
commit b4dff559ab
3 changed files with 41 additions and 0 deletions
+4
View File
@@ -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
+2
View File
@@ -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');
}
}