Default.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. class PHPParser_PrettyPrinter_Default extends PHPParser_PrettyPrinterAbstract
  3. {
  4. // Special nodes
  5. public function pParam(PHPParser_Node_Param $node) {
  6. return ($node->type ? (is_string($node->type) ? $node->type : $this->p($node->type)) . ' ' : '')
  7. . ($node->byRef ? '&' : '')
  8. . '$' . $node->name
  9. . ($node->default ? ' = ' . $this->p($node->default) : '');
  10. }
  11. public function pArg(PHPParser_Node_Arg $node) {
  12. return ($node->byRef ? '&' : '') . $this->p($node->value);
  13. }
  14. public function pConst(PHPParser_Node_Const $node) {
  15. return $node->name . ' = ' . $this->p($node->value);
  16. }
  17. // Names
  18. public function pName(PHPParser_Node_Name $node) {
  19. return implode('\\', $node->parts);
  20. }
  21. public function pName_FullyQualified(PHPParser_Node_Name_FullyQualified $node) {
  22. return '\\' . implode('\\', $node->parts);
  23. }
  24. public function pName_Relative(PHPParser_Node_Name_Relative $node) {
  25. return 'namespace\\' . implode('\\', $node->parts);
  26. }
  27. // Magic Constants
  28. public function pScalar_ClassConst(PHPParser_Node_Scalar_ClassConst $node) {
  29. return '__CLASS__';
  30. }
  31. public function pScalar_TraitConst(PHPParser_Node_Scalar_TraitConst $node) {
  32. return '__TRAIT__';
  33. }
  34. public function pScalar_DirConst(PHPParser_Node_Scalar_DirConst $node) {
  35. return '__DIR__';
  36. }
  37. public function pScalar_FileConst(PHPParser_Node_Scalar_FileConst $node) {
  38. return '__FILE__';
  39. }
  40. public function pScalar_FuncConst(PHPParser_Node_Scalar_FuncConst $node) {
  41. return '__FUNCTION__';
  42. }
  43. public function pScalar_LineConst(PHPParser_Node_Scalar_LineConst $node) {
  44. return '__LINE__';
  45. }
  46. public function pScalar_MethodConst(PHPParser_Node_Scalar_MethodConst $node) {
  47. return '__METHOD__';
  48. }
  49. public function pScalar_NSConst(PHPParser_Node_Scalar_NSConst $node) {
  50. return '__NAMESPACE__';
  51. }
  52. // Scalars
  53. public function pScalar_String(PHPParser_Node_Scalar_String $node) {
  54. return '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
  55. }
  56. public function pScalar_Encapsed(PHPParser_Node_Scalar_Encapsed $node) {
  57. return '"' . $this->pEncapsList($node->parts, '"') . '"';
  58. }
  59. public function pScalar_LNumber(PHPParser_Node_Scalar_LNumber $node) {
  60. return (string) $node->value;
  61. }
  62. public function pScalar_DNumber(PHPParser_Node_Scalar_DNumber $node) {
  63. $stringValue = (string) $node->value;
  64. // ensure that number is really printed as float
  65. return ctype_digit($stringValue) ? $stringValue . '.0' : $stringValue;
  66. }
  67. // Assignments
  68. public function pExpr_Assign(PHPParser_Node_Expr_Assign $node) {
  69. return $this->pInfixOp('Expr_Assign', $node->var, ' = ', $node->expr);
  70. }
  71. public function pExpr_AssignRef(PHPParser_Node_Expr_AssignRef $node) {
  72. return $this->pInfixOp('Expr_AssignRef', $node->var, ' =& ', $node->expr);
  73. }
  74. public function pExpr_AssignPlus(PHPParser_Node_Expr_AssignPlus $node) {
  75. return $this->pInfixOp('Expr_AssignPlus', $node->var, ' += ', $node->expr);
  76. }
  77. public function pExpr_AssignMinus(PHPParser_Node_Expr_AssignMinus $node) {
  78. return $this->pInfixOp('Expr_AssignMinus', $node->var, ' -= ', $node->expr);
  79. }
  80. public function pExpr_AssignMul(PHPParser_Node_Expr_AssignMul $node) {
  81. return $this->pInfixOp('Expr_AssignMul', $node->var, ' *= ', $node->expr);
  82. }
  83. public function pExpr_AssignDiv(PHPParser_Node_Expr_AssignDiv $node) {
  84. return $this->pInfixOp('Expr_AssignDiv', $node->var, ' /= ', $node->expr);
  85. }
  86. public function pExpr_AssignConcat(PHPParser_Node_Expr_AssignConcat $node) {
  87. return $this->pInfixOp('Expr_AssignConcat', $node->var, ' .= ', $node->expr);
  88. }
  89. public function pExpr_AssignMod(PHPParser_Node_Expr_AssignMod $node) {
  90. return $this->pInfixOp('Expr_AssignMod', $node->var, ' %= ', $node->expr);
  91. }
  92. public function pExpr_AssignBitwiseAnd(PHPParser_Node_Expr_AssignBitwiseAnd $node) {
  93. return $this->pInfixOp('Expr_AssignBitwiseAnd', $node->var, ' &= ', $node->expr);
  94. }
  95. public function pExpr_AssignBitwiseOr(PHPParser_Node_Expr_AssignBitwiseOr $node) {
  96. return $this->pInfixOp('Expr_AssignBitwiseOr', $node->var, ' |= ', $node->expr);
  97. }
  98. public function pExpr_AssignBitwiseXor(PHPParser_Node_Expr_AssignBitwiseXor $node) {
  99. return $this->pInfixOp('Expr_AssignBitwiseXor', $node->var, ' ^= ', $node->expr);
  100. }
  101. public function pExpr_AssignShiftLeft(PHPParser_Node_Expr_AssignShiftLeft $node) {
  102. return $this->pInfixOp('Expr_AssignShiftLeft', $node->var, ' <<= ', $node->expr);
  103. }
  104. public function pExpr_AssignShiftRight(PHPParser_Node_Expr_AssignShiftRight $node) {
  105. return $this->pInfixOp('Expr_AssignShiftRight', $node->var, ' >>= ', $node->expr);
  106. }
  107. // Binary expressions
  108. public function pExpr_Plus(PHPParser_Node_Expr_Plus $node) {
  109. return $this->pInfixOp('Expr_Plus', $node->left, ' + ', $node->right);
  110. }
  111. public function pExpr_Minus(PHPParser_Node_Expr_Minus $node) {
  112. return $this->pInfixOp('Expr_Minus', $node->left, ' - ', $node->right);
  113. }
  114. public function pExpr_Mul(PHPParser_Node_Expr_Mul $node) {
  115. return $this->pInfixOp('Expr_Mul', $node->left, ' * ', $node->right);
  116. }
  117. public function pExpr_Div(PHPParser_Node_Expr_Div $node) {
  118. return $this->pInfixOp('Expr_Div', $node->left, ' / ', $node->right);
  119. }
  120. public function pExpr_Concat(PHPParser_Node_Expr_Concat $node) {
  121. return $this->pInfixOp('Expr_Concat', $node->left, ' . ', $node->right);
  122. }
  123. public function pExpr_Mod(PHPParser_Node_Expr_Mod $node) {
  124. return $this->pInfixOp('Expr_Mod', $node->left, ' % ', $node->right);
  125. }
  126. public function pExpr_BooleanAnd(PHPParser_Node_Expr_BooleanAnd $node) {
  127. return $this->pInfixOp('Expr_BooleanAnd', $node->left, ' && ', $node->right);
  128. }
  129. public function pExpr_BooleanOr(PHPParser_Node_Expr_BooleanOr $node) {
  130. return $this->pInfixOp('Expr_BooleanOr', $node->left, ' || ', $node->right);
  131. }
  132. public function pExpr_BitwiseAnd(PHPParser_Node_Expr_BitwiseAnd $node) {
  133. return $this->pInfixOp('Expr_BitwiseAnd', $node->left, ' & ', $node->right);
  134. }
  135. public function pExpr_BitwiseOr(PHPParser_Node_Expr_BitwiseOr $node) {
  136. return $this->pInfixOp('Expr_BitwiseOr', $node->left, ' | ', $node->right);
  137. }
  138. public function pExpr_BitwiseXor(PHPParser_Node_Expr_BitwiseXor $node) {
  139. return $this->pInfixOp('Expr_BitwiseXor', $node->left, ' ^ ', $node->right);
  140. }
  141. public function pExpr_ShiftLeft(PHPParser_Node_Expr_ShiftLeft $node) {
  142. return $this->pInfixOp('Expr_ShiftLeft', $node->left, ' << ', $node->right);
  143. }
  144. public function pExpr_ShiftRight(PHPParser_Node_Expr_ShiftRight $node) {
  145. return $this->pInfixOp('Expr_ShiftRight', $node->left, ' >> ', $node->right);
  146. }
  147. public function pExpr_LogicalAnd(PHPParser_Node_Expr_LogicalAnd $node) {
  148. return $this->pInfixOp('Expr_LogicalAnd', $node->left, ' and ', $node->right);
  149. }
  150. public function pExpr_LogicalOr(PHPParser_Node_Expr_LogicalOr $node) {
  151. return $this->pInfixOp('Expr_LogicalOr', $node->left, ' or ', $node->right);
  152. }
  153. public function pExpr_LogicalXor(PHPParser_Node_Expr_LogicalXor $node) {
  154. return $this->pInfixOp('Expr_LogicalXor', $node->left, ' xor ', $node->right);
  155. }
  156. public function pExpr_Equal(PHPParser_Node_Expr_Equal $node) {
  157. return $this->pInfixOp('Expr_Equal', $node->left, ' == ', $node->right);
  158. }
  159. public function pExpr_NotEqual(PHPParser_Node_Expr_NotEqual $node) {
  160. return $this->pInfixOp('Expr_NotEqual', $node->left, ' != ', $node->right);
  161. }
  162. public function pExpr_Identical(PHPParser_Node_Expr_Identical $node) {
  163. return $this->pInfixOp('Expr_Identical', $node->left, ' === ', $node->right);
  164. }
  165. public function pExpr_NotIdentical(PHPParser_Node_Expr_NotIdentical $node) {
  166. return $this->pInfixOp('Expr_NotIdentical', $node->left, ' !== ', $node->right);
  167. }
  168. public function pExpr_Greater(PHPParser_Node_Expr_Greater $node) {
  169. return $this->pInfixOp('Expr_Greater', $node->left, ' > ', $node->right);
  170. }
  171. public function pExpr_GreaterOrEqual(PHPParser_Node_Expr_GreaterOrEqual $node) {
  172. return $this->pInfixOp('Expr_GreaterOrEqual', $node->left, ' >= ', $node->right);
  173. }
  174. public function pExpr_Smaller(PHPParser_Node_Expr_Smaller $node) {
  175. return $this->pInfixOp('Expr_Smaller', $node->left, ' < ', $node->right);
  176. }
  177. public function pExpr_SmallerOrEqual(PHPParser_Node_Expr_SmallerOrEqual $node) {
  178. return $this->pInfixOp('Expr_SmallerOrEqual', $node->left, ' <= ', $node->right);
  179. }
  180. public function pExpr_Instanceof(PHPParser_Node_Expr_Instanceof $node) {
  181. return $this->pInfixOp('Expr_Instanceof', $node->expr, ' instanceof ', $node->class);
  182. }
  183. // Unary expressions
  184. public function pExpr_BooleanNot(PHPParser_Node_Expr_BooleanNot $node) {
  185. return $this->pPrefixOp('Expr_BooleanNot', '!', $node->expr);
  186. }
  187. public function pExpr_BitwiseNot(PHPParser_Node_Expr_BitwiseNot $node) {
  188. return $this->pPrefixOp('Expr_BitwiseNot', '~', $node->expr);
  189. }
  190. public function pExpr_UnaryMinus(PHPParser_Node_Expr_UnaryMinus $node) {
  191. return $this->pPrefixOp('Expr_UnaryMinus', '-', $node->expr);
  192. }
  193. public function pExpr_UnaryPlus(PHPParser_Node_Expr_UnaryPlus $node) {
  194. return $this->pPrefixOp('Expr_UnaryPlus', '+', $node->expr);
  195. }
  196. public function pExpr_PreInc(PHPParser_Node_Expr_PreInc $node) {
  197. return $this->pPrefixOp('Expr_PreInc', '++', $node->var);
  198. }
  199. public function pExpr_PreDec(PHPParser_Node_Expr_PreDec $node) {
  200. return $this->pPrefixOp('Expr_PreDec', '--', $node->var);
  201. }
  202. public function pExpr_PostInc(PHPParser_Node_Expr_PostInc $node) {
  203. return $this->pPostfixOp('Expr_PostInc', $node->var, '++');
  204. }
  205. public function pExpr_PostDec(PHPParser_Node_Expr_PostDec $node) {
  206. return $this->pPostfixOp('Expr_PostDec', $node->var, '--');
  207. }
  208. public function pExpr_ErrorSuppress(PHPParser_Node_Expr_ErrorSuppress $node) {
  209. return $this->pPrefixOp('Expr_ErrorSuppress', '@', $node->expr);
  210. }
  211. // Casts
  212. public function pExpr_Cast_Int(PHPParser_Node_Expr_Cast_Int $node) {
  213. return $this->pPrefixOp('Expr_Cast_Int', '(int) ', $node->expr);
  214. }
  215. public function pExpr_Cast_Double(PHPParser_Node_Expr_Cast_Double $node) {
  216. return $this->pPrefixOp('Expr_Cast_Double', '(double) ', $node->expr);
  217. }
  218. public function pExpr_Cast_String(PHPParser_Node_Expr_Cast_String $node) {
  219. return $this->pPrefixOp('Expr_Cast_String', '(string) ', $node->expr);
  220. }
  221. public function pExpr_Cast_Array(PHPParser_Node_Expr_Cast_Array $node) {
  222. return $this->pPrefixOp('Expr_Cast_Array', '(array) ', $node->expr);
  223. }
  224. public function pExpr_Cast_Object(PHPParser_Node_Expr_Cast_Object $node) {
  225. return $this->pPrefixOp('Expr_Cast_Object', '(object) ', $node->expr);
  226. }
  227. public function pExpr_Cast_Bool(PHPParser_Node_Expr_Cast_Bool $node) {
  228. return $this->pPrefixOp('Expr_Cast_Bool', '(bool) ', $node->expr);
  229. }
  230. public function pExpr_Cast_Unset(PHPParser_Node_Expr_Cast_Unset $node) {
  231. return $this->pPrefixOp('Expr_Cast_Unset', '(unset) ', $node->expr);
  232. }
  233. // Function calls and similar constructs
  234. public function pExpr_FuncCall(PHPParser_Node_Expr_FuncCall $node) {
  235. return $this->p($node->name) . '(' . $this->pCommaSeparated($node->args) . ')';
  236. }
  237. public function pExpr_MethodCall(PHPParser_Node_Expr_MethodCall $node) {
  238. return $this->pVarOrNewExpr($node->var) . '->' . $this->pObjectProperty($node->name)
  239. . '(' . $this->pCommaSeparated($node->args) . ')';
  240. }
  241. public function pExpr_StaticCall(PHPParser_Node_Expr_StaticCall $node) {
  242. return $this->p($node->class) . '::'
  243. . ($node->name instanceof PHPParser_Node_Expr
  244. ? ($node->name instanceof PHPParser_Node_Expr_Variable
  245. || $node->name instanceof PHPParser_Node_Expr_ArrayDimFetch
  246. ? $this->p($node->name)
  247. : '{' . $this->p($node->name) . '}')
  248. : $node->name)
  249. . '(' . $this->pCommaSeparated($node->args) . ')';
  250. }
  251. public function pExpr_Empty(PHPParser_Node_Expr_Empty $node) {
  252. return 'empty(' . $this->p($node->expr) . ')';
  253. }
  254. public function pExpr_Isset(PHPParser_Node_Expr_Isset $node) {
  255. return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
  256. }
  257. public function pExpr_Print(PHPParser_Node_Expr_Print $node) {
  258. return 'print ' . $this->p($node->expr);
  259. }
  260. public function pExpr_Eval(PHPParser_Node_Expr_Eval $node) {
  261. return 'eval(' . $this->p($node->expr) . ')';
  262. }
  263. public function pExpr_Include(PHPParser_Node_Expr_Include $node) {
  264. static $map = array(
  265. PHPParser_Node_Expr_Include::TYPE_INCLUDE => 'include',
  266. PHPParser_Node_Expr_Include::TYPE_INCLUDE_ONCE => 'include_once',
  267. PHPParser_Node_Expr_Include::TYPE_REQUIRE => 'require',
  268. PHPParser_Node_Expr_Include::TYPE_REQUIRE_ONCE => 'require_once',
  269. );
  270. return $map[$node->type] . ' ' . $this->p($node->expr);
  271. }
  272. public function pExpr_List(PHPParser_Node_Expr_List $node) {
  273. $pList = array();
  274. foreach ($node->vars as $var) {
  275. if (null === $var) {
  276. $pList[] = '';
  277. } else {
  278. $pList[] = $this->p($var);
  279. }
  280. }
  281. return 'list(' . implode(', ', $pList) . ')';
  282. }
  283. // Other
  284. public function pExpr_Variable(PHPParser_Node_Expr_Variable $node) {
  285. if ($node->name instanceof PHPParser_Node_Expr) {
  286. return '${' . $this->p($node->name) . '}';
  287. } else {
  288. return '$' . $node->name;
  289. }
  290. }
  291. public function pExpr_Array(PHPParser_Node_Expr_Array $node) {
  292. return 'array(' . $this->pCommaSeparated($node->items) . ')';
  293. }
  294. public function pExpr_ArrayItem(PHPParser_Node_Expr_ArrayItem $node) {
  295. return (null !== $node->key ? $this->p($node->key) . ' => ' : '')
  296. . ($node->byRef ? '&' : '') . $this->p($node->value);
  297. }
  298. public function pExpr_ArrayDimFetch(PHPParser_Node_Expr_ArrayDimFetch $node) {
  299. return $this->pVarOrNewExpr($node->var)
  300. . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
  301. }
  302. public function pExpr_ConstFetch(PHPParser_Node_Expr_ConstFetch $node) {
  303. return $this->p($node->name);
  304. }
  305. public function pExpr_ClassConstFetch(PHPParser_Node_Expr_ClassConstFetch $node) {
  306. return $this->p($node->class) . '::' . $node->name;
  307. }
  308. public function pExpr_PropertyFetch(PHPParser_Node_Expr_PropertyFetch $node) {
  309. return $this->pVarOrNewExpr($node->var) . '->' . $this->pObjectProperty($node->name);
  310. }
  311. public function pExpr_StaticPropertyFetch(PHPParser_Node_Expr_StaticPropertyFetch $node) {
  312. return $this->p($node->class) . '::$' . $this->pObjectProperty($node->name);
  313. }
  314. public function pExpr_ShellExec(PHPParser_Node_Expr_ShellExec $node) {
  315. return '`' . $this->pEncapsList($node->parts, '`') . '`';
  316. }
  317. public function pExpr_Closure(PHPParser_Node_Expr_Closure $node) {
  318. return ($node->static ? 'static ' : '')
  319. . 'function ' . ($node->byRef ? '&' : '')
  320. . '(' . $this->pCommaSeparated($node->params) . ')'
  321. . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')': '')
  322. . ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  323. }
  324. public function pExpr_ClosureUse(PHPParser_Node_Expr_ClosureUse $node) {
  325. return ($node->byRef ? '&' : '') . '$' . $node->var;
  326. }
  327. public function pExpr_New(PHPParser_Node_Expr_New $node) {
  328. return 'new ' . $this->p($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
  329. }
  330. public function pExpr_Clone(PHPParser_Node_Expr_Clone $node) {
  331. return 'clone ' . $this->p($node->expr);
  332. }
  333. public function pExpr_Ternary(PHPParser_Node_Expr_Ternary $node) {
  334. // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
  335. // this is okay because the part between ? and : never needs parentheses.
  336. return $this->pInfixOp('Expr_Ternary',
  337. $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else
  338. );
  339. }
  340. public function pExpr_Exit(PHPParser_Node_Expr_Exit $node) {
  341. return 'die' . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
  342. }
  343. public function pExpr_Yield(PHPParser_Node_Expr_Yield $node) {
  344. if ($node->value === null) {
  345. return 'yield';
  346. } else {
  347. // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary
  348. return '(yield '
  349. . ($node->key !== null ? $this->p($node->key) . ' => ' : '')
  350. . $this->p($node->value)
  351. . ')';
  352. }
  353. }
  354. // Declarations
  355. public function pStmt_Namespace(PHPParser_Node_Stmt_Namespace $node) {
  356. if ($this->canUseSemicolonNamespaces) {
  357. return 'namespace ' . $this->p($node->name) . ';' . "\n\n" . $this->pStmts($node->stmts, false);
  358. } else {
  359. return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
  360. . ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  361. }
  362. }
  363. public function pStmt_Use(PHPParser_Node_Stmt_Use $node) {
  364. return 'use ' . $this->pCommaSeparated($node->uses) . ';';
  365. }
  366. public function pStmt_UseUse(PHPParser_Node_Stmt_UseUse $node) {
  367. return $this->p($node->name)
  368. . ($node->name->getLast() !== $node->alias ? ' as ' . $node->alias : '');
  369. }
  370. public function pStmt_Interface(PHPParser_Node_Stmt_Interface $node) {
  371. return 'interface ' . $node->name
  372. . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
  373. . "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  374. }
  375. public function pStmt_Class(PHPParser_Node_Stmt_Class $node) {
  376. return $this->pModifiers($node->type)
  377. . 'class ' . $node->name
  378. . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
  379. . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
  380. . "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  381. }
  382. public function pStmt_Trait(PHPParser_Node_Stmt_Trait $node) {
  383. return 'trait ' . $node->name
  384. . "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  385. }
  386. public function pStmt_TraitUse(PHPParser_Node_Stmt_TraitUse $node) {
  387. return 'use ' . $this->pCommaSeparated($node->traits)
  388. . (empty($node->adaptations)
  389. ? ';'
  390. : ' {' . "\n" . $this->pStmts($node->adaptations) . "\n" . '}');
  391. }
  392. public function pStmt_TraitUseAdaptation_Precedence(PHPParser_Node_Stmt_TraitUseAdaptation_Precedence $node) {
  393. return $this->p($node->trait) . '::' . $node->method
  394. . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
  395. }
  396. public function pStmt_TraitUseAdaptation_Alias(PHPParser_Node_Stmt_TraitUseAdaptation_Alias $node) {
  397. return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
  398. . $node->method . ' as'
  399. . (null !== $node->newModifier ? ' ' . $this->pModifiers($node->newModifier) : '')
  400. . (null !== $node->newName ? ' ' . $node->newName : '')
  401. . ';';
  402. }
  403. public function pStmt_Property(PHPParser_Node_Stmt_Property $node) {
  404. return $this->pModifiers($node->type) . $this->pCommaSeparated($node->props) . ';';
  405. }
  406. public function pStmt_PropertyProperty(PHPParser_Node_Stmt_PropertyProperty $node) {
  407. return '$' . $node->name
  408. . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
  409. }
  410. public function pStmt_ClassMethod(PHPParser_Node_Stmt_ClassMethod $node) {
  411. return $this->pModifiers($node->type)
  412. . 'function ' . ($node->byRef ? '&' : '') . $node->name
  413. . '(' . $this->pCommaSeparated($node->params) . ')'
  414. . (null !== $node->stmts
  415. ? "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}'
  416. : ';');
  417. }
  418. public function pStmt_ClassConst(PHPParser_Node_Stmt_ClassConst $node) {
  419. return 'const ' . $this->pCommaSeparated($node->consts) . ';';
  420. }
  421. public function pStmt_Function(PHPParser_Node_Stmt_Function $node) {
  422. return 'function ' . ($node->byRef ? '&' : '') . $node->name
  423. . '(' . $this->pCommaSeparated($node->params) . ')'
  424. . "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  425. }
  426. public function pStmt_Const(PHPParser_Node_Stmt_Const $node) {
  427. return 'const ' . $this->pCommaSeparated($node->consts) . ';';
  428. }
  429. public function pStmt_Declare(PHPParser_Node_Stmt_Declare $node) {
  430. return 'declare (' . $this->pCommaSeparated($node->declares) . ') {'
  431. . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  432. }
  433. public function pStmt_DeclareDeclare(PHPParser_Node_Stmt_DeclareDeclare $node) {
  434. return $node->key . ' = ' . $this->p($node->value);
  435. }
  436. // Control flow
  437. public function pStmt_If(PHPParser_Node_Stmt_If $node) {
  438. return 'if (' . $this->p($node->cond) . ') {'
  439. . "\n" . $this->pStmts($node->stmts) . "\n" . '}'
  440. . $this->pImplode($node->elseifs)
  441. . (null !== $node->else ? $this->p($node->else) : '');
  442. }
  443. public function pStmt_Elseif(PHPParser_Node_Stmt_Elseif $node) {
  444. return ' elseif (' . $this->p($node->cond) . ') {'
  445. . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  446. }
  447. public function pStmt_Else(PHPParser_Node_Stmt_Else $node) {
  448. return ' else {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  449. }
  450. public function pStmt_For(PHPParser_Node_Stmt_For $node) {
  451. return 'for ('
  452. . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
  453. . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
  454. . $this->pCommaSeparated($node->loop)
  455. . ') {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  456. }
  457. public function pStmt_Foreach(PHPParser_Node_Stmt_Foreach $node) {
  458. return 'foreach (' . $this->p($node->expr) . ' as '
  459. . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
  460. . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
  461. . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  462. }
  463. public function pStmt_While(PHPParser_Node_Stmt_While $node) {
  464. return 'while (' . $this->p($node->cond) . ') {'
  465. . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  466. }
  467. public function pStmt_Do(PHPParser_Node_Stmt_Do $node) {
  468. return 'do {' . "\n" . $this->pStmts($node->stmts) . "\n"
  469. . '} while (' . $this->p($node->cond) . ');';
  470. }
  471. public function pStmt_Switch(PHPParser_Node_Stmt_Switch $node) {
  472. return 'switch (' . $this->p($node->cond) . ') {'
  473. . "\n" . $this->pStmts($node->cases) . "\n" . '}';
  474. }
  475. public function pStmt_Case(PHPParser_Node_Stmt_Case $node) {
  476. return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
  477. . ($node->stmts ? "\n" . $this->pStmts($node->stmts) : '');
  478. }
  479. public function pStmt_TryCatch(PHPParser_Node_Stmt_TryCatch $node) {
  480. return 'try {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}'
  481. . $this->pImplode($node->catches)
  482. . ($node->finallyStmts !== null
  483. ? ' finally {' . "\n" . $this->pStmts($node->finallyStmts) . "\n" . '}'
  484. : '');
  485. }
  486. public function pStmt_Catch(PHPParser_Node_Stmt_Catch $node) {
  487. return ' catch (' . $this->p($node->type) . ' $' . $node->var . ') {'
  488. . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
  489. }
  490. public function pStmt_Break(PHPParser_Node_Stmt_Break $node) {
  491. return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
  492. }
  493. public function pStmt_Continue(PHPParser_Node_Stmt_Continue $node) {
  494. return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
  495. }
  496. public function pStmt_Return(PHPParser_Node_Stmt_Return $node) {
  497. return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
  498. }
  499. public function pStmt_Throw(PHPParser_Node_Stmt_Throw $node) {
  500. return 'throw ' . $this->p($node->expr) . ';';
  501. }
  502. public function pStmt_Label(PHPParser_Node_Stmt_Label $node) {
  503. return $node->name . ':';
  504. }
  505. public function pStmt_Goto(PHPParser_Node_Stmt_Goto $node) {
  506. return 'goto ' . $node->name . ';';
  507. }
  508. // Other
  509. public function pStmt_Echo(PHPParser_Node_Stmt_Echo $node) {
  510. return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
  511. }
  512. public function pStmt_Static(PHPParser_Node_Stmt_Static $node) {
  513. return 'static ' . $this->pCommaSeparated($node->vars) . ';';
  514. }
  515. public function pStmt_Global(PHPParser_Node_Stmt_Global $node) {
  516. return 'global ' . $this->pCommaSeparated($node->vars) . ';';
  517. }
  518. public function pStmt_StaticVar(PHPParser_Node_Stmt_StaticVar $node) {
  519. return '$' . $node->name
  520. . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
  521. }
  522. public function pStmt_Unset(PHPParser_Node_Stmt_Unset $node) {
  523. return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
  524. }
  525. public function pStmt_InlineHTML(PHPParser_Node_Stmt_InlineHTML $node) {
  526. return '?>' . $this->pNoIndent("\n" . $node->value) . '<?php ';
  527. }
  528. public function pStmt_HaltCompiler(PHPParser_Node_Stmt_HaltCompiler $node) {
  529. return '__halt_compiler();' . $node->remaining;
  530. }
  531. // Helpers
  532. public function pObjectProperty($node) {
  533. if ($node instanceof PHPParser_Node_Expr) {
  534. return '{' . $this->p($node) . '}';
  535. } else {
  536. return $node;
  537. }
  538. }
  539. public function pModifiers($modifiers) {
  540. return ($modifiers & PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC ? 'public ' : '')
  541. . ($modifiers & PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED ? 'protected ' : '')
  542. . ($modifiers & PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE ? 'private ' : '')
  543. . ($modifiers & PHPParser_Node_Stmt_Class::MODIFIER_STATIC ? 'static ' : '')
  544. . ($modifiers & PHPParser_Node_Stmt_Class::MODIFIER_ABSTRACT ? 'abstract ' : '')
  545. . ($modifiers & PHPParser_Node_Stmt_Class::MODIFIER_FINAL ? 'final ' : '');
  546. }
  547. public function pEncapsList(array $encapsList, $quote) {
  548. $return = '';
  549. foreach ($encapsList as $element) {
  550. if (is_string($element)) {
  551. $return .= addcslashes($element, "\n\r\t\f\v$" . $quote . "\\");
  552. } else {
  553. $return .= '{' . $this->p($element) . '}';
  554. }
  555. }
  556. return $return;
  557. }
  558. public function pVarOrNewExpr(PHPParser_Node $node) {
  559. if ($node instanceof PHPParser_Node_Expr_New) {
  560. return '(' . $this->p($node) . ')';
  561. } else {
  562. return $this->p($node);
  563. }
  564. }
  565. }