No Description

Standard.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. <?php
  2. namespace PhpParser\PrettyPrinter;
  3. use PhpParser\PrettyPrinterAbstract;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Scalar;
  6. use PhpParser\Node\Scalar\MagicConst;
  7. use PhpParser\Node\Expr;
  8. use PhpParser\Node\Expr\AssignOp;
  9. use PhpParser\Node\Expr\BinaryOp;
  10. use PhpParser\Node\Expr\Cast;
  11. use PhpParser\Node\Stmt;
  12. use PhpParser\Node\Name;
  13. class Standard extends PrettyPrinterAbstract
  14. {
  15. // Special nodes
  16. public function pParam(Node\Param $node) {
  17. return ($node->type ? (is_string($node->type) ? $node->type : $this->p($node->type)) . ' ' : '')
  18. . ($node->byRef ? '&' : '')
  19. . ($node->variadic ? '... ' : '')
  20. . '$' . $node->name
  21. . ($node->default ? ' = ' . $this->p($node->default) : '');
  22. }
  23. public function pArg(Node\Arg $node) {
  24. return ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') . $this->p($node->value);
  25. }
  26. public function pConst(Node\Const_ $node) {
  27. return $node->name . ' = ' . $this->p($node->value);
  28. }
  29. // Names
  30. public function pName(Name $node) {
  31. return implode('\\', $node->parts);
  32. }
  33. public function pName_FullyQualified(Name\FullyQualified $node) {
  34. return '\\' . implode('\\', $node->parts);
  35. }
  36. public function pName_Relative(Name\Relative $node) {
  37. return 'namespace\\' . implode('\\', $node->parts);
  38. }
  39. // Magic Constants
  40. public function pScalar_MagicConst_Class(MagicConst\Class_ $node) {
  41. return '__CLASS__';
  42. }
  43. public function pScalar_MagicConst_Dir(MagicConst\Dir $node) {
  44. return '__DIR__';
  45. }
  46. public function pScalar_MagicConst_File(MagicConst\File $node) {
  47. return '__FILE__';
  48. }
  49. public function pScalar_MagicConst_Function(MagicConst\Function_ $node) {
  50. return '__FUNCTION__';
  51. }
  52. public function pScalar_MagicConst_Line(MagicConst\Line $node) {
  53. return '__LINE__';
  54. }
  55. public function pScalar_MagicConst_Method(MagicConst\Method $node) {
  56. return '__METHOD__';
  57. }
  58. public function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) {
  59. return '__NAMESPACE__';
  60. }
  61. public function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) {
  62. return '__TRAIT__';
  63. }
  64. // Scalars
  65. public function pScalar_String(Scalar\String $node) {
  66. return '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
  67. }
  68. public function pScalar_Encapsed(Scalar\Encapsed $node) {
  69. return '"' . $this->pEncapsList($node->parts, '"') . '"';
  70. }
  71. public function pScalar_LNumber(Scalar\LNumber $node) {
  72. return (string) $node->value;
  73. }
  74. public function pScalar_DNumber(Scalar\DNumber $node) {
  75. $stringValue = (string) $node->value;
  76. // ensure that number is really printed as float
  77. return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
  78. }
  79. // Assignments
  80. public function pExpr_Assign(Expr\Assign $node) {
  81. return $this->pInfixOp('Expr_Assign', $node->var, ' = ', $node->expr);
  82. }
  83. public function pExpr_AssignRef(Expr\AssignRef $node) {
  84. return $this->pInfixOp('Expr_AssignRef', $node->var, ' =& ', $node->expr);
  85. }
  86. public function pExpr_AssignOp_Plus(AssignOp\Plus $node) {
  87. return $this->pInfixOp('Expr_AssignOp_Plus', $node->var, ' += ', $node->expr);
  88. }
  89. public function pExpr_AssignOp_Minus(AssignOp\Minus $node) {
  90. return $this->pInfixOp('Expr_AssignOp_Minus', $node->var, ' -= ', $node->expr);
  91. }
  92. public function pExpr_AssignOp_Mul(AssignOp\Mul $node) {
  93. return $this->pInfixOp('Expr_AssignOp_Mul', $node->var, ' *= ', $node->expr);
  94. }
  95. public function pExpr_AssignOp_Div(AssignOp\Div $node) {
  96. return $this->pInfixOp('Expr_AssignOp_Div', $node->var, ' /= ', $node->expr);
  97. }
  98. public function pExpr_AssignOp_Concat(AssignOp\Concat $node) {
  99. return $this->pInfixOp('Expr_AssignOp_Concat', $node->var, ' .= ', $node->expr);
  100. }
  101. public function pExpr_AssignOp_Mod(AssignOp\Mod $node) {
  102. return $this->pInfixOp('Expr_AssignOp_Mod', $node->var, ' %= ', $node->expr);
  103. }
  104. public function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) {
  105. return $this->pInfixOp('Expr_AssignOp_BitwiseAnd', $node->var, ' &= ', $node->expr);
  106. }
  107. public function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) {
  108. return $this->pInfixOp('Expr_AssignOp_BitwiseOr', $node->var, ' |= ', $node->expr);
  109. }
  110. public function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) {
  111. return $this->pInfixOp('Expr_AssignOp_BitwiseXor', $node->var, ' ^= ', $node->expr);
  112. }
  113. public function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) {
  114. return $this->pInfixOp('Expr_AssignOp_ShiftLeft', $node->var, ' <<= ', $node->expr);
  115. }
  116. public function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) {
  117. return $this->pInfixOp('Expr_AssignOp_ShiftRight', $node->var, ' >>= ', $node->expr);
  118. }
  119. public function pExpr_AssignOp_Pow(AssignOp\Pow $node) {
  120. return $this->pInfixOp('Expr_AssignOp_Pow', $node->var, ' **= ', $node->expr);
  121. }
  122. // Binary expressions
  123. public function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) {
  124. return $this->pInfixOp('Expr_BinaryOp_Plus', $node->left, ' + ', $node->right);
  125. }
  126. public function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) {
  127. return $this->pInfixOp('Expr_BinaryOp_Minus', $node->left, ' - ', $node->right);
  128. }
  129. public function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) {
  130. return $this->pInfixOp('Expr_BinaryOp_Mul', $node->left, ' * ', $node->right);
  131. }
  132. public function pExpr_BinaryOp_Div(BinaryOp\Div $node) {
  133. return $this->pInfixOp('Expr_BinaryOp_Div', $node->left, ' / ', $node->right);
  134. }
  135. public function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) {
  136. return $this->pInfixOp('Expr_BinaryOp_Concat', $node->left, ' . ', $node->right);
  137. }
  138. public function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) {
  139. return $this->pInfixOp('Expr_BinaryOp_Mod', $node->left, ' % ', $node->right);
  140. }
  141. public function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) {
  142. return $this->pInfixOp('Expr_BinaryOp_BooleanAnd', $node->left, ' && ', $node->right);
  143. }
  144. public function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) {
  145. return $this->pInfixOp('Expr_BinaryOp_BooleanOr', $node->left, ' || ', $node->right);
  146. }
  147. public function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) {
  148. return $this->pInfixOp('Expr_BinaryOp_BitwiseAnd', $node->left, ' & ', $node->right);
  149. }
  150. public function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) {
  151. return $this->pInfixOp('Expr_BinaryOp_BitwiseOr', $node->left, ' | ', $node->right);
  152. }
  153. public function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) {
  154. return $this->pInfixOp('Expr_BinaryOp_BitwiseXor', $node->left, ' ^ ', $node->right);
  155. }
  156. public function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) {
  157. return $this->pInfixOp('Expr_BinaryOp_ShiftLeft', $node->left, ' << ', $node->right);
  158. }
  159. public function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) {
  160. return $this->pInfixOp('Expr_BinaryOp_ShiftRight', $node->left, ' >> ', $node->right);
  161. }
  162. public function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) {
  163. return $this->pInfixOp('Expr_BinaryOp_Pow', $node->left, ' ** ', $node->right);
  164. }
  165. public function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) {
  166. return $this->pInfixOp('Expr_BinaryOp_LogicalAnd', $node->left, ' and ', $node->right);
  167. }
  168. public function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) {
  169. return $this->pInfixOp('Expr_BinaryOp_LogicalOr', $node->left, ' or ', $node->right);
  170. }
  171. public function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) {
  172. return $this->pInfixOp('Expr_BinaryOp_LogicalXor', $node->left, ' xor ', $node->right);
  173. }
  174. public function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) {
  175. return $this->pInfixOp('Expr_BinaryOp_Equal', $node->left, ' == ', $node->right);
  176. }
  177. public function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) {
  178. return $this->pInfixOp('Expr_BinaryOp_NotEqual', $node->left, ' != ', $node->right);
  179. }
  180. public function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) {
  181. return $this->pInfixOp('Expr_BinaryOp_Identical', $node->left, ' === ', $node->right);
  182. }
  183. public function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) {
  184. return $this->pInfixOp('Expr_BinaryOp_NotIdentical', $node->left, ' !== ', $node->right);
  185. }
  186. public function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) {
  187. return $this->pInfixOp('Expr_BinaryOp_Greater', $node->left, ' > ', $node->right);
  188. }
  189. public function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) {
  190. return $this->pInfixOp('Expr_BinaryOp_GreaterOrEqual', $node->left, ' >= ', $node->right);
  191. }
  192. public function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) {
  193. return $this->pInfixOp('Expr_BinaryOp_Smaller', $node->left, ' < ', $node->right);
  194. }
  195. public function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) {
  196. return $this->pInfixOp('Expr_BinaryOp_SmallerOrEqual', $node->left, ' <= ', $node->right);
  197. }
  198. public function pExpr_Instanceof(Expr\Instanceof_ $node) {
  199. return $this->pInfixOp('Expr_Instanceof', $node->expr, ' instanceof ', $node->class);
  200. }
  201. // Unary expressions
  202. public function pExpr_BooleanNot(Expr\BooleanNot $node) {
  203. return $this->pPrefixOp('Expr_BooleanNot', '!', $node->expr);
  204. }
  205. public function pExpr_BitwiseNot(Expr\BitwiseNot $node) {
  206. return $this->pPrefixOp('Expr_BitwiseNot', '~', $node->expr);
  207. }
  208. public function pExpr_UnaryMinus(Expr\UnaryMinus $node) {
  209. return $this->pPrefixOp('Expr_UnaryMinus', '-', $node->expr);
  210. }
  211. public function pExpr_UnaryPlus(Expr\UnaryPlus $node) {
  212. return $this->pPrefixOp('Expr_UnaryPlus', '+', $node->expr);
  213. }
  214. public function pExpr_PreInc(Expr\PreInc $node) {
  215. return $this->pPrefixOp('Expr_PreInc', '++', $node->var);
  216. }
  217. public function pExpr_PreDec(Expr\PreDec $node) {
  218. return $this->pPrefixOp('Expr_PreDec', '--', $node->var);
  219. }
  220. public function pExpr_PostInc(Expr\PostInc $node) {
  221. return $this->pPostfixOp('Expr_PostInc', $node->var, '++');
  222. }
  223. public function pExpr_PostDec(Expr\PostDec $node) {
  224. return $this->pPostfixOp('Expr_PostDec', $node->var, '--');
  225. }
  226. public function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) {
  227. return $this->pPrefixOp('Expr_ErrorSuppress', '@', $node->expr);
  228. }
  229. // Casts
  230. public function pExpr_Cast_Int(Cast\Int $node) {
  231. return $this->pPrefixOp('Expr_Cast_Int', '(int) ', $node->expr);
  232. }
  233. public function pExpr_Cast_Double(Cast\Double $node) {
  234. return $this->pPrefixOp('Expr_Cast_Double', '(double) ', $node->expr);
  235. }
  236. public function pExpr_Cast_String(Cast\String $node) {
  237. return $this->pPrefixOp('Expr_Cast_String', '(string) ', $node->expr);
  238. }
  239. public function pExpr_Cast_Array(Cast\Array_ $node) {
  240. return $this->pPrefixOp('Expr_Cast_Array', '(array) ', $node->expr);
  241. }
  242. public function pExpr_Cast_Object(Cast\Object $node) {
  243. return $this->pPrefixOp('Expr_Cast_Object', '(object) ', $node->expr);
  244. }
  245. public function pExpr_Cast_Bool(Cast\Bool $node) {
  246. return $this->pPrefixOp('Expr_Cast_Bool', '(bool) ', $node->expr);
  247. }
  248. public function pExpr_Cast_Unset(Cast\Unset_ $node) {
  249. return $this->pPrefixOp('Expr_Cast_Unset', '(unset) ', $node->expr);
  250. }
  251. // Function calls and similar constructs
  252. public function pExpr_FuncCall(Expr\FuncCall $node) {
  253. return $this->p($node->name) . '(' . $this->pCommaSeparated($node->args) . ')';
  254. }
  255. public function pExpr_MethodCall(Expr\MethodCall $node) {
  256. return $this->pVarOrNewExpr($node->var) . '->' . $this->pObjectProperty($node->name)
  257. . '(' . $this->pCommaSeparated($node->args) . ')';
  258. }
  259. public function pExpr_StaticCall(Expr\StaticCall $node) {
  260. return $this->p($node->class) . '::'
  261. . ($node->name instanceof Expr
  262. ? ($node->name instanceof Expr\Variable
  263. || $node->name instanceof Expr\ArrayDimFetch
  264. ? $this->p($node->name)
  265. : '{' . $this->p($node->name) . '}')
  266. : $node->name)
  267. . '(' . $this->pCommaSeparated($node->args) . ')';
  268. }
  269. public function pExpr_Empty(Expr\Empty_ $node) {
  270. return 'empty(' . $this->p($node->expr) . ')';
  271. }
  272. public function pExpr_Isset(Expr\Isset_ $node) {
  273. return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
  274. }
  275. public function pExpr_Print(Expr\Print_ $node) {
  276. return 'print ' . $this->p($node->expr);
  277. }
  278. public function pExpr_Eval(Expr\Eval_ $node) {
  279. return 'eval(' . $this->p($node->expr) . ')';
  280. }
  281. public function pExpr_Include(Expr\Include_ $node) {
  282. static $map = array(
  283. Expr\Include_::TYPE_INCLUDE => 'include',
  284. Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
  285. Expr\Include_::TYPE_REQUIRE => 'require',
  286. Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
  287. );
  288. return $map[$node->type] . ' ' . $this->p($node->expr);
  289. }
  290. public function pExpr_List(Expr\List_ $node) {
  291. $pList = array();
  292. foreach ($node->vars as $var) {
  293. if (null === $var) {
  294. $pList[] = '';
  295. } else {
  296. $pList[] = $this->p($var);
  297. }
  298. }
  299. return 'list(' . implode(', ', $pList) . ')';
  300. }
  301. // Other
  302. public function pExpr_Variable(Expr\Variable $node) {
  303. if ($node->name instanceof Expr) {
  304. return '${' . $this->p($node->name) . '}';
  305. } else {
  306. return '$' . $node->name;
  307. }
  308. }
  309. public function pExpr_Array(Expr\Array_ $node) {
  310. return 'array(' . $this->pCommaSeparated($node->items) . ')';
  311. }
  312. public function pExpr_ArrayItem(Expr\ArrayItem $node) {
  313. return (null !== $node->key ? $this->p($node->key) . ' => ' : '')
  314. . ($node->byRef ? '&' : '') . $this->p($node->value);
  315. }
  316. public function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) {
  317. return $this->pVarOrNewExpr($node->var)
  318. . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
  319. }
  320. public function pExpr_ConstFetch(Expr\ConstFetch $node) {
  321. return $this->p($node->name);
  322. }
  323. public function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) {
  324. return $this->p($node->class) . '::' . $node->name;
  325. }
  326. public function pExpr_PropertyFetch(Expr\PropertyFetch $node) {
  327. return $this->pVarOrNewExpr($node->var) . '->' . $this->pObjectProperty($node->name);
  328. }
  329. public function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) {
  330. return $this->p($node->class) . '::$' . $this->pObjectProperty($node->name);
  331. }
  332. public function pExpr_ShellExec(Expr\ShellExec $node) {
  333. return '`' . $this->pEncapsList($node->parts, '`') . '`';
  334. }
  335. public function pExpr_Closure(Expr\Closure $node) {
  336. return ($node->static ? 'static ' : '')
  337. . 'function ' . ($node->byRef ? '&' : '')
  338. . '(' . $this->pCommaSeparated($node->params) . ')'
  339. . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')': '')
  340. . ' {' . $this->pStmts($node->stmts) . "\n" . '}';
  341. }
  342. public function pExpr_ClosureUse(Expr\ClosureUse $node) {
  343. return ($node->byRef ? '&' : '') . '$' . $node->var;
  344. }
  345. public function pExpr_New(Expr\New_ $node) {
  346. return 'new ' . $this->p($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
  347. }
  348. public function pExpr_Clone(Expr\Clone_ $node) {
  349. return 'clone ' . $this->p($node->expr);
  350. }
  351. public function pExpr_Ternary(Expr\Ternary $node) {
  352. // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
  353. // this is okay because the part between ? and : never needs parentheses.
  354. return $this->pInfixOp('Expr_Ternary',
  355. $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else
  356. );
  357. }
  358. public function pExpr_Exit(Expr\Exit_ $node) {
  359. return 'die' . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
  360. }
  361. public function pExpr_Yield(Expr\Yield_ $node) {
  362. if ($node->value === null) {
  363. return 'yield';
  364. } else {
  365. // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary
  366. return '(yield '
  367. . ($node->key !== null ? $this->p($node->key) . ' => ' : '')
  368. . $this->p($node->value)
  369. . ')';
  370. }
  371. }
  372. // Declarations
  373. public function pStmt_Namespace(Stmt\Namespace_ $node) {
  374. if ($this->canUseSemicolonNamespaces) {
  375. return 'namespace ' . $this->p($node->name) . ';' . "\n" . $this->pStmts($node->stmts, false);
  376. } else {
  377. return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
  378. . ' {' . $this->pStmts($node->stmts) . "\n" . '}';
  379. }
  380. }
  381. public function pStmt_Use(Stmt\Use_ $node) {
  382. return 'use '
  383. . ($node->type === Stmt\Use_::TYPE_FUNCTION ? 'function ' : '')
  384. . ($node->type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '')
  385. . $this->pCommaSeparated($node->uses) . ';';
  386. }
  387. public function pStmt_UseUse(Stmt\UseUse $node) {
  388. return $this->p($node->name)
  389. . ($node->name->getLast() !== $node->alias ? ' as ' . $node->alias : '');
  390. }
  391. public function pStmt_Interface(Stmt\Interface_ $node) {
  392. return 'interface ' . $node->name
  393. . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
  394. . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
  395. }
  396. public function pStmt_Class(Stmt\Class_ $node) {
  397. return $this->pModifiers($node->type)
  398. . 'class ' . $node->name
  399. . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
  400. . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
  401. . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
  402. }
  403. public function pStmt_Trait(Stmt\Trait_ $node) {
  404. return 'trait ' . $node->name
  405. . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
  406. }
  407. public function pStmt_TraitUse(Stmt\TraitUse $node) {
  408. return 'use ' . $this->pCommaSeparated($node->traits)
  409. . (empty($node->adaptations)
  410. ? ';'
  411. : ' {' . $this->pStmts($node->adaptations) . "\n" . '}');
  412. }
  413. public function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) {
  414. return $this->p($node->trait) . '::' . $node->method
  415. . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
  416. }
  417. public function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) {
  418. return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
  419. . $node->method . ' as'
  420. . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '')
  421. . (null !== $node->newName ? ' ' . $node->newName : '')
  422. . ';';
  423. }
  424. public function pStmt_Property(Stmt\Property $node) {
  425. return $this->pModifiers($node->type) . $this->pCommaSeparated($node->props) . ';';
  426. }
  427. public function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
  428. return '$' . $node->name
  429. . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
  430. }
  431. public function pStmt_ClassMethod(Stmt\ClassMethod $node) {
  432. return $this->pModifiers($node->type)
  433. . 'function ' . ($node->byRef ? '&' : '') . $node->name
  434. . '(' . $this->pCommaSeparated($node->params) . ')'
  435. . (null !== $node->stmts
  436. ? "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}'
  437. : ';');
  438. }
  439. public function pStmt_ClassConst(Stmt\ClassConst $node) {
  440. return 'const ' . $this->pCommaSeparated($node->consts) . ';';
  441. }
  442. public function pStmt_Function(Stmt\Function_ $node) {
  443. return 'function ' . ($node->byRef ? '&' : '') . $node->name
  444. . '(' . $this->pCommaSeparated($node->params) . ')'
  445. . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
  446. }
  447. public function pStmt_Const(Stmt\Const_ $node) {
  448. return 'const ' . $this->pCommaSeparated($node->consts) . ';';
  449. }
  450. public function pStmt_Declare(Stmt\Declare_ $node) {
  451. return 'declare (' . $this->pCommaSeparated($node->declares) . ') {'
  452. . $this->pStmts($node->stmts) . "\n" . '}';
  453. }
  454. public function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) {
  455. return $node->key . ' = ' . $this->p($node->value);
  456. }
  457. // Control flow
  458. public function pStmt_If(Stmt\If_ $node) {
  459. return 'if (' . $this->p($node->cond) . ') {'
  460. . $this->pStmts($node->stmts) . "\n" . '}'
  461. . $this->pImplode($node->elseifs)
  462. . (null !== $node->else ? $this->p($node->else) : '');
  463. }
  464. public function pStmt_ElseIf(Stmt\ElseIf_ $node) {
  465. return ' elseif (' . $this->p($node->cond) . ') {'
  466. . $this->pStmts($node->stmts) . "\n" . '}';
  467. }
  468. public function pStmt_Else(Stmt\Else_ $node) {
  469. return ' else {' . $this->pStmts($node->stmts) . "\n" . '}';
  470. }
  471. public function pStmt_For(Stmt\For_ $node) {
  472. return 'for ('
  473. . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
  474. . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
  475. . $this->pCommaSeparated($node->loop)
  476. . ') {' . $this->pStmts($node->stmts) . "\n" . '}';
  477. }
  478. public function pStmt_Foreach(Stmt\Foreach_ $node) {
  479. return 'foreach (' . $this->p($node->expr) . ' as '
  480. . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
  481. . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
  482. . $this->pStmts($node->stmts) . "\n" . '}';
  483. }
  484. public function pStmt_While(Stmt\While_ $node) {
  485. return 'while (' . $this->p($node->cond) . ') {'
  486. . $this->pStmts($node->stmts) . "\n" . '}';
  487. }
  488. public function pStmt_Do(Stmt\Do_ $node) {
  489. return 'do {' . $this->pStmts($node->stmts) . "\n"
  490. . '} while (' . $this->p($node->cond) . ');';
  491. }
  492. public function pStmt_Switch(Stmt\Switch_ $node) {
  493. return 'switch (' . $this->p($node->cond) . ') {'
  494. . $this->pStmts($node->cases) . "\n" . '}';
  495. }
  496. public function pStmt_Case(Stmt\Case_ $node) {
  497. return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
  498. . $this->pStmts($node->stmts);
  499. }
  500. public function pStmt_TryCatch(Stmt\TryCatch $node) {
  501. return 'try {' . $this->pStmts($node->stmts) . "\n" . '}'
  502. . $this->pImplode($node->catches)
  503. . ($node->finallyStmts !== null
  504. ? ' finally {' . $this->pStmts($node->finallyStmts) . "\n" . '}'
  505. : '');
  506. }
  507. public function pStmt_Catch(Stmt\Catch_ $node) {
  508. return ' catch (' . $this->p($node->type) . ' $' . $node->var . ') {'
  509. . $this->pStmts($node->stmts) . "\n" . '}';
  510. }
  511. public function pStmt_Break(Stmt\Break_ $node) {
  512. return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
  513. }
  514. public function pStmt_Continue(Stmt\Continue_ $node) {
  515. return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
  516. }
  517. public function pStmt_Return(Stmt\Return_ $node) {
  518. return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
  519. }
  520. public function pStmt_Throw(Stmt\Throw_ $node) {
  521. return 'throw ' . $this->p($node->expr) . ';';
  522. }
  523. public function pStmt_Label(Stmt\Label $node) {
  524. return $node->name . ':';
  525. }
  526. public function pStmt_Goto(Stmt\Goto_ $node) {
  527. return 'goto ' . $node->name . ';';
  528. }
  529. // Other
  530. public function pStmt_Echo(Stmt\Echo_ $node) {
  531. return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
  532. }
  533. public function pStmt_Static(Stmt\Static_ $node) {
  534. return 'static ' . $this->pCommaSeparated($node->vars) . ';';
  535. }
  536. public function pStmt_Global(Stmt\Global_ $node) {
  537. return 'global ' . $this->pCommaSeparated($node->vars) . ';';
  538. }
  539. public function pStmt_StaticVar(Stmt\StaticVar $node) {
  540. return '$' . $node->name
  541. . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
  542. }
  543. public function pStmt_Unset(Stmt\Unset_ $node) {
  544. return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
  545. }
  546. public function pStmt_InlineHTML(Stmt\InlineHTML $node) {
  547. return '?>' . $this->pNoIndent("\n" . $node->value) . '<?php ';
  548. }
  549. public function pStmt_HaltCompiler(Stmt\HaltCompiler $node) {
  550. return '__halt_compiler();' . $node->remaining;
  551. }
  552. // Helpers
  553. public function pObjectProperty($node) {
  554. if ($node instanceof Expr) {
  555. return '{' . $this->p($node) . '}';
  556. } else {
  557. return $node;
  558. }
  559. }
  560. public function pModifiers($modifiers) {
  561. return ($modifiers & Stmt\Class_::MODIFIER_PUBLIC ? 'public ' : '')
  562. . ($modifiers & Stmt\Class_::MODIFIER_PROTECTED ? 'protected ' : '')
  563. . ($modifiers & Stmt\Class_::MODIFIER_PRIVATE ? 'private ' : '')
  564. . ($modifiers & Stmt\Class_::MODIFIER_STATIC ? 'static ' : '')
  565. . ($modifiers & Stmt\Class_::MODIFIER_ABSTRACT ? 'abstract ' : '')
  566. . ($modifiers & Stmt\Class_::MODIFIER_FINAL ? 'final ' : '');
  567. }
  568. public function pEncapsList(array $encapsList, $quote) {
  569. $return = '';
  570. foreach ($encapsList as $element) {
  571. if (is_string($element)) {
  572. $return .= addcslashes($element, "\n\r\t\f\v$" . $quote . "\\");
  573. } else {
  574. $return .= '{' . $this->p($element) . '}';
  575. }
  576. }
  577. return $return;
  578. }
  579. public function pVarOrNewExpr(Node $node) {
  580. if ($node instanceof Expr\New_) {
  581. return '(' . $this->p($node) . ')';
  582. } else {
  583. return $this->p($node);
  584. }
  585. }
  586. }