1 <?php declare(strict_types=1);
3 namespace PhpParser\PrettyPrinter;
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;
16 class Standard extends PrettyPrinterAbstract
20 protected function pParam(Node\Param $node) {
21 return ($node->type ? $this->p($node->type) . ' ' : '')
22 . ($node->byRef ? '&' : '')
23 . ($node->variadic ? '...' : '')
24 . $this->p($node->var)
25 . ($node->default ? ' = ' . $this->p($node->default) : '');
28 protected function pArg(Node\Arg $node) {
29 return ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') . $this->p($node->value);
32 protected function pConst(Node\Const_ $node) {
33 return $node->name . ' = ' . $this->p($node->value);
36 protected function pNullableType(Node\NullableType $node) {
37 return '?' . $this->p($node->type);
40 protected function pIdentifier(Node\Identifier $node) {
44 protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node) {
45 return '$' . $node->name;
50 protected function pName(Name $node) {
51 return implode('\\', $node->parts);
54 protected function pName_FullyQualified(Name\FullyQualified $node) {
55 return '\\' . implode('\\', $node->parts);
58 protected function pName_Relative(Name\Relative $node) {
59 return 'namespace\\' . implode('\\', $node->parts);
64 protected function pScalar_MagicConst_Class(MagicConst\Class_ $node) {
68 protected function pScalar_MagicConst_Dir(MagicConst\Dir $node) {
72 protected function pScalar_MagicConst_File(MagicConst\File $node) {
76 protected function pScalar_MagicConst_Function(MagicConst\Function_ $node) {
77 return '__FUNCTION__';
80 protected function pScalar_MagicConst_Line(MagicConst\Line $node) {
84 protected function pScalar_MagicConst_Method(MagicConst\Method $node) {
88 protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) {
89 return '__NAMESPACE__';
92 protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) {
98 protected function pScalar_String(Scalar\String_ $node) {
99 $kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED);
101 case Scalar\String_::KIND_NOWDOC:
102 $label = $node->getAttribute('docLabel');
103 if ($label && !$this->containsEndLabel($node->value, $label)) {
104 if ($node->value === '') {
105 return "<<<'$label'\n$label" . $this->docStringEndToken;
108 return "<<<'$label'\n$node->value\n$label"
109 . $this->docStringEndToken;
111 /* break missing intentionally */
112 case Scalar\String_::KIND_SINGLE_QUOTED:
113 return $this->pSingleQuotedString($node->value);
114 case Scalar\String_::KIND_HEREDOC:
115 $label = $node->getAttribute('docLabel');
116 if ($label && !$this->containsEndLabel($node->value, $label)) {
117 if ($node->value === '') {
118 return "<<<$label\n$label" . $this->docStringEndToken;
121 $escaped = $this->escapeString($node->value, null);
122 return "<<<$label\n" . $escaped . "\n$label"
123 . $this->docStringEndToken;
125 /* break missing intentionally */
126 case Scalar\String_::KIND_DOUBLE_QUOTED:
127 return '"' . $this->escapeString($node->value, '"') . '"';
129 throw new \Exception('Invalid string kind');
132 protected function pScalar_Encapsed(Scalar\Encapsed $node) {
133 if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) {
134 $label = $node->getAttribute('docLabel');
135 if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) {
136 if (count($node->parts) === 1
137 && $node->parts[0] instanceof Scalar\EncapsedStringPart
138 && $node->parts[0]->value === ''
140 return "<<<$label\n$label" . $this->docStringEndToken;
143 return "<<<$label\n" . $this->pEncapsList($node->parts, null) . "\n$label"
144 . $this->docStringEndToken;
147 return '"' . $this->pEncapsList($node->parts, '"') . '"';
150 protected function pScalar_LNumber(Scalar\LNumber $node) {
151 if ($node->value === -\PHP_INT_MAX-1) {
152 // PHP_INT_MIN cannot be represented as a literal,
153 // because the sign is not part of the literal
154 return '(-' . \PHP_INT_MAX . '-1)';
157 $kind = $node->getAttribute('kind', Scalar\LNumber::KIND_DEC);
158 if (Scalar\LNumber::KIND_DEC === $kind) {
159 return (string) $node->value;
162 $sign = $node->value < 0 ? '-' : '';
163 $str = (string) $node->value;
165 case Scalar\LNumber::KIND_BIN:
166 return $sign . '0b' . base_convert($str, 10, 2);
167 case Scalar\LNumber::KIND_OCT:
168 return $sign . '0' . base_convert($str, 10, 8);
169 case Scalar\LNumber::KIND_HEX:
170 return $sign . '0x' . base_convert($str, 10, 16);
172 throw new \Exception('Invalid number kind');
175 protected function pScalar_DNumber(Scalar\DNumber $node) {
176 if (!is_finite($node->value)) {
177 if ($node->value === \INF) {
179 } elseif ($node->value === -\INF) {
186 // Try to find a short full-precision representation
187 $stringValue = sprintf('%.16G', $node->value);
188 if ($node->value !== (double) $stringValue) {
189 $stringValue = sprintf('%.17G', $node->value);
192 // %G is locale dependent and there exists no locale-independent alternative. We don't want
193 // mess with switching locales here, so let's assume that a comma is the only non-standard
194 // decimal separator we may encounter...
195 $stringValue = str_replace(',', '.', $stringValue);
197 // ensure that number is really printed as float
198 return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
203 protected function pExpr_Assign(Expr\Assign $node) {
204 return $this->pInfixOp(Expr\Assign::class, $node->var, ' = ', $node->expr);
207 protected function pExpr_AssignRef(Expr\AssignRef $node) {
208 return $this->pInfixOp(Expr\AssignRef::class, $node->var, ' =& ', $node->expr);
211 protected function pExpr_AssignOp_Plus(AssignOp\Plus $node) {
212 return $this->pInfixOp(AssignOp\Plus::class, $node->var, ' += ', $node->expr);
215 protected function pExpr_AssignOp_Minus(AssignOp\Minus $node) {
216 return $this->pInfixOp(AssignOp\Minus::class, $node->var, ' -= ', $node->expr);
219 protected function pExpr_AssignOp_Mul(AssignOp\Mul $node) {
220 return $this->pInfixOp(AssignOp\Mul::class, $node->var, ' *= ', $node->expr);
223 protected function pExpr_AssignOp_Div(AssignOp\Div $node) {
224 return $this->pInfixOp(AssignOp\Div::class, $node->var, ' /= ', $node->expr);
227 protected function pExpr_AssignOp_Concat(AssignOp\Concat $node) {
228 return $this->pInfixOp(AssignOp\Concat::class, $node->var, ' .= ', $node->expr);
231 protected function pExpr_AssignOp_Mod(AssignOp\Mod $node) {
232 return $this->pInfixOp(AssignOp\Mod::class, $node->var, ' %= ', $node->expr);
235 protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) {
236 return $this->pInfixOp(AssignOp\BitwiseAnd::class, $node->var, ' &= ', $node->expr);
239 protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) {
240 return $this->pInfixOp(AssignOp\BitwiseOr::class, $node->var, ' |= ', $node->expr);
243 protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) {
244 return $this->pInfixOp(AssignOp\BitwiseXor::class, $node->var, ' ^= ', $node->expr);
247 protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) {
248 return $this->pInfixOp(AssignOp\ShiftLeft::class, $node->var, ' <<= ', $node->expr);
251 protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) {
252 return $this->pInfixOp(AssignOp\ShiftRight::class, $node->var, ' >>= ', $node->expr);
255 protected function pExpr_AssignOp_Pow(AssignOp\Pow $node) {
256 return $this->pInfixOp(AssignOp\Pow::class, $node->var, ' **= ', $node->expr);
259 // Binary expressions
261 protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) {
262 return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right);
265 protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) {
266 return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right);
269 protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) {
270 return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right);
273 protected function pExpr_BinaryOp_Div(BinaryOp\Div $node) {
274 return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right);
277 protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) {
278 return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right);
281 protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) {
282 return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right);
285 protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) {
286 return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right);
289 protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) {
290 return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right);
293 protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) {
294 return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right);
297 protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) {
298 return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right);
301 protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) {
302 return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right);
305 protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) {
306 return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right);
309 protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) {
310 return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right);
313 protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) {
314 return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right);
317 protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) {
318 return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right);
321 protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) {
322 return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right);
325 protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) {
326 return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right);
329 protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) {
330 return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right);
333 protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) {
334 return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right);
337 protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) {
338 return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right);
341 protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) {
342 return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right);
345 protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) {
346 return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right);
349 protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) {
350 return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right);
353 protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) {
354 return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right);
357 protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) {
358 return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right);
361 protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) {
362 return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right);
365 protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) {
366 return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right);
369 protected function pExpr_Instanceof(Expr\Instanceof_ $node) {
370 return $this->pInfixOp(Expr\Instanceof_::class, $node->expr, ' instanceof ', $node->class);
375 protected function pExpr_BooleanNot(Expr\BooleanNot $node) {
376 return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr);
379 protected function pExpr_BitwiseNot(Expr\BitwiseNot $node) {
380 return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr);
383 protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) {
384 if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) {
385 // Enforce -(-$expr) instead of --$expr
386 return '-(' . $this->p($node->expr) . ')';
388 return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr);
391 protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) {
392 if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) {
393 // Enforce +(+$expr) instead of ++$expr
394 return '+(' . $this->p($node->expr) . ')';
396 return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr);
399 protected function pExpr_PreInc(Expr\PreInc $node) {
400 return $this->pPrefixOp(Expr\PreInc::class, '++', $node->var);
403 protected function pExpr_PreDec(Expr\PreDec $node) {
404 return $this->pPrefixOp(Expr\PreDec::class, '--', $node->var);
407 protected function pExpr_PostInc(Expr\PostInc $node) {
408 return $this->pPostfixOp(Expr\PostInc::class, $node->var, '++');
411 protected function pExpr_PostDec(Expr\PostDec $node) {
412 return $this->pPostfixOp(Expr\PostDec::class, $node->var, '--');
415 protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) {
416 return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr);
419 protected function pExpr_YieldFrom(Expr\YieldFrom $node) {
420 return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr);
423 protected function pExpr_Print(Expr\Print_ $node) {
424 return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr);
429 protected function pExpr_Cast_Int(Cast\Int_ $node) {
430 return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr);
433 protected function pExpr_Cast_Double(Cast\Double $node) {
434 return $this->pPrefixOp(Cast\Double::class, '(double) ', $node->expr);
437 protected function pExpr_Cast_String(Cast\String_ $node) {
438 return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr);
441 protected function pExpr_Cast_Array(Cast\Array_ $node) {
442 return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr);
445 protected function pExpr_Cast_Object(Cast\Object_ $node) {
446 return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr);
449 protected function pExpr_Cast_Bool(Cast\Bool_ $node) {
450 return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr);
453 protected function pExpr_Cast_Unset(Cast\Unset_ $node) {
454 return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr);
457 // Function calls and similar constructs
459 protected function pExpr_FuncCall(Expr\FuncCall $node) {
460 return $this->pCallLhs($node->name)
461 . '(' . $this->pMaybeMultiline($node->args) . ')';
464 protected function pExpr_MethodCall(Expr\MethodCall $node) {
465 return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
466 . '(' . $this->pMaybeMultiline($node->args) . ')';
469 protected function pExpr_StaticCall(Expr\StaticCall $node) {
470 return $this->pDereferenceLhs($node->class) . '::'
471 . ($node->name instanceof Expr
472 ? ($node->name instanceof Expr\Variable
473 ? $this->p($node->name)
474 : '{' . $this->p($node->name) . '}')
476 . '(' . $this->pMaybeMultiline($node->args) . ')';
479 protected function pExpr_Empty(Expr\Empty_ $node) {
480 return 'empty(' . $this->p($node->expr) . ')';
483 protected function pExpr_Isset(Expr\Isset_ $node) {
484 return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
487 protected function pExpr_Eval(Expr\Eval_ $node) {
488 return 'eval(' . $this->p($node->expr) . ')';
491 protected function pExpr_Include(Expr\Include_ $node) {
493 Expr\Include_::TYPE_INCLUDE => 'include',
494 Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
495 Expr\Include_::TYPE_REQUIRE => 'require',
496 Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
499 return $map[$node->type] . ' ' . $this->p($node->expr);
502 protected function pExpr_List(Expr\List_ $node) {
503 return 'list(' . $this->pCommaSeparated($node->items) . ')';
508 protected function pExpr_Error(Expr\Error $node) {
509 throw new \LogicException('Cannot pretty-print AST with Error nodes');
512 protected function pExpr_Variable(Expr\Variable $node) {
513 if ($node->name instanceof Expr) {
514 return '${' . $this->p($node->name) . '}';
516 return '$' . $node->name;
520 protected function pExpr_Array(Expr\Array_ $node) {
521 $syntax = $node->getAttribute('kind',
522 $this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
523 if ($syntax === Expr\Array_::KIND_SHORT) {
524 return '[' . $this->pMaybeMultiline($node->items, true) . ']';
526 return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
530 protected function pExpr_ArrayItem(Expr\ArrayItem $node) {
531 return (null !== $node->key ? $this->p($node->key) . ' => ' : '')
532 . ($node->byRef ? '&' : '') . $this->p($node->value);
535 protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) {
536 return $this->pDereferenceLhs($node->var)
537 . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
540 protected function pExpr_ConstFetch(Expr\ConstFetch $node) {
541 return $this->p($node->name);
544 protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) {
545 return $this->p($node->class) . '::' . $this->p($node->name);
548 protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) {
549 return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name);
552 protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) {
553 return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name);
556 protected function pExpr_ShellExec(Expr\ShellExec $node) {
557 return '`' . $this->pEncapsList($node->parts, '`') . '`';
560 protected function pExpr_Closure(Expr\Closure $node) {
561 return ($node->static ? 'static ' : '')
562 . 'function ' . ($node->byRef ? '&' : '')
563 . '(' . $this->pCommaSeparated($node->params) . ')'
564 . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')' : '')
565 . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
566 . ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
569 protected function pExpr_ClosureUse(Expr\ClosureUse $node) {
570 return ($node->byRef ? '&' : '') . $this->p($node->var);
573 protected function pExpr_New(Expr\New_ $node) {
574 if ($node->class instanceof Stmt\Class_) {
575 $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
576 return 'new ' . $this->pClassCommon($node->class, $args);
578 return 'new ' . $this->p($node->class) . '(' . $this->pMaybeMultiline($node->args) . ')';
581 protected function pExpr_Clone(Expr\Clone_ $node) {
582 return 'clone ' . $this->p($node->expr);
585 protected function pExpr_Ternary(Expr\Ternary $node) {
586 // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
587 // this is okay because the part between ? and : never needs parentheses.
588 return $this->pInfixOp(Expr\Ternary::class,
589 $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else
593 protected function pExpr_Exit(Expr\Exit_ $node) {
594 $kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE);
595 return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die')
596 . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
599 protected function pExpr_Yield(Expr\Yield_ $node) {
600 if ($node->value === null) {
603 // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary
605 . ($node->key !== null ? $this->p($node->key) . ' => ' : '')
606 . $this->p($node->value)
613 protected function pStmt_Namespace(Stmt\Namespace_ $node) {
614 if ($this->canUseSemicolonNamespaces) {
615 return 'namespace ' . $this->p($node->name) . ';'
616 . $this->nl . $this->pStmts($node->stmts, false);
618 return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
619 . ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
623 protected function pStmt_Use(Stmt\Use_ $node) {
624 return 'use ' . $this->pUseType($node->type)
625 . $this->pCommaSeparated($node->uses) . ';';
628 protected function pStmt_GroupUse(Stmt\GroupUse $node) {
629 return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix)
630 . '\{' . $this->pCommaSeparated($node->uses) . '};';
633 protected function pStmt_UseUse(Stmt\UseUse $node) {
634 return $this->pUseType($node->type) . $this->p($node->name)
635 . (null !== $node->alias ? ' as ' . $node->alias : '');
638 protected function pUseType($type) {
639 return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
640 : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
643 protected function pStmt_Interface(Stmt\Interface_ $node) {
644 return 'interface ' . $node->name
645 . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
646 . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
649 protected function pStmt_Class(Stmt\Class_ $node) {
650 return $this->pClassCommon($node, ' ' . $node->name);
653 protected function pStmt_Trait(Stmt\Trait_ $node) {
654 return 'trait ' . $node->name
655 . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
658 protected function pStmt_TraitUse(Stmt\TraitUse $node) {
659 return 'use ' . $this->pCommaSeparated($node->traits)
660 . (empty($node->adaptations)
662 : ' {' . $this->pStmts($node->adaptations) . $this->nl . '}');
665 protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) {
666 return $this->p($node->trait) . '::' . $node->method
667 . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
670 protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) {
671 return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
672 . $node->method . ' as'
673 . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '')
674 . (null !== $node->newName ? ' ' . $node->newName : '')
678 protected function pStmt_Property(Stmt\Property $node) {
679 return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags)) . $this->pCommaSeparated($node->props) . ';';
682 protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
683 return '$' . $node->name
684 . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
687 protected function pStmt_ClassMethod(Stmt\ClassMethod $node) {
688 return $this->pModifiers($node->flags)
689 . 'function ' . ($node->byRef ? '&' : '') . $node->name
690 . '(' . $this->pCommaSeparated($node->params) . ')'
691 . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
692 . (null !== $node->stmts
693 ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'
697 protected function pStmt_ClassConst(Stmt\ClassConst $node) {
698 return $this->pModifiers($node->flags)
699 . 'const ' . $this->pCommaSeparated($node->consts) . ';';
702 protected function pStmt_Function(Stmt\Function_ $node) {
703 return 'function ' . ($node->byRef ? '&' : '') . $node->name
704 . '(' . $this->pCommaSeparated($node->params) . ')'
705 . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
706 . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
709 protected function pStmt_Const(Stmt\Const_ $node) {
710 return 'const ' . $this->pCommaSeparated($node->consts) . ';';
713 protected function pStmt_Declare(Stmt\Declare_ $node) {
714 return 'declare (' . $this->pCommaSeparated($node->declares) . ')'
715 . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';');
718 protected function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) {
719 return $node->key . '=' . $this->p($node->value);
724 protected function pStmt_If(Stmt\If_ $node) {
725 return 'if (' . $this->p($node->cond) . ') {'
726 . $this->pStmts($node->stmts) . $this->nl . '}'
727 . ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '')
728 . (null !== $node->else ? ' ' . $this->p($node->else) : '');
731 protected function pStmt_ElseIf(Stmt\ElseIf_ $node) {
732 return 'elseif (' . $this->p($node->cond) . ') {'
733 . $this->pStmts($node->stmts) . $this->nl . '}';
736 protected function pStmt_Else(Stmt\Else_ $node) {
737 return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}';
740 protected function pStmt_For(Stmt\For_ $node) {
742 . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
743 . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
744 . $this->pCommaSeparated($node->loop)
745 . ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
748 protected function pStmt_Foreach(Stmt\Foreach_ $node) {
749 return 'foreach (' . $this->p($node->expr) . ' as '
750 . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
751 . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
752 . $this->pStmts($node->stmts) . $this->nl . '}';
755 protected function pStmt_While(Stmt\While_ $node) {
756 return 'while (' . $this->p($node->cond) . ') {'
757 . $this->pStmts($node->stmts) . $this->nl . '}';
760 protected function pStmt_Do(Stmt\Do_ $node) {
761 return 'do {' . $this->pStmts($node->stmts) . $this->nl
762 . '} while (' . $this->p($node->cond) . ');';
765 protected function pStmt_Switch(Stmt\Switch_ $node) {
766 return 'switch (' . $this->p($node->cond) . ') {'
767 . $this->pStmts($node->cases) . $this->nl . '}';
770 protected function pStmt_Case(Stmt\Case_ $node) {
771 return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
772 . $this->pStmts($node->stmts);
775 protected function pStmt_TryCatch(Stmt\TryCatch $node) {
776 return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}'
777 . ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '')
778 . ($node->finally !== null ? ' ' . $this->p($node->finally) : '');
781 protected function pStmt_Catch(Stmt\Catch_ $node) {
782 return 'catch (' . $this->pImplode($node->types, '|') . ' '
783 . $this->p($node->var)
784 . ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
787 protected function pStmt_Finally(Stmt\Finally_ $node) {
788 return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}';
791 protected function pStmt_Break(Stmt\Break_ $node) {
792 return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
795 protected function pStmt_Continue(Stmt\Continue_ $node) {
796 return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
799 protected function pStmt_Return(Stmt\Return_ $node) {
800 return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
803 protected function pStmt_Throw(Stmt\Throw_ $node) {
804 return 'throw ' . $this->p($node->expr) . ';';
807 protected function pStmt_Label(Stmt\Label $node) {
808 return $node->name . ':';
811 protected function pStmt_Goto(Stmt\Goto_ $node) {
812 return 'goto ' . $node->name . ';';
817 protected function pStmt_Expression(Stmt\Expression $node) {
818 return $this->p($node->expr) . ';';
821 protected function pStmt_Echo(Stmt\Echo_ $node) {
822 return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
825 protected function pStmt_Static(Stmt\Static_ $node) {
826 return 'static ' . $this->pCommaSeparated($node->vars) . ';';
829 protected function pStmt_Global(Stmt\Global_ $node) {
830 return 'global ' . $this->pCommaSeparated($node->vars) . ';';
833 protected function pStmt_StaticVar(Stmt\StaticVar $node) {
834 return $this->p($node->var)
835 . (null !== $node->default ? ' = ' . $this->p($node->default) : '');
838 protected function pStmt_Unset(Stmt\Unset_ $node) {
839 return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
842 protected function pStmt_InlineHTML(Stmt\InlineHTML $node) {
843 $newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : '';
844 return '?>' . $newline . $node->value . '<?php ';
847 protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) {
848 return '__halt_compiler();' . $node->remaining;
851 protected function pStmt_Nop(Stmt\Nop $node) {
857 protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) {
858 return $this->pModifiers($node->flags)
859 . 'class' . $afterClassToken
860 . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
861 . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
862 . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
865 protected function pObjectProperty($node) {
866 if ($node instanceof Expr) {
867 return '{' . $this->p($node) . '}';
873 protected function pEncapsList(array $encapsList, $quote) {
875 foreach ($encapsList as $element) {
876 if ($element instanceof Scalar\EncapsedStringPart) {
877 $return .= $this->escapeString($element->value, $quote);
879 $return .= '{' . $this->p($element) . '}';
886 protected function pSingleQuotedString(string $string) {
887 return '\'' . addcslashes($string, '\'\\') . '\'';
890 protected function escapeString($string, $quote) {
891 if (null === $quote) {
892 // For doc strings, don't escape newlines
893 $escaped = addcslashes($string, "\t\f\v$\\");
895 $escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
898 // Escape other control characters
899 return preg_replace_callback('/([\0-\10\16-\37])(?=([0-7]?))/', function ($matches) {
900 $oct = decoct(ord($matches[1]));
901 if ($matches[2] !== '') {
902 // If there is a trailing digit, use the full three character form
903 return '\\' . str_pad($oct, 3, '0', \STR_PAD_LEFT);
909 protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) {
910 $start = $atStart ? '(?:^|[\r\n])' : '[\r\n]';
911 $end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]';
912 return false !== strpos($string, $label)
913 && preg_match('/' . $start . $label . $end . '/', $string);
916 protected function encapsedContainsEndLabel(array $parts, $label) {
917 foreach ($parts as $i => $part) {
919 $atEnd = $i === count($parts) - 1;
920 if ($part instanceof Scalar\EncapsedStringPart
921 && $this->containsEndLabel($part->value, $label, $atStart, $atEnd)
929 protected function pDereferenceLhs(Node $node) {
930 if (!$this->dereferenceLhsRequiresParens($node)) {
931 return $this->p($node);
933 return '(' . $this->p($node) . ')';
937 protected function pCallLhs(Node $node) {
938 if (!$this->callLhsRequiresParens($node)) {
939 return $this->p($node);
941 return '(' . $this->p($node) . ')';
946 * @param Node[] $nodes
949 private function hasNodeWithComments(array $nodes) {
950 foreach ($nodes as $node) {
951 if ($node && $node->getComments()) {
958 private function pMaybeMultiline(array $nodes, $trailingComma = false) {
959 if (!$this->hasNodeWithComments($nodes)) {
960 return $this->pCommaSeparated($nodes);
962 return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;