PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PhpParser/PrettyPrinterAbstract.php

https://gitlab.com/x33n/PHP-Parser
PHP | 268 lines | 161 code | 29 blank | 78 comment | 11 complexity | c7437cd3dc4920d4cecaf42b3733a951 MD5 | raw file
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. use PhpParser\Node\Stmt;
  5. abstract class PrettyPrinterAbstract
  6. {
  7. protected $precedenceMap = array(
  8. // [precedence, associativity] where for the latter -1 is %left, 0 is %nonassoc and 1 is %right
  9. 'Expr_BinaryOp_Pow' => array( 0, 1),
  10. 'Expr_BitwiseNot' => array( 10, 1),
  11. 'Expr_PreInc' => array( 10, 1),
  12. 'Expr_PreDec' => array( 10, 1),
  13. 'Expr_PostInc' => array( 10, -1),
  14. 'Expr_PostDec' => array( 10, -1),
  15. 'Expr_UnaryPlus' => array( 10, 1),
  16. 'Expr_UnaryMinus' => array( 10, 1),
  17. 'Expr_Cast_Int' => array( 10, 1),
  18. 'Expr_Cast_Double' => array( 10, 1),
  19. 'Expr_Cast_String' => array( 10, 1),
  20. 'Expr_Cast_Array' => array( 10, 1),
  21. 'Expr_Cast_Object' => array( 10, 1),
  22. 'Expr_Cast_Bool' => array( 10, 1),
  23. 'Expr_Cast_Unset' => array( 10, 1),
  24. 'Expr_ErrorSuppress' => array( 10, 1),
  25. 'Expr_Instanceof' => array( 20, 0),
  26. 'Expr_BooleanNot' => array( 30, 1),
  27. 'Expr_BinaryOp_Mul' => array( 40, -1),
  28. 'Expr_BinaryOp_Div' => array( 40, -1),
  29. 'Expr_BinaryOp_Mod' => array( 40, -1),
  30. 'Expr_BinaryOp_Plus' => array( 50, -1),
  31. 'Expr_BinaryOp_Minus' => array( 50, -1),
  32. 'Expr_BinaryOp_Concat' => array( 50, -1),
  33. 'Expr_BinaryOp_ShiftLeft' => array( 60, -1),
  34. 'Expr_BinaryOp_ShiftRight' => array( 60, -1),
  35. 'Expr_BinaryOp_Smaller' => array( 70, 0),
  36. 'Expr_BinaryOp_SmallerOrEqual' => array( 70, 0),
  37. 'Expr_BinaryOp_Greater' => array( 70, 0),
  38. 'Expr_BinaryOp_GreaterOrEqual' => array( 70, 0),
  39. 'Expr_BinaryOp_Equal' => array( 80, 0),
  40. 'Expr_BinaryOp_NotEqual' => array( 80, 0),
  41. 'Expr_BinaryOp_Identical' => array( 80, 0),
  42. 'Expr_BinaryOp_NotIdentical' => array( 80, 0),
  43. 'Expr_BinaryOp_Spaceship' => array( 80, 0),
  44. 'Expr_BinaryOp_BitwiseAnd' => array( 90, -1),
  45. 'Expr_BinaryOp_BitwiseXor' => array(100, -1),
  46. 'Expr_BinaryOp_BitwiseOr' => array(110, -1),
  47. 'Expr_BinaryOp_BooleanAnd' => array(120, -1),
  48. 'Expr_BinaryOp_BooleanOr' => array(130, -1),
  49. 'Expr_BinaryOp_Coalesce' => array(140, 1),
  50. 'Expr_Ternary' => array(150, -1),
  51. // parser uses %left for assignments, but they really behave as %right
  52. 'Expr_Assign' => array(160, 1),
  53. 'Expr_AssignRef' => array(160, 1),
  54. 'Expr_AssignOp_Plus' => array(160, 1),
  55. 'Expr_AssignOp_Minus' => array(160, 1),
  56. 'Expr_AssignOp_Mul' => array(160, 1),
  57. 'Expr_AssignOp_Div' => array(160, 1),
  58. 'Expr_AssignOp_Concat' => array(160, 1),
  59. 'Expr_AssignOp_Mod' => array(160, 1),
  60. 'Expr_AssignOp_BitwiseAnd' => array(160, 1),
  61. 'Expr_AssignOp_BitwiseOr' => array(160, 1),
  62. 'Expr_AssignOp_BitwiseXor' => array(160, 1),
  63. 'Expr_AssignOp_ShiftLeft' => array(160, 1),
  64. 'Expr_AssignOp_ShiftRight' => array(160, 1),
  65. 'Expr_AssignOp_Pow' => array(160, 1),
  66. 'Expr_BinaryOp_LogicalAnd' => array(170, -1),
  67. 'Expr_BinaryOp_LogicalXor' => array(180, -1),
  68. 'Expr_BinaryOp_LogicalOr' => array(190, -1),
  69. 'Expr_Include' => array(200, -1),
  70. );
  71. protected $noIndentToken;
  72. protected $canUseSemicolonNamespaces;
  73. public function __construct() {
  74. $this->noIndentToken = '_NO_INDENT_' . mt_rand();
  75. }
  76. /**
  77. * Pretty prints an array of statements.
  78. *
  79. * @param Node[] $stmts Array of statements
  80. *
  81. * @return string Pretty printed statements
  82. */
  83. public function prettyPrint(array $stmts) {
  84. $this->preprocessNodes($stmts);
  85. return ltrim(str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false)));
  86. }
  87. /**
  88. * Pretty prints an expression.
  89. *
  90. * @param Expr $node Expression node
  91. *
  92. * @return string Pretty printed node
  93. */
  94. public function prettyPrintExpr(Expr $node) {
  95. return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
  96. }
  97. /**
  98. * Pretty prints a file of statements (includes the opening <?php tag if it is required).
  99. *
  100. * @param Node[] $stmts Array of statements
  101. *
  102. * @return string Pretty printed statements
  103. */
  104. public function prettyPrintFile(array $stmts) {
  105. $p = rtrim($this->prettyPrint($stmts));
  106. $p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
  107. $p = preg_replace('/<\?php$/', '', $p);
  108. if (!$count) {
  109. $p = "<?php\n\n" . $p;
  110. }
  111. return $p;
  112. }
  113. /**
  114. * Preprocesses the top-level nodes to initialize pretty printer state.
  115. *
  116. * @param Node[] $nodes Array of nodes
  117. */
  118. protected function preprocessNodes(array $nodes) {
  119. /* We can use semicolon-namespaces unless there is a global namespace declaration */
  120. $this->canUseSemicolonNamespaces = true;
  121. foreach ($nodes as $node) {
  122. if ($node instanceof Stmt\Namespace_ && null === $node->name) {
  123. $this->canUseSemicolonNamespaces = false;
  124. }
  125. }
  126. }
  127. /**
  128. * Pretty prints an array of nodes (statements) and indents them optionally.
  129. *
  130. * @param Node[] $nodes Array of nodes
  131. * @param bool $indent Whether to indent the printed nodes
  132. *
  133. * @return string Pretty printed statements
  134. */
  135. protected function pStmts(array $nodes, $indent = true) {
  136. $result = '';
  137. foreach ($nodes as $node) {
  138. $result .= "\n"
  139. . $this->pComments($node->getAttribute('comments', array()))
  140. . $this->p($node)
  141. . ($node instanceof Expr ? ';' : '');
  142. }
  143. if ($indent) {
  144. return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
  145. } else {
  146. return $result;
  147. }
  148. }
  149. /**
  150. * Pretty prints a node.
  151. *
  152. * @param Node $node Node to be pretty printed
  153. *
  154. * @return string Pretty printed node
  155. */
  156. protected function p(Node $node) {
  157. return $this->{'p' . $node->getType()}($node);
  158. }
  159. protected function pInfixOp($type, Node $leftNode, $operatorString, Node $rightNode) {
  160. list($precedence, $associativity) = $this->precedenceMap[$type];
  161. return $this->pPrec($leftNode, $precedence, $associativity, -1)
  162. . $operatorString
  163. . $this->pPrec($rightNode, $precedence, $associativity, 1);
  164. }
  165. protected function pPrefixOp($type, $operatorString, Node $node) {
  166. list($precedence, $associativity) = $this->precedenceMap[$type];
  167. return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
  168. }
  169. protected function pPostfixOp($type, Node $node, $operatorString) {
  170. list($precedence, $associativity) = $this->precedenceMap[$type];
  171. return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
  172. }
  173. /**
  174. * Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
  175. *
  176. * @param Node $node Node to pretty print
  177. * @param int $parentPrecedence Precedence of the parent operator
  178. * @param int $parentAssociativity Associativity of parent operator
  179. * (-1 is left, 0 is nonassoc, 1 is right)
  180. * @param int $childPosition Position of the node relative to the operator
  181. * (-1 is left, 1 is right)
  182. *
  183. * @return string The pretty printed node
  184. */
  185. protected function pPrec(Node $node, $parentPrecedence, $parentAssociativity, $childPosition) {
  186. $type = $node->getType();
  187. if (isset($this->precedenceMap[$type])) {
  188. $childPrecedence = $this->precedenceMap[$type][0];
  189. if ($childPrecedence > $parentPrecedence
  190. || ($parentPrecedence == $childPrecedence && $parentAssociativity != $childPosition)
  191. ) {
  192. return '(' . $this->{'p' . $type}($node) . ')';
  193. }
  194. }
  195. return $this->{'p' . $type}($node);
  196. }
  197. /**
  198. * Pretty prints an array of nodes and implodes the printed values.
  199. *
  200. * @param Node[] $nodes Array of Nodes to be printed
  201. * @param string $glue Character to implode with
  202. *
  203. * @return string Imploded pretty printed nodes
  204. */
  205. protected function pImplode(array $nodes, $glue = '') {
  206. $pNodes = array();
  207. foreach ($nodes as $node) {
  208. $pNodes[] = $this->p($node);
  209. }
  210. return implode($glue, $pNodes);
  211. }
  212. /**
  213. * Pretty prints an array of nodes and implodes the printed values with commas.
  214. *
  215. * @param Node[] $nodes Array of Nodes to be printed
  216. *
  217. * @return string Comma separated pretty printed nodes
  218. */
  219. protected function pCommaSeparated(array $nodes) {
  220. return $this->pImplode($nodes, ', ');
  221. }
  222. /**
  223. * Signals the pretty printer that a string shall not be indented.
  224. *
  225. * @param string $string Not to be indented string
  226. *
  227. * @return mixed String marked with $this->noIndentToken's.
  228. */
  229. protected function pNoIndent($string) {
  230. return str_replace("\n", "\n" . $this->noIndentToken, $string);
  231. }
  232. protected function pComments(array $comments) {
  233. $result = '';
  234. foreach ($comments as $comment) {
  235. $result .= $comment->getReformattedText() . "\n";
  236. }
  237. return $result;
  238. }
  239. }