Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<UndefinedAttributeClass errorLevel="suppress" />
</issueHandlers>
</psalm>
3 changes: 3 additions & 0 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private function __construct()
*
* @throws \Exception
*/
#[\NoDiscard]
public static function decode(string $string): mixed
{
try {
Expand All @@ -48,6 +49,7 @@ public static function decode(string $string): mixed
*
* @return Maybe<mixed>
*/
#[\NoDiscard]
public static function maybeDecode(string $string): Maybe
{
try {
Expand All @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions tests/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -73,7 +73,7 @@ public function testThrowOnSyntaxError()
{
$this->expectException(SyntaxError::class);

Json::decode('{"foo"');
$_ = Json::decode('{"foo"');
}

public function testReturnNothingOnDecodingError()
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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()]);
}
}