From 7102e985378210247a5fbafa91ab61f5c61f1045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gamez?= Date: Fri, 11 Feb 2022 21:04:12 +0100 Subject: [PATCH] Use `UnexpectedValueException` instead of `InvalidArgumentException` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As suggested by @MGatner 🥰 --- README.md | 6 +++--- src/Json.php | 16 ++++++++-------- tests/DecodeJsonTest.php | 8 ++++---- tests/EncodeJsonTest.php | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 0fe79df..216cf33 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A simple helper to decode and encode JSON, including from files. ```php use Beste\Json; -use InvalidArgumentException; +use UnexpectedValueException; $object = Json::decode('{"key": "value"}'); @@ -25,10 +25,10 @@ $json = Json::encode($object); $prettyJson = Json::pretty($object); // When something goes wring while decoding/encoding, -// an `InvalidArgumentException` is thrown +// an `UnexpectedValueException` is thrown try { Json::decode('{]'); -} catch (InvalidArgumentException $e) { +} catch (UnexpectedValueException $e) { // Handle error } ``` diff --git a/src/Json.php b/src/Json.php index 497f62a..ff38e9c 100644 --- a/src/Json.php +++ b/src/Json.php @@ -4,8 +4,8 @@ namespace Beste; -use InvalidArgumentException; use JsonException; +use UnexpectedValueException; final class Json { @@ -14,7 +14,7 @@ final class Json private const DECODE_DEFAULT = JSON_BIGINT_AS_STRING; /** - * @throws InvalidArgumentException + * @throws UnexpectedValueException * * @return mixed */ @@ -26,19 +26,19 @@ public static function decode(string $json, ?bool $forceArray = null) try { return json_decode($json, $forceArray, 512, $flags | self::DECODE_DEFAULT | JSON_THROW_ON_ERROR); } catch (JsonException $e) { - throw new InvalidArgumentException($e->getMessage()); + throw new UnexpectedValueException($e->getMessage()); } } /** - * @throws InvalidArgumentException + * @throws UnexpectedValueException * * @return mixed */ public static function decodeFile(string $path, bool $forceArray = null) { if (!is_readable($path)) { - throw new InvalidArgumentException("The file at '$path' does not exist"); + throw new UnexpectedValueException("The file at '$path' does not exist"); } return self::decode((string) file_get_contents($path), $forceArray); @@ -47,7 +47,7 @@ public static function decodeFile(string $path, bool $forceArray = null) /** * @param mixed $data * - * @throws InvalidArgumentException + * @throws UnexpectedValueException */ public static function encode($data, ?int $options = null): string { @@ -56,14 +56,14 @@ public static function encode($data, ?int $options = null): string try { return json_encode($data, $options | self::ENCODE_DEFAULT | JSON_THROW_ON_ERROR); } catch (JsonException $e) { - throw new InvalidArgumentException($e->getMessage()); + throw new UnexpectedValueException($e->getMessage()); } } /** * @param mixed $value * - * @throws InvalidArgumentException + * @throws UnexpectedValueException */ public static function pretty($value, ?int $options = null): string { diff --git a/tests/DecodeJsonTest.php b/tests/DecodeJsonTest.php index d2d623a..ec52930 100644 --- a/tests/DecodeJsonTest.php +++ b/tests/DecodeJsonTest.php @@ -5,7 +5,7 @@ namespace Beste\Json\Tests; use Beste\Json; -use InvalidArgumentException; +use UnexpectedValueException; use PHPUnit\Framework\TestCase; /** @@ -16,7 +16,7 @@ class DecodeJsonTest extends TestCase /** @test */ public function it_rejects_invalid_json(): void { - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnexpectedValueException::class); Json::decode('{'); } @@ -58,7 +58,7 @@ public function it_rejects_an_unreadable_file(): void { $path = __DIR__.'/non-existing.json'; - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnexpectedValueException::class); assert(!file_exists($path)); Json::decodeFile($path); @@ -69,7 +69,7 @@ public function it_rejects_a_file_with_invalid_json(): void { $path = __DIR__.'/invalid.json'; - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnexpectedValueException::class); assert(file_exists($path)); $this->assertIsObject(Json::decodeFile($path)); diff --git a/tests/EncodeJsonTest.php b/tests/EncodeJsonTest.php index 878090f..7d8d918 100644 --- a/tests/EncodeJsonTest.php +++ b/tests/EncodeJsonTest.php @@ -5,7 +5,7 @@ namespace Beste\Json\Tests; use Beste\Json; -use InvalidArgumentException; +use UnexpectedValueException; use PHPUnit\Framework\TestCase; /** @@ -28,7 +28,7 @@ public function it_does_not_escape_unicode(): void /** @test */ public function it_rejects_invalid_resources(): void { - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnexpectedValueException::class); // The point is that resources cannot be encoded, not what's in the file :) Json::encode(fopen(__DIR__.'/valid.json', 'rb'));