unused guard methods

This commit is contained in:
robert dennis
2026-10-12 05:18:57 -04:00
parent a80b1a9b1d
commit c36161ec44
2 changed files with 0 additions and 113 deletions
-103
View File
@@ -74,18 +74,6 @@ final class JsonValueGuard
throw_if($value !== null && ! is_bool($value) && ! is_float($value) && ! is_int($value) && ! is_string($value), UnexpectedValueException::class, 'Expected a scalar JSON value.');
}
/**
* @phpstan-assert ApiScalar $value
*
* @return ApiScalar
*/
public static function scalar(mixed $value): bool|float|int|string|null
{
self::assertScalar($value);
return $value;
}
/** @phpstan-assert string $value */
public static function string(mixed $value): string
{
@@ -94,56 +82,6 @@ final class JsonValueGuard
return $value;
}
/**
* @phpstan-assert non-empty-string $value
*
* @return non-empty-string
*/
public static function nonEmptyString(mixed $value): string
{
$value = self::string($value);
throw_if($value === '', UnexpectedValueException::class, 'Expected a non-empty string value.');
return $value;
}
/** @phpstan-assert string|null $value */
public static function nullableString(mixed $value): ?string
{
throw_if($value !== null && ! is_string($value), UnexpectedValueException::class, 'Expected a nullable string value.');
return $value;
}
/** @phpstan-assert ApiScalar $value */
public static function nullableScalarString(mixed $value): ?string
{
$value = self::scalar($value);
// SAFETY: assertScalar() limits the value to JSON scalars, all of which have a defined string representation.
return $value === null ? null : (string) $value;
}
/**
* Accepts what Laravel's `boolean` rule accepts: true, false, 1, 0, "1" and "0".
*
* @phpstan-assert bool|int|string $value
*/
public static function boolean(mixed $value): bool
{
return match ($value) {
true, 1, '1' => true,
false, 0, '0' => false,
default => throw new UnexpectedValueException('Expected a boolean value.'),
};
}
/** @phpstan-assert int|float|string|null $value */
public static function nullableInteger(mixed $value): ?int
{
return $value === null ? null : self::integer($value);
}
/**
* Accepts what Laravel's `integer` rule accepts: integers, integer strings, and floats
* with no fractional part, which is how JSON decodes a value such as `1024.0`.
@@ -195,47 +133,6 @@ final class JsonValueGuard
return $value;
}
/**
* @phpstan-assert list<int> $value
*
* @return list<int>
*/
public static function integerList(mixed $value): array
{
throw_if(! is_array($value) || ! array_is_list($value), UnexpectedValueException::class, 'Expected a list of integers.');
foreach ($value as $item) {
throw_unless(is_int($item), UnexpectedValueException::class, 'Expected a list of integers.');
}
return $value;
}
/**
* @param list<int|string> $value
* @return list<int>
*/
public static function normalizedIntegerList(array $value): array
{
return array_map(self::integer(...), $value);
}
/**
* @phpstan-assert list<int|string> $value
*
* @return list<int|string>
*/
public static function integerStringList(mixed $value): array
{
throw_if(! is_array($value) || ! array_is_list($value), UnexpectedValueException::class, 'Expected a list of integers or strings.');
foreach ($value as $item) {
throw_if(! is_int($item) && ! is_string($item), UnexpectedValueException::class, 'Expected a list of integers or strings.');
}
return $value;
}
/** @phpstan-assert array<string, ApiScalar> $value */
public static function assertScalarPayload(mixed $value): void
{
-10
View File
@@ -39,10 +39,6 @@ test('rejects objects', function () {
test('accepts json empty objects', function () {
expect(fn () => JsonValueGuard::assertValue(new JsonEmptyObject()))->not->toThrow(UnexpectedValueException::class);
});
test('nullable strings preserve strings and null', function () {
expect(JsonValueGuard::nullableString('value'))->toBe('value');
expect(JsonValueGuard::nullableString(null))->toBeNull();
});
test('string lists preserve sequential string values', function () {
expect(JsonValueGuard::stringList(['first', 'second']))->toBe(['first', 'second']);
});
@@ -54,12 +50,6 @@ test('string lists reject invalid shapes', function (mixed $value) {
'associative array' => [['name' => 'value']],
'non-string item' => [['value', 1]],
]);
test('booleans accept what the boolean validation rule accepts', function (mixed $value, bool $expected) {
expect(JsonValueGuard::boolean($value))->toBe($expected);
})->with([[true, true], [1, true], ['1', true], [false, false], [0, false], ['0', false]]);
test('booleans reject anything else', function (mixed $value) {
expect(fn (): bool => JsonValueGuard::boolean($value))->toThrow(UnexpectedValueException::class, 'Expected a boolean value.');
})->with([[null], ['true'], ['yes'], [2], [1.0], [[]]]);
test('integers accept what the integer validation rule accepts', function (mixed $value, int $expected) {
expect(JsonValueGuard::integer($value))->toBe($expected);
})->with([[5, 5], ['5', 5], ['-5', -5], [1024.0, 1024]]);