Standard.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\PrettyPrinter;
  3. use PhpParser\Node;
  4. use PhpParser\Node\Expr;
  5. use PhpParser\Node\Expr\AssignOp;
  6. use PhpParser\Node\Expr\BinaryOp;
  7. use PhpParser\Node\Expr\Cast;
  8. use PhpParser\Node\Name;
  9. use PhpParser\Node\Scalar;
  10. use PhpParser\Node\Scalar\MagicConst;
  11. use PhpParser\Node\Stmt;
  12. use PhpParser\PrettyPrinterAbstract;
  13. class Standard extends PrettyPrinterAbstract
  14. {
  15. // Special nodes
  16. protected function pParam(Node\Param $node) {
  17. return $this->pAttrGroups($node->attrGroups, true)
  18. . $this->pModifiers($node->flags)
  19. . ($node->type ? $this->p($node->type) . ' ' : '')
  20. . ($node->byRef ? '&' : '')
  21. . ($node->variadic ? '...' : '')
  22. . $this->p($node->var)
  23. . ($node->default ? ' = ' . $this->p($node->default) : '');
  24. }
  25. protected function pArg(Node\Arg $node) {
  26. return ($node->name ? $node->name->toString() . ': ' : '')
  27. . ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '')
  28. . $this->p($node->value);
  29. }
  30. protected function pVariadicPlaceholder(Node\VariadicPlaceholder $node) {
  31. return '...';
  32. }
  33. protected function pConst(Node\Const_ $node) {
  34. return $node->name . ' = ' . $this->p($node->value);
  35. }
  36. protected function pNullableType(Node\NullableType $node) {
  37. return '?' . $this->p($node->type);
  38. }
  39. protected function pUnionType(Node\UnionType $node) {
  40. return $this->pImplode($node->types, '|');
  41. }
  42. protected function pIntersectionType(Node\IntersectionType $node) {
  43. return $this->pImplode($node->types, '&');
  44. }
  45. protected function pIdentifier(Node\Identifier $node) {
  46. return $node->name;
  47. }
  48. protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node) {
  49. return '$' . $node->name;
  50. }
  51. protected function pAttribute(Node\Attribute $node) {
  52. return $this->p($node->name)
  53. . ($node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '');
  54. }
  55. protected function pAttributeGroup(Node\AttributeGroup $node) {
  56. return '#[' . $this->pCommaSeparated($node->attrs) . ']';
  57. }
  58. // Names
  59. protected function pName(Name $node) {
  60. return implode('\\', $node->parts);
  61. }
  62. protected function pName_FullyQualified(Name\FullyQualified $node) {
  63. return '\\' . implode('\\', $node->parts);
  64. }
  65. protected function pName_Relative(Name\Relative $node) {
  66. return 'namespace\\' . implode('\\', $node->parts);
  67. }
  68. // Magic Constants
  69. protected function pScalar_MagicConst_Class(MagicConst\Class_ $node) {
  70. return '__CLASS__';
  71. }
  72. protected function pScalar_MagicConst_Dir(MagicConst\Dir $node) {
  73. return '__DIR__';
  74. }
  75. protected function pScalar_MagicConst_File(MagicConst\File $node) {
  76. return '__FILE__';
  77. }
  78. protected function pScalar_MagicConst_Function(MagicConst\Function_ $node) {
  79. return '__FUNCTION__';
  80. }
  81. protected function pScalar_MagicConst_Line(MagicConst\Line $node) {
  82. return '__LINE__';
  83. }
  84. protected function pScalar_MagicConst_Method(MagicConst\Method $node) {
  85. return '__METHOD__';
  86. }
  87. protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) {
  88. return '__NAMESPACE__';
  89. }
  90. protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) {
  91. return '__TRAIT__';
  92. }
  93. // Scalars
  94. protected function pScalar_String(Scalar\String_ $node) {
  95. $kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED);
  96. switch ($kind) {
  97. case Scalar\String_::KIND_NOWDOC:
  98. $label = $node->getAttribute('docLabel');
  99. if ($label && !$this->containsEndLabel($node->value, $label)) {
  100. if ($node->value === '') {
  101. return "<<<'$label'\n$label" . $this->docStringEndToken;
  102. }
  103. return "<<<'$label'\n$node->value\n$label"
  104. . $this->docStringEndToken;
  105. }
  106. /* break missing intentionally */
  107. case Scalar\String_::KIND_SINGLE_QUOTED:
  108. return $this->pSingleQuotedString($node->value);
  109. case Scalar\String_::KIND_HEREDOC:
  110. $label = $node->getAttribute('docLabel');
  111. if ($label && !$this->containsEndLabel($node->value, $label)) {
  112. if ($node->value === '') {
  113. return "<<<$label\n$label" . $this->docStringEndToken;
  114. }
  115. $escaped = $this->escapeString($node->value, null);
  116. return "<<<$label\n" . $escaped . "\n$label"
  117. . $this->docStringEndToken;
  118. }
  119. /* break missing intentionally */
  120. case Scalar\String_::KIND_DOUBLE_QUOTED:
  121. return '"' . $this->escapeString($node->value, '"') . '"';
  122. }
  123. throw new \Exception('Invalid string kind');
  124. }
  125. protected function pScalar_Encapsed(Scalar\Encapsed $node) {
  126. if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) {
  127. $label = $node->getAttribute('docLabel');
  128. if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) {
  129. if (count($node->parts) === 1
  130. && $node->parts[0] instanceof Scalar\EncapsedStringPart
  131. && $node->parts[0]->value === ''
  132. ) {
  133. return "<<<$label\n$label" . $this->docStringEndToken;
  134. }
  135. return "<<<$label\n" . $this->pEncapsList($node->parts, null) . "\n$label"
  136. . $this->docStringEndToken;
  137. }
  138. }
  139. return '"' . $this->pEncapsList($node->parts, '"') . '"';
  140. }
  141. protected function pScalar_LNumber(Scalar\LNumber $node) {
  142. if ($node->value === -\PHP_INT_MAX-1) {
  143. // PHP_INT_MIN cannot be represented as a literal,
  144. // because the sign is not part of the literal
  145. return '(-' . \PHP_INT_MAX . '-1)';
  146. }
  147. $kind = $node->getAttribute('kind', Scalar\LNumber::KIND_DEC);
  148. if (Scalar\LNumber::KIND_DEC === $kind) {
  149. return (string) $node->value;
  150. }
  151. if ($node->value < 0) {
  152. $sign = '-';
  153. $str = (string) -$node->value;
  154. } else {
  155. $sign = '';
  156. $str = (string) $node->value;
  157. }
  158. switch ($kind) {
  159. case Scalar\LNumber::KIND_BIN:
  160. return $sign . '0b' . base_convert($str, 10, 2);
  161. case Scalar\LNumber::KIND_OCT:
  162. return $sign . '0' . base_convert($str, 10, 8);
  163. case Scalar\LNumber::KIND_HEX:
  164. return $sign . '0x' . base_convert($str, 10, 16);
  165. }
  166. throw new \Exception('Invalid number kind');
  167. }
  168. protected function pScalar_DNumber(Scalar\DNumber $node) {
  169. if (!is_finite($node->value)) {
  170. if ($node->value === \INF) {
  171. return '\INF';
  172. } elseif ($node->value === -\INF) {
  173. return '-\INF';
  174. } else {
  175. return '\NAN';
  176. }
  177. }
  178. // Try to find a short full-precision representation
  179. $stringValue = sprintf('%.16G', $node->value);
  180. if ($node->value !== (double) $stringValue) {
  181. $stringValue = sprintf('%.17G', $node->value);
  182. }
  183. // %G is locale dependent and there exists no locale-independent alternative. We don't want
  184. // mess with switching locales here, so let's assume that a comma is the only non-standard
  185. // decimal separator we may encounter...
  186. $stringValue = str_replace(',', '.', $stringValue);
  187. // ensure that number is really printed as float
  188. return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
  189. }
  190. protected function pScalar_EncapsedStringPart(Scalar\EncapsedStringPart $node) {
  191. throw new \LogicException('Cannot directly print EncapsedStringPart');
  192. }
  193. // Assignments
  194. protected function pExpr_Assign(Expr\Assign $node) {
  195. return $this->pInfixOp(Expr\Assign::class, $node->var, ' = ', $node->expr);
  196. }
  197. protected function pExpr_AssignRef(Expr\AssignRef $node) {
  198. return $this->pInfixOp(Expr\AssignRef::class, $node->var, ' =& ', $node->expr);
  199. }
  200. protected function pExpr_AssignOp_Plus(AssignOp\Plus $node) {
  201. return $this->pInfixOp(AssignOp\Plus::class, $node->var, ' += ', $node->expr);
  202. }
  203. protected function pExpr_AssignOp_Minus(AssignOp\Minus $node) {
  204. return $this->pInfixOp(AssignOp\Minus::class, $node->var, ' -= ', $node->expr);
  205. }
  206. protected function pExpr_AssignOp_Mul(AssignOp\Mul $node) {
  207. return $this->pInfixOp(AssignOp\Mul::class, $node->var, ' *= ', $node->expr);
  208. }
  209. protected function pExpr_AssignOp_Div(AssignOp\Div $node) {
  210. return $this->pInfixOp(AssignOp\Div::class, $node->var, ' /= ', $node->expr);
  211. }
  212. protected function pExpr_AssignOp_Concat(AssignOp\Concat $node) {
  213. return $this->pInfixOp(AssignOp\Concat::class, $node->var, ' .= ', $node->expr);
  214. }
  215. protected function pExpr_AssignOp_Mod(AssignOp\Mod $node) {
  216. return $this->pInfixOp(AssignOp\Mod::class, $node->var, ' %= ', $node->expr);
  217. }
  218. protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) {
  219. return $this->pInfixOp(AssignOp\BitwiseAnd::class, $node->var, ' &= ', $node->expr);
  220. }
  221. protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) {
  222. return $this->pInfixOp(AssignOp\BitwiseOr::class, $node->var, ' |= ', $node->expr);
  223. }
  224. protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) {
  225. return $this->pInfixOp(AssignOp\BitwiseXor::class, $node->var, ' ^= ', $node->expr);
  226. }
  227. protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) {
  228. return $this->pInfixOp(AssignOp\ShiftLeft::class, $node->var, ' <<= ', $node->expr);
  229. }
  230. protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) {
  231. return $this->pInfixOp(AssignOp\ShiftRight::class, $node->var, ' >>= ', $node->expr);
  232. }
  233. protected function pExpr_AssignOp_Pow(AssignOp\Pow $node) {
  234. return $this->pInfixOp(AssignOp\Pow::class, $node->var, ' **= ', $node->expr);
  235. }
  236. protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node) {
  237. return $this->pInfixOp(AssignOp\Coalesce::class, $node->var, ' ??= ', $node->expr);
  238. }
  239. // Binary expressions
  240. protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) {
  241. return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right);
  242. }
  243. protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) {
  244. return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right);
  245. }
  246. protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) {
  247. return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right);
  248. }
  249. protected function pExpr_BinaryOp_Div(BinaryOp\Div $node) {
  250. return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right);
  251. }
  252. protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) {
  253. return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right);
  254. }
  255. protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) {
  256. return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right);
  257. }
  258. protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) {
  259. return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right);
  260. }
  261. protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) {
  262. return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right);
  263. }
  264. protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) {
  265. return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right);
  266. }
  267. protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) {
  268. return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right);
  269. }
  270. protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) {
  271. return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right);
  272. }
  273. protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) {
  274. return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right);
  275. }
  276. protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) {
  277. return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right);
  278. }
  279. protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) {
  280. return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right);
  281. }
  282. protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) {
  283. return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right);
  284. }
  285. protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) {
  286. return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right);
  287. }
  288. protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) {
  289. return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right);
  290. }
  291. protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) {
  292. return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right);
  293. }
  294. protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) {
  295. return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right);
  296. }
  297. protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) {
  298. return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right);
  299. }
  300. protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) {
  301. return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right);
  302. }
  303. protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) {
  304. return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right);
  305. }
  306. protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) {
  307. return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right);
  308. }
  309. protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) {
  310. return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right);
  311. }
  312. protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) {
  313. return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right);
  314. }
  315. protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) {
  316. return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right);
  317. }
  318. protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) {
  319. return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right);
  320. }
  321. protected function pExpr_Instanceof(Expr\Instanceof_ $node) {
  322. list($precedence, $associativity) = $this->precedenceMap[Expr\Instanceof_::class];
  323. return $this->pPrec($node->expr, $precedence, $associativity, -1)
  324. . ' instanceof '
  325. . $this->pNewVariable($node->class);
  326. }
  327. // Unary expressions
  328. protected function pExpr_BooleanNot(Expr\BooleanNot $node) {
  329. return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr);
  330. }
  331. protected function pExpr_BitwiseNot(Expr\BitwiseNot $node) {
  332. return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr);
  333. }
  334. protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) {
  335. if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) {
  336. // Enforce -(-$expr) instead of --$expr
  337. return '-(' . $this->p($node->expr) . ')';
  338. }
  339. return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr);
  340. }
  341. protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) {
  342. if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) {
  343. // Enforce +(+$expr) instead of ++$expr
  344. return '+(' . $this->p($node->expr) . ')';
  345. }
  346. return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr);
  347. }
  348. protected function pExpr_PreInc(Expr\PreInc $node) {
  349. return $this->pPrefixOp(Expr\PreInc::class, '++', $node->var);
  350. }
  351. protected function pExpr_PreDec(Expr\PreDec $node) {
  352. return $this->pPrefixOp(Expr\PreDec::class, '--', $node->var);
  353. }
  354. protected function pExpr_PostInc(Expr\PostInc $node) {
  355. return $this->pPostfixOp(Expr\PostInc::class, $node->var, '++');
  356. }
  357. protected function pExpr_PostDec(Expr\PostDec $node) {
  358. return $this->pPostfixOp(Expr\PostDec::class, $node->var, '--');
  359. }
  360. protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) {
  361. return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr);
  362. }
  363. protected function pExpr_YieldFrom(Expr\YieldFrom $node) {
  364. return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr);
  365. }
  366. protected function pExpr_Print(Expr\Print_ $node) {
  367. return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr);
  368. }
  369. // Casts
  370. protected function pExpr_Cast_Int(Cast\Int_ $node) {
  371. return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr);
  372. }
  373. protected function pExpr_Cast_Double(Cast\Double $node) {
  374. $kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE);
  375. if ($kind === Cast\Double::KIND_DOUBLE) {
  376. $cast = '(double)';
  377. } elseif ($kind === Cast\Double::KIND_FLOAT) {
  378. $cast = '(float)';
  379. } elseif ($kind === Cast\Double::KIND_REAL) {
  380. $cast = '(real)';
  381. }
  382. return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr);
  383. }
  384. protected function pExpr_Cast_String(Cast\String_ $node) {
  385. return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr);
  386. }
  387. protected function pExpr_Cast_Array(Cast\Array_ $node) {
  388. return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr);
  389. }
  390. protected function pExpr_Cast_Object(Cast\Object_ $node) {
  391. return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr);
  392. }
  393. protected function pExpr_Cast_Bool(Cast\Bool_ $node) {
  394. return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr);
  395. }
  396. protected function pExpr_Cast_Unset(Cast\Unset_ $node) {
  397. return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr);
  398. }
  399. // Function calls and similar constructs
  400. protected function pExpr_FuncCall(Expr\FuncCall $node) {
  401. return $this->pCallLhs($node->name)
  402. . '(' . $this->pMaybeMultiline($node->args) . ')';
  403. }
  404. protected function pExpr_MethodCall(Expr\MethodCall $node) {
  405. return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
  406. . '(' . $this->pMaybeMultiline($node->args) . ')';
  407. }
  408. protected function pExpr_NullsafeMethodCall(Expr\NullsafeMethodCall $node) {
  409. return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name)
  410. . '(' . $this->pMaybeMultiline($node->args) . ')';
  411. }
  412. protected function pExpr_StaticCall(Expr\StaticCall $node) {
  413. return $this->pDereferenceLhs($node->class) . '::'
  414. . ($node->name instanceof Expr
  415. ? ($node->name instanceof Expr\Variable
  416. ? $this->p($node->name)
  417. : '{' . $this->p($node->name) . '}')
  418. : $node->name)
  419. . '(' . $this->pMaybeMultiline($node->args) . ')';
  420. }
  421. protected function pExpr_Empty(Expr\Empty_ $node) {
  422. return 'empty(' . $this->p($node->expr) . ')';
  423. }
  424. protected function pExpr_Isset(Expr\Isset_ $node) {
  425. return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
  426. }
  427. protected function pExpr_Eval(Expr\Eval_ $node) {
  428. return 'eval(' . $this->p($node->expr) . ')';
  429. }
  430. protected function pExpr_Include(Expr\Include_ $node) {
  431. static $map = [
  432. Expr\Include_::TYPE_INCLUDE => 'include',
  433. Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
  434. Expr\Include_::TYPE_REQUIRE => 'require',
  435. Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
  436. ];
  437. return $map[$node->type] . ' ' . $this->p($node->expr);
  438. }
  439. protected function pExpr_List(Expr\List_ $node) {
  440. return 'list(' . $this->pCommaSeparated($node->items) . ')';
  441. }
  442. // Other
  443. protected function pExpr_Error(Expr\Error $node) {
  444. throw new \LogicException('Cannot pretty-print AST with Error nodes');
  445. }
  446. protected function pExpr_Variable(Expr\Variable $node) {
  447. if ($node->name instanceof Expr) {
  448. return '${' . $this->p($node->name) . '}';
  449. } else {
  450. return '$' . $node->name;
  451. }
  452. }
  453. protected function pExpr_Array(Expr\Array_ $node) {
  454. $syntax = $node->getAttribute('kind',
  455. $this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
  456. if ($syntax === Expr\Array_::KIND_SHORT) {
  457. return '[' . $this->pMaybeMultiline($node->items, true) . ']';
  458. } else {
  459. return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
  460. }
  461. }
  462. protected function pExpr_ArrayItem(Expr\ArrayItem $node) {
  463. return (null !== $node->key ? $this->p($node->key) . ' => ' : '')
  464. . ($node->byRef ? '&' : '')
  465. . ($node->unpack ? '...' : '')
  466. . $this->p($node->value);
  467. }
  468. protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) {
  469. return $this->pDereferenceLhs($node->var)
  470. . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
  471. }
  472. protected function pExpr_ConstFetch(Expr\ConstFetch $node) {
  473. return $this->p($node->name);
  474. }
  475. protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) {
  476. return $this->pDereferenceLhs($node->class) . '::' . $this->p($node->name);
  477. }
  478. protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) {
  479. return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name);
  480. }
  481. protected function pExpr_NullsafePropertyFetch(Expr\NullsafePropertyFetch $node) {
  482. return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name);
  483. }
  484. protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) {
  485. return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name);
  486. }
  487. protected function pExpr_ShellExec(Expr\ShellExec $node) {
  488. return '`' . $this->pEncapsList($node->parts, '`') . '`';
  489. }
  490. protected function pExpr_Closure(Expr\Closure $node) {
  491. return $this->pAttrGroups($node->attrGroups, true)
  492. . ($node->static ? 'static ' : '')
  493. . 'function ' . ($node->byRef ? '&' : '')
  494. . '(' . $this->pCommaSeparated($node->params) . ')'
  495. . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')' : '')
  496. . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
  497. . ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
  498. }
  499. protected function pExpr_Match(Expr\Match_ $node) {
  500. return 'match (' . $this->p($node->cond) . ') {'
  501. . $this->pCommaSeparatedMultiline($node->arms, true)
  502. . $this->nl
  503. . '}';
  504. }
  505. protected function pMatchArm(Node\MatchArm $node) {
  506. return ($node->conds ? $this->pCommaSeparated($node->conds) : 'default')
  507. . ' => ' . $this->p($node->body);
  508. }
  509. protected function pExpr_ArrowFunction(Expr\ArrowFunction $node) {
  510. return $this->pAttrGroups($node->attrGroups, true)
  511. . ($node->static ? 'static ' : '')
  512. . 'fn' . ($node->byRef ? '&' : '')
  513. . '(' . $this->pCommaSeparated($node->params) . ')'
  514. . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
  515. . ' => '
  516. . $this->p($node->expr);
  517. }
  518. protected function pExpr_ClosureUse(Expr\ClosureUse $node) {
  519. return ($node->byRef ? '&' : '') . $this->p($node->var);
  520. }
  521. protected function pExpr_New(Expr\New_ $node) {
  522. if ($node->class instanceof Stmt\Class_) {
  523. $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
  524. return 'new ' . $this->pClassCommon($node->class, $args);
  525. }
  526. return 'new ' . $this->pNewVariable($node->class)
  527. . '(' . $this->pMaybeMultiline($node->args) . ')';
  528. }
  529. protected function pExpr_Clone(Expr\Clone_ $node) {
  530. return 'clone ' . $this->p($node->expr);
  531. }
  532. protected function pExpr_Ternary(Expr\Ternary $node) {
  533. // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
  534. // this is okay because the part between ? and : never needs parentheses.
  535. return $this->pInfixOp(Expr\Ternary::class,
  536. $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else
  537. );
  538. }
  539. protected function pExpr_Exit(Expr\Exit_ $node) {
  540. $kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE);
  541. return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die')
  542. . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
  543. }
  544. protected function pExpr_Throw(Expr\Throw_ $node) {
  545. return 'throw ' . $this->p($node->expr);
  546. }
  547. protected function pExpr_Yield(Expr\Yield_ $node) {
  548. if ($node->value === null) {
  549. return 'yield';
  550. } else {
  551. // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary
  552. return '(yield '
  553. . ($node->key !== null ? $this->p($node->key) . ' => ' : '')
  554. . $this->p($node->value)
  555. . ')';
  556. }
  557. }
  558. // Declarations
  559. protected function pStmt_Namespace(Stmt\Namespace_ $node) {
  560. if ($this->canUseSemicolonNamespaces) {
  561. return 'namespace ' . $this->p($node->name) . ';'
  562. . $this->nl . $this->pStmts($node->stmts, false);
  563. } else {
  564. return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
  565. . ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
  566. }
  567. }
  568. protected function pStmt_Use(Stmt\Use_ $node) {
  569. return 'use ' . $this->pUseType($node->type)
  570. . $this->pCommaSeparated($node->uses) . ';';
  571. }
  572. protected function pStmt_GroupUse(Stmt\GroupUse $node) {
  573. return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix)
  574. . '\{' . $this->pCommaSeparated($node->uses) . '};';
  575. }
  576. protected function pStmt_UseUse(Stmt\UseUse $node) {
  577. return $this->pUseType($node->type) . $this->p($node->name)
  578. . (null !== $node->alias ? ' as ' . $node->alias : '');
  579. }
  580. protected function pUseType($type) {
  581. return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
  582. : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
  583. }
  584. protected function pStmt_Interface(Stmt\Interface_ $node) {
  585. return $this->pAttrGroups($node->attrGroups)
  586. . 'interface ' . $node->name
  587. . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
  588. . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
  589. }
  590. protected function pStmt_Enum(Stmt\Enum_ $node) {
  591. return $this->pAttrGroups($node->attrGroups)
  592. . 'enum ' . $node->name
  593. . ($node->scalarType ? " : $node->scalarType" : '')
  594. . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
  595. . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
  596. }
  597. protected function pStmt_Class(Stmt\Class_ $node) {
  598. return $this->pClassCommon($node, ' ' . $node->name);
  599. }
  600. protected function pStmt_Trait(Stmt\Trait_ $node) {
  601. return $this->pAttrGroups($node->attrGroups)
  602. . 'trait ' . $node->name
  603. . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
  604. }
  605. protected function pStmt_EnumCase(Stmt\EnumCase $node) {
  606. return $this->pAttrGroups($node->attrGroups)
  607. . 'case ' . $node->name
  608. . ($node->expr ? ' = ' . $this->p($node->expr) : '')
  609. . ';';
  610. }
  611. protected function pStmt_TraitUse(Stmt\TraitUse $node) {
  612. return 'use ' . $this->pCommaSeparated($node->traits)
  613. . (empty($node->adaptations)
  614. ? ';'
  615. : ' {' . $this->pStmts($node->adaptations) . $this->nl . '}');
  616. }
  617. protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) {
  618. return $this->p($node->trait) . '::' . $node->method
  619. . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
  620. }
  621. protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) {
  622. return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
  623. . $node->method . ' as'
  624. . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '')
  625. . (null !== $node->newName ? ' ' . $node->newName : '')
  626. . ';';
  627. }
  628. protected function pStmt_Property(Stmt\Property $node) {
  629. return $this->pAttrGroups($node->attrGroups)
  630. . (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags))
  631. . ($node->type ? $this->p($node->type) . ' ' : '')
  632. . $this->pCommaSeparated($node->props) . ';';
  633. }
  634. protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
  635. return '$' . $node->name
  636. . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
  637. }
  638. protected function pStmt_ClassMethod(Stmt\ClassMethod $node) {
  639. return $this->pAttrGroups($node->attrGroups)
  640. . $this->pModifiers($node->flags)
  641. . 'function ' . ($node->byRef ? '&' : '') . $node->name
  642. . '(' . $this->pMaybeMultiline($node->params) . ')'
  643. . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
  644. . (null !== $node->stmts
  645. ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'
  646. : ';');
  647. }
  648. protected function pStmt_ClassConst(Stmt\ClassConst $node) {
  649. return $this->pAttrGroups($node->attrGroups)
  650. . $this->pModifiers($node->flags)
  651. . 'const ' . $this->pCommaSeparated($node->consts) . ';';
  652. }
  653. protected function pStmt_Function(Stmt\Function_ $node) {
  654. return $this->pAttrGroups($node->attrGroups)
  655. . 'function ' . ($node->byRef ? '&' : '') . $node->name
  656. . '(' . $this->pCommaSeparated($node->params) . ')'
  657. . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
  658. . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
  659. }
  660. protected function pStmt_Const(Stmt\Const_ $node) {
  661. return 'const ' . $this->pCommaSeparated($node->consts) . ';';
  662. }
  663. protected function pStmt_Declare(Stmt\Declare_ $node) {
  664. return 'declare (' . $this->pCommaSeparated($node->declares) . ')'
  665. . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';');
  666. }
  667. protected function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) {
  668. return $node->key . '=' . $this->p($node->value);
  669. }
  670. // Control flow
  671. protected function pStmt_If(Stmt\If_ $node) {
  672. return 'if (' . $this->p($node->cond) . ') {'
  673. . $this->pStmts($node->stmts) . $this->nl . '}'
  674. . ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '')
  675. . (null !== $node->else ? ' ' . $this->p($node->else) : '');
  676. }
  677. protected function pStmt_ElseIf(Stmt\ElseIf_ $node) {
  678. return 'elseif (' . $this->p($node->cond) . ') {'
  679. . $this->pStmts($node->stmts) . $this->nl . '}';
  680. }
  681. protected function pStmt_Else(Stmt\Else_ $node) {
  682. return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}';
  683. }
  684. protected function pStmt_For(Stmt\For_ $node) {
  685. return 'for ('
  686. . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
  687. . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
  688. . $this->pCommaSeparated($node->loop)
  689. . ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
  690. }
  691. protected function pStmt_Foreach(Stmt\Foreach_ $node) {
  692. return 'foreach (' . $this->p($node->expr) . ' as '
  693. . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
  694. . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
  695. . $this->pStmts($node->stmts) . $this->nl . '}';
  696. }
  697. protected function pStmt_While(Stmt\While_ $node) {
  698. return 'while (' . $this->p($node->cond) . ') {'
  699. . $this->pStmts($node->stmts) . $this->nl . '}';
  700. }
  701. protected function pStmt_Do(Stmt\Do_ $node) {
  702. return 'do {' . $this->pStmts($node->stmts) . $this->nl
  703. . '} while (' . $this->p($node->cond) . ');';
  704. }
  705. protected function pStmt_Switch(Stmt\Switch_ $node) {
  706. return 'switch (' . $this->p($node->cond) . ') {'
  707. . $this->pStmts($node->cases) . $this->nl . '}';
  708. }
  709. protected function pStmt_Case(Stmt\Case_ $node) {
  710. return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
  711. . $this->pStmts($node->stmts);
  712. }
  713. protected function pStmt_TryCatch(Stmt\TryCatch $node) {
  714. return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}'
  715. . ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '')
  716. . ($node->finally !== null ? ' ' . $this->p($node->finally) : '');
  717. }
  718. protected function pStmt_Catch(Stmt\Catch_ $node) {
  719. return 'catch (' . $this->pImplode($node->types, '|')
  720. . ($node->var !== null ? ' ' . $this->p($node->var) : '')
  721. . ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
  722. }
  723. protected function pStmt_Finally(Stmt\Finally_ $node) {
  724. return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}';
  725. }
  726. protected function pStmt_Break(Stmt\Break_ $node) {
  727. return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
  728. }
  729. protected function pStmt_Continue(Stmt\Continue_ $node) {
  730. return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
  731. }
  732. protected function pStmt_Return(Stmt\Return_ $node) {
  733. return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
  734. }
  735. protected function pStmt_Throw(Stmt\Throw_ $node) {
  736. return 'throw ' . $this->p($node->expr) . ';';
  737. }
  738. protected function pStmt_Label(Stmt\Label $node) {
  739. return $node->name . ':';
  740. }
  741. protected function pStmt_Goto(Stmt\Goto_ $node) {
  742. return 'goto ' . $node->name . ';';
  743. }
  744. // Other
  745. protected function pStmt_Expression(Stmt\Expression $node) {
  746. return $this->p($node->expr) . ';';
  747. }
  748. protected function pStmt_Echo(Stmt\Echo_ $node) {
  749. return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
  750. }
  751. protected function pStmt_Static(Stmt\Static_ $node) {
  752. return 'static ' . $this->pCommaSeparated($node->vars) . ';';
  753. }
  754. protected function pStmt_Global(Stmt\Global_ $node) {
  755. return 'global ' . $this->pCommaSeparated($node->vars) . ';';
  756. }
  757. protected function pStmt_StaticVar(Stmt\StaticVar $node) {
  758. return $this->p($node->var)
  759. . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
  760. }
  761. protected function pStmt_Unset(Stmt\Unset_ $node) {
  762. return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
  763. }
  764. protected function pStmt_InlineHTML(Stmt\InlineHTML $node) {
  765. $newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : '';
  766. return '?>' . $newline . $node->value . '<?php ';
  767. }
  768. protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) {
  769. return '__halt_compiler();' . $node->remaining;
  770. }
  771. protected function pStmt_Nop(Stmt\Nop $node) {
  772. return '';
  773. }
  774. // Helpers
  775. protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) {
  776. return $this->pAttrGroups($node->attrGroups, $node->name === null)
  777. . $this->pModifiers($node->flags)
  778. . 'class' . $afterClassToken
  779. . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
  780. . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
  781. . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
  782. }
  783. protected function pObjectProperty($node) {
  784. if ($node instanceof Expr) {
  785. return '{' . $this->p($node) . '}';
  786. } else {
  787. return $node;
  788. }
  789. }
  790. protected function pEncapsList(array $encapsList, $quote) {
  791. $return = '';
  792. foreach ($encapsList as $element) {
  793. if ($element instanceof Scalar\EncapsedStringPart) {
  794. $return .= $this->escapeString($element->value, $quote);
  795. } else {
  796. $return .= '{' . $this->p($element) . '}';
  797. }
  798. }
  799. return $return;
  800. }
  801. protected function pSingleQuotedString(string $string) {
  802. return '\'' . addcslashes($string, '\'\\') . '\'';
  803. }
  804. protected function escapeString($string, $quote) {
  805. if (null === $quote) {
  806. // For doc strings, don't escape newlines
  807. $escaped = addcslashes($string, "\t\f\v$\\");
  808. } else {
  809. $escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
  810. }
  811. // Escape control characters and non-UTF-8 characters.
  812. // Regex based on https://stackoverflow.com/a/11709412/385378.
  813. $regex = '/(
  814. [\x00-\x08\x0E-\x1F] # Control characters
  815. | [\xC0-\xC1] # Invalid UTF-8 Bytes
  816. | [\xF5-\xFF] # Invalid UTF-8 Bytes
  817. | \xE0(?=[\x80-\x9F]) # Overlong encoding of prior code point
  818. | \xF0(?=[\x80-\x8F]) # Overlong encoding of prior code point
  819. | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
  820. | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
  821. | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
  822. | (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
  823. | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
  824. | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
  825. | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
  826. | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
  827. )/x';
  828. return preg_replace_callback($regex, function ($matches) {
  829. assert(strlen($matches[0]) === 1);
  830. $hex = dechex(ord($matches[0]));;
  831. return '\\x' . str_pad($hex, 2, '0', \STR_PAD_LEFT);
  832. }, $escaped);
  833. }
  834. protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) {
  835. $start = $atStart ? '(?:^|[\r\n])' : '[\r\n]';
  836. $end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]';
  837. return false !== strpos($string, $label)
  838. && preg_match('/' . $start . $label . $end . '/', $string);
  839. }
  840. protected function encapsedContainsEndLabel(array $parts, $label) {
  841. foreach ($parts as $i => $part) {
  842. $atStart = $i === 0;
  843. $atEnd = $i === count($parts) - 1;
  844. if ($part instanceof Scalar\EncapsedStringPart
  845. && $this->containsEndLabel($part->value, $label, $atStart, $atEnd)
  846. ) {
  847. return true;
  848. }
  849. }
  850. return false;
  851. }
  852. protected function pDereferenceLhs(Node $node) {
  853. if (!$this->dereferenceLhsRequiresParens($node)) {
  854. return $this->p($node);
  855. } else {
  856. return '(' . $this->p($node) . ')';
  857. }
  858. }
  859. protected function pCallLhs(Node $node) {
  860. if (!$this->callLhsRequiresParens($node)) {
  861. return $this->p($node);
  862. } else {
  863. return '(' . $this->p($node) . ')';
  864. }
  865. }
  866. protected function pNewVariable(Node $node) {
  867. // TODO: This is not fully accurate.
  868. return $this->pDereferenceLhs($node);
  869. }
  870. /**
  871. * @param Node[] $nodes
  872. * @return bool
  873. */
  874. protected function hasNodeWithComments(array $nodes) {
  875. foreach ($nodes as $node) {
  876. if ($node && $node->getComments()) {
  877. return true;
  878. }
  879. }
  880. return false;
  881. }
  882. protected function pMaybeMultiline(array $nodes, bool $trailingComma = false) {
  883. if (!$this->hasNodeWithComments($nodes)) {
  884. return $this->pCommaSeparated($nodes);
  885. } else {
  886. return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
  887. }
  888. }
  889. protected function pAttrGroups(array $nodes, bool $inline = false): string {
  890. $result = '';
  891. $sep = $inline ? ' ' : $this->nl;
  892. foreach ($nodes as $node) {
  893. $result .= $this->p($node) . $sep;
  894. }
  895. return $result;
  896. }
  897. }