1 <?php declare(strict_types=1);
5 use PhpParser\Internal\DiffElem;
6 use PhpParser\Internal\PrintableNewAnonClassNode;
7 use PhpParser\Internal\TokenStream;
8 use PhpParser\Node\Expr;
9 use PhpParser\Node\Expr\AssignOp;
10 use PhpParser\Node\Expr\BinaryOp;
11 use PhpParser\Node\Expr\Cast;
12 use PhpParser\Node\Name;
13 use PhpParser\Node\Scalar;
14 use PhpParser\Node\Stmt;
16 abstract class PrettyPrinterAbstract
18 const FIXUP_PREC_LEFT = 0; // LHS operand affected by precedence
19 const FIXUP_PREC_RIGHT = 1; // RHS operand affected by precedence
20 const FIXUP_CALL_LHS = 2; // LHS of call
21 const FIXUP_DEREF_LHS = 3; // LHS of dereferencing operation
22 const FIXUP_BRACED_NAME = 4; // Name operand that may require bracing
23 const FIXUP_VAR_BRACED_NAME = 5; // Name operand that may require ${} bracing
24 const FIXUP_ENCAPSED = 6; // Encapsed string part
26 protected $precedenceMap = [
27 // [precedence, associativity]
28 // where for precedence -1 is %left, 0 is %nonassoc and 1 is %right
29 BinaryOp\Pow::class => [ 0, 1],
30 Expr\BitwiseNot::class => [ 10, 1],
31 Expr\PreInc::class => [ 10, 1],
32 Expr\PreDec::class => [ 10, 1],
33 Expr\PostInc::class => [ 10, -1],
34 Expr\PostDec::class => [ 10, -1],
35 Expr\UnaryPlus::class => [ 10, 1],
36 Expr\UnaryMinus::class => [ 10, 1],
37 Cast\Int_::class => [ 10, 1],
38 Cast\Double::class => [ 10, 1],
39 Cast\String_::class => [ 10, 1],
40 Cast\Array_::class => [ 10, 1],
41 Cast\Object_::class => [ 10, 1],
42 Cast\Bool_::class => [ 10, 1],
43 Cast\Unset_::class => [ 10, 1],
44 Expr\ErrorSuppress::class => [ 10, 1],
45 Expr\Instanceof_::class => [ 20, 0],
46 Expr\BooleanNot::class => [ 30, 1],
47 BinaryOp\Mul::class => [ 40, -1],
48 BinaryOp\Div::class => [ 40, -1],
49 BinaryOp\Mod::class => [ 40, -1],
50 BinaryOp\Plus::class => [ 50, -1],
51 BinaryOp\Minus::class => [ 50, -1],
52 BinaryOp\Concat::class => [ 50, -1],
53 BinaryOp\ShiftLeft::class => [ 60, -1],
54 BinaryOp\ShiftRight::class => [ 60, -1],
55 BinaryOp\Smaller::class => [ 70, 0],
56 BinaryOp\SmallerOrEqual::class => [ 70, 0],
57 BinaryOp\Greater::class => [ 70, 0],
58 BinaryOp\GreaterOrEqual::class => [ 70, 0],
59 BinaryOp\Equal::class => [ 80, 0],
60 BinaryOp\NotEqual::class => [ 80, 0],
61 BinaryOp\Identical::class => [ 80, 0],
62 BinaryOp\NotIdentical::class => [ 80, 0],
63 BinaryOp\Spaceship::class => [ 80, 0],
64 BinaryOp\BitwiseAnd::class => [ 90, -1],
65 BinaryOp\BitwiseXor::class => [100, -1],
66 BinaryOp\BitwiseOr::class => [110, -1],
67 BinaryOp\BooleanAnd::class => [120, -1],
68 BinaryOp\BooleanOr::class => [130, -1],
69 BinaryOp\Coalesce::class => [140, 1],
70 Expr\Ternary::class => [150, -1],
71 // parser uses %left for assignments, but they really behave as %right
72 Expr\Assign::class => [160, 1],
73 Expr\AssignRef::class => [160, 1],
74 AssignOp\Plus::class => [160, 1],
75 AssignOp\Minus::class => [160, 1],
76 AssignOp\Mul::class => [160, 1],
77 AssignOp\Div::class => [160, 1],
78 AssignOp\Concat::class => [160, 1],
79 AssignOp\Mod::class => [160, 1],
80 AssignOp\BitwiseAnd::class => [160, 1],
81 AssignOp\BitwiseOr::class => [160, 1],
82 AssignOp\BitwiseXor::class => [160, 1],
83 AssignOp\ShiftLeft::class => [160, 1],
84 AssignOp\ShiftRight::class => [160, 1],
85 AssignOp\Pow::class => [160, 1],
86 Expr\YieldFrom::class => [165, 1],
87 Expr\Print_::class => [168, 1],
88 BinaryOp\LogicalAnd::class => [170, -1],
89 BinaryOp\LogicalXor::class => [180, -1],
90 BinaryOp\LogicalOr::class => [190, -1],
91 Expr\Include_::class => [200, -1],
94 /** @var int Current indentation level. */
95 protected $indentLevel;
96 /** @var string Newline including current indentation. */
98 /** @var string Token placed at end of doc string to ensure it is followed by a newline. */
99 protected $docStringEndToken;
100 /** @var bool Whether semicolon namespaces can be used (i.e. no global namespace is used) */
101 protected $canUseSemicolonNamespaces;
102 /** @var array Pretty printer options */
105 /** @var TokenStream Original tokens for use in format-preserving pretty print */
106 protected $origTokens;
107 /** @var Internal\Differ Differ for node lists */
108 protected $nodeListDiffer;
109 /** @var bool[] Map determining whether a certain character is a label character */
110 protected $labelCharMap;
112 * @var int[][] Map from token classes and subnode names to FIXUP_* constants. This is used
113 * during format-preserving prints to place additional parens/braces if necessary.
117 * @var int[][] Map from "{$node->getType()}->{$subNode}" to ['left' => $l, 'right' => $r],
118 * where $l and $r specify the token type that needs to be stripped when removing
121 protected $removalMap;
123 * @var mixed[] Map from "{$node->getType()}->{$subNode}" to [$find, $extraLeft, $extraRight].
124 * $find is an optional token after which the insertion occurs. $extraLeft/Right
125 * are optionally added before/after the main insertions.
127 protected $insertionMap;
129 * @var string[] Map From "{$node->getType()}->{$subNode}" to string that should be inserted
130 * between elements of this list subnode.
132 protected $listInsertionMap;
133 /** @var int[] Map from "{$node->getType()}->{$subNode}" to token before which the modifiers
134 * should be reprinted. */
135 protected $modifierChangeMap;
138 * Creates a pretty printer instance using the given options.
141 * * bool $shortArraySyntax = false: Whether to use [] instead of array() as the default array
142 * syntax, if the node does not specify a format.
144 * @param array $options Dictionary of formatting options
146 public function __construct(array $options = []) {
147 $this->docStringEndToken = '_DOC_STRING_END_' . mt_rand();
149 $defaultOptions = ['shortArraySyntax' => false];
150 $this->options = $options + $defaultOptions;
154 * Reset pretty printing state.
156 protected function resetState() {
157 $this->indentLevel = 0;
159 $this->origTokens = null;
163 * Set indentation level
165 * @param int $level Level in number of spaces
167 protected function setIndentLevel(int $level) {
168 $this->indentLevel = $level;
169 $this->nl = "\n" . \str_repeat(' ', $level);
173 * Increase indentation level.
175 protected function indent() {
176 $this->indentLevel += 4;
181 * Decrease indentation level.
183 protected function outdent() {
184 assert($this->indentLevel >= 4);
185 $this->indentLevel -= 4;
186 $this->nl = "\n" . str_repeat(' ', $this->indentLevel);
190 * Pretty prints an array of statements.
192 * @param Node[] $stmts Array of statements
194 * @return string Pretty printed statements
196 public function prettyPrint(array $stmts) : string {
198 $this->preprocessNodes($stmts);
200 return ltrim($this->handleMagicTokens($this->pStmts($stmts, false)));
204 * Pretty prints an expression.
206 * @param Expr $node Expression node
208 * @return string Pretty printed node
210 public function prettyPrintExpr(Expr $node) : string {
212 return $this->handleMagicTokens($this->p($node));
216 * Pretty prints a file of statements (includes the opening <?php tag if it is required).
218 * @param Node[] $stmts Array of statements
220 * @return string Pretty printed statements
222 public function prettyPrintFile(array $stmts) : string {
227 $p = "<?php\n\n" . $this->prettyPrint($stmts);
229 if ($stmts[0] instanceof Stmt\InlineHTML) {
230 $p = preg_replace('/^<\?php\s+\?>\n?/', '', $p);
232 if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
233 $p = preg_replace('/<\?php$/', '', rtrim($p));
240 * Preprocesses the top-level nodes to initialize pretty printer state.
242 * @param Node[] $nodes Array of nodes
244 protected function preprocessNodes(array $nodes) {
245 /* We can use semicolon-namespaces unless there is a global namespace declaration */
246 $this->canUseSemicolonNamespaces = true;
247 foreach ($nodes as $node) {
248 if ($node instanceof Stmt\Namespace_ && null === $node->name) {
249 $this->canUseSemicolonNamespaces = false;
256 * Handles (and removes) no-indent and doc-string-end tokens.
261 protected function handleMagicTokens(string $str) : string {
262 // Replace doc-string-end tokens with nothing or a newline
263 $str = str_replace($this->docStringEndToken . ";\n", ";\n", $str);
264 $str = str_replace($this->docStringEndToken, "\n", $str);
270 * Pretty prints an array of nodes (statements) and indents them optionally.
272 * @param Node[] $nodes Array of nodes
273 * @param bool $indent Whether to indent the printed nodes
275 * @return string Pretty printed statements
277 protected function pStmts(array $nodes, bool $indent = true) : string {
283 foreach ($nodes as $node) {
284 $comments = $node->getComments();
286 $result .= $this->nl . $this->pComments($comments);
287 if ($node instanceof Stmt\Nop) {
292 $result .= $this->nl . $this->p($node);
303 * Pretty-print an infix operation while taking precedence into account.
305 * @param string $class Node class of operator
306 * @param Node $leftNode Left-hand side node
307 * @param string $operatorString String representation of the operator
308 * @param Node $rightNode Right-hand side node
310 * @return string Pretty printed infix operation
312 protected function pInfixOp(string $class, Node $leftNode, string $operatorString, Node $rightNode) : string {
313 list($precedence, $associativity) = $this->precedenceMap[$class];
315 return $this->pPrec($leftNode, $precedence, $associativity, -1)
317 . $this->pPrec($rightNode, $precedence, $associativity, 1);
321 * Pretty-print a prefix operation while taking precedence into account.
323 * @param string $class Node class of operator
324 * @param string $operatorString String representation of the operator
325 * @param Node $node Node
327 * @return string Pretty printed prefix operation
329 protected function pPrefixOp(string $class, string $operatorString, Node $node) : string {
330 list($precedence, $associativity) = $this->precedenceMap[$class];
331 return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
335 * Pretty-print a postfix operation while taking precedence into account.
337 * @param string $class Node class of operator
338 * @param string $operatorString String representation of the operator
339 * @param Node $node Node
341 * @return string Pretty printed postfix operation
343 protected function pPostfixOp(string $class, Node $node, string $operatorString) : string {
344 list($precedence, $associativity) = $this->precedenceMap[$class];
345 return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
349 * Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
351 * @param Node $node Node to pretty print
352 * @param int $parentPrecedence Precedence of the parent operator
353 * @param int $parentAssociativity Associativity of parent operator
354 * (-1 is left, 0 is nonassoc, 1 is right)
355 * @param int $childPosition Position of the node relative to the operator
356 * (-1 is left, 1 is right)
358 * @return string The pretty printed node
360 protected function pPrec(Node $node, int $parentPrecedence, int $parentAssociativity, int $childPosition) : string {
361 $class = \get_class($node);
362 if (isset($this->precedenceMap[$class])) {
363 $childPrecedence = $this->precedenceMap[$class][0];
364 if ($childPrecedence > $parentPrecedence
365 || ($parentPrecedence === $childPrecedence && $parentAssociativity !== $childPosition)
367 return '(' . $this->p($node) . ')';
371 return $this->p($node);
375 * Pretty prints an array of nodes and implodes the printed values.
377 * @param Node[] $nodes Array of Nodes to be printed
378 * @param string $glue Character to implode with
380 * @return string Imploded pretty printed nodes
382 protected function pImplode(array $nodes, string $glue = '') : string {
384 foreach ($nodes as $node) {
385 if (null === $node) {
388 $pNodes[] = $this->p($node);
392 return implode($glue, $pNodes);
396 * Pretty prints an array of nodes and implodes the printed values with commas.
398 * @param Node[] $nodes Array of Nodes to be printed
400 * @return string Comma separated pretty printed nodes
402 protected function pCommaSeparated(array $nodes) : string {
403 return $this->pImplode($nodes, ', ');
407 * Pretty prints a comma-separated list of nodes in multiline style, including comments.
409 * The result includes a leading newline and one level of indentation (same as pStmts).
411 * @param Node[] $nodes Array of Nodes to be printed
412 * @param bool $trailingComma Whether to use a trailing comma
414 * @return string Comma separated pretty printed nodes in multiline style
416 protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma) : string {
420 $lastIdx = count($nodes) - 1;
421 foreach ($nodes as $idx => $node) {
422 if ($node !== null) {
423 $comments = $node->getComments();
425 $result .= $this->nl . $this->pComments($comments);
428 $result .= $this->nl . $this->p($node);
430 $result .= $this->nl;
432 if ($trailingComma || $idx !== $lastIdx) {
442 * Prints reformatted text of the passed comments.
444 * @param Comment[] $comments List of comments
446 * @return string Reformatted text of comments
448 protected function pComments(array $comments) : string {
449 $formattedComments = [];
451 foreach ($comments as $comment) {
452 $formattedComments[] = str_replace("\n", $this->nl, $comment->getReformattedText());
455 return implode($this->nl, $formattedComments);
459 * Perform a format-preserving pretty print of an AST.
461 * The format preservation is best effort. For some changes to the AST the formatting will not
462 * be preserved (at least not locally).
464 * In order to use this method a number of prerequisites must be satisfied:
465 * * The startTokenPos and endTokenPos attributes in the lexer must be enabled.
466 * * The CloningVisitor must be run on the AST prior to modification.
467 * * The original tokens must be provided, using the getTokens() method on the lexer.
469 * @param Node[] $stmts Modified AST with links to original AST
470 * @param Node[] $origStmts Original AST with token offset information
471 * @param array $origTokens Tokens of the original code
475 public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens) : string {
476 $this->initializeNodeListDiffer();
477 $this->initializeLabelCharMap();
478 $this->initializeFixupMap();
479 $this->initializeRemovalMap();
480 $this->initializeInsertionMap();
481 $this->initializeListInsertionMap();
482 $this->initializeModifierChangeMap();
485 $this->origTokens = new TokenStream($origTokens);
487 $this->preprocessNodes($stmts);
490 $result = $this->pArray($stmts, $origStmts, $pos, 0, 'stmts', null, "\n");
491 if (null !== $result) {
492 $result .= $this->origTokens->getTokenCode($pos, count($origTokens), 0);
495 // TODO Add <?php properly
496 $result = "<?php\n" . $this->pStmts($stmts, false);
499 return ltrim($this->handleMagicTokens($result));
502 protected function pFallback(Node $node) {
503 return $this->{'p' . $node->getType()}($node);
507 * Pretty prints a node.
509 * This method also handles formatting preservation for nodes.
511 * @param Node $node Node to be pretty printed
512 * @param bool $parentFormatPreserved Whether parent node has preserved formatting
514 * @return string Pretty printed node
516 protected function p(Node $node, $parentFormatPreserved = false) : string {
517 // No orig tokens means this is a normal pretty print without preservation of formatting
518 if (!$this->origTokens) {
519 return $this->{'p' . $node->getType()}($node);
522 /** @var Node $origNode */
523 $origNode = $node->getAttribute('origNode');
524 if (null === $origNode) {
525 return $this->pFallback($node);
528 $class = \get_class($node);
529 \assert($class === \get_class($origNode));
531 $startPos = $origNode->getStartTokenPos();
532 $endPos = $origNode->getEndTokenPos();
533 \assert($startPos >= 0 && $endPos >= 0);
535 $fallbackNode = $node;
536 if ($node instanceof Expr\New_ && $node->class instanceof Stmt\Class_) {
537 // Normalize node structure of anonymous classes
538 $node = PrintableNewAnonClassNode::fromNewNode($node);
539 $origNode = PrintableNewAnonClassNode::fromNewNode($origNode);
542 // InlineHTML node does not contain closing and opening PHP tags. If the parent formatting
543 // is not preserved, then we need to use the fallback code to make sure the tags are
545 if ($node instanceof Stmt\InlineHTML && !$parentFormatPreserved) {
546 return $this->pFallback($fallbackNode);
549 $indentAdjustment = $this->indentLevel - $this->origTokens->getIndentationBefore($startPos);
551 $type = $node->getType();
552 $fixupInfo = $this->fixupMap[$class] ?? null;
556 foreach ($node->getSubNodeNames() as $subNodeName) {
557 $subNode = $node->$subNodeName;
558 $origSubNode = $origNode->$subNodeName;
560 if ((!$subNode instanceof Node && $subNode !== null)
561 || (!$origSubNode instanceof Node && $origSubNode !== null)
563 if ($subNode === $origSubNode) {
564 // Unchanged, can reuse old code
568 if (is_array($subNode) && is_array($origSubNode)) {
569 // Array subnode changed, we might be able to reconstruct it
570 $listResult = $this->pArray(
571 $subNode, $origSubNode, $pos, $indentAdjustment, $subNodeName,
572 $fixupInfo[$subNodeName] ?? null,
573 $this->listInsertionMap[$type . '->' . $subNodeName] ?? null
575 if (null === $listResult) {
576 return $this->pFallback($fallbackNode);
579 $result .= $listResult;
583 if (is_int($subNode) && is_int($origSubNode)) {
584 // Check if this is a modifier change
585 $key = $type . '->' . $subNodeName;
586 if (!isset($this->modifierChangeMap[$key])) {
587 return $this->pFallback($fallbackNode);
590 $findToken = $this->modifierChangeMap[$key];
591 $result .= $this->pModifiers($subNode);
592 $pos = $this->origTokens->findRight($pos, $findToken);
596 // If a non-node, non-array subnode changed, we don't be able to do a partial
597 // reconstructions, as we don't have enough offset information. Pretty print the
598 // whole node instead.
599 return $this->pFallback($fallbackNode);
604 if ($origSubNode !== null) {
605 $subStartPos = $origSubNode->getStartTokenPos();
606 $subEndPos = $origSubNode->getEndTokenPos();
607 \assert($subStartPos >= 0 && $subEndPos >= 0);
609 if ($subNode === null) {
610 // Both null, nothing to do
614 // A node has been inserted, check if we have insertion information for it
615 $key = $type . '->' . $subNodeName;
616 if (!isset($this->insertionMap[$key])) {
617 return $this->pFallback($fallbackNode);
620 list($findToken, $extraLeft, $extraRight) = $this->insertionMap[$key];
621 if (null !== $findToken) {
622 $subStartPos = $this->origTokens->findRight($pos, $findToken) + 1;
626 if (null === $extraLeft && null !== $extraRight) {
627 // If inserting on the right only, skipping whitespace looks better
628 $subStartPos = $this->origTokens->skipRightWhitespace($subStartPos);
630 $subEndPos = $subStartPos - 1;
633 if (null === $subNode) {
634 // A node has been removed, check if we have removal information for it
635 $key = $type . '->' . $subNodeName;
636 if (!isset($this->removalMap[$key])) {
637 return $this->pFallback($fallbackNode);
640 // Adjust positions to account for additional tokens that must be skipped
641 $removalInfo = $this->removalMap[$key];
642 if (isset($removalInfo['left'])) {
643 $subStartPos = $this->origTokens->skipLeft($subStartPos - 1, $removalInfo['left']) + 1;
645 if (isset($removalInfo['right'])) {
646 $subEndPos = $this->origTokens->skipRight($subEndPos + 1, $removalInfo['right']) - 1;
650 $result .= $this->origTokens->getTokenCode($pos, $subStartPos, $indentAdjustment);
652 if (null !== $subNode) {
653 $result .= $extraLeft;
655 $origIndentLevel = $this->indentLevel;
656 $this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment);
658 // If it's the same node that was previously in this position, it certainly doesn't
659 // need fixup. It's important to check this here, because our fixup checks are more
660 // conservative than strictly necessary.
661 if (isset($fixupInfo[$subNodeName])
662 && $subNode->getAttribute('origNode') !== $origSubNode
664 $fixup = $fixupInfo[$subNodeName];
665 $res = $this->pFixup($fixup, $subNode, $class, $subStartPos, $subEndPos);
667 $res = $this->p($subNode, true);
670 $this->safeAppend($result, $res);
671 $this->setIndentLevel($origIndentLevel);
673 $result .= $extraRight;
676 $pos = $subEndPos + 1;
679 $result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
684 * Perform a format-preserving pretty print of an array.
686 * @param array $nodes New nodes
687 * @param array $origNodes Original nodes
688 * @param int $pos Current token position (updated by reference)
689 * @param int $indentAdjustment Adjustment for indentation
690 * @param string $subNodeName Name of array subnode.
691 * @param null|int $fixup Fixup information for array item nodes
692 * @param null|string $insertStr Separator string to use for insertions
694 * @return null|string Result of pretty print or null if cannot preserve formatting
696 protected function pArray(
697 array $nodes, array $origNodes, int &$pos, int $indentAdjustment,
698 string $subNodeName, $fixup, $insertStr
700 $diff = $this->nodeListDiffer->diffWithReplacements($origNodes, $nodes);
702 $beforeFirstKeepOrReplace = true;
704 $lastElemIndentLevel = $this->indentLevel;
706 $insertNewline = false;
707 if ($insertStr === "\n") {
709 $insertNewline = true;
712 if ($subNodeName === 'stmts' && \count($origNodes) === 1 && \count($nodes) !== 1) {
713 $startPos = $origNodes[0]->getStartTokenPos();
714 $endPos = $origNodes[0]->getEndTokenPos();
715 \assert($startPos >= 0 && $endPos >= 0);
716 if (!$this->origTokens->haveBraces($startPos, $endPos)) {
717 // This was a single statement without braces, but either additional statements
718 // have been added, or the single statement has been removed. This requires the
719 // addition of braces. For now fall back.
720 // TODO: Try to preserve formatting
726 foreach ($diff as $i => $diffElem) {
727 $diffType = $diffElem->type;
728 /** @var Node|null $arrItem */
729 $arrItem = $diffElem->new;
730 /** @var Node|null $origArrItem */
731 $origArrItem = $diffElem->old;
733 if ($diffType === DiffElem::TYPE_KEEP || $diffType === DiffElem::TYPE_REPLACE) {
734 $beforeFirstKeepOrReplace = false;
736 if ($origArrItem === null || $arrItem === null) {
737 // We can only handle the case where both are null
738 if ($origArrItem === $arrItem) {
744 if (!$arrItem instanceof Node || !$origArrItem instanceof Node) {
745 // We can only deal with nodes. This can occur for Names, which use string arrays.
749 $itemStartPos = $origArrItem->getStartTokenPos();
750 $itemEndPos = $origArrItem->getEndTokenPos();
751 \assert($itemStartPos >= 0 && $itemEndPos >= 0);
753 if ($itemEndPos < $itemStartPos) {
754 // End can be before start for Nop nodes, because offsets refer to non-whitespace
755 // locations, which for an "empty" node might result in an inverted order.
756 assert($origArrItem instanceof Stmt\Nop);
760 $origIndentLevel = $this->indentLevel;
761 $lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
762 $this->setIndentLevel($lastElemIndentLevel);
764 $comments = $arrItem->getComments();
765 $origComments = $origArrItem->getComments();
766 $commentStartPos = $origComments ? $origComments[0]->getTokenPos() : $itemStartPos;
767 \assert($commentStartPos >= 0);
769 $commentsChanged = $comments !== $origComments;
770 if ($commentsChanged) {
771 // Remove old comments
772 $itemStartPos = $commentStartPos;
775 if (!empty($delayedAdd)) {
776 $result .= $this->origTokens->getTokenCode(
777 $pos, $commentStartPos, $indentAdjustment);
779 /** @var Node $delayedAddNode */
780 foreach ($delayedAdd as $delayedAddNode) {
781 if ($insertNewline) {
782 $delayedAddComments = $delayedAddNode->getComments();
783 if ($delayedAddComments) {
784 $result .= $this->pComments($delayedAddComments) . $this->nl;
788 $this->safeAppend($result, $this->p($delayedAddNode, true));
790 if ($insertNewline) {
791 $result .= $insertStr . $this->nl;
793 $result .= $insertStr;
797 $result .= $this->origTokens->getTokenCode(
798 $commentStartPos, $itemStartPos, $indentAdjustment);
802 $result .= $this->origTokens->getTokenCode(
803 $pos, $itemStartPos, $indentAdjustment);
806 if ($commentsChanged && $comments) {
808 $result .= $this->pComments($comments) . $this->nl;
810 } elseif ($diffType === DiffElem::TYPE_ADD) {
811 if (null === $insertStr) {
812 // We don't have insertion information for this list type
816 if ($insertStr === ', ' && $this->isMultiline($origNodes)) {
818 $insertNewline = true;
821 if ($beforeFirstKeepOrReplace) {
822 // Will be inserted at the next "replace" or "keep" element
823 $delayedAdd[] = $arrItem;
827 $itemStartPos = $pos;
828 $itemEndPos = $pos - 1;
830 $origIndentLevel = $this->indentLevel;
831 $this->setIndentLevel($lastElemIndentLevel);
833 if ($insertNewline) {
834 $comments = $arrItem->getComments();
836 $result .= $this->nl . $this->pComments($comments);
838 $result .= $insertStr . $this->nl;
840 $result .= $insertStr;
842 } elseif ($diffType === DiffElem::TYPE_REMOVE) {
844 // TODO Handle removal at the start
848 if (!$origArrItem instanceof Node) {
849 // We only support removal for nodes
853 $itemEndPos = $origArrItem->getEndTokenPos();
854 \assert($itemEndPos >= 0);
856 $pos = $itemEndPos + 1;
859 throw new \Exception("Shouldn't happen");
862 if (null !== $fixup && $arrItem->getAttribute('origNode') !== $origArrItem) {
863 $res = $this->pFixup($fixup, $arrItem, null, $itemStartPos, $itemEndPos);
865 $res = $this->p($arrItem, true);
867 $this->safeAppend($result, $res);
869 $this->setIndentLevel($origIndentLevel);
870 $pos = $itemEndPos + 1;
873 if (!empty($delayedAdd)) {
874 // TODO Handle insertion into empty list
882 * Print node with fixups.
884 * Fixups here refer to the addition of extra parentheses, braces or other characters, that
885 * are required to preserve program semantics in a certain context (e.g. to maintain precedence
886 * or because only certain expressions are allowed in certain places).
888 * @param int $fixup Fixup type
889 * @param Node $subNode Subnode to print
890 * @param string|null $parentClass Class of parent node
891 * @param int $subStartPos Original start pos of subnode
892 * @param int $subEndPos Original end pos of subnode
894 * @return string Result of fixed-up print of subnode
896 protected function pFixup(int $fixup, Node $subNode, $parentClass, int $subStartPos, int $subEndPos) : string {
898 case self::FIXUP_PREC_LEFT:
899 case self::FIXUP_PREC_RIGHT:
900 if (!$this->origTokens->haveParens($subStartPos, $subEndPos)) {
901 list($precedence, $associativity) = $this->precedenceMap[$parentClass];
902 return $this->pPrec($subNode, $precedence, $associativity,
903 $fixup === self::FIXUP_PREC_LEFT ? -1 : 1);
906 case self::FIXUP_CALL_LHS:
907 if ($this->callLhsRequiresParens($subNode)
908 && !$this->origTokens->haveParens($subStartPos, $subEndPos)
910 return '(' . $this->p($subNode) . ')';
913 case self::FIXUP_DEREF_LHS:
914 if ($this->dereferenceLhsRequiresParens($subNode)
915 && !$this->origTokens->haveParens($subStartPos, $subEndPos)
917 return '(' . $this->p($subNode) . ')';
920 case self::FIXUP_BRACED_NAME:
921 case self::FIXUP_VAR_BRACED_NAME:
922 if ($subNode instanceof Expr
923 && !$this->origTokens->haveBraces($subStartPos, $subEndPos)
925 return ($fixup === self::FIXUP_VAR_BRACED_NAME ? '$' : '')
926 . '{' . $this->p($subNode) . '}';
929 case self::FIXUP_ENCAPSED:
930 if (!$subNode instanceof Scalar\EncapsedStringPart
931 && !$this->origTokens->haveBraces($subStartPos, $subEndPos)
933 return '{' . $this->p($subNode) . '}';
937 throw new \Exception('Cannot happen');
940 // Nothing special to do
941 return $this->p($subNode);
945 * Appends to a string, ensuring whitespace between label characters.
947 * Example: "echo" and "$x" result in "echo$x", but "echo" and "x" result in "echo x".
948 * Without safeAppend the result would be "echox", which does not preserve semantics.
951 * @param string $append
953 protected function safeAppend(string &$str, string $append) {
954 // $append must not be empty in this function
960 if (!$this->labelCharMap[$append[0]]
961 || !$this->labelCharMap[$str[\strlen($str) - 1]]) {
964 $str .= " " . $append;
969 * Determines whether the LHS of a call must be wrapped in parenthesis.
971 * @param Node $node LHS of a call
973 * @return bool Whether parentheses are required
975 protected function callLhsRequiresParens(Node $node) : bool {
976 return !($node instanceof Node\Name
977 || $node instanceof Expr\Variable
978 || $node instanceof Expr\ArrayDimFetch
979 || $node instanceof Expr\FuncCall
980 || $node instanceof Expr\MethodCall
981 || $node instanceof Expr\StaticCall
982 || $node instanceof Expr\Array_);
986 * Determines whether the LHS of a dereferencing operation must be wrapped in parenthesis.
988 * @param Node $node LHS of dereferencing operation
990 * @return bool Whether parentheses are required
992 protected function dereferenceLhsRequiresParens(Node $node) : bool {
993 return !($node instanceof Expr\Variable
994 || $node instanceof Node\Name
995 || $node instanceof Expr\ArrayDimFetch
996 || $node instanceof Expr\PropertyFetch
997 || $node instanceof Expr\StaticPropertyFetch
998 || $node instanceof Expr\FuncCall
999 || $node instanceof Expr\MethodCall
1000 || $node instanceof Expr\StaticCall
1001 || $node instanceof Expr\Array_
1002 || $node instanceof Scalar\String_
1003 || $node instanceof Expr\ConstFetch
1004 || $node instanceof Expr\ClassConstFetch);
1008 * Print modifiers, including trailing whitespace.
1010 * @param int $modifiers Modifier mask to print
1012 * @return string Printed modifiers
1014 protected function pModifiers(int $modifiers) {
1015 return ($modifiers & Stmt\Class_::MODIFIER_PUBLIC ? 'public ' : '')
1016 . ($modifiers & Stmt\Class_::MODIFIER_PROTECTED ? 'protected ' : '')
1017 . ($modifiers & Stmt\Class_::MODIFIER_PRIVATE ? 'private ' : '')
1018 . ($modifiers & Stmt\Class_::MODIFIER_STATIC ? 'static ' : '')
1019 . ($modifiers & Stmt\Class_::MODIFIER_ABSTRACT ? 'abstract ' : '')
1020 . ($modifiers & Stmt\Class_::MODIFIER_FINAL ? 'final ' : '');
1024 * Determine whether a list of nodes uses multiline formatting.
1026 * @param (Node|null)[] $nodes Node list
1028 * @return bool Whether multiline formatting is used
1030 protected function isMultiline(array $nodes) : bool {
1031 if (\count($nodes) < 2) {
1036 foreach ($nodes as $node) {
1037 if (null === $node) {
1041 $endPos = $node->getEndTokenPos() + 1;
1043 $text = $this->origTokens->getTokenCode($pos, $endPos, 0);
1044 if (false === strpos($text, "\n")) {
1045 // We require that a newline is present between *every* item. If the formatting
1046 // is inconsistent, with only some items having newlines, we don't consider it
1058 * Lazily initializes label char map.
1060 * The label char map determines whether a certain character may occur in a label.
1062 protected function initializeLabelCharMap() {
1063 if ($this->labelCharMap) return;
1065 $this->labelCharMap = [];
1066 for ($i = 0; $i < 256; $i++) {
1067 // Since PHP 7.1 The lower range is 0x80. However, we also want to support code for
1069 $this->labelCharMap[chr($i)] = $i >= 0x7f || ctype_alnum($i);
1074 * Lazily initializes node list differ.
1076 * The node list differ is used to determine differences between two array subnodes.
1078 protected function initializeNodeListDiffer() {
1079 if ($this->nodeListDiffer) return;
1081 $this->nodeListDiffer = new Internal\Differ(function ($a, $b) {
1082 if ($a instanceof Node && $b instanceof Node) {
1083 return $a === $b->getAttribute('origNode');
1085 // Can happen for array destructuring
1086 return $a === null && $b === null;
1091 * Lazily initializes fixup map.
1093 * The fixup map is used to determine whether a certain subnode of a certain node may require
1094 * some kind of "fixup" operation, e.g. the addition of parenthesis or braces.
1096 protected function initializeFixupMap() {
1097 if ($this->fixupMap) return;
1100 Expr\PreInc::class => ['var' => self::FIXUP_PREC_RIGHT],
1101 Expr\PreDec::class => ['var' => self::FIXUP_PREC_RIGHT],
1102 Expr\PostInc::class => ['var' => self::FIXUP_PREC_LEFT],
1103 Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT],
1104 Expr\Instanceof_::class => [
1105 'expr' => self::FIXUP_PREC_LEFT,
1106 'class' => self::FIXUP_PREC_RIGHT,
1108 Expr\Ternary::class => [
1109 'cond' => self::FIXUP_PREC_LEFT,
1110 'else' => self::FIXUP_PREC_RIGHT,
1113 Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS],
1114 Expr\StaticCall::class => ['class' => self::FIXUP_DEREF_LHS],
1115 Expr\ArrayDimFetch::class => ['var' => self::FIXUP_DEREF_LHS],
1116 Expr\MethodCall::class => [
1117 'var' => self::FIXUP_DEREF_LHS,
1118 'name' => self::FIXUP_BRACED_NAME,
1120 Expr\StaticPropertyFetch::class => [
1121 'class' => self::FIXUP_DEREF_LHS,
1122 'name' => self::FIXUP_VAR_BRACED_NAME,
1124 Expr\PropertyFetch::class => [
1125 'var' => self::FIXUP_DEREF_LHS,
1126 'name' => self::FIXUP_BRACED_NAME,
1128 Scalar\Encapsed::class => [
1129 'parts' => self::FIXUP_ENCAPSED,
1134 BinaryOp\Pow::class, BinaryOp\Mul::class, BinaryOp\Div::class, BinaryOp\Mod::class,
1135 BinaryOp\Plus::class, BinaryOp\Minus::class, BinaryOp\Concat::class,
1136 BinaryOp\ShiftLeft::class, BinaryOp\ShiftRight::class, BinaryOp\Smaller::class,
1137 BinaryOp\SmallerOrEqual::class, BinaryOp\Greater::class, BinaryOp\GreaterOrEqual::class,
1138 BinaryOp\Equal::class, BinaryOp\NotEqual::class, BinaryOp\Identical::class,
1139 BinaryOp\NotIdentical::class, BinaryOp\Spaceship::class, BinaryOp\BitwiseAnd::class,
1140 BinaryOp\BitwiseXor::class, BinaryOp\BitwiseOr::class, BinaryOp\BooleanAnd::class,
1141 BinaryOp\BooleanOr::class, BinaryOp\Coalesce::class, BinaryOp\LogicalAnd::class,
1142 BinaryOp\LogicalXor::class, BinaryOp\LogicalOr::class,
1144 foreach ($binaryOps as $binaryOp) {
1145 $this->fixupMap[$binaryOp] = [
1146 'left' => self::FIXUP_PREC_LEFT,
1147 'right' => self::FIXUP_PREC_RIGHT
1152 Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class,
1153 AssignOp\Mul::class, AssignOp\Div::class, AssignOp\Concat::class, AssignOp\Mod::class,
1154 AssignOp\BitwiseAnd::class, AssignOp\BitwiseOr::class, AssignOp\BitwiseXor::class,
1155 AssignOp\ShiftLeft::class, AssignOp\ShiftRight::class, AssignOp\Pow::class,
1157 foreach ($assignOps as $assignOp) {
1158 $this->fixupMap[$assignOp] = [
1159 'var' => self::FIXUP_PREC_LEFT,
1160 'expr' => self::FIXUP_PREC_RIGHT,
1165 Expr\BitwiseNot::class, Expr\BooleanNot::class, Expr\UnaryPlus::class, Expr\UnaryMinus::class,
1166 Cast\Int_::class, Cast\Double::class, Cast\String_::class, Cast\Array_::class,
1167 Cast\Object_::class, Cast\Bool_::class, Cast\Unset_::class, Expr\ErrorSuppress::class,
1168 Expr\YieldFrom::class, Expr\Print_::class, Expr\Include_::class,
1170 foreach ($prefixOps as $prefixOp) {
1171 $this->fixupMap[$prefixOp] = ['expr' => self::FIXUP_PREC_RIGHT];
1176 * Lazily initializes the removal map.
1178 * The removal map is used to determine which additional tokens should be returned when a
1179 * certain node is replaced by null.
1181 protected function initializeRemovalMap() {
1182 if ($this->removalMap) return;
1184 $stripBoth = ['left' => \T_WHITESPACE, 'right' => \T_WHITESPACE];
1185 $stripLeft = ['left' => \T_WHITESPACE];
1186 $stripRight = ['right' => \T_WHITESPACE];
1187 $stripDoubleArrow = ['right' => \T_DOUBLE_ARROW];
1188 $stripColon = ['left' => ':'];
1189 $stripEquals = ['left' => '='];
1190 $this->removalMap = [
1191 'Expr_ArrayDimFetch->dim' => $stripBoth,
1192 'Expr_ArrayItem->key' => $stripDoubleArrow,
1193 'Expr_Closure->returnType' => $stripColon,
1194 'Expr_Exit->expr' => $stripBoth,
1195 'Expr_Ternary->if' => $stripBoth,
1196 'Expr_Yield->key' => $stripDoubleArrow,
1197 'Expr_Yield->value' => $stripBoth,
1198 'Param->type' => $stripRight,
1199 'Param->default' => $stripEquals,
1200 'Stmt_Break->num' => $stripBoth,
1201 'Stmt_ClassMethod->returnType' => $stripColon,
1202 'Stmt_Class->extends' => ['left' => \T_EXTENDS],
1203 'Expr_PrintableNewAnonClass->extends' => ['left' => \T_EXTENDS],
1204 'Stmt_Continue->num' => $stripBoth,
1205 'Stmt_Foreach->keyVar' => $stripDoubleArrow,
1206 'Stmt_Function->returnType' => $stripColon,
1207 'Stmt_If->else' => $stripLeft,
1208 'Stmt_Namespace->name' => $stripLeft,
1209 'Stmt_PropertyProperty->default' => $stripEquals,
1210 'Stmt_Return->expr' => $stripBoth,
1211 'Stmt_StaticVar->default' => $stripEquals,
1212 'Stmt_TraitUseAdaptation_Alias->newName' => $stripLeft,
1213 'Stmt_TryCatch->finally' => $stripLeft,
1214 // 'Stmt_Case->cond': Replace with "default"
1215 // 'Stmt_Class->name': Unclear what to do
1216 // 'Stmt_Declare->stmts': Not a plain node
1217 // 'Stmt_TraitUseAdaptation_Alias->newModifier': Not a plain node
1221 protected function initializeInsertionMap() {
1222 if ($this->insertionMap) return;
1224 // TODO: "yield" where both key and value are inserted doesn't work
1225 $this->insertionMap = [
1226 'Expr_ArrayDimFetch->dim' => ['[', null, null],
1227 'Expr_ArrayItem->key' => [null, null, ' => '],
1228 'Expr_Closure->returnType' => [')', ' : ', null],
1229 'Expr_Ternary->if' => ['?', ' ', ' '],
1230 'Expr_Yield->key' => [\T_YIELD, null, ' => '],
1231 'Expr_Yield->value' => [\T_YIELD, ' ', null],
1232 'Param->type' => [null, null, ' '],
1233 'Param->default' => [null, ' = ', null],
1234 'Stmt_Break->num' => [\T_BREAK, ' ', null],
1235 'Stmt_ClassMethod->returnType' => [')', ' : ', null],
1236 'Stmt_Class->extends' => [null, ' extends ', null],
1237 'Expr_PrintableNewAnonClass->extends' => [null, ' extends ', null],
1238 'Stmt_Continue->num' => [\T_CONTINUE, ' ', null],
1239 'Stmt_Foreach->keyVar' => [\T_AS, null, ' => '],
1240 'Stmt_Function->returnType' => [')', ' : ', null],
1241 'Stmt_If->else' => [null, ' ', null],
1242 'Stmt_Namespace->name' => [\T_NAMESPACE, ' ', null],
1243 'Stmt_PropertyProperty->default' => [null, ' = ', null],
1244 'Stmt_Return->expr' => [\T_RETURN, ' ', null],
1245 'Stmt_StaticVar->default' => [null, ' = ', null],
1246 //'Stmt_TraitUseAdaptation_Alias->newName' => [T_AS, ' ', null], // TODO
1247 'Stmt_TryCatch->finally' => [null, ' ', null],
1249 // 'Expr_Exit->expr': Complicated due to optional ()
1250 // 'Stmt_Case->cond': Conversion from default to case
1251 // 'Stmt_Class->name': Unclear
1252 // 'Stmt_Declare->stmts': Not a proper node
1253 // 'Stmt_TraitUseAdaptation_Alias->newModifier': Not a proper node
1257 protected function initializeListInsertionMap() {
1258 if ($this->listInsertionMap) return;
1260 $this->listInsertionMap = [
1262 //'Expr_ShellExec->parts' => '', // TODO These need to be treated more carefully
1263 //'Scalar_Encapsed->parts' => '',
1264 'Stmt_Catch->types' => '|',
1265 'Stmt_If->elseifs' => ' ',
1266 'Stmt_TryCatch->catches' => ' ',
1268 // comma-separated lists
1269 'Expr_Array->items' => ', ',
1270 'Expr_Closure->params' => ', ',
1271 'Expr_Closure->uses' => ', ',
1272 'Expr_FuncCall->args' => ', ',
1273 'Expr_Isset->vars' => ', ',
1274 'Expr_List->items' => ', ',
1275 'Expr_MethodCall->args' => ', ',
1276 'Expr_New->args' => ', ',
1277 'Expr_PrintableNewAnonClass->args' => ', ',
1278 'Expr_StaticCall->args' => ', ',
1279 'Stmt_ClassConst->consts' => ', ',
1280 'Stmt_ClassMethod->params' => ', ',
1281 'Stmt_Class->implements' => ', ',
1282 'Expr_PrintableNewAnonClass->implements' => ', ',
1283 'Stmt_Const->consts' => ', ',
1284 'Stmt_Declare->declares' => ', ',
1285 'Stmt_Echo->exprs' => ', ',
1286 'Stmt_For->init' => ', ',
1287 'Stmt_For->cond' => ', ',
1288 'Stmt_For->loop' => ', ',
1289 'Stmt_Function->params' => ', ',
1290 'Stmt_Global->vars' => ', ',
1291 'Stmt_GroupUse->uses' => ', ',
1292 'Stmt_Interface->extends' => ', ',
1293 'Stmt_Property->props' => ', ',
1294 'Stmt_StaticVar->vars' => ', ',
1295 'Stmt_TraitUse->traits' => ', ',
1296 'Stmt_TraitUseAdaptation_Precedence->insteadof' => ', ',
1297 'Stmt_Unset->vars' => ', ',
1298 'Stmt_Use->uses' => ', ',
1301 'Expr_Closure->stmts' => "\n",
1302 'Stmt_Case->stmts' => "\n",
1303 'Stmt_Catch->stmts' => "\n",
1304 'Stmt_Class->stmts' => "\n",
1305 'Expr_PrintableNewAnonClass->stmts' => "\n",
1306 'Stmt_Interface->stmts' => "\n",
1307 'Stmt_Trait->stmts' => "\n",
1308 'Stmt_ClassMethod->stmts' => "\n",
1309 'Stmt_Declare->stmts' => "\n",
1310 'Stmt_Do->stmts' => "\n",
1311 'Stmt_ElseIf->stmts' => "\n",
1312 'Stmt_Else->stmts' => "\n",
1313 'Stmt_Finally->stmts' => "\n",
1314 'Stmt_Foreach->stmts' => "\n",
1315 'Stmt_For->stmts' => "\n",
1316 'Stmt_Function->stmts' => "\n",
1317 'Stmt_If->stmts' => "\n",
1318 'Stmt_Namespace->stmts' => "\n",
1319 'Stmt_Switch->cases' => "\n",
1320 'Stmt_TraitUse->adaptations' => "\n",
1321 'Stmt_TryCatch->stmts' => "\n",
1322 'Stmt_While->stmts' => "\n",
1326 protected function initializeModifierChangeMap() {
1327 if ($this->modifierChangeMap) return;
1329 $this->modifierChangeMap = [
1330 'Stmt_ClassConst->flags' => \T_CONST,
1331 'Stmt_ClassMethod->flags' => \T_FUNCTION,
1332 'Stmt_Class->flags' => \T_CLASS,
1333 'Stmt_Property->flags' => \T_VARIABLE,
1334 //'Stmt_TraitUseAdaptation_Alias->newModifier' => 0, // TODO
1337 // List of integer subnodes that are not modifiers:
1338 // Expr_Include->type
1339 // Stmt_GroupUse->type
1341 // Stmt_UseUse->type