Skip to content

Commit

Permalink
EnhancementL Use JSON_THROW_ON_ERROR flag
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 28, 2022
1 parent addc246 commit 8b948d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.6.4@97fe86c4e158b5a57c5150aa5055c38b5a809aab"/>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Printer.php">
<UnusedFunctionCall occurrences="1">
<code>\json_decode</code>
</UnusedFunctionCall>
</file>
</files>
9 changes: 8 additions & 1 deletion src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ final class Printer implements PrinterInterface
*/
public function print(string $json, string $indent = ' ', string $newLine = \PHP_EOL): string
{
if (null === \json_decode($json) && \JSON_ERROR_NONE !== \json_last_error()) {
try {
\json_decode(
$json,
false,
512,
\JSON_THROW_ON_ERROR,
);
} catch (\JsonException $exception) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not valid JSON.',
$json,
Expand Down

0 comments on commit 8b948d4

Please sign in to comment.