From 6ec0aa8c5481b026be280204484f9bcd19ce4568 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 8 Feb 2026 14:51:48 +0100 Subject: [PATCH] add NoDiscard attributes --- psalm.xml | 3 +++ src/Json.php | 3 +++ tests/JsonTest.php | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/psalm.xml b/psalm.xml index 510148d..feccc34 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,4 +14,7 @@ + + + diff --git a/src/Json.php b/src/Json.php index 7976490..be4848c 100644 --- a/src/Json.php +++ b/src/Json.php @@ -29,6 +29,7 @@ private function __construct() * * @throws \Exception */ + #[\NoDiscard] public static function decode(string $string): mixed { try { @@ -48,6 +49,7 @@ public static function decode(string $string): mixed * * @return Maybe */ + #[\NoDiscard] public static function maybeDecode(string $string): Maybe { try { @@ -64,6 +66,7 @@ public static function maybeDecode(string $string): Maybe * * @throws \Exception */ + #[\NoDiscard] public static function encode(mixed $content, int $options = 0, int $depth = 512): string { try { diff --git a/tests/JsonTest.php b/tests/JsonTest.php index 125bb67..d2b7138 100644 --- a/tests/JsonTest.php +++ b/tests/JsonTest.php @@ -49,7 +49,7 @@ public function testThrowWhenExceedingSpecifiedMaxDepth() { $this->expectException(MaximumDepthExceeded::class); - Json::encode(['foo' => ['bar' => 'baz']], 0, 1); + $_ = Json::encode(['foo' => ['bar' => 'baz']], 0, 1); } public function testDecode() @@ -73,7 +73,7 @@ public function testThrowOnSyntaxError() { $this->expectException(SyntaxError::class); - Json::decode('{"foo"'); + $_ = Json::decode('{"foo"'); } public function testReturnNothingOnDecodingError() @@ -99,20 +99,20 @@ public function testThrowOnMaximumDepthExceeded() return $deepen(['foo' => $array]); })(['foo' => 'bar']); - Json::encode($array); + $_ = Json::encode($array); } public function testThrowOnStateMismatch() { $this->expectException(StateMismatch::class); - Json::decode('{"foo":"bar"]'); + $_ = Json::decode('{"foo":"bar"]'); } public function testThrowOnMalformedUTF8OrCharacterControlError() { try { - Json::decode('{"foo":"'.\random_bytes(42).'"}'); + $_ = Json::decode('{"foo":"'.\random_bytes(42).'"}'); $this->fail('it should throw'); } catch (MalformedUTF8 | CharacterControlError | SyntaxError $e) { $this->assertTrue(true); @@ -123,14 +123,14 @@ public function testThrowOnCharacterControlError() { $this->expectException(CharacterControlError::class); - Json::decode(\chr(23)); + $_ = Json::decode(\chr(23)); } public function testThrowOnMalformedUTF16() { $this->expectException(MalformedUTF16::class); - Json::decode('["\ude00\ud83d"]'); + $_ = Json::decode('["\ude00\ud83d"]'); } public function testThrowOnRecursiveReference() @@ -139,20 +139,20 @@ public function testThrowOnRecursiveReference() $array = ['foo' => 'bar', 'bar' => null]; $array['bar'] = &$array; - Json::encode($array); + $_ = Json::encode($array); } public function testThrowOnInfiniteOrNanCannotBeEncoded() { $this->expectException(InfiniteOrNanCannotBeEncoded::class); - Json::encode(['foo' => \INF]); + $_ = Json::encode(['foo' => \INF]); } public function testThrowOnValueCannotBeEncoded() { $this->expectException(ValueCannotBeEncoded::class); - Json::encode(['foo' => \tmpfile()]); + $_ = Json::encode(['foo' => \tmpfile()]); } }