Skip to content

Commit

Permalink
Make PHPStan happy
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 14, 2024
1 parent b9cef45 commit 92bb4d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Base32/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Base32/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
8 changes: 8 additions & 0 deletions src/Base32/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 92bb4d1

Please sign in to comment.