From 9521f53b39d1d8d2e9e7c84bd5eeb5d9bad76a10 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:13:05 +0100 Subject: [PATCH] refactor: now uses `petrknap/optional` --- Dockerfile | 9 +++++++++ composer.json | 6 ++++-- src/Coder/Base64.php | 11 ++++++----- src/Coder/Hex.php | 10 +++++----- src/Coder/Zlib.php | 17 +++++++---------- src/Serializer/Igbinary.php | 18 ++++++++---------- src/Serializer/Php.php | 10 +++++----- tests/Serializer/IgbinaryTest.php | 10 ++-------- 8 files changed, 46 insertions(+), 45 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4a973fa..865ecb7 100755 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,15 @@ RUN apt-get update \ COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer # endregion +# region included igbinary +# hadolint ignore=DL3008 +RUN pecl install -o -f \ + igbinary \ + && docker-php-ext-enable \ + igbinary \ +; +# endregion + # region included composer-library WORKDIR /app COPY . . diff --git a/composer.json b/composer.json index 6d2a4a2..5728ad9 100755 --- a/composer.json +++ b/composer.json @@ -40,9 +40,11 @@ "name": "petrknap/binary", "require": { "php": ">=8.1", + "petrknap/optional": "^3.1", "petrknap/shorts": "^2.0" }, "require-dev": { + "ext-igbinary": "*", "ext-mbstring": "*", "ext-zlib": "*", "nunomaduro/phpinsights": "^2.11", @@ -51,7 +53,7 @@ "squizlabs/php_codesniffer": "^3.7" }, "scripts": { - "test": "phpunit --colors=always --testdox tests", + "test": "@test-implementation", "ci-script": [ "@check-implementation", "@check-requirements", @@ -68,7 +70,7 @@ "composer outdated \"petrknap/*\" --major-only --strict --ansi --no-interaction" ], "test-implementation": [ - "@test" + "phpunit --colors=always --testdox tests" ] }, "suggest": { diff --git a/src/Coder/Base64.php b/src/Coder/Base64.php index a9462af..001f61b 100644 --- a/src/Coder/Base64.php +++ b/src/Coder/Base64.php @@ -4,6 +4,8 @@ namespace PetrKnap\Binary\Coder; +use PetrKnap\Optional\OptionalString; + /** * @see base64_encode() * @see base64_decode() @@ -11,6 +13,7 @@ final class Base64 extends Coder { public const URL_SAFE = false; + private const URL_REPLACE_TABLE = [ ['+', '/', '='], ['-', '_', ''], @@ -35,13 +38,11 @@ protected function doEncode(string $decoded): string protected function doDecode(string $encoded): string { - $decoded = base64_decode( + return OptionalString::ofFalsable(base64_decode( str_replace(self::URL_REPLACE_TABLE[1], self::URL_REPLACE_TABLE[0], $encoded), strict: true, + ))->orElseThrow( + static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded), ); - if ($decoded === false) { - throw new Exception\CouldNotDecodeData(__METHOD__, $encoded); - } - return $decoded; } } diff --git a/src/Coder/Hex.php b/src/Coder/Hex.php index daf16a6..6a510dc 100644 --- a/src/Coder/Hex.php +++ b/src/Coder/Hex.php @@ -4,6 +4,8 @@ namespace PetrKnap\Binary\Coder; +use PetrKnap\Optional\OptionalString; + /** * @see bin2hex() * @see hex2bin() @@ -17,10 +19,8 @@ protected function doEncode(string $decoded): string protected function doDecode(string $encoded): string { - $decoded = hex2bin($encoded); - if ($decoded === false) { - throw new Exception\CouldNotDecodeData(__METHOD__, $encoded); - } - return $decoded; + return OptionalString::ofFalsable(hex2bin($encoded))->orElseThrow( + static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded), + ); } } diff --git a/src/Coder/Zlib.php b/src/Coder/Zlib.php index 1970f43..8be15fe 100644 --- a/src/Coder/Zlib.php +++ b/src/Coder/Zlib.php @@ -4,6 +4,7 @@ namespace PetrKnap\Binary\Coder; +use PetrKnap\Optional\OptionalString; use PetrKnap\Shorts\HasRequirements; /** @@ -50,19 +51,15 @@ public function decode(string $encoded, ?int $maxLength = null): string protected function doEncode(string $decoded): string { - $encoded = zlib_encode($decoded, $this->encoding, $this->level); - if ($encoded === false) { - throw new Exception\CouldNotEncodeData(__METHOD__, $decoded); - } - return $encoded; + return OptionalString::ofFalsable(zlib_encode($decoded, $this->encoding, $this->level))->orElseThrow( + static fn () => new Exception\CouldNotEncodeData(__METHOD__, $decoded), + ); } protected function doDecode(string $encoded): string { - $decoded = zlib_decode($encoded, $this->maxLength); - if ($decoded === false) { - throw new Exception\CouldNotDecodeData(__METHOD__, $encoded); - } - return $decoded; + return OptionalString::ofFalsable(zlib_decode($encoded, $this->maxLength))->orElseThrow( + static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded), + ); } } diff --git a/src/Serializer/Igbinary.php b/src/Serializer/Igbinary.php index 82ac09e..2a5102f 100644 --- a/src/Serializer/Igbinary.php +++ b/src/Serializer/Igbinary.php @@ -4,6 +4,8 @@ namespace PetrKnap\Binary\Serializer; +use PetrKnap\Optional\Optional; +use PetrKnap\Optional\OptionalString; use PetrKnap\Shorts\HasRequirements; /** @@ -26,19 +28,15 @@ functions: [ protected function doSerialize(mixed $serializable): string { - $serialized = igbinary_serialize($serializable); - if ($serialized === null) { - throw new Exception\CouldNotSerializeData(__METHOD__, $serializable); - } - return $serialized; + return OptionalString::ofFalsable(igbinary_serialize($serializable) ?? false)->orElseThrow( + static fn () => new Exception\CouldNotSerializeData(__METHOD__, $serializable), + ); } protected function doUnserialize(string $serialized): mixed { - $serializable = igbinary_unserialize($serialized); - if ($serializable === null || $serializable === false) { - throw new Exception\CouldNotUnserializeData(__METHOD__, $serialized); - } - return $serializable; + return Optional::ofFalsable(igbinary_unserialize($serialized) ?? false)->orElseThrow( + static fn () => new Exception\CouldNotUnserializeData(__METHOD__, $serialized), + ); } } diff --git a/src/Serializer/Php.php b/src/Serializer/Php.php index dafbdef..7ab95ae 100644 --- a/src/Serializer/Php.php +++ b/src/Serializer/Php.php @@ -4,6 +4,8 @@ namespace PetrKnap\Binary\Serializer; +use PetrKnap\Optional\Optional; + /** * @see serialize() * @see unserialize() @@ -17,10 +19,8 @@ protected function doSerialize(mixed $serializable): string protected function doUnserialize(string $serialized): mixed { - $serializable = unserialize($serialized); - if ($serializable === false) { - throw throw new Exception\CouldNotUnserializeData(__METHOD__, $serialized); - } - return $serializable; + return Optional::ofFalsable(unserialize($serialized))->orElseThrow( + static fn () => new Exception\CouldNotUnserializeData(__METHOD__, $serialized), + ); } } diff --git a/tests/Serializer/IgbinaryTest.php b/tests/Serializer/IgbinaryTest.php index 956e81a..67c7cce 100644 --- a/tests/Serializer/IgbinaryTest.php +++ b/tests/Serializer/IgbinaryTest.php @@ -4,21 +4,15 @@ namespace PetrKnap\Binary\Serializer; -use PetrKnap\Shorts\Exception\MissingRequirement; - final class IgbinaryTest extends SerializerTestCase { public static function getSerialized(): string { - return base64_decode(''); // @TODO + return base64_decode('AAAAAhcIc3RkQ2xhc3MUBhEFYXJyYXkUABEGYmluYXJ5BgARBWZsb2F0DAAAAAAAAAAAEQNpbnQGABEEbnVsbAARBnN0cmluZw0='); } public static function getSerializer(): SerializerInterface { - try { - return new Igbinary(); - } catch (MissingRequirement $reason) { - self::markTestSkipped($reason->getMessage()); - } + return new Igbinary(); } }