From 92bb4d1e2081fdbc8b9d8884017e0e0017d66f92 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Thu, 14 Mar 2024 07:18:23 +0100 Subject: [PATCH] Make PHPStan happy --- src/Base32/Base32.php | 15 +++++++++++++-- src/Base32/Base32Test.php | 2 +- src/Base32/functions.php | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Base32/Base32.php b/src/Base32/Base32.php index 7d6e916..d55aa11 100644 --- a/src/Base32/Base32.php +++ b/src/Base32/Base32.php @@ -9,9 +9,16 @@ final class Base32 { + private const ALPHABET_SIZE = 32; + /** @var non-empty-string */ private readonly string $alphabet; + /** @var non-empty-string */ private readonly string $padding; + /** + * @param non-empty-string $alphabet + * @param non-empty-string $padding + */ private function __construct(string $alphabet, string $padding) { $normalizeAlphabet = strtoupper($alphabet); @@ -20,15 +27,19 @@ private function __construct(string $alphabet, string $padding) 1 !== strlen($padding) => throw new ValueError('The padding character must a single character.'), "\r" === $padding => throw new ValueError('The padding character can not be the carriage return character.'), "\n" === $padding => throw new ValueError('The padding character can not be the newline escape sequence.'), - 32 !== strlen($alphabet) => throw new ValueError('The alphabet must be a 32 bytes long string.'), + self::ALPHABET_SIZE !== strlen($alphabet) => throw new ValueError('The alphabet must be a 32 bytes long string.'), str_contains($alphabet, "\r") => throw new ValueError('The alphabet can not contain the carriage return character.'), str_contains($alphabet, "\n") => throw new ValueError('The alphabet can not contain the newline escape sequence.'), str_contains($normalizeAlphabet, strtoupper($padding)) => throw new ValueError('The alphabet can not contain the padding character.'), - 32 !== count(array_unique(str_split($normalizeAlphabet))) => throw new ValueError('The alphabet must contain unique characters.'), + self::ALPHABET_SIZE !== count(array_unique(str_split($normalizeAlphabet))) => throw new ValueError('The alphabet must contain unique characters.'), default => [$alphabet, $padding], }; } + /** + * @param non-empty-string $alphabet + * @param non-empty-string $padding + */ public static function new(string $alphabet, string $padding): self { return new self($alphabet, $padding); diff --git a/src/Base32/Base32Test.php b/src/Base32/Base32Test.php index dd7a730..94e16da 100644 --- a/src/Base32/Base32Test.php +++ b/src/Base32/Base32Test.php @@ -86,7 +86,7 @@ public function it_will_return_false_from_invalid_encoded_string_with_base32_dec string $padding ): void { try { - self::assertFalse(base32_decode($sequence, $alphabet, $padding, true)); + self::assertFalse(base32_decode($sequence, $alphabet, $padding, true)); /* @phpstan-ignore-line */ } catch (ValueError $exception) { self::assertSame($message, $exception->getMessage()); } diff --git a/src/Base32/functions.php b/src/Base32/functions.php index 534f7d2..955d01d 100644 --- a/src/Base32/functions.php +++ b/src/Base32/functions.php @@ -8,6 +8,10 @@ defined('PHP_BASE32_HEX') || define('PHP_BASE32_HEX', '0123456789ABCDEFGHIJKLMNOPQRSTUV'); if (!function_exists('base32_encode')) { + /** + * @param non-empty-string $alphabet + * @param non-empty-string $padding + */ function base32_encode( string $decoded, string $alphabet = PHP_BASE32_ASCII, @@ -18,6 +22,10 @@ function base32_encode( } if (!function_exists('base32_decode')) { + /** + * @param non-empty-string $alphabet + * @param non-empty-string $padding + */ function base32_decode( string $encoded, string $alphabet = PHP_BASE32_ASCII,