Skip to content

Commit 6577ca1

Browse files
committed
Emit JSON errors from PHP Parser
The PHP parser had a bug here in that it presumed JSON encoding would succeed (and there are reasons it might not). This checks the return value of `json_encode` and emits a relevant error message if appropriate. Before this, a triggering case would result in empty output and an unhelpful error message from `JSON.parse` upstream in the Ruby code.
1 parent 7b5a477 commit 6577ca1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

vendor/php-parser/parser.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313

1414
$serializer = new PhpParser\Serializer\JSON;
1515
$nodes = $serializer->serialize($stmts);
16-
echo json_encode($nodes);
17-
16+
$json = json_encode($nodes);
17+
if (false === $json) {
18+
fwrite(STDERR, "Parse Error: JSON encoding failed: ".json_last_error_msg()."\n");
19+
exit(1);
20+
} else {
21+
echo $json;
22+
}
1823
} catch (PHPParser\Error $e) {
1924
fwrite(STDERR, "Parse Error: ".$e->getMessage()."\n");
2025
exit(1);

0 commit comments

Comments
 (0)