NodeDumper.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr\Include_;
  4. use PhpParser\Node\Stmt\Class_;
  5. use PhpParser\Node\Stmt\GroupUse;
  6. use PhpParser\Node\Stmt\Use_;
  7. use PhpParser\Node\Stmt\UseUse;
  8. class NodeDumper
  9. {
  10. private $dumpComments;
  11. private $dumpPositions;
  12. private $code;
  13. /**
  14. * Constructs a NodeDumper.
  15. *
  16. * Supported options:
  17. * * bool dumpComments: Whether comments should be dumped.
  18. * * bool dumpPositions: Whether line/offset information should be dumped. To dump offset
  19. * information, the code needs to be passed to dump().
  20. *
  21. * @param array $options Options (see description)
  22. */
  23. public function __construct(array $options = []) {
  24. $this->dumpComments = !empty($options['dumpComments']);
  25. $this->dumpPositions = !empty($options['dumpPositions']);
  26. }
  27. /**
  28. * Dumps a node or array.
  29. *
  30. * @param array|Node $node Node or array to dump
  31. * @param string|null $code Code corresponding to dumped AST. This only needs to be passed if
  32. * the dumpPositions option is enabled and the dumping of node offsets
  33. * is desired.
  34. *
  35. * @return string Dumped value
  36. */
  37. public function dump($node, $code = null) {
  38. $this->code = $code;
  39. return $this->dumpRecursive($node);
  40. }
  41. protected function dumpRecursive($node) {
  42. if ($node instanceof Node) {
  43. $r = $node->getType();
  44. if ($this->dumpPositions && null !== $p = $this->dumpPosition($node)) {
  45. $r .= $p;
  46. }
  47. $r .= '(';
  48. foreach ($node->getSubNodeNames() as $key) {
  49. $r .= "\n " . $key . ': ';
  50. $value = $node->$key;
  51. if (null === $value) {
  52. $r .= 'null';
  53. } elseif (false === $value) {
  54. $r .= 'false';
  55. } elseif (true === $value) {
  56. $r .= 'true';
  57. } elseif (is_scalar($value)) {
  58. if ('flags' === $key || 'newModifier' === $key) {
  59. $r .= $this->dumpFlags($value);
  60. } else if ('type' === $key && $node instanceof Include_) {
  61. $r .= $this->dumpIncludeType($value);
  62. } else if ('type' === $key
  63. && ($node instanceof Use_ || $node instanceof UseUse || $node instanceof GroupUse)) {
  64. $r .= $this->dumpUseType($value);
  65. } else {
  66. $r .= $value;
  67. }
  68. } else {
  69. $r .= str_replace("\n", "\n ", $this->dumpRecursive($value));
  70. }
  71. }
  72. if ($this->dumpComments && $comments = $node->getAttribute('comments')) {
  73. $r .= "\n comments: " . str_replace("\n", "\n ", $this->dumpRecursive($comments));
  74. }
  75. } elseif (is_array($node)) {
  76. $r = 'array(';
  77. foreach ($node as $key => $value) {
  78. $r .= "\n " . $key . ': ';
  79. if (null === $value) {
  80. $r .= 'null';
  81. } elseif (false === $value) {
  82. $r .= 'false';
  83. } elseif (true === $value) {
  84. $r .= 'true';
  85. } elseif (is_scalar($value)) {
  86. $r .= $value;
  87. } else {
  88. $r .= str_replace("\n", "\n ", $this->dumpRecursive($value));
  89. }
  90. }
  91. } elseif ($node instanceof Comment) {
  92. return $node->getReformattedText();
  93. } else {
  94. throw new \InvalidArgumentException('Can only dump nodes and arrays.');
  95. }
  96. return $r . "\n)";
  97. }
  98. protected function dumpFlags($flags) {
  99. $strs = [];
  100. if ($flags & Class_::MODIFIER_PUBLIC) {
  101. $strs[] = 'MODIFIER_PUBLIC';
  102. }
  103. if ($flags & Class_::MODIFIER_PROTECTED) {
  104. $strs[] = 'MODIFIER_PROTECTED';
  105. }
  106. if ($flags & Class_::MODIFIER_PRIVATE) {
  107. $strs[] = 'MODIFIER_PRIVATE';
  108. }
  109. if ($flags & Class_::MODIFIER_ABSTRACT) {
  110. $strs[] = 'MODIFIER_ABSTRACT';
  111. }
  112. if ($flags & Class_::MODIFIER_STATIC) {
  113. $strs[] = 'MODIFIER_STATIC';
  114. }
  115. if ($flags & Class_::MODIFIER_FINAL) {
  116. $strs[] = 'MODIFIER_FINAL';
  117. }
  118. if ($strs) {
  119. return implode(' | ', $strs) . ' (' . $flags . ')';
  120. } else {
  121. return $flags;
  122. }
  123. }
  124. protected function dumpIncludeType($type) {
  125. $map = [
  126. Include_::TYPE_INCLUDE => 'TYPE_INCLUDE',
  127. Include_::TYPE_INCLUDE_ONCE => 'TYPE_INCLUDE_ONCE',
  128. Include_::TYPE_REQUIRE => 'TYPE_REQUIRE',
  129. Include_::TYPE_REQUIRE_ONCE => 'TYPE_REQURE_ONCE',
  130. ];
  131. if (!isset($map[$type])) {
  132. return $type;
  133. }
  134. return $map[$type] . ' (' . $type . ')';
  135. }
  136. protected function dumpUseType($type) {
  137. $map = [
  138. Use_::TYPE_UNKNOWN => 'TYPE_UNKNOWN',
  139. Use_::TYPE_NORMAL => 'TYPE_NORMAL',
  140. Use_::TYPE_FUNCTION => 'TYPE_FUNCTION',
  141. Use_::TYPE_CONSTANT => 'TYPE_CONSTANT',
  142. ];
  143. if (!isset($map[$type])) {
  144. return $type;
  145. }
  146. return $map[$type] . ' (' . $type . ')';
  147. }
  148. protected function dumpPosition(Node $node) {
  149. if (!$node->hasAttribute('startLine') || !$node->hasAttribute('endLine')) {
  150. return null;
  151. }
  152. $start = $node->getAttribute('startLine');
  153. $end = $node->getAttribute('endLine');
  154. if ($node->hasAttribute('startFilePos') && $node->hasAttribute('endFilePos')
  155. && null !== $this->code
  156. ) {
  157. $start .= ':' . $this->toColumn($this->code, $node->getAttribute('startFilePos'));
  158. $end .= ':' . $this->toColumn($this->code, $node->getAttribute('endFilePos'));
  159. }
  160. return "[$start - $end]";
  161. }
  162. // Copied from Error class
  163. private function toColumn($code, $pos) {
  164. if ($pos > strlen($code)) {
  165. throw new \RuntimeException('Invalid position information');
  166. }
  167. $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
  168. if (false === $lineStartPos) {
  169. $lineStartPos = -1;
  170. }
  171. return $pos - $lineStartPos;
  172. }
  173. }