Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip check when flag is set in
Browse files Browse the repository at this point in the history
marmichalski committed Sep 5, 2024

Verified

This commit was signed with the committer’s verified signature.
marmichalski Marcin Michalski
1 parent 3a6e43b commit 9c69d1a
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/special_cases.php
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
function json_decode(string $json, bool $assoc = false, int $depth = 512, int $flags = 0): mixed
{
$data = \json_decode($json, $assoc, $depth, $flags);
if (JSON_ERROR_NONE !== json_last_error()) {
if (!($flags & JSON_THROW_ON_ERROR) && JSON_ERROR_NONE !== json_last_error()) {
throw JsonException::createFromPhpError();
}
return $data;
16 changes: 16 additions & 0 deletions tests/JsonDecodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class JsonDecodeTest extends TestCase
{
public function testJsonLastErrorIsNotCheckedIfFlagIsSet(): void
{
\json_decode('{');
self::assertNotSame(JSON_ERROR_NONE, \json_last_error());

\Safe\json_decode('[]', flags: \JSON_THROW_ON_ERROR);
}
}

0 comments on commit 9c69d1a

Please sign in to comment.