No Description

NodeDumper.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php declare(strict_types=1);
  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, string $code = null) : string {
  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. } elseif ('type' === $key && $node instanceof Include_) {
  61. $r .= $this->dumpIncludeType($value);
  62. } elseif ('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->getComments()) {
  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 ($flags & Class_::MODIFIER_READONLY) {
  119. $strs[] = 'MODIFIER_READONLY';
  120. }
  121. if ($strs) {
  122. return implode(' | ', $strs) . ' (' . $flags . ')';
  123. } else {
  124. return $flags;
  125. }
  126. }
  127. protected function dumpIncludeType($type) {
  128. $map = [
  129. Include_::TYPE_INCLUDE => 'TYPE_INCLUDE',
  130. Include_::TYPE_INCLUDE_ONCE => 'TYPE_INCLUDE_ONCE',
  131. Include_::TYPE_REQUIRE => 'TYPE_REQUIRE',
  132. Include_::TYPE_REQUIRE_ONCE => 'TYPE_REQUIRE_ONCE',
  133. ];
  134. if (!isset($map[$type])) {
  135. return $type;
  136. }
  137. return $map[$type] . ' (' . $type . ')';
  138. }
  139. protected function dumpUseType($type) {
  140. $map = [
  141. Use_::TYPE_UNKNOWN => 'TYPE_UNKNOWN',
  142. Use_::TYPE_NORMAL => 'TYPE_NORMAL',
  143. Use_::TYPE_FUNCTION => 'TYPE_FUNCTION',
  144. Use_::TYPE_CONSTANT => 'TYPE_CONSTANT',
  145. ];
  146. if (!isset($map[$type])) {
  147. return $type;
  148. }
  149. return $map[$type] . ' (' . $type . ')';
  150. }
  151. /**
  152. * Dump node position, if possible.
  153. *
  154. * @param Node $node Node for which to dump position
  155. *
  156. * @return string|null Dump of position, or null if position information not available
  157. */
  158. protected function dumpPosition(Node $node) {
  159. if (!$node->hasAttribute('startLine') || !$node->hasAttribute('endLine')) {
  160. return null;
  161. }
  162. $start = $node->getStartLine();
  163. $end = $node->getEndLine();
  164. if ($node->hasAttribute('startFilePos') && $node->hasAttribute('endFilePos')
  165. && null !== $this->code
  166. ) {
  167. $start .= ':' . $this->toColumn($this->code, $node->getStartFilePos());
  168. $end .= ':' . $this->toColumn($this->code, $node->getEndFilePos());
  169. }
  170. return "[$start - $end]";
  171. }
  172. // Copied from Error class
  173. private function toColumn($code, $pos) {
  174. if ($pos > strlen($code)) {
  175. throw new \RuntimeException('Invalid position information');
  176. }
  177. $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
  178. if (false === $lineStartPos) {
  179. $lineStartPos = -1;
  180. }
  181. return $pos - $lineStartPos;
  182. }
  183. }