PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php

https://gitlab.com/techniconline/kmc
PHP | 285 lines | 178 code | 29 blank | 78 comment | 11 complexity | 2e764d5065bad8981882e4b047d3cd2f 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_YieldFrom' => array(165, 1),
  67. 'Expr_Print' => array(168, 1),
  68. 'Expr_BinaryOp_LogicalAnd' => array(170, -1),
  69. 'Expr_BinaryOp_LogicalXor' => array(180, -1),
  70. 'Expr_BinaryOp_LogicalOr' => array(190, -1),
  71. 'Expr_Include' => array(200, -1),
  72. );
  73. protected $noIndentToken;
  74. protected $canUseSemicolonNamespaces;
  75. public function __construct()
  76. {
  77. $this->noIndentToken = '_NO_INDENT_' . mt_rand();
  78. }
  79. /**
  80. * Pretty prints an array of statements.
  81. *
  82. * @param Node[] $stmts Array of statements
  83. *
  84. * @return string Pretty printed statements
  85. */
  86. public function prettyPrint(array $stmts)
  87. {
  88. $this->preprocessNodes($stmts);
  89. return ltrim(str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false)));
  90. }
  91. /**
  92. * Pretty prints an expression.
  93. *
  94. * @param Expr $node Expression node
  95. *
  96. * @return string Pretty printed node
  97. */
  98. public function prettyPrintExpr(Expr $node)
  99. {
  100. return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
  101. }
  102. /**
  103. * Pretty prints a file of statements (includes the opening <?php tag if it is required).
  104. *
  105. * @param Node[] $stmts Array of statements
  106. *
  107. * @return string Pretty printed statements
  108. */
  109. public function prettyPrintFile(array $stmts)
  110. {
  111. $p = rtrim($this->prettyPrint($stmts));
  112. $p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
  113. $p = preg_replace('/<\?php$/', '', $p);
  114. if (!$count) {
  115. $p = "<?php\n\n" . $p;
  116. }
  117. return $p;
  118. }
  119. /**
  120. * Preprocesses the top-level nodes to initialize pretty printer state.
  121. *
  122. * @param Node[] $nodes Array of nodes
  123. */
  124. protected function preprocessNodes(array $nodes)
  125. {
  126. /* We can use semicolon-namespaces unless there is a global namespace declaration */
  127. $this->canUseSemicolonNamespaces = true;
  128. foreach ($nodes as $node) {
  129. if ($node instanceof Stmt\Namespace_ && null === $node->name) {
  130. $this->canUseSemicolonNamespaces = false;
  131. }
  132. }
  133. }
  134. /**
  135. * Pretty prints an array of nodes (statements) and indents them optionally.
  136. *
  137. * @param Node[] $nodes Array of nodes
  138. * @param bool $indent Whether to indent the printed nodes
  139. *
  140. * @return string Pretty printed statements
  141. */
  142. protected function pStmts(array $nodes, $indent = true)
  143. {
  144. $result = '';
  145. foreach ($nodes as $node) {
  146. $result .= "\n"
  147. . $this->pComments($node->getAttribute('comments', array()))
  148. . $this->p($node)
  149. . ($node instanceof Expr ? ';' : '');
  150. }
  151. if ($indent) {
  152. return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
  153. } else {
  154. return $result;
  155. }
  156. }
  157. /**
  158. * Pretty prints a node.
  159. *
  160. * @param Node $node Node to be pretty printed
  161. *
  162. * @return string Pretty printed node
  163. */
  164. protected function p(Node $node)
  165. {
  166. return $this->{'p' . $node->getType()}($node);
  167. }
  168. protected function pInfixOp($type, Node $leftNode, $operatorString, Node $rightNode)
  169. {
  170. list($precedence, $associativity) = $this->precedenceMap[$type];
  171. return $this->pPrec($leftNode, $precedence, $associativity, -1)
  172. . $operatorString
  173. . $this->pPrec($rightNode, $precedence, $associativity, 1);
  174. }
  175. protected function pPrefixOp($type, $operatorString, Node $node)
  176. {
  177. list($precedence, $associativity) = $this->precedenceMap[$type];
  178. return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
  179. }
  180. protected function pPostfixOp($type, Node $node, $operatorString)
  181. {
  182. list($precedence, $associativity) = $this->precedenceMap[$type];
  183. return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
  184. }
  185. /**
  186. * Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
  187. *
  188. * @param Node $node Node to pretty print
  189. * @param int $parentPrecedence Precedence of the parent operator
  190. * @param int $parentAssociativity Associativity of parent operator
  191. * (-1 is left, 0 is nonassoc, 1 is right)
  192. * @param int $childPosition Position of the node relative to the operator
  193. * (-1 is left, 1 is right)
  194. *
  195. * @return string The pretty printed node
  196. */
  197. protected function pPrec(Node $node, $parentPrecedence, $parentAssociativity, $childPosition)
  198. {
  199. $type = $node->getType();
  200. if (isset($this->precedenceMap[$type])) {
  201. $childPrecedence = $this->precedenceMap[$type][0];
  202. if ($childPrecedence > $parentPrecedence
  203. || ($parentPrecedence == $childPrecedence && $parentAssociativity != $childPosition)
  204. ) {
  205. return '(' . $this->{'p' . $type}($node) . ')';
  206. }
  207. }
  208. return $this->{'p' . $type}($node);
  209. }
  210. /**
  211. * Pretty prints an array of nodes and implodes the printed values.
  212. *
  213. * @param Node[] $nodes Array of Nodes to be printed
  214. * @param string $glue Character to implode with
  215. *
  216. * @return string Imploded pretty printed nodes
  217. */
  218. protected function pImplode(array $nodes, $glue = '')
  219. {
  220. $pNodes = array();
  221. foreach ($nodes as $node) {
  222. $pNodes[] = $this->p($node);
  223. }
  224. return implode($glue, $pNodes);
  225. }
  226. /**
  227. * Pretty prints an array of nodes and implodes the printed values with commas.
  228. *
  229. * @param Node[] $nodes Array of Nodes to be printed
  230. *
  231. * @return string Comma separated pretty printed nodes
  232. */
  233. protected function pCommaSeparated(array $nodes)
  234. {
  235. return $this->pImplode($nodes, ', ');
  236. }
  237. /**
  238. * Signals the pretty printer that a string shall not be indented.
  239. *
  240. * @param string $string Not to be indented string
  241. *
  242. * @return mixed String marked with $this->noIndentToken's.
  243. */
  244. protected function pNoIndent($string)
  245. {
  246. return str_replace("\n", "\n" . $this->noIndentToken, $string);
  247. }
  248. protected function pComments(array $comments)
  249. {
  250. $result = '';
  251. foreach ($comments as $comment) {
  252. $result .= $comment->getReformattedText() . "\n";
  253. }
  254. return $result;
  255. }
  256. }