1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\PrettyPrinter;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\Expr;
7: use PhpParser\Node\Expr\AssignOp;
8: use PhpParser\Node\Expr\BinaryOp;
9: use PhpParser\Node\Expr\Cast;
10: use PhpParser\Node\Name;
11: use PhpParser\Node\Scalar;
12: use PhpParser\Node\Scalar\MagicConst;
13: use PhpParser\Node\Stmt;
14: use PhpParser\PrettyPrinterAbstract;
15:
16: class Standard extends PrettyPrinterAbstract {
17: // Special nodes
18:
19: protected function pParam(Node\Param $node): string {
20: return $this->pAttrGroups($node->attrGroups, $this->phpVersion->supportsAttributes())
21: . $this->pModifiers($node->flags)
22: . ($node->type ? $this->p($node->type) . ' ' : '')
23: . ($node->byRef ? '&' : '')
24: . ($node->variadic ? '...' : '')
25: . $this->p($node->var)
26: . ($node->default ? ' = ' . $this->p($node->default) : '')
27: . ($node->hooks ? ' {' . $this->pStmts($node->hooks) . $this->nl . '}' : '');
28: }
29:
30: protected function pArg(Node\Arg $node): string {
31: return ($node->name ? $node->name->toString() . ': ' : '')
32: . ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '')
33: . $this->p($node->value);
34: }
35:
36: protected function pVariadicPlaceholder(Node\VariadicPlaceholder $node): string {
37: return '...';
38: }
39:
40: protected function pConst(Node\Const_ $node): string {
41: return $node->name . ' = ' . $this->p($node->value);
42: }
43:
44: protected function pNullableType(Node\NullableType $node): string {
45: return '?' . $this->p($node->type);
46: }
47:
48: protected function pUnionType(Node\UnionType $node): string {
49: $types = [];
50: foreach ($node->types as $typeNode) {
51: if ($typeNode instanceof Node\IntersectionType) {
52: $types[] = '('. $this->p($typeNode) . ')';
53: continue;
54: }
55: $types[] = $this->p($typeNode);
56: }
57: return implode('|', $types);
58: }
59:
60: protected function pIntersectionType(Node\IntersectionType $node): string {
61: return $this->pImplode($node->types, '&');
62: }
63:
64: protected function pIdentifier(Node\Identifier $node): string {
65: return $node->name;
66: }
67:
68: protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node): string {
69: return '$' . $node->name;
70: }
71:
72: protected function pAttribute(Node\Attribute $node): string {
73: return $this->p($node->name)
74: . ($node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '');
75: }
76:
77: protected function pAttributeGroup(Node\AttributeGroup $node): string {
78: return '#[' . $this->pCommaSeparated($node->attrs) . ']';
79: }
80:
81: // Names
82:
83: protected function pName(Name $node): string {
84: return $node->name;
85: }
86:
87: protected function pName_FullyQualified(Name\FullyQualified $node): string {
88: return '\\' . $node->name;
89: }
90:
91: protected function pName_Relative(Name\Relative $node): string {
92: return 'namespace\\' . $node->name;
93: }
94:
95: // Magic Constants
96:
97: protected function pScalar_MagicConst_Class(MagicConst\Class_ $node): string {
98: return '__CLASS__';
99: }
100:
101: protected function pScalar_MagicConst_Dir(MagicConst\Dir $node): string {
102: return '__DIR__';
103: }
104:
105: protected function pScalar_MagicConst_File(MagicConst\File $node): string {
106: return '__FILE__';
107: }
108:
109: protected function pScalar_MagicConst_Function(MagicConst\Function_ $node): string {
110: return '__FUNCTION__';
111: }
112:
113: protected function pScalar_MagicConst_Line(MagicConst\Line $node): string {
114: return '__LINE__';
115: }
116:
117: protected function pScalar_MagicConst_Method(MagicConst\Method $node): string {
118: return '__METHOD__';
119: }
120:
121: protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node): string {
122: return '__NAMESPACE__';
123: }
124:
125: protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node): string {
126: return '__TRAIT__';
127: }
128:
129: protected function pScalar_MagicConst_Property(MagicConst\Property $node): string {
130: return '__PROPERTY__';
131: }
132:
133: // Scalars
134:
135: private function indentString(string $str): string {
136: return str_replace("\n", $this->nl, $str);
137: }
138:
139: protected function pScalar_String(Scalar\String_ $node): string {
140: $kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED);
141: switch ($kind) {
142: case Scalar\String_::KIND_NOWDOC:
143: $label = $node->getAttribute('docLabel');
144: if ($label && !$this->containsEndLabel($node->value, $label)) {
145: $shouldIdent = $this->phpVersion->supportsFlexibleHeredoc();
146: $nl = $shouldIdent ? $this->nl : $this->newline;
147: if ($node->value === '') {
148: return "<<<'$label'$nl$label{$this->docStringEndToken}";
149: }
150:
151: // Make sure trailing \r is not combined with following \n into CRLF.
152: if ($node->value[strlen($node->value) - 1] !== "\r") {
153: $value = $shouldIdent ? $this->indentString($node->value) : $node->value;
154: return "<<<'$label'$nl$value$nl$label{$this->docStringEndToken}";
155: }
156: }
157: /* break missing intentionally */
158: // no break
159: case Scalar\String_::KIND_SINGLE_QUOTED:
160: return $this->pSingleQuotedString($node->value);
161: case Scalar\String_::KIND_HEREDOC:
162: $label = $node->getAttribute('docLabel');
163: $escaped = $this->escapeString($node->value, null);
164: if ($label && !$this->containsEndLabel($escaped, $label)) {
165: $nl = $this->phpVersion->supportsFlexibleHeredoc() ? $this->nl : $this->newline;
166: if ($escaped === '') {
167: return "<<<$label$nl$label{$this->docStringEndToken}";
168: }
169:
170: return "<<<$label$nl$escaped$nl$label{$this->docStringEndToken}";
171: }
172: /* break missing intentionally */
173: // no break
174: case Scalar\String_::KIND_DOUBLE_QUOTED:
175: return '"' . $this->escapeString($node->value, '"') . '"';
176: }
177: throw new \Exception('Invalid string kind');
178: }
179:
180: protected function pScalar_InterpolatedString(Scalar\InterpolatedString $node): string {
181: if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) {
182: $label = $node->getAttribute('docLabel');
183: if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) {
184: $nl = $this->phpVersion->supportsFlexibleHeredoc() ? $this->nl : $this->newline;
185: if (count($node->parts) === 1
186: && $node->parts[0] instanceof Node\InterpolatedStringPart
187: && $node->parts[0]->value === ''
188: ) {
189: return "<<<$label$nl$label{$this->docStringEndToken}";
190: }
191:
192: return "<<<$label$nl" . $this->pEncapsList($node->parts, null)
193: . "$nl$label{$this->docStringEndToken}";
194: }
195: }
196: return '"' . $this->pEncapsList($node->parts, '"') . '"';
197: }
198:
199: protected function pScalar_Int(Scalar\Int_ $node): string {
200: if ($node->value === -\PHP_INT_MAX - 1) {
201: // PHP_INT_MIN cannot be represented as a literal,
202: // because the sign is not part of the literal
203: return '(-' . \PHP_INT_MAX . '-1)';
204: }
205:
206: $kind = $node->getAttribute('kind', Scalar\Int_::KIND_DEC);
207: if (Scalar\Int_::KIND_DEC === $kind) {
208: return (string) $node->value;
209: }
210:
211: if ($node->value < 0) {
212: $sign = '-';
213: $str = (string) -$node->value;
214: } else {
215: $sign = '';
216: $str = (string) $node->value;
217: }
218: switch ($kind) {
219: case Scalar\Int_::KIND_BIN:
220: return $sign . '0b' . base_convert($str, 10, 2);
221: case Scalar\Int_::KIND_OCT:
222: return $sign . '0' . base_convert($str, 10, 8);
223: case Scalar\Int_::KIND_HEX:
224: return $sign . '0x' . base_convert($str, 10, 16);
225: }
226: throw new \Exception('Invalid number kind');
227: }
228:
229: protected function pScalar_Float(Scalar\Float_ $node): string {
230: if (!is_finite($node->value)) {
231: if ($node->value === \INF) {
232: return '1.0E+1000';
233: }
234: if ($node->value === -\INF) {
235: return '-1.0E+1000';
236: } else {
237: return '\NAN';
238: }
239: }
240:
241: // Try to find a short full-precision representation
242: $stringValue = sprintf('%.16G', $node->value);
243: if ($node->value !== (float) $stringValue) {
244: $stringValue = sprintf('%.17G', $node->value);
245: }
246:
247: // %G is locale dependent and there exists no locale-independent alternative. We don't want
248: // mess with switching locales here, so let's assume that a comma is the only non-standard
249: // decimal separator we may encounter...
250: $stringValue = str_replace(',', '.', $stringValue);
251:
252: // ensure that number is really printed as float
253: return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
254: }
255:
256: // Assignments
257:
258: protected function pExpr_Assign(Expr\Assign $node, int $precedence, int $lhsPrecedence): string {
259: return $this->pPrefixOp(Expr\Assign::class, $this->p($node->var) . ' = ', $node->expr, $precedence, $lhsPrecedence);
260: }
261:
262: protected function pExpr_AssignRef(Expr\AssignRef $node, int $precedence, int $lhsPrecedence): string {
263: return $this->pPrefixOp(Expr\AssignRef::class, $this->p($node->var) . ' =& ', $node->expr, $precedence, $lhsPrecedence);
264: }
265:
266: protected function pExpr_AssignOp_Plus(AssignOp\Plus $node, int $precedence, int $lhsPrecedence): string {
267: return $this->pPrefixOp(AssignOp\Plus::class, $this->p($node->var) . ' += ', $node->expr, $precedence, $lhsPrecedence);
268: }
269:
270: protected function pExpr_AssignOp_Minus(AssignOp\Minus $node, int $precedence, int $lhsPrecedence): string {
271: return $this->pPrefixOp(AssignOp\Minus::class, $this->p($node->var) . ' -= ', $node->expr, $precedence, $lhsPrecedence);
272: }
273:
274: protected function pExpr_AssignOp_Mul(AssignOp\Mul $node, int $precedence, int $lhsPrecedence): string {
275: return $this->pPrefixOp(AssignOp\Mul::class, $this->p($node->var) . ' *= ', $node->expr, $precedence, $lhsPrecedence);
276: }
277:
278: protected function pExpr_AssignOp_Div(AssignOp\Div $node, int $precedence, int $lhsPrecedence): string {
279: return $this->pPrefixOp(AssignOp\Div::class, $this->p($node->var) . ' /= ', $node->expr, $precedence, $lhsPrecedence);
280: }
281:
282: protected function pExpr_AssignOp_Concat(AssignOp\Concat $node, int $precedence, int $lhsPrecedence): string {
283: return $this->pPrefixOp(AssignOp\Concat::class, $this->p($node->var) . ' .= ', $node->expr, $precedence, $lhsPrecedence);
284: }
285:
286: protected function pExpr_AssignOp_Mod(AssignOp\Mod $node, int $precedence, int $lhsPrecedence): string {
287: return $this->pPrefixOp(AssignOp\Mod::class, $this->p($node->var) . ' %= ', $node->expr, $precedence, $lhsPrecedence);
288: }
289:
290: protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node, int $precedence, int $lhsPrecedence): string {
291: return $this->pPrefixOp(AssignOp\BitwiseAnd::class, $this->p($node->var) . ' &= ', $node->expr, $precedence, $lhsPrecedence);
292: }
293:
294: protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node, int $precedence, int $lhsPrecedence): string {
295: return $this->pPrefixOp(AssignOp\BitwiseOr::class, $this->p($node->var) . ' |= ', $node->expr, $precedence, $lhsPrecedence);
296: }
297:
298: protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node, int $precedence, int $lhsPrecedence): string {
299: return $this->pPrefixOp(AssignOp\BitwiseXor::class, $this->p($node->var) . ' ^= ', $node->expr, $precedence, $lhsPrecedence);
300: }
301:
302: protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node, int $precedence, int $lhsPrecedence): string {
303: return $this->pPrefixOp(AssignOp\ShiftLeft::class, $this->p($node->var) . ' <<= ', $node->expr, $precedence, $lhsPrecedence);
304: }
305:
306: protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node, int $precedence, int $lhsPrecedence): string {
307: return $this->pPrefixOp(AssignOp\ShiftRight::class, $this->p($node->var) . ' >>= ', $node->expr, $precedence, $lhsPrecedence);
308: }
309:
310: protected function pExpr_AssignOp_Pow(AssignOp\Pow $node, int $precedence, int $lhsPrecedence): string {
311: return $this->pPrefixOp(AssignOp\Pow::class, $this->p($node->var) . ' **= ', $node->expr, $precedence, $lhsPrecedence);
312: }
313:
314: protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node, int $precedence, int $lhsPrecedence): string {
315: return $this->pPrefixOp(AssignOp\Coalesce::class, $this->p($node->var) . ' ??= ', $node->expr, $precedence, $lhsPrecedence);
316: }
317:
318: // Binary expressions
319:
320: protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node, int $precedence, int $lhsPrecedence): string {
321: return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right, $precedence, $lhsPrecedence);
322: }
323:
324: protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node, int $precedence, int $lhsPrecedence): string {
325: return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right, $precedence, $lhsPrecedence);
326: }
327:
328: protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node, int $precedence, int $lhsPrecedence): string {
329: return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right, $precedence, $lhsPrecedence);
330: }
331:
332: protected function pExpr_BinaryOp_Div(BinaryOp\Div $node, int $precedence, int $lhsPrecedence): string {
333: return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right, $precedence, $lhsPrecedence);
334: }
335:
336: protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node, int $precedence, int $lhsPrecedence): string {
337: return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right, $precedence, $lhsPrecedence);
338: }
339:
340: protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node, int $precedence, int $lhsPrecedence): string {
341: return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right, $precedence, $lhsPrecedence);
342: }
343:
344: protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node, int $precedence, int $lhsPrecedence): string {
345: return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right, $precedence, $lhsPrecedence);
346: }
347:
348: protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node, int $precedence, int $lhsPrecedence): string {
349: return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right, $precedence, $lhsPrecedence);
350: }
351:
352: protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node, int $precedence, int $lhsPrecedence): string {
353: return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right, $precedence, $lhsPrecedence);
354: }
355:
356: protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node, int $precedence, int $lhsPrecedence): string {
357: return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right, $precedence, $lhsPrecedence);
358: }
359:
360: protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node, int $precedence, int $lhsPrecedence): string {
361: return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right, $precedence, $lhsPrecedence);
362: }
363:
364: protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node, int $precedence, int $lhsPrecedence): string {
365: return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right, $precedence, $lhsPrecedence);
366: }
367:
368: protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node, int $precedence, int $lhsPrecedence): string {
369: return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right, $precedence, $lhsPrecedence);
370: }
371:
372: protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node, int $precedence, int $lhsPrecedence): string {
373: return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right, $precedence, $lhsPrecedence);
374: }
375:
376: protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node, int $precedence, int $lhsPrecedence): string {
377: return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right, $precedence, $lhsPrecedence);
378: }
379:
380: protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node, int $precedence, int $lhsPrecedence): string {
381: return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right, $precedence, $lhsPrecedence);
382: }
383:
384: protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node, int $precedence, int $lhsPrecedence): string {
385: return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right, $precedence, $lhsPrecedence);
386: }
387:
388: protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node, int $precedence, int $lhsPrecedence): string {
389: return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right, $precedence, $lhsPrecedence);
390: }
391:
392: protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node, int $precedence, int $lhsPrecedence): string {
393: return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right, $precedence, $lhsPrecedence);
394: }
395:
396: protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node, int $precedence, int $lhsPrecedence): string {
397: return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right, $precedence, $lhsPrecedence);
398: }
399:
400: protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node, int $precedence, int $lhsPrecedence): string {
401: return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right, $precedence, $lhsPrecedence);
402: }
403:
404: protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node, int $precedence, int $lhsPrecedence): string {
405: return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right, $precedence, $lhsPrecedence);
406: }
407:
408: protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node, int $precedence, int $lhsPrecedence): string {
409: return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right, $precedence, $lhsPrecedence);
410: }
411:
412: protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node, int $precedence, int $lhsPrecedence): string {
413: return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right, $precedence, $lhsPrecedence);
414: }
415:
416: protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node, int $precedence, int $lhsPrecedence): string {
417: return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right, $precedence, $lhsPrecedence);
418: }
419:
420: protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node, int $precedence, int $lhsPrecedence): string {
421: return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right, $precedence, $lhsPrecedence);
422: }
423:
424: protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node, int $precedence, int $lhsPrecedence): string {
425: return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right, $precedence, $lhsPrecedence);
426: }
427:
428: protected function pExpr_BinaryOp_Pipe(BinaryOp\Pipe $node, int $precedence, int $lhsPrecedence): string {
429: return $this->pInfixOp(BinaryOp\Pipe::class, $node->left, ' |> ', $node->right, $precedence, $lhsPrecedence);
430: }
431:
432: protected function pExpr_Instanceof(Expr\Instanceof_ $node, int $precedence, int $lhsPrecedence): string {
433: return $this->pPostfixOp(
434: Expr\Instanceof_::class, $node->expr,
435: ' instanceof ' . $this->pNewOperand($node->class),
436: $precedence, $lhsPrecedence);
437: }
438:
439: // Unary expressions
440:
441: protected function pExpr_BooleanNot(Expr\BooleanNot $node, int $precedence, int $lhsPrecedence): string {
442: return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr, $precedence, $lhsPrecedence);
443: }
444:
445: protected function pExpr_BitwiseNot(Expr\BitwiseNot $node, int $precedence, int $lhsPrecedence): string {
446: return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr, $precedence, $lhsPrecedence);
447: }
448:
449: protected function pExpr_UnaryMinus(Expr\UnaryMinus $node, int $precedence, int $lhsPrecedence): string {
450: return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr, $precedence, $lhsPrecedence);
451: }
452:
453: protected function pExpr_UnaryPlus(Expr\UnaryPlus $node, int $precedence, int $lhsPrecedence): string {
454: return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr, $precedence, $lhsPrecedence);
455: }
456:
457: protected function pExpr_PreInc(Expr\PreInc $node): string {
458: return '++' . $this->p($node->var);
459: }
460:
461: protected function pExpr_PreDec(Expr\PreDec $node): string {
462: return '--' . $this->p($node->var);
463: }
464:
465: protected function pExpr_PostInc(Expr\PostInc $node): string {
466: return $this->p($node->var) . '++';
467: }
468:
469: protected function pExpr_PostDec(Expr\PostDec $node): string {
470: return $this->p($node->var) . '--';
471: }
472:
473: protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node, int $precedence, int $lhsPrecedence): string {
474: return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr, $precedence, $lhsPrecedence);
475: }
476:
477: protected function pExpr_YieldFrom(Expr\YieldFrom $node, int $precedence, int $lhsPrecedence): string {
478: return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr, $precedence, $lhsPrecedence);
479: }
480:
481: protected function pExpr_Print(Expr\Print_ $node, int $precedence, int $lhsPrecedence): string {
482: return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr, $precedence, $lhsPrecedence);
483: }
484:
485: // Casts
486:
487: protected function pExpr_Cast_Int(Cast\Int_ $node, int $precedence, int $lhsPrecedence): string {
488: return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr, $precedence, $lhsPrecedence);
489: }
490:
491: protected function pExpr_Cast_Double(Cast\Double $node, int $precedence, int $lhsPrecedence): string {
492: $kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE);
493: if ($kind === Cast\Double::KIND_DOUBLE) {
494: $cast = '(double)';
495: } elseif ($kind === Cast\Double::KIND_FLOAT) {
496: $cast = '(float)';
497: } else {
498: assert($kind === Cast\Double::KIND_REAL);
499: $cast = '(real)';
500: }
501: return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr, $precedence, $lhsPrecedence);
502: }
503:
504: protected function pExpr_Cast_String(Cast\String_ $node, int $precedence, int $lhsPrecedence): string {
505: return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr, $precedence, $lhsPrecedence);
506: }
507:
508: protected function pExpr_Cast_Array(Cast\Array_ $node, int $precedence, int $lhsPrecedence): string {
509: return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr, $precedence, $lhsPrecedence);
510: }
511:
512: protected function pExpr_Cast_Object(Cast\Object_ $node, int $precedence, int $lhsPrecedence): string {
513: return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr, $precedence, $lhsPrecedence);
514: }
515:
516: protected function pExpr_Cast_Bool(Cast\Bool_ $node, int $precedence, int $lhsPrecedence): string {
517: return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr, $precedence, $lhsPrecedence);
518: }
519:
520: protected function pExpr_Cast_Unset(Cast\Unset_ $node, int $precedence, int $lhsPrecedence): string {
521: return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr, $precedence, $lhsPrecedence);
522: }
523:
524: protected function pExpr_Cast_Void(Cast\Void_ $node, int $precedence, int $lhsPrecedence): string {
525: return $this->pPrefixOp(Cast\Void_::class, '(void) ', $node->expr, $precedence, $lhsPrecedence);
526: }
527:
528: // Function calls and similar constructs
529:
530: protected function pExpr_FuncCall(Expr\FuncCall $node): string {
531: return $this->pCallLhs($node->name)
532: . '(' . $this->pMaybeMultiline($node->args) . ')';
533: }
534:
535: protected function pExpr_MethodCall(Expr\MethodCall $node): string {
536: return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
537: . '(' . $this->pMaybeMultiline($node->args) . ')';
538: }
539:
540: protected function pExpr_NullsafeMethodCall(Expr\NullsafeMethodCall $node): string {
541: return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name)
542: . '(' . $this->pMaybeMultiline($node->args) . ')';
543: }
544:
545: protected function pExpr_StaticCall(Expr\StaticCall $node): string {
546: return $this->pStaticDereferenceLhs($node->class) . '::'
547: . ($node->name instanceof Expr
548: ? ($node->name instanceof Expr\Variable
549: ? $this->p($node->name)
550: : '{' . $this->p($node->name) . '}')
551: : $node->name)
552: . '(' . $this->pMaybeMultiline($node->args) . ')';
553: }
554:
555: protected function pExpr_Empty(Expr\Empty_ $node): string {
556: return 'empty(' . $this->p($node->expr) . ')';
557: }
558:
559: protected function pExpr_Isset(Expr\Isset_ $node): string {
560: return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
561: }
562:
563: protected function pExpr_Eval(Expr\Eval_ $node): string {
564: return 'eval(' . $this->p($node->expr) . ')';
565: }
566:
567: protected function pExpr_Include(Expr\Include_ $node, int $precedence, int $lhsPrecedence): string {
568: static $map = [
569: Expr\Include_::TYPE_INCLUDE => 'include',
570: Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
571: Expr\Include_::TYPE_REQUIRE => 'require',
572: Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
573: ];
574:
575: return $this->pPrefixOp(Expr\Include_::class, $map[$node->type] . ' ', $node->expr, $precedence, $lhsPrecedence);
576: }
577:
578: protected function pExpr_List(Expr\List_ $node): string {
579: $syntax = $node->getAttribute('kind',
580: $this->phpVersion->supportsShortArrayDestructuring() ? Expr\List_::KIND_ARRAY : Expr\List_::KIND_LIST);
581: if ($syntax === Expr\List_::KIND_ARRAY) {
582: return '[' . $this->pMaybeMultiline($node->items, true) . ']';
583: } else {
584: return 'list(' . $this->pMaybeMultiline($node->items, true) . ')';
585: }
586: }
587:
588: // Other
589:
590: protected function pExpr_Error(Expr\Error $node): string {
591: throw new \LogicException('Cannot pretty-print AST with Error nodes');
592: }
593:
594: protected function pExpr_Variable(Expr\Variable $node): string {
595: if ($node->name instanceof Expr) {
596: return '${' . $this->p($node->name) . '}';
597: } else {
598: return '$' . $node->name;
599: }
600: }
601:
602: protected function pExpr_Array(Expr\Array_ $node): string {
603: $syntax = $node->getAttribute('kind',
604: $this->shortArraySyntax ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
605: if ($syntax === Expr\Array_::KIND_SHORT) {
606: return '[' . $this->pMaybeMultiline($node->items, true) . ']';
607: } else {
608: return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
609: }
610: }
611:
612: protected function pKey(?Node $node): string {
613: if ($node === null) {
614: return '';
615: }
616:
617: // => is not really an operator and does not typically participate in precedence resolution.
618: // However, there is an exception if yield expressions with keys are involved:
619: // [yield $a => $b] is interpreted as [(yield $a => $b)], so we need to ensure that
620: // [(yield $a) => $b] is printed with parentheses. We approximate this by lowering the LHS
621: // precedence to that of yield (which will also print unnecessary parentheses for rare low
622: // precedence unary operators like include).
623: $yieldPrecedence = $this->precedenceMap[Expr\Yield_::class][0];
624: return $this->p($node, self::MAX_PRECEDENCE, $yieldPrecedence) . ' => ';
625: }
626:
627: protected function pArrayItem(Node\ArrayItem $node): string {
628: return $this->pKey($node->key)
629: . ($node->byRef ? '&' : '')
630: . ($node->unpack ? '...' : '')
631: . $this->p($node->value);
632: }
633:
634: protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node): string {
635: return $this->pDereferenceLhs($node->var)
636: . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
637: }
638:
639: protected function pExpr_ConstFetch(Expr\ConstFetch $node): string {
640: return $this->p($node->name);
641: }
642:
643: protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node): string {
644: return $this->pStaticDereferenceLhs($node->class) . '::' . $this->pObjectProperty($node->name);
645: }
646:
647: protected function pExpr_PropertyFetch(Expr\PropertyFetch $node): string {
648: return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name);
649: }
650:
651: protected function pExpr_NullsafePropertyFetch(Expr\NullsafePropertyFetch $node): string {
652: return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name);
653: }
654:
655: protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node): string {
656: return $this->pStaticDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name);
657: }
658:
659: protected function pExpr_ShellExec(Expr\ShellExec $node): string {
660: return '`' . $this->pEncapsList($node->parts, '`') . '`';
661: }
662:
663: protected function pExpr_Closure(Expr\Closure $node): string {
664: return $this->pAttrGroups($node->attrGroups, true)
665: . $this->pStatic($node->static)
666: . 'function ' . ($node->byRef ? '&' : '')
667: . '(' . $this->pParams($node->params) . ')'
668: . (!empty($node->uses) ? ' use (' . $this->pCommaSeparated($node->uses) . ')' : '')
669: . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
670: . ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
671: }
672:
673: protected function pExpr_Match(Expr\Match_ $node): string {
674: return 'match (' . $this->p($node->cond) . ') {'
675: . $this->pCommaSeparatedMultiline($node->arms, true)
676: . $this->nl
677: . '}';
678: }
679:
680: protected function pMatchArm(Node\MatchArm $node): string {
681: $result = '';
682: if ($node->conds) {
683: for ($i = 0, $c = \count($node->conds); $i + 1 < $c; $i++) {
684: $result .= $this->p($node->conds[$i]) . ', ';
685: }
686: $result .= $this->pKey($node->conds[$i]);
687: } else {
688: $result = 'default => ';
689: }
690: return $result . $this->p($node->body);
691: }
692:
693: protected function pExpr_ArrowFunction(Expr\ArrowFunction $node, int $precedence, int $lhsPrecedence): string {
694: return $this->pPrefixOp(
695: Expr\ArrowFunction::class,
696: $this->pAttrGroups($node->attrGroups, true)
697: . $this->pStatic($node->static)
698: . 'fn' . ($node->byRef ? '&' : '')
699: . '(' . $this->pParams($node->params) . ')'
700: . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
701: . ' => ',
702: $node->expr, $precedence, $lhsPrecedence);
703: }
704:
705: protected function pClosureUse(Node\ClosureUse $node): string {
706: return ($node->byRef ? '&' : '') . $this->p($node->var);
707: }
708:
709: protected function pExpr_New(Expr\New_ $node): string {
710: if ($node->class instanceof Stmt\Class_) {
711: $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
712: return 'new ' . $this->pClassCommon($node->class, $args);
713: }
714: return 'new ' . $this->pNewOperand($node->class)
715: . '(' . $this->pMaybeMultiline($node->args) . ')';
716: }
717:
718: protected function pExpr_Clone(Expr\Clone_ $node, int $precedence, int $lhsPrecedence): string {
719: return $this->pPrefixOp(Expr\Clone_::class, 'clone ', $node->expr, $precedence, $lhsPrecedence);
720: }
721:
722: protected function pExpr_Ternary(Expr\Ternary $node, int $precedence, int $lhsPrecedence): string {
723: // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
724: // this is okay because the part between ? and : never needs parentheses.
725: return $this->pInfixOp(Expr\Ternary::class,
726: $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else,
727: $precedence, $lhsPrecedence
728: );
729: }
730:
731: protected function pExpr_Exit(Expr\Exit_ $node): string {
732: $kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE);
733: return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die')
734: . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
735: }
736:
737: protected function pExpr_Throw(Expr\Throw_ $node, int $precedence, int $lhsPrecedence): string {
738: return $this->pPrefixOp(Expr\Throw_::class, 'throw ', $node->expr, $precedence, $lhsPrecedence);
739: }
740:
741: protected function pExpr_Yield(Expr\Yield_ $node, int $precedence, int $lhsPrecedence): string {
742: if ($node->value === null) {
743: $opPrecedence = $this->precedenceMap[Expr\Yield_::class][0];
744: return $opPrecedence >= $lhsPrecedence ? '(yield)' : 'yield';
745: } else {
746: if (!$this->phpVersion->supportsYieldWithoutParentheses()) {
747: return '(yield ' . $this->pKey($node->key) . $this->p($node->value) . ')';
748: }
749: return $this->pPrefixOp(
750: Expr\Yield_::class, 'yield ' . $this->pKey($node->key),
751: $node->value, $precedence, $lhsPrecedence);
752: }
753: }
754:
755: // Declarations
756:
757: protected function pStmt_Namespace(Stmt\Namespace_ $node): string {
758: if ($this->canUseSemicolonNamespaces) {
759: return 'namespace ' . $this->p($node->name) . ';'
760: . $this->nl . $this->pStmts($node->stmts, false);
761: } else {
762: return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
763: . ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
764: }
765: }
766:
767: protected function pStmt_Use(Stmt\Use_ $node): string {
768: return 'use ' . $this->pUseType($node->type)
769: . $this->pCommaSeparated($node->uses) . ';';
770: }
771:
772: protected function pStmt_GroupUse(Stmt\GroupUse $node): string {
773: return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix)
774: . '\{' . $this->pCommaSeparated($node->uses) . '};';
775: }
776:
777: protected function pUseItem(Node\UseItem $node): string {
778: return $this->pUseType($node->type) . $this->p($node->name)
779: . (null !== $node->alias ? ' as ' . $node->alias : '');
780: }
781:
782: protected function pUseType(int $type): string {
783: return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
784: : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
785: }
786:
787: protected function pStmt_Interface(Stmt\Interface_ $node): string {
788: return $this->pAttrGroups($node->attrGroups)
789: . 'interface ' . $node->name
790: . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
791: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
792: }
793:
794: protected function pStmt_Enum(Stmt\Enum_ $node): string {
795: return $this->pAttrGroups($node->attrGroups)
796: . 'enum ' . $node->name
797: . ($node->scalarType ? ' : ' . $this->p($node->scalarType) : '')
798: . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
799: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
800: }
801:
802: protected function pStmt_Class(Stmt\Class_ $node): string {
803: return $this->pClassCommon($node, ' ' . $node->name);
804: }
805:
806: protected function pStmt_Trait(Stmt\Trait_ $node): string {
807: return $this->pAttrGroups($node->attrGroups)
808: . 'trait ' . $node->name
809: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
810: }
811:
812: protected function pStmt_EnumCase(Stmt\EnumCase $node): string {
813: return $this->pAttrGroups($node->attrGroups)
814: . 'case ' . $node->name
815: . ($node->expr ? ' = ' . $this->p($node->expr) : '')
816: . ';';
817: }
818:
819: protected function pStmt_TraitUse(Stmt\TraitUse $node): string {
820: return 'use ' . $this->pCommaSeparated($node->traits)
821: . (empty($node->adaptations)
822: ? ';'
823: : ' {' . $this->pStmts($node->adaptations) . $this->nl . '}');
824: }
825:
826: protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node): string {
827: return $this->p($node->trait) . '::' . $node->method
828: . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
829: }
830:
831: protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node): string {
832: return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
833: . $node->method . ' as'
834: . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '')
835: . (null !== $node->newName ? ' ' . $node->newName : '')
836: . ';';
837: }
838:
839: protected function pStmt_Property(Stmt\Property $node): string {
840: return $this->pAttrGroups($node->attrGroups)
841: . (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags))
842: . ($node->type ? $this->p($node->type) . ' ' : '')
843: . $this->pCommaSeparated($node->props)
844: . ($node->hooks ? ' {' . $this->pStmts($node->hooks) . $this->nl . '}' : ';');
845: }
846:
847: protected function pPropertyItem(Node\PropertyItem $node): string {
848: return '$' . $node->name
849: . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
850: }
851:
852: protected function pPropertyHook(Node\PropertyHook $node): string {
853: return $this->pAttrGroups($node->attrGroups)
854: . $this->pModifiers($node->flags)
855: . ($node->byRef ? '&' : '') . $node->name
856: . ($node->params ? '(' . $this->pParams($node->params) . ')' : '')
857: . (\is_array($node->body) ? ' {' . $this->pStmts($node->body) . $this->nl . '}'
858: : ($node->body !== null ? ' => ' . $this->p($node->body) : '') . ';');
859: }
860:
861: protected function pStmt_ClassMethod(Stmt\ClassMethod $node): string {
862: return $this->pAttrGroups($node->attrGroups)
863: . $this->pModifiers($node->flags)
864: . 'function ' . ($node->byRef ? '&' : '') . $node->name
865: . '(' . $this->pParams($node->params) . ')'
866: . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
867: . (null !== $node->stmts
868: ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'
869: : ';');
870: }
871:
872: protected function pStmt_ClassConst(Stmt\ClassConst $node): string {
873: return $this->pAttrGroups($node->attrGroups)
874: . $this->pModifiers($node->flags)
875: . 'const '
876: . (null !== $node->type ? $this->p($node->type) . ' ' : '')
877: . $this->pCommaSeparated($node->consts) . ';';
878: }
879:
880: protected function pStmt_Function(Stmt\Function_ $node): string {
881: return $this->pAttrGroups($node->attrGroups)
882: . 'function ' . ($node->byRef ? '&' : '') . $node->name
883: . '(' . $this->pParams($node->params) . ')'
884: . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
885: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
886: }
887:
888: protected function pStmt_Const(Stmt\Const_ $node): string {
889: return $this->pAttrGroups($node->attrGroups)
890: . 'const '
891: . $this->pCommaSeparated($node->consts) . ';';
892: }
893:
894: protected function pStmt_Declare(Stmt\Declare_ $node): string {
895: return 'declare (' . $this->pCommaSeparated($node->declares) . ')'
896: . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';');
897: }
898:
899: protected function pDeclareItem(Node\DeclareItem $node): string {
900: return $node->key . '=' . $this->p($node->value);
901: }
902:
903: // Control flow
904:
905: protected function pStmt_If(Stmt\If_ $node): string {
906: return 'if (' . $this->p($node->cond) . ') {'
907: . $this->pStmts($node->stmts) . $this->nl . '}'
908: . ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '')
909: . (null !== $node->else ? ' ' . $this->p($node->else) : '');
910: }
911:
912: protected function pStmt_ElseIf(Stmt\ElseIf_ $node): string {
913: return 'elseif (' . $this->p($node->cond) . ') {'
914: . $this->pStmts($node->stmts) . $this->nl . '}';
915: }
916:
917: protected function pStmt_Else(Stmt\Else_ $node): string {
918: if (\count($node->stmts) === 1 && $node->stmts[0] instanceof Stmt\If_) {
919: // Print as "else if" rather than "else { if }"
920: return 'else ' . $this->p($node->stmts[0]);
921: }
922: return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}';
923: }
924:
925: protected function pStmt_For(Stmt\For_ $node): string {
926: return 'for ('
927: . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
928: . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
929: . $this->pCommaSeparated($node->loop)
930: . ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
931: }
932:
933: protected function pStmt_Foreach(Stmt\Foreach_ $node): string {
934: return 'foreach (' . $this->p($node->expr) . ' as '
935: . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
936: . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
937: . $this->pStmts($node->stmts) . $this->nl . '}';
938: }
939:
940: protected function pStmt_While(Stmt\While_ $node): string {
941: return 'while (' . $this->p($node->cond) . ') {'
942: . $this->pStmts($node->stmts) . $this->nl . '}';
943: }
944:
945: protected function pStmt_Do(Stmt\Do_ $node): string {
946: return 'do {' . $this->pStmts($node->stmts) . $this->nl
947: . '} while (' . $this->p($node->cond) . ');';
948: }
949:
950: protected function pStmt_Switch(Stmt\Switch_ $node): string {
951: return 'switch (' . $this->p($node->cond) . ') {'
952: . $this->pStmts($node->cases) . $this->nl . '}';
953: }
954:
955: protected function pStmt_Case(Stmt\Case_ $node): string {
956: return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
957: . $this->pStmts($node->stmts);
958: }
959:
960: protected function pStmt_TryCatch(Stmt\TryCatch $node): string {
961: return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}'
962: . ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '')
963: . ($node->finally !== null ? ' ' . $this->p($node->finally) : '');
964: }
965:
966: protected function pStmt_Catch(Stmt\Catch_ $node): string {
967: return 'catch (' . $this->pImplode($node->types, '|')
968: . ($node->var !== null ? ' ' . $this->p($node->var) : '')
969: . ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
970: }
971:
972: protected function pStmt_Finally(Stmt\Finally_ $node): string {
973: return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}';
974: }
975:
976: protected function pStmt_Break(Stmt\Break_ $node): string {
977: return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
978: }
979:
980: protected function pStmt_Continue(Stmt\Continue_ $node): string {
981: return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
982: }
983:
984: protected function pStmt_Return(Stmt\Return_ $node): string {
985: return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
986: }
987:
988: protected function pStmt_Label(Stmt\Label $node): string {
989: return $node->name . ':';
990: }
991:
992: protected function pStmt_Goto(Stmt\Goto_ $node): string {
993: return 'goto ' . $node->name . ';';
994: }
995:
996: // Other
997:
998: protected function pStmt_Expression(Stmt\Expression $node): string {
999: return $this->p($node->expr) . ';';
1000: }
1001:
1002: protected function pStmt_Echo(Stmt\Echo_ $node): string {
1003: return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
1004: }
1005:
1006: protected function pStmt_Static(Stmt\Static_ $node): string {
1007: return 'static ' . $this->pCommaSeparated($node->vars) . ';';
1008: }
1009:
1010: protected function pStmt_Global(Stmt\Global_ $node): string {
1011: return 'global ' . $this->pCommaSeparated($node->vars) . ';';
1012: }
1013:
1014: protected function pStaticVar(Node\StaticVar $node): string {
1015: return $this->p($node->var)
1016: . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
1017: }
1018:
1019: protected function pStmt_Unset(Stmt\Unset_ $node): string {
1020: return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
1021: }
1022:
1023: protected function pStmt_InlineHTML(Stmt\InlineHTML $node): string {
1024: $newline = $node->getAttribute('hasLeadingNewline', true) ? $this->newline : '';
1025: return '?>' . $newline . $node->value . '<?php ';
1026: }
1027:
1028: protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node): string {
1029: return '__halt_compiler();' . $node->remaining;
1030: }
1031:
1032: protected function pStmt_Nop(Stmt\Nop $node): string {
1033: return '';
1034: }
1035:
1036: protected function pStmt_Block(Stmt\Block $node): string {
1037: return '{' . $this->pStmts($node->stmts) . $this->nl . '}';
1038: }
1039:
1040: // Helpers
1041:
1042: protected function pClassCommon(Stmt\Class_ $node, string $afterClassToken): string {
1043: return $this->pAttrGroups($node->attrGroups, $node->name === null)
1044: . $this->pModifiers($node->flags)
1045: . 'class' . $afterClassToken
1046: . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
1047: . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
1048: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
1049: }
1050:
1051: protected function pObjectProperty(Node $node): string {
1052: if ($node instanceof Expr) {
1053: return '{' . $this->p($node) . '}';
1054: } else {
1055: assert($node instanceof Node\Identifier);
1056: return $node->name;
1057: }
1058: }
1059:
1060: /** @param (Expr|Node\InterpolatedStringPart)[] $encapsList */
1061: protected function pEncapsList(array $encapsList, ?string $quote): string {
1062: $return = '';
1063: foreach ($encapsList as $element) {
1064: if ($element instanceof Node\InterpolatedStringPart) {
1065: $return .= $this->escapeString($element->value, $quote);
1066: } else {
1067: $return .= '{' . $this->p($element) . '}';
1068: }
1069: }
1070:
1071: return $return;
1072: }
1073:
1074: protected function pSingleQuotedString(string $string): string {
1075: // It is idiomatic to only escape backslashes when necessary, i.e. when followed by ', \ or
1076: // the end of the string ('Foo\Bar' instead of 'Foo\\Bar'). However, we also don't want to
1077: // produce an odd number of backslashes, so '\\\\a' should not get rendered as '\\\a', even
1078: // though that would be legal.
1079: $regex = '/\'|\\\\(?=[\'\\\\]|$)|(?<=\\\\)\\\\/';
1080: return '\'' . preg_replace($regex, '\\\\$0', $string) . '\'';
1081: }
1082:
1083: protected function escapeString(string $string, ?string $quote): string {
1084: if (null === $quote) {
1085: // For doc strings, don't escape newlines
1086: $escaped = addcslashes($string, "\t\f\v$\\");
1087: // But do escape isolated \r. Combined with the terminating newline, it might get
1088: // interpreted as \r\n and dropped from the string contents.
1089: $escaped = preg_replace('/\r(?!\n)/', '\\r', $escaped);
1090: if ($this->phpVersion->supportsFlexibleHeredoc()) {
1091: $escaped = $this->indentString($escaped);
1092: }
1093: } else {
1094: $escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
1095: }
1096:
1097: // Escape control characters and non-UTF-8 characters.
1098: // Regex based on https://stackoverflow.com/a/11709412/385378.
1099: $regex = '/(
1100: [\x00-\x08\x0E-\x1F] # Control characters
1101: | [\xC0-\xC1] # Invalid UTF-8 Bytes
1102: | [\xF5-\xFF] # Invalid UTF-8 Bytes
1103: | \xE0(?=[\x80-\x9F]) # Overlong encoding of prior code point
1104: | \xF0(?=[\x80-\x8F]) # Overlong encoding of prior code point
1105: | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
1106: | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
1107: | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
1108: | (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
1109: | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
1110: | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
1111: | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
1112: | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
1113: )/x';
1114: return preg_replace_callback($regex, function ($matches): string {
1115: assert(strlen($matches[0]) === 1);
1116: $hex = dechex(ord($matches[0]));
1117: return '\\x' . str_pad($hex, 2, '0', \STR_PAD_LEFT);
1118: }, $escaped);
1119: }
1120:
1121: protected function containsEndLabel(string $string, string $label, bool $atStart = true): bool {
1122: $start = $atStart ? '(?:^|[\r\n])[ \t]*' : '[\r\n][ \t]*';
1123: return false !== strpos($string, $label)
1124: && preg_match('/' . $start . $label . '(?:$|[^_A-Za-z0-9\x80-\xff])/', $string);
1125: }
1126:
1127: /** @param (Expr|Node\InterpolatedStringPart)[] $parts */
1128: protected function encapsedContainsEndLabel(array $parts, string $label): bool {
1129: foreach ($parts as $i => $part) {
1130: if ($part instanceof Node\InterpolatedStringPart
1131: && $this->containsEndLabel($this->escapeString($part->value, null), $label, $i === 0)
1132: ) {
1133: return true;
1134: }
1135: }
1136: return false;
1137: }
1138:
1139: protected function pDereferenceLhs(Node $node): string {
1140: if (!$this->dereferenceLhsRequiresParens($node)) {
1141: return $this->p($node);
1142: } else {
1143: return '(' . $this->p($node) . ')';
1144: }
1145: }
1146:
1147: protected function pStaticDereferenceLhs(Node $node): string {
1148: if (!$this->staticDereferenceLhsRequiresParens($node)) {
1149: return $this->p($node);
1150: } else {
1151: return '(' . $this->p($node) . ')';
1152: }
1153: }
1154:
1155: protected function pCallLhs(Node $node): string {
1156: if (!$this->callLhsRequiresParens($node)) {
1157: return $this->p($node);
1158: } else {
1159: return '(' . $this->p($node) . ')';
1160: }
1161: }
1162:
1163: protected function pNewOperand(Node $node): string {
1164: if (!$this->newOperandRequiresParens($node)) {
1165: return $this->p($node);
1166: } else {
1167: return '(' . $this->p($node) . ')';
1168: }
1169: }
1170:
1171: /**
1172: * @param Node[] $nodes
1173: */
1174: protected function hasNodeWithComments(array $nodes): bool {
1175: foreach ($nodes as $node) {
1176: if ($node && $node->getComments()) {
1177: return true;
1178: }
1179: }
1180: return false;
1181: }
1182:
1183: /** @param Node[] $nodes */
1184: protected function pMaybeMultiline(array $nodes, bool $trailingComma = false): string {
1185: if (!$this->hasNodeWithComments($nodes)) {
1186: return $this->pCommaSeparated($nodes);
1187: } else {
1188: return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
1189: }
1190: }
1191:
1192: /** @param Node\Param[] $params
1193: */
1194: private function hasParamWithAttributes(array $params): bool {
1195: foreach ($params as $param) {
1196: if ($param->attrGroups) {
1197: return true;
1198: }
1199: }
1200: return false;
1201: }
1202:
1203: /** @param Node\Param[] $params */
1204: protected function pParams(array $params): string {
1205: if ($this->hasNodeWithComments($params) ||
1206: ($this->hasParamWithAttributes($params) && !$this->phpVersion->supportsAttributes())
1207: ) {
1208: return $this->pCommaSeparatedMultiline($params, $this->phpVersion->supportsTrailingCommaInParamList()) . $this->nl;
1209: }
1210: return $this->pCommaSeparated($params);
1211: }
1212:
1213: /** @param Node\AttributeGroup[] $nodes */
1214: protected function pAttrGroups(array $nodes, bool $inline = false): string {
1215: $result = '';
1216: $sep = $inline ? ' ' : $this->nl;
1217: foreach ($nodes as $node) {
1218: $result .= $this->p($node) . $sep;
1219: }
1220:
1221: return $result;
1222: }
1223: }
1224: