From 6577ca167fd821f57c4e440dc8a03c3c1c5dca53 Mon Sep 17 00:00:00 2001 From: Will Fleming Date: Sun, 3 Jan 2016 22:39:08 -0500 Subject: [PATCH] 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. --- vendor/php-parser/parser.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vendor/php-parser/parser.php b/vendor/php-parser/parser.php index 152f095c..097d19f5 100644 --- a/vendor/php-parser/parser.php +++ b/vendor/php-parser/parser.php @@ -13,8 +13,13 @@ $serializer = new PhpParser\Serializer\JSON; $nodes = $serializer->serialize($stmts); - echo json_encode($nodes); - + $json = json_encode($nodes); + if (false === $json) { + fwrite(STDERR, "Parse Error: JSON encoding failed: ".json_last_error_msg()."\n"); + exit(1); + } else { + echo $json; + } } catch (PHPParser\Error $e) { fwrite(STDERR, "Parse Error: ".$e->getMessage()."\n"); exit(1);