You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.3 KiB

3 years ago
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class JsonDecoder
  4. {
  5. /** @var \ReflectionClass[] Node type to reflection class map */
  6. private $reflectionClassCache;
  7. public function decode(string $json) {
  8. $value = json_decode($json, true);
  9. if (json_last_error()) {
  10. throw new \RuntimeException('JSON decoding error: ' . json_last_error_msg());
  11. }
  12. return $this->decodeRecursive($value);
  13. }
  14. private function decodeRecursive($value) {
  15. if (\is_array($value)) {
  16. if (isset($value['nodeType'])) {
  17. if ($value['nodeType'] === 'Comment' || $value['nodeType'] === 'Comment_Doc') {
  18. return $this->decodeComment($value);
  19. }
  20. return $this->decodeNode($value);
  21. }
  22. return $this->decodeArray($value);
  23. }
  24. return $value;
  25. }
  26. private function decodeArray(array $array) : array {
  27. $decodedArray = [];
  28. foreach ($array as $key => $value) {
  29. $decodedArray[$key] = $this->decodeRecursive($value);
  30. }
  31. return $decodedArray;
  32. }
  33. private function decodeNode(array $value) : Node {
  34. $nodeType = $value['nodeType'];
  35. if (!\is_string($nodeType)) {
  36. throw new \RuntimeException('Node type must be a string');
  37. }
  38. $reflectionClass = $this->reflectionClassFromNodeType($nodeType);
  39. /** @var Node $node */
  40. $node = $reflectionClass->newInstanceWithoutConstructor();
  41. if (isset($value['attributes'])) {
  42. if (!\is_array($value['attributes'])) {
  43. throw new \RuntimeException('Attributes must be an array');
  44. }
  45. $node->setAttributes($this->decodeArray($value['attributes']));
  46. }
  47. foreach ($value as $name => $subNode) {
  48. if ($name === 'nodeType' || $name === 'attributes') {
  49. continue;
  50. }
  51. $node->$name = $this->decodeRecursive($subNode);
  52. }
  53. return $node;
  54. }
  55. private function decodeComment(array $value) : Comment {
  56. $className = $value['nodeType'] === 'Comment' ? Comment::class : Comment\Doc::class;
  57. if (!isset($value['text'])) {
  58. throw new \RuntimeException('Comment must have text');
  59. }
  60. return new $className(
  61. $value['text'],
  62. $value['line'] ?? -1, $value['filePos'] ?? -1, $value['tokenPos'] ?? -1,
  63. $value['endLine'] ?? -1, $value['endFilePos'] ?? -1, $value['endTokenPos'] ?? -1
  64. );
  65. }
  66. private function reflectionClassFromNodeType(string $nodeType) : \ReflectionClass {
  67. if (!isset($this->reflectionClassCache[$nodeType])) {
  68. $className = $this->classNameFromNodeType($nodeType);
  69. $this->reflectionClassCache[$nodeType] = new \ReflectionClass($className);
  70. }
  71. return $this->reflectionClassCache[$nodeType];
  72. }
  73. private function classNameFromNodeType(string $nodeType) : string {
  74. $className = 'PhpParser\\Node\\' . strtr($nodeType, '_', '\\');
  75. if (class_exists($className)) {
  76. return $className;
  77. }
  78. $className .= '_';
  79. if (class_exists($className)) {
  80. return $className;
  81. }
  82. throw new \RuntimeException("Unknown node type \"$nodeType\"");
  83. }
  84. }