diff --git a/bin/php-parse b/bin/php-parse index bb3e46df4b..3714e67b3f 100755 --- a/bin/php-parse +++ b/bin/php-parse @@ -8,6 +8,14 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php } } +spl_autoload_register(function ($class_name) { + $file = __DIR__ . '/../lib/' . $class_name . '.php'; + $file = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file); + if (file_exists($file)) { + require $file; + } +}); + ini_set('xdebug.max_nesting_level', 3000); // Disable Xdebug var_dump() output truncation @@ -85,7 +93,7 @@ foreach ($files as $file) { echo $prettyPrinter->prettyPrintFile($stmts), "\n"; } elseif ('json-dump' === $operation) { fwrite(STDERR, "==> JSON dump:\n"); - echo json_encode($stmts, JSON_PRETTY_PRINT), "\n"; + echo json_encode($stmts, JSON_PRETTY_PRINT | JSON_INVALID_UTF8_SUBSTITUTE), "\n"; } elseif ('var-dump' === $operation) { fwrite(STDERR, "==> var_dump():\n"); var_dump($stmts); diff --git a/lib/PhpParser/Node/Scalar/String_.php b/lib/PhpParser/Node/Scalar/String_.php index 6690a16bfb..38969a0dc7 100644 --- a/lib/PhpParser/Node/Scalar/String_.php +++ b/lib/PhpParser/Node/Scalar/String_.php @@ -116,7 +116,7 @@ function($matches) { if (isset(self::$replacements[$str])) { return self::$replacements[$str]; } elseif ('x' === $str[0] || 'X' === $str[0]) { - return chr(hexdec(substr($str, 1))); + return self::singleByteHexReplacement(substr($str, 1)); } elseif ('u' === $str[0]) { return self::codePointToUtf8(hexdec($matches[2])); } else { @@ -127,6 +127,15 @@ function($matches) { ); } + private static function singleByteHexReplacement(string $hexstr) : string { + $num = hexdec($hexstr); + if ($num <= 0x7F) { + return chr($num); + } else { + return "\x" . $hexstr; + } + } + /** * Converts a Unicode code point to its UTF-8 encoded representation. *