annotate vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser;
Chris@0 4
Chris@13 5 use PhpParser\Internal\DiffElem;
Chris@13 6 use PhpParser\Internal\PrintableNewAnonClassNode;
Chris@13 7 use PhpParser\Internal\TokenStream;
Chris@0 8 use PhpParser\Node\Expr;
Chris@13 9 use PhpParser\Node\Expr\AssignOp;
Chris@13 10 use PhpParser\Node\Expr\BinaryOp;
Chris@13 11 use PhpParser\Node\Expr\Cast;
Chris@13 12 use PhpParser\Node\Scalar;
Chris@0 13 use PhpParser\Node\Stmt;
Chris@0 14
Chris@0 15 abstract class PrettyPrinterAbstract
Chris@0 16 {
Chris@13 17 const FIXUP_PREC_LEFT = 0; // LHS operand affected by precedence
Chris@13 18 const FIXUP_PREC_RIGHT = 1; // RHS operand affected by precedence
Chris@13 19 const FIXUP_CALL_LHS = 2; // LHS of call
Chris@13 20 const FIXUP_DEREF_LHS = 3; // LHS of dereferencing operation
Chris@13 21 const FIXUP_BRACED_NAME = 4; // Name operand that may require bracing
Chris@13 22 const FIXUP_VAR_BRACED_NAME = 5; // Name operand that may require ${} bracing
Chris@13 23 const FIXUP_ENCAPSED = 6; // Encapsed string part
Chris@13 24
Chris@13 25 protected $precedenceMap = [
Chris@13 26 // [precedence, associativity]
Chris@13 27 // where for precedence -1 is %left, 0 is %nonassoc and 1 is %right
Chris@13 28 BinaryOp\Pow::class => [ 0, 1],
Chris@13 29 Expr\BitwiseNot::class => [ 10, 1],
Chris@13 30 Expr\PreInc::class => [ 10, 1],
Chris@13 31 Expr\PreDec::class => [ 10, 1],
Chris@13 32 Expr\PostInc::class => [ 10, -1],
Chris@13 33 Expr\PostDec::class => [ 10, -1],
Chris@13 34 Expr\UnaryPlus::class => [ 10, 1],
Chris@13 35 Expr\UnaryMinus::class => [ 10, 1],
Chris@13 36 Cast\Int_::class => [ 10, 1],
Chris@13 37 Cast\Double::class => [ 10, 1],
Chris@13 38 Cast\String_::class => [ 10, 1],
Chris@13 39 Cast\Array_::class => [ 10, 1],
Chris@13 40 Cast\Object_::class => [ 10, 1],
Chris@13 41 Cast\Bool_::class => [ 10, 1],
Chris@13 42 Cast\Unset_::class => [ 10, 1],
Chris@13 43 Expr\ErrorSuppress::class => [ 10, 1],
Chris@13 44 Expr\Instanceof_::class => [ 20, 0],
Chris@13 45 Expr\BooleanNot::class => [ 30, 1],
Chris@13 46 BinaryOp\Mul::class => [ 40, -1],
Chris@13 47 BinaryOp\Div::class => [ 40, -1],
Chris@13 48 BinaryOp\Mod::class => [ 40, -1],
Chris@13 49 BinaryOp\Plus::class => [ 50, -1],
Chris@13 50 BinaryOp\Minus::class => [ 50, -1],
Chris@13 51 BinaryOp\Concat::class => [ 50, -1],
Chris@13 52 BinaryOp\ShiftLeft::class => [ 60, -1],
Chris@13 53 BinaryOp\ShiftRight::class => [ 60, -1],
Chris@13 54 BinaryOp\Smaller::class => [ 70, 0],
Chris@13 55 BinaryOp\SmallerOrEqual::class => [ 70, 0],
Chris@13 56 BinaryOp\Greater::class => [ 70, 0],
Chris@13 57 BinaryOp\GreaterOrEqual::class => [ 70, 0],
Chris@13 58 BinaryOp\Equal::class => [ 80, 0],
Chris@13 59 BinaryOp\NotEqual::class => [ 80, 0],
Chris@13 60 BinaryOp\Identical::class => [ 80, 0],
Chris@13 61 BinaryOp\NotIdentical::class => [ 80, 0],
Chris@13 62 BinaryOp\Spaceship::class => [ 80, 0],
Chris@13 63 BinaryOp\BitwiseAnd::class => [ 90, -1],
Chris@13 64 BinaryOp\BitwiseXor::class => [100, -1],
Chris@13 65 BinaryOp\BitwiseOr::class => [110, -1],
Chris@13 66 BinaryOp\BooleanAnd::class => [120, -1],
Chris@13 67 BinaryOp\BooleanOr::class => [130, -1],
Chris@13 68 BinaryOp\Coalesce::class => [140, 1],
Chris@13 69 Expr\Ternary::class => [150, -1],
Chris@0 70 // parser uses %left for assignments, but they really behave as %right
Chris@13 71 Expr\Assign::class => [160, 1],
Chris@13 72 Expr\AssignRef::class => [160, 1],
Chris@13 73 AssignOp\Plus::class => [160, 1],
Chris@13 74 AssignOp\Minus::class => [160, 1],
Chris@13 75 AssignOp\Mul::class => [160, 1],
Chris@13 76 AssignOp\Div::class => [160, 1],
Chris@13 77 AssignOp\Concat::class => [160, 1],
Chris@13 78 AssignOp\Mod::class => [160, 1],
Chris@13 79 AssignOp\BitwiseAnd::class => [160, 1],
Chris@13 80 AssignOp\BitwiseOr::class => [160, 1],
Chris@13 81 AssignOp\BitwiseXor::class => [160, 1],
Chris@13 82 AssignOp\ShiftLeft::class => [160, 1],
Chris@13 83 AssignOp\ShiftRight::class => [160, 1],
Chris@13 84 AssignOp\Pow::class => [160, 1],
Chris@17 85 AssignOp\Coalesce::class => [160, 1],
Chris@13 86 Expr\YieldFrom::class => [165, 1],
Chris@13 87 Expr\Print_::class => [168, 1],
Chris@13 88 BinaryOp\LogicalAnd::class => [170, -1],
Chris@13 89 BinaryOp\LogicalXor::class => [180, -1],
Chris@13 90 BinaryOp\LogicalOr::class => [190, -1],
Chris@13 91 Expr\Include_::class => [200, -1],
Chris@13 92 ];
Chris@0 93
Chris@13 94 /** @var int Current indentation level. */
Chris@13 95 protected $indentLevel;
Chris@13 96 /** @var string Newline including current indentation. */
Chris@13 97 protected $nl;
Chris@13 98 /** @var string Token placed at end of doc string to ensure it is followed by a newline. */
Chris@0 99 protected $docStringEndToken;
Chris@13 100 /** @var bool Whether semicolon namespaces can be used (i.e. no global namespace is used) */
Chris@0 101 protected $canUseSemicolonNamespaces;
Chris@13 102 /** @var array Pretty printer options */
Chris@0 103 protected $options;
Chris@0 104
Chris@13 105 /** @var TokenStream Original tokens for use in format-preserving pretty print */
Chris@13 106 protected $origTokens;
Chris@13 107 /** @var Internal\Differ Differ for node lists */
Chris@13 108 protected $nodeListDiffer;
Chris@13 109 /** @var bool[] Map determining whether a certain character is a label character */
Chris@13 110 protected $labelCharMap;
Chris@13 111 /**
Chris@13 112 * @var int[][] Map from token classes and subnode names to FIXUP_* constants. This is used
Chris@13 113 * during format-preserving prints to place additional parens/braces if necessary.
Chris@13 114 */
Chris@13 115 protected $fixupMap;
Chris@13 116 /**
Chris@13 117 * @var int[][] Map from "{$node->getType()}->{$subNode}" to ['left' => $l, 'right' => $r],
Chris@13 118 * where $l and $r specify the token type that needs to be stripped when removing
Chris@13 119 * this node.
Chris@13 120 */
Chris@13 121 protected $removalMap;
Chris@13 122 /**
Chris@13 123 * @var mixed[] Map from "{$node->getType()}->{$subNode}" to [$find, $extraLeft, $extraRight].
Chris@13 124 * $find is an optional token after which the insertion occurs. $extraLeft/Right
Chris@13 125 * are optionally added before/after the main insertions.
Chris@13 126 */
Chris@13 127 protected $insertionMap;
Chris@13 128 /**
Chris@13 129 * @var string[] Map From "{$node->getType()}->{$subNode}" to string that should be inserted
Chris@13 130 * between elements of this list subnode.
Chris@13 131 */
Chris@13 132 protected $listInsertionMap;
Chris@13 133 /** @var int[] Map from "{$node->getType()}->{$subNode}" to token before which the modifiers
Chris@13 134 * should be reprinted. */
Chris@13 135 protected $modifierChangeMap;
Chris@13 136
Chris@0 137 /**
Chris@0 138 * Creates a pretty printer instance using the given options.
Chris@0 139 *
Chris@0 140 * Supported options:
Chris@0 141 * * bool $shortArraySyntax = false: Whether to use [] instead of array() as the default array
Chris@0 142 * syntax, if the node does not specify a format.
Chris@0 143 *
Chris@0 144 * @param array $options Dictionary of formatting options
Chris@0 145 */
Chris@0 146 public function __construct(array $options = []) {
Chris@0 147 $this->docStringEndToken = '_DOC_STRING_END_' . mt_rand();
Chris@0 148
Chris@0 149 $defaultOptions = ['shortArraySyntax' => false];
Chris@0 150 $this->options = $options + $defaultOptions;
Chris@0 151 }
Chris@0 152
Chris@0 153 /**
Chris@13 154 * Reset pretty printing state.
Chris@13 155 */
Chris@13 156 protected function resetState() {
Chris@13 157 $this->indentLevel = 0;
Chris@13 158 $this->nl = "\n";
Chris@13 159 $this->origTokens = null;
Chris@13 160 }
Chris@13 161
Chris@13 162 /**
Chris@13 163 * Set indentation level
Chris@13 164 *
Chris@13 165 * @param int $level Level in number of spaces
Chris@13 166 */
Chris@13 167 protected function setIndentLevel(int $level) {
Chris@13 168 $this->indentLevel = $level;
Chris@13 169 $this->nl = "\n" . \str_repeat(' ', $level);
Chris@13 170 }
Chris@13 171
Chris@13 172 /**
Chris@13 173 * Increase indentation level.
Chris@13 174 */
Chris@13 175 protected function indent() {
Chris@13 176 $this->indentLevel += 4;
Chris@13 177 $this->nl .= ' ';
Chris@13 178 }
Chris@13 179
Chris@13 180 /**
Chris@13 181 * Decrease indentation level.
Chris@13 182 */
Chris@13 183 protected function outdent() {
Chris@13 184 assert($this->indentLevel >= 4);
Chris@13 185 $this->indentLevel -= 4;
Chris@13 186 $this->nl = "\n" . str_repeat(' ', $this->indentLevel);
Chris@13 187 }
Chris@13 188
Chris@13 189 /**
Chris@0 190 * Pretty prints an array of statements.
Chris@0 191 *
Chris@0 192 * @param Node[] $stmts Array of statements
Chris@0 193 *
Chris@0 194 * @return string Pretty printed statements
Chris@0 195 */
Chris@13 196 public function prettyPrint(array $stmts) : string {
Chris@13 197 $this->resetState();
Chris@0 198 $this->preprocessNodes($stmts);
Chris@0 199
Chris@0 200 return ltrim($this->handleMagicTokens($this->pStmts($stmts, false)));
Chris@0 201 }
Chris@0 202
Chris@0 203 /**
Chris@0 204 * Pretty prints an expression.
Chris@0 205 *
Chris@0 206 * @param Expr $node Expression node
Chris@0 207 *
Chris@0 208 * @return string Pretty printed node
Chris@0 209 */
Chris@13 210 public function prettyPrintExpr(Expr $node) : string {
Chris@13 211 $this->resetState();
Chris@0 212 return $this->handleMagicTokens($this->p($node));
Chris@0 213 }
Chris@0 214
Chris@0 215 /**
Chris@0 216 * Pretty prints a file of statements (includes the opening <?php tag if it is required).
Chris@0 217 *
Chris@0 218 * @param Node[] $stmts Array of statements
Chris@0 219 *
Chris@0 220 * @return string Pretty printed statements
Chris@0 221 */
Chris@13 222 public function prettyPrintFile(array $stmts) : string {
Chris@0 223 if (!$stmts) {
Chris@0 224 return "<?php\n\n";
Chris@0 225 }
Chris@0 226
Chris@0 227 $p = "<?php\n\n" . $this->prettyPrint($stmts);
Chris@0 228
Chris@0 229 if ($stmts[0] instanceof Stmt\InlineHTML) {
Chris@0 230 $p = preg_replace('/^<\?php\s+\?>\n?/', '', $p);
Chris@0 231 }
Chris@0 232 if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
Chris@0 233 $p = preg_replace('/<\?php$/', '', rtrim($p));
Chris@0 234 }
Chris@0 235
Chris@0 236 return $p;
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * Preprocesses the top-level nodes to initialize pretty printer state.
Chris@0 241 *
Chris@0 242 * @param Node[] $nodes Array of nodes
Chris@0 243 */
Chris@0 244 protected function preprocessNodes(array $nodes) {
Chris@0 245 /* We can use semicolon-namespaces unless there is a global namespace declaration */
Chris@0 246 $this->canUseSemicolonNamespaces = true;
Chris@0 247 foreach ($nodes as $node) {
Chris@0 248 if ($node instanceof Stmt\Namespace_ && null === $node->name) {
Chris@0 249 $this->canUseSemicolonNamespaces = false;
Chris@13 250 break;
Chris@0 251 }
Chris@0 252 }
Chris@0 253 }
Chris@0 254
Chris@13 255 /**
Chris@13 256 * Handles (and removes) no-indent and doc-string-end tokens.
Chris@13 257 *
Chris@13 258 * @param string $str
Chris@13 259 * @return string
Chris@13 260 */
Chris@13 261 protected function handleMagicTokens(string $str) : string {
Chris@0 262 // Replace doc-string-end tokens with nothing or a newline
Chris@0 263 $str = str_replace($this->docStringEndToken . ";\n", ";\n", $str);
Chris@0 264 $str = str_replace($this->docStringEndToken, "\n", $str);
Chris@0 265
Chris@0 266 return $str;
Chris@0 267 }
Chris@0 268
Chris@0 269 /**
Chris@0 270 * Pretty prints an array of nodes (statements) and indents them optionally.
Chris@0 271 *
Chris@0 272 * @param Node[] $nodes Array of nodes
Chris@0 273 * @param bool $indent Whether to indent the printed nodes
Chris@0 274 *
Chris@0 275 * @return string Pretty printed statements
Chris@0 276 */
Chris@13 277 protected function pStmts(array $nodes, bool $indent = true) : string {
Chris@13 278 if ($indent) {
Chris@13 279 $this->indent();
Chris@13 280 }
Chris@13 281
Chris@0 282 $result = '';
Chris@0 283 foreach ($nodes as $node) {
Chris@13 284 $comments = $node->getComments();
Chris@0 285 if ($comments) {
Chris@13 286 $result .= $this->nl . $this->pComments($comments);
Chris@0 287 if ($node instanceof Stmt\Nop) {
Chris@0 288 continue;
Chris@0 289 }
Chris@0 290 }
Chris@0 291
Chris@13 292 $result .= $this->nl . $this->p($node);
Chris@0 293 }
Chris@0 294
Chris@0 295 if ($indent) {
Chris@13 296 $this->outdent();
Chris@0 297 }
Chris@13 298
Chris@13 299 return $result;
Chris@0 300 }
Chris@0 301
Chris@0 302 /**
Chris@13 303 * Pretty-print an infix operation while taking precedence into account.
Chris@0 304 *
Chris@13 305 * @param string $class Node class of operator
Chris@13 306 * @param Node $leftNode Left-hand side node
Chris@13 307 * @param string $operatorString String representation of the operator
Chris@13 308 * @param Node $rightNode Right-hand side node
Chris@0 309 *
Chris@13 310 * @return string Pretty printed infix operation
Chris@0 311 */
Chris@13 312 protected function pInfixOp(string $class, Node $leftNode, string $operatorString, Node $rightNode) : string {
Chris@13 313 list($precedence, $associativity) = $this->precedenceMap[$class];
Chris@0 314
Chris@0 315 return $this->pPrec($leftNode, $precedence, $associativity, -1)
Chris@0 316 . $operatorString
Chris@0 317 . $this->pPrec($rightNode, $precedence, $associativity, 1);
Chris@0 318 }
Chris@0 319
Chris@13 320 /**
Chris@13 321 * Pretty-print a prefix operation while taking precedence into account.
Chris@13 322 *
Chris@13 323 * @param string $class Node class of operator
Chris@13 324 * @param string $operatorString String representation of the operator
Chris@13 325 * @param Node $node Node
Chris@13 326 *
Chris@13 327 * @return string Pretty printed prefix operation
Chris@13 328 */
Chris@13 329 protected function pPrefixOp(string $class, string $operatorString, Node $node) : string {
Chris@13 330 list($precedence, $associativity) = $this->precedenceMap[$class];
Chris@0 331 return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
Chris@0 332 }
Chris@0 333
Chris@13 334 /**
Chris@13 335 * Pretty-print a postfix operation while taking precedence into account.
Chris@13 336 *
Chris@13 337 * @param string $class Node class of operator
Chris@13 338 * @param string $operatorString String representation of the operator
Chris@13 339 * @param Node $node Node
Chris@13 340 *
Chris@13 341 * @return string Pretty printed postfix operation
Chris@13 342 */
Chris@13 343 protected function pPostfixOp(string $class, Node $node, string $operatorString) : string {
Chris@13 344 list($precedence, $associativity) = $this->precedenceMap[$class];
Chris@0 345 return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
Chris@0 346 }
Chris@0 347
Chris@0 348 /**
Chris@0 349 * Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
Chris@0 350 *
Chris@0 351 * @param Node $node Node to pretty print
Chris@0 352 * @param int $parentPrecedence Precedence of the parent operator
Chris@0 353 * @param int $parentAssociativity Associativity of parent operator
Chris@0 354 * (-1 is left, 0 is nonassoc, 1 is right)
Chris@0 355 * @param int $childPosition Position of the node relative to the operator
Chris@0 356 * (-1 is left, 1 is right)
Chris@0 357 *
Chris@0 358 * @return string The pretty printed node
Chris@0 359 */
Chris@13 360 protected function pPrec(Node $node, int $parentPrecedence, int $parentAssociativity, int $childPosition) : string {
Chris@13 361 $class = \get_class($node);
Chris@13 362 if (isset($this->precedenceMap[$class])) {
Chris@13 363 $childPrecedence = $this->precedenceMap[$class][0];
Chris@0 364 if ($childPrecedence > $parentPrecedence
Chris@13 365 || ($parentPrecedence === $childPrecedence && $parentAssociativity !== $childPosition)
Chris@0 366 ) {
Chris@0 367 return '(' . $this->p($node) . ')';
Chris@0 368 }
Chris@0 369 }
Chris@0 370
Chris@0 371 return $this->p($node);
Chris@0 372 }
Chris@0 373
Chris@0 374 /**
Chris@0 375 * Pretty prints an array of nodes and implodes the printed values.
Chris@0 376 *
Chris@0 377 * @param Node[] $nodes Array of Nodes to be printed
Chris@0 378 * @param string $glue Character to implode with
Chris@0 379 *
Chris@0 380 * @return string Imploded pretty printed nodes
Chris@0 381 */
Chris@13 382 protected function pImplode(array $nodes, string $glue = '') : string {
Chris@13 383 $pNodes = [];
Chris@0 384 foreach ($nodes as $node) {
Chris@0 385 if (null === $node) {
Chris@0 386 $pNodes[] = '';
Chris@0 387 } else {
Chris@0 388 $pNodes[] = $this->p($node);
Chris@0 389 }
Chris@0 390 }
Chris@0 391
Chris@0 392 return implode($glue, $pNodes);
Chris@0 393 }
Chris@0 394
Chris@0 395 /**
Chris@0 396 * Pretty prints an array of nodes and implodes the printed values with commas.
Chris@0 397 *
Chris@0 398 * @param Node[] $nodes Array of Nodes to be printed
Chris@0 399 *
Chris@0 400 * @return string Comma separated pretty printed nodes
Chris@0 401 */
Chris@13 402 protected function pCommaSeparated(array $nodes) : string {
Chris@0 403 return $this->pImplode($nodes, ', ');
Chris@0 404 }
Chris@0 405
Chris@0 406 /**
Chris@0 407 * Pretty prints a comma-separated list of nodes in multiline style, including comments.
Chris@0 408 *
Chris@0 409 * The result includes a leading newline and one level of indentation (same as pStmts).
Chris@0 410 *
Chris@0 411 * @param Node[] $nodes Array of Nodes to be printed
Chris@0 412 * @param bool $trailingComma Whether to use a trailing comma
Chris@0 413 *
Chris@0 414 * @return string Comma separated pretty printed nodes in multiline style
Chris@0 415 */
Chris@13 416 protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma) : string {
Chris@13 417 $this->indent();
Chris@13 418
Chris@0 419 $result = '';
Chris@0 420 $lastIdx = count($nodes) - 1;
Chris@0 421 foreach ($nodes as $idx => $node) {
Chris@0 422 if ($node !== null) {
Chris@13 423 $comments = $node->getComments();
Chris@0 424 if ($comments) {
Chris@13 425 $result .= $this->nl . $this->pComments($comments);
Chris@0 426 }
Chris@0 427
Chris@13 428 $result .= $this->nl . $this->p($node);
Chris@0 429 } else {
Chris@13 430 $result .= $this->nl;
Chris@0 431 }
Chris@0 432 if ($trailingComma || $idx !== $lastIdx) {
Chris@0 433 $result .= ',';
Chris@0 434 }
Chris@0 435 }
Chris@0 436
Chris@13 437 $this->outdent();
Chris@13 438 return $result;
Chris@0 439 }
Chris@0 440
Chris@0 441 /**
Chris@0 442 * Prints reformatted text of the passed comments.
Chris@0 443 *
Chris@0 444 * @param Comment[] $comments List of comments
Chris@0 445 *
Chris@0 446 * @return string Reformatted text of comments
Chris@0 447 */
Chris@13 448 protected function pComments(array $comments) : string {
Chris@0 449 $formattedComments = [];
Chris@0 450
Chris@0 451 foreach ($comments as $comment) {
Chris@13 452 $formattedComments[] = str_replace("\n", $this->nl, $comment->getReformattedText());
Chris@0 453 }
Chris@0 454
Chris@13 455 return implode($this->nl, $formattedComments);
Chris@13 456 }
Chris@13 457
Chris@13 458 /**
Chris@13 459 * Perform a format-preserving pretty print of an AST.
Chris@13 460 *
Chris@13 461 * The format preservation is best effort. For some changes to the AST the formatting will not
Chris@13 462 * be preserved (at least not locally).
Chris@13 463 *
Chris@13 464 * In order to use this method a number of prerequisites must be satisfied:
Chris@13 465 * * The startTokenPos and endTokenPos attributes in the lexer must be enabled.
Chris@13 466 * * The CloningVisitor must be run on the AST prior to modification.
Chris@13 467 * * The original tokens must be provided, using the getTokens() method on the lexer.
Chris@13 468 *
Chris@13 469 * @param Node[] $stmts Modified AST with links to original AST
Chris@13 470 * @param Node[] $origStmts Original AST with token offset information
Chris@13 471 * @param array $origTokens Tokens of the original code
Chris@13 472 *
Chris@13 473 * @return string
Chris@13 474 */
Chris@13 475 public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens) : string {
Chris@13 476 $this->initializeNodeListDiffer();
Chris@13 477 $this->initializeLabelCharMap();
Chris@13 478 $this->initializeFixupMap();
Chris@13 479 $this->initializeRemovalMap();
Chris@13 480 $this->initializeInsertionMap();
Chris@13 481 $this->initializeListInsertionMap();
Chris@13 482 $this->initializeModifierChangeMap();
Chris@13 483
Chris@13 484 $this->resetState();
Chris@13 485 $this->origTokens = new TokenStream($origTokens);
Chris@13 486
Chris@13 487 $this->preprocessNodes($stmts);
Chris@13 488
Chris@13 489 $pos = 0;
Chris@13 490 $result = $this->pArray($stmts, $origStmts, $pos, 0, 'stmts', null, "\n");
Chris@13 491 if (null !== $result) {
Chris@13 492 $result .= $this->origTokens->getTokenCode($pos, count($origTokens), 0);
Chris@13 493 } else {
Chris@13 494 // Fallback
Chris@13 495 // TODO Add <?php properly
Chris@13 496 $result = "<?php\n" . $this->pStmts($stmts, false);
Chris@13 497 }
Chris@13 498
Chris@13 499 return ltrim($this->handleMagicTokens($result));
Chris@13 500 }
Chris@13 501
Chris@13 502 protected function pFallback(Node $node) {
Chris@13 503 return $this->{'p' . $node->getType()}($node);
Chris@13 504 }
Chris@13 505
Chris@13 506 /**
Chris@13 507 * Pretty prints a node.
Chris@13 508 *
Chris@13 509 * This method also handles formatting preservation for nodes.
Chris@13 510 *
Chris@13 511 * @param Node $node Node to be pretty printed
Chris@13 512 * @param bool $parentFormatPreserved Whether parent node has preserved formatting
Chris@13 513 *
Chris@13 514 * @return string Pretty printed node
Chris@13 515 */
Chris@13 516 protected function p(Node $node, $parentFormatPreserved = false) : string {
Chris@13 517 // No orig tokens means this is a normal pretty print without preservation of formatting
Chris@13 518 if (!$this->origTokens) {
Chris@13 519 return $this->{'p' . $node->getType()}($node);
Chris@13 520 }
Chris@13 521
Chris@13 522 /** @var Node $origNode */
Chris@13 523 $origNode = $node->getAttribute('origNode');
Chris@13 524 if (null === $origNode) {
Chris@13 525 return $this->pFallback($node);
Chris@13 526 }
Chris@13 527
Chris@13 528 $class = \get_class($node);
Chris@13 529 \assert($class === \get_class($origNode));
Chris@13 530
Chris@13 531 $startPos = $origNode->getStartTokenPos();
Chris@13 532 $endPos = $origNode->getEndTokenPos();
Chris@13 533 \assert($startPos >= 0 && $endPos >= 0);
Chris@13 534
Chris@13 535 $fallbackNode = $node;
Chris@13 536 if ($node instanceof Expr\New_ && $node->class instanceof Stmt\Class_) {
Chris@13 537 // Normalize node structure of anonymous classes
Chris@13 538 $node = PrintableNewAnonClassNode::fromNewNode($node);
Chris@13 539 $origNode = PrintableNewAnonClassNode::fromNewNode($origNode);
Chris@13 540 }
Chris@13 541
Chris@13 542 // InlineHTML node does not contain closing and opening PHP tags. If the parent formatting
Chris@13 543 // is not preserved, then we need to use the fallback code to make sure the tags are
Chris@13 544 // printed.
Chris@13 545 if ($node instanceof Stmt\InlineHTML && !$parentFormatPreserved) {
Chris@13 546 return $this->pFallback($fallbackNode);
Chris@13 547 }
Chris@13 548
Chris@13 549 $indentAdjustment = $this->indentLevel - $this->origTokens->getIndentationBefore($startPos);
Chris@13 550
Chris@13 551 $type = $node->getType();
Chris@13 552 $fixupInfo = $this->fixupMap[$class] ?? null;
Chris@13 553
Chris@13 554 $result = '';
Chris@13 555 $pos = $startPos;
Chris@13 556 foreach ($node->getSubNodeNames() as $subNodeName) {
Chris@13 557 $subNode = $node->$subNodeName;
Chris@13 558 $origSubNode = $origNode->$subNodeName;
Chris@13 559
Chris@13 560 if ((!$subNode instanceof Node && $subNode !== null)
Chris@13 561 || (!$origSubNode instanceof Node && $origSubNode !== null)
Chris@13 562 ) {
Chris@13 563 if ($subNode === $origSubNode) {
Chris@13 564 // Unchanged, can reuse old code
Chris@13 565 continue;
Chris@13 566 }
Chris@13 567
Chris@13 568 if (is_array($subNode) && is_array($origSubNode)) {
Chris@13 569 // Array subnode changed, we might be able to reconstruct it
Chris@13 570 $listResult = $this->pArray(
Chris@13 571 $subNode, $origSubNode, $pos, $indentAdjustment, $subNodeName,
Chris@13 572 $fixupInfo[$subNodeName] ?? null,
Chris@13 573 $this->listInsertionMap[$type . '->' . $subNodeName] ?? null
Chris@13 574 );
Chris@13 575 if (null === $listResult) {
Chris@13 576 return $this->pFallback($fallbackNode);
Chris@13 577 }
Chris@13 578
Chris@13 579 $result .= $listResult;
Chris@13 580 continue;
Chris@13 581 }
Chris@13 582
Chris@13 583 if (is_int($subNode) && is_int($origSubNode)) {
Chris@13 584 // Check if this is a modifier change
Chris@13 585 $key = $type . '->' . $subNodeName;
Chris@13 586 if (!isset($this->modifierChangeMap[$key])) {
Chris@13 587 return $this->pFallback($fallbackNode);
Chris@13 588 }
Chris@13 589
Chris@13 590 $findToken = $this->modifierChangeMap[$key];
Chris@13 591 $result .= $this->pModifiers($subNode);
Chris@13 592 $pos = $this->origTokens->findRight($pos, $findToken);
Chris@13 593 continue;
Chris@13 594 }
Chris@13 595
Chris@13 596 // If a non-node, non-array subnode changed, we don't be able to do a partial
Chris@13 597 // reconstructions, as we don't have enough offset information. Pretty print the
Chris@13 598 // whole node instead.
Chris@13 599 return $this->pFallback($fallbackNode);
Chris@13 600 }
Chris@13 601
Chris@13 602 $extraLeft = '';
Chris@13 603 $extraRight = '';
Chris@13 604 if ($origSubNode !== null) {
Chris@13 605 $subStartPos = $origSubNode->getStartTokenPos();
Chris@13 606 $subEndPos = $origSubNode->getEndTokenPos();
Chris@13 607 \assert($subStartPos >= 0 && $subEndPos >= 0);
Chris@13 608 } else {
Chris@13 609 if ($subNode === null) {
Chris@13 610 // Both null, nothing to do
Chris@13 611 continue;
Chris@13 612 }
Chris@13 613
Chris@13 614 // A node has been inserted, check if we have insertion information for it
Chris@13 615 $key = $type . '->' . $subNodeName;
Chris@13 616 if (!isset($this->insertionMap[$key])) {
Chris@13 617 return $this->pFallback($fallbackNode);
Chris@13 618 }
Chris@13 619
Chris@17 620 list($findToken, $beforeToken, $extraLeft, $extraRight) = $this->insertionMap[$key];
Chris@13 621 if (null !== $findToken) {
Chris@17 622 $subStartPos = $this->origTokens->findRight($pos, $findToken)
Chris@17 623 + (int) !$beforeToken;
Chris@13 624 } else {
Chris@13 625 $subStartPos = $pos;
Chris@13 626 }
Chris@17 627
Chris@13 628 if (null === $extraLeft && null !== $extraRight) {
Chris@13 629 // If inserting on the right only, skipping whitespace looks better
Chris@13 630 $subStartPos = $this->origTokens->skipRightWhitespace($subStartPos);
Chris@13 631 }
Chris@13 632 $subEndPos = $subStartPos - 1;
Chris@13 633 }
Chris@13 634
Chris@13 635 if (null === $subNode) {
Chris@13 636 // A node has been removed, check if we have removal information for it
Chris@13 637 $key = $type . '->' . $subNodeName;
Chris@13 638 if (!isset($this->removalMap[$key])) {
Chris@13 639 return $this->pFallback($fallbackNode);
Chris@13 640 }
Chris@13 641
Chris@13 642 // Adjust positions to account for additional tokens that must be skipped
Chris@13 643 $removalInfo = $this->removalMap[$key];
Chris@13 644 if (isset($removalInfo['left'])) {
Chris@13 645 $subStartPos = $this->origTokens->skipLeft($subStartPos - 1, $removalInfo['left']) + 1;
Chris@13 646 }
Chris@13 647 if (isset($removalInfo['right'])) {
Chris@13 648 $subEndPos = $this->origTokens->skipRight($subEndPos + 1, $removalInfo['right']) - 1;
Chris@13 649 }
Chris@13 650 }
Chris@13 651
Chris@13 652 $result .= $this->origTokens->getTokenCode($pos, $subStartPos, $indentAdjustment);
Chris@13 653
Chris@13 654 if (null !== $subNode) {
Chris@13 655 $result .= $extraLeft;
Chris@13 656
Chris@13 657 $origIndentLevel = $this->indentLevel;
Chris@13 658 $this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment);
Chris@13 659
Chris@13 660 // If it's the same node that was previously in this position, it certainly doesn't
Chris@13 661 // need fixup. It's important to check this here, because our fixup checks are more
Chris@13 662 // conservative than strictly necessary.
Chris@13 663 if (isset($fixupInfo[$subNodeName])
Chris@13 664 && $subNode->getAttribute('origNode') !== $origSubNode
Chris@13 665 ) {
Chris@13 666 $fixup = $fixupInfo[$subNodeName];
Chris@13 667 $res = $this->pFixup($fixup, $subNode, $class, $subStartPos, $subEndPos);
Chris@13 668 } else {
Chris@13 669 $res = $this->p($subNode, true);
Chris@13 670 }
Chris@13 671
Chris@13 672 $this->safeAppend($result, $res);
Chris@13 673 $this->setIndentLevel($origIndentLevel);
Chris@13 674
Chris@13 675 $result .= $extraRight;
Chris@13 676 }
Chris@13 677
Chris@13 678 $pos = $subEndPos + 1;
Chris@13 679 }
Chris@13 680
Chris@13 681 $result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
Chris@13 682 return $result;
Chris@13 683 }
Chris@13 684
Chris@13 685 /**
Chris@13 686 * Perform a format-preserving pretty print of an array.
Chris@13 687 *
Chris@13 688 * @param array $nodes New nodes
Chris@13 689 * @param array $origNodes Original nodes
Chris@13 690 * @param int $pos Current token position (updated by reference)
Chris@13 691 * @param int $indentAdjustment Adjustment for indentation
Chris@13 692 * @param string $subNodeName Name of array subnode.
Chris@13 693 * @param null|int $fixup Fixup information for array item nodes
Chris@13 694 * @param null|string $insertStr Separator string to use for insertions
Chris@13 695 *
Chris@13 696 * @return null|string Result of pretty print or null if cannot preserve formatting
Chris@13 697 */
Chris@13 698 protected function pArray(
Chris@13 699 array $nodes, array $origNodes, int &$pos, int $indentAdjustment,
Chris@13 700 string $subNodeName, $fixup, $insertStr
Chris@13 701 ) {
Chris@13 702 $diff = $this->nodeListDiffer->diffWithReplacements($origNodes, $nodes);
Chris@13 703
Chris@13 704 $beforeFirstKeepOrReplace = true;
Chris@13 705 $delayedAdd = [];
Chris@13 706 $lastElemIndentLevel = $this->indentLevel;
Chris@13 707
Chris@13 708 $insertNewline = false;
Chris@13 709 if ($insertStr === "\n") {
Chris@13 710 $insertStr = '';
Chris@13 711 $insertNewline = true;
Chris@13 712 }
Chris@13 713
Chris@13 714 if ($subNodeName === 'stmts' && \count($origNodes) === 1 && \count($nodes) !== 1) {
Chris@13 715 $startPos = $origNodes[0]->getStartTokenPos();
Chris@13 716 $endPos = $origNodes[0]->getEndTokenPos();
Chris@13 717 \assert($startPos >= 0 && $endPos >= 0);
Chris@13 718 if (!$this->origTokens->haveBraces($startPos, $endPos)) {
Chris@13 719 // This was a single statement without braces, but either additional statements
Chris@13 720 // have been added, or the single statement has been removed. This requires the
Chris@13 721 // addition of braces. For now fall back.
Chris@13 722 // TODO: Try to preserve formatting
Chris@13 723 return null;
Chris@13 724 }
Chris@13 725 }
Chris@13 726
Chris@13 727 $result = '';
Chris@13 728 foreach ($diff as $i => $diffElem) {
Chris@13 729 $diffType = $diffElem->type;
Chris@13 730 /** @var Node|null $arrItem */
Chris@13 731 $arrItem = $diffElem->new;
Chris@13 732 /** @var Node|null $origArrItem */
Chris@13 733 $origArrItem = $diffElem->old;
Chris@13 734
Chris@13 735 if ($diffType === DiffElem::TYPE_KEEP || $diffType === DiffElem::TYPE_REPLACE) {
Chris@13 736 $beforeFirstKeepOrReplace = false;
Chris@13 737
Chris@13 738 if ($origArrItem === null || $arrItem === null) {
Chris@13 739 // We can only handle the case where both are null
Chris@13 740 if ($origArrItem === $arrItem) {
Chris@13 741 continue;
Chris@13 742 }
Chris@13 743 return null;
Chris@13 744 }
Chris@13 745
Chris@13 746 if (!$arrItem instanceof Node || !$origArrItem instanceof Node) {
Chris@13 747 // We can only deal with nodes. This can occur for Names, which use string arrays.
Chris@13 748 return null;
Chris@13 749 }
Chris@13 750
Chris@13 751 $itemStartPos = $origArrItem->getStartTokenPos();
Chris@13 752 $itemEndPos = $origArrItem->getEndTokenPos();
Chris@13 753 \assert($itemStartPos >= 0 && $itemEndPos >= 0);
Chris@13 754
Chris@13 755 if ($itemEndPos < $itemStartPos) {
Chris@13 756 // End can be before start for Nop nodes, because offsets refer to non-whitespace
Chris@13 757 // locations, which for an "empty" node might result in an inverted order.
Chris@13 758 assert($origArrItem instanceof Stmt\Nop);
Chris@13 759 continue;
Chris@13 760 }
Chris@13 761
Chris@13 762 $origIndentLevel = $this->indentLevel;
Chris@13 763 $lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
Chris@13 764 $this->setIndentLevel($lastElemIndentLevel);
Chris@13 765
Chris@13 766 $comments = $arrItem->getComments();
Chris@13 767 $origComments = $origArrItem->getComments();
Chris@13 768 $commentStartPos = $origComments ? $origComments[0]->getTokenPos() : $itemStartPos;
Chris@13 769 \assert($commentStartPos >= 0);
Chris@13 770
Chris@13 771 $commentsChanged = $comments !== $origComments;
Chris@13 772 if ($commentsChanged) {
Chris@13 773 // Remove old comments
Chris@13 774 $itemStartPos = $commentStartPos;
Chris@13 775 }
Chris@13 776
Chris@13 777 if (!empty($delayedAdd)) {
Chris@13 778 $result .= $this->origTokens->getTokenCode(
Chris@13 779 $pos, $commentStartPos, $indentAdjustment);
Chris@13 780
Chris@13 781 /** @var Node $delayedAddNode */
Chris@13 782 foreach ($delayedAdd as $delayedAddNode) {
Chris@13 783 if ($insertNewline) {
Chris@13 784 $delayedAddComments = $delayedAddNode->getComments();
Chris@13 785 if ($delayedAddComments) {
Chris@13 786 $result .= $this->pComments($delayedAddComments) . $this->nl;
Chris@13 787 }
Chris@13 788 }
Chris@13 789
Chris@13 790 $this->safeAppend($result, $this->p($delayedAddNode, true));
Chris@13 791
Chris@13 792 if ($insertNewline) {
Chris@13 793 $result .= $insertStr . $this->nl;
Chris@13 794 } else {
Chris@13 795 $result .= $insertStr;
Chris@13 796 }
Chris@13 797 }
Chris@13 798
Chris@13 799 $result .= $this->origTokens->getTokenCode(
Chris@13 800 $commentStartPos, $itemStartPos, $indentAdjustment);
Chris@13 801
Chris@13 802 $delayedAdd = [];
Chris@13 803 } else {
Chris@13 804 $result .= $this->origTokens->getTokenCode(
Chris@13 805 $pos, $itemStartPos, $indentAdjustment);
Chris@13 806 }
Chris@13 807
Chris@13 808 if ($commentsChanged && $comments) {
Chris@13 809 // Add new comments
Chris@13 810 $result .= $this->pComments($comments) . $this->nl;
Chris@13 811 }
Chris@13 812 } elseif ($diffType === DiffElem::TYPE_ADD) {
Chris@13 813 if (null === $insertStr) {
Chris@13 814 // We don't have insertion information for this list type
Chris@13 815 return null;
Chris@13 816 }
Chris@13 817
Chris@13 818 if ($insertStr === ', ' && $this->isMultiline($origNodes)) {
Chris@13 819 $insertStr = ',';
Chris@13 820 $insertNewline = true;
Chris@13 821 }
Chris@13 822
Chris@13 823 if ($beforeFirstKeepOrReplace) {
Chris@13 824 // Will be inserted at the next "replace" or "keep" element
Chris@13 825 $delayedAdd[] = $arrItem;
Chris@13 826 continue;
Chris@13 827 }
Chris@13 828
Chris@13 829 $itemStartPos = $pos;
Chris@13 830 $itemEndPos = $pos - 1;
Chris@13 831
Chris@13 832 $origIndentLevel = $this->indentLevel;
Chris@13 833 $this->setIndentLevel($lastElemIndentLevel);
Chris@13 834
Chris@13 835 if ($insertNewline) {
Chris@13 836 $comments = $arrItem->getComments();
Chris@13 837 if ($comments) {
Chris@13 838 $result .= $this->nl . $this->pComments($comments);
Chris@13 839 }
Chris@13 840 $result .= $insertStr . $this->nl;
Chris@13 841 } else {
Chris@13 842 $result .= $insertStr;
Chris@13 843 }
Chris@13 844 } elseif ($diffType === DiffElem::TYPE_REMOVE) {
Chris@13 845 if ($i === 0) {
Chris@13 846 // TODO Handle removal at the start
Chris@13 847 return null;
Chris@13 848 }
Chris@13 849
Chris@13 850 if (!$origArrItem instanceof Node) {
Chris@13 851 // We only support removal for nodes
Chris@13 852 return null;
Chris@13 853 }
Chris@13 854
Chris@13 855 $itemEndPos = $origArrItem->getEndTokenPos();
Chris@13 856 \assert($itemEndPos >= 0);
Chris@13 857
Chris@13 858 $pos = $itemEndPos + 1;
Chris@13 859 continue;
Chris@13 860 } else {
Chris@13 861 throw new \Exception("Shouldn't happen");
Chris@13 862 }
Chris@13 863
Chris@13 864 if (null !== $fixup && $arrItem->getAttribute('origNode') !== $origArrItem) {
Chris@13 865 $res = $this->pFixup($fixup, $arrItem, null, $itemStartPos, $itemEndPos);
Chris@13 866 } else {
Chris@13 867 $res = $this->p($arrItem, true);
Chris@13 868 }
Chris@13 869 $this->safeAppend($result, $res);
Chris@13 870
Chris@13 871 $this->setIndentLevel($origIndentLevel);
Chris@13 872 $pos = $itemEndPos + 1;
Chris@13 873 }
Chris@13 874
Chris@13 875 if (!empty($delayedAdd)) {
Chris@13 876 // TODO Handle insertion into empty list
Chris@13 877 return null;
Chris@13 878 }
Chris@13 879
Chris@13 880 return $result;
Chris@13 881 }
Chris@13 882
Chris@13 883 /**
Chris@13 884 * Print node with fixups.
Chris@13 885 *
Chris@13 886 * Fixups here refer to the addition of extra parentheses, braces or other characters, that
Chris@13 887 * are required to preserve program semantics in a certain context (e.g. to maintain precedence
Chris@13 888 * or because only certain expressions are allowed in certain places).
Chris@13 889 *
Chris@13 890 * @param int $fixup Fixup type
Chris@13 891 * @param Node $subNode Subnode to print
Chris@13 892 * @param string|null $parentClass Class of parent node
Chris@13 893 * @param int $subStartPos Original start pos of subnode
Chris@13 894 * @param int $subEndPos Original end pos of subnode
Chris@13 895 *
Chris@13 896 * @return string Result of fixed-up print of subnode
Chris@13 897 */
Chris@13 898 protected function pFixup(int $fixup, Node $subNode, $parentClass, int $subStartPos, int $subEndPos) : string {
Chris@13 899 switch ($fixup) {
Chris@13 900 case self::FIXUP_PREC_LEFT:
Chris@13 901 case self::FIXUP_PREC_RIGHT:
Chris@13 902 if (!$this->origTokens->haveParens($subStartPos, $subEndPos)) {
Chris@13 903 list($precedence, $associativity) = $this->precedenceMap[$parentClass];
Chris@13 904 return $this->pPrec($subNode, $precedence, $associativity,
Chris@13 905 $fixup === self::FIXUP_PREC_LEFT ? -1 : 1);
Chris@13 906 }
Chris@13 907 break;
Chris@13 908 case self::FIXUP_CALL_LHS:
Chris@13 909 if ($this->callLhsRequiresParens($subNode)
Chris@13 910 && !$this->origTokens->haveParens($subStartPos, $subEndPos)
Chris@13 911 ) {
Chris@13 912 return '(' . $this->p($subNode) . ')';
Chris@13 913 }
Chris@13 914 break;
Chris@13 915 case self::FIXUP_DEREF_LHS:
Chris@13 916 if ($this->dereferenceLhsRequiresParens($subNode)
Chris@13 917 && !$this->origTokens->haveParens($subStartPos, $subEndPos)
Chris@13 918 ) {
Chris@13 919 return '(' . $this->p($subNode) . ')';
Chris@13 920 }
Chris@13 921 break;
Chris@13 922 case self::FIXUP_BRACED_NAME:
Chris@13 923 case self::FIXUP_VAR_BRACED_NAME:
Chris@13 924 if ($subNode instanceof Expr
Chris@13 925 && !$this->origTokens->haveBraces($subStartPos, $subEndPos)
Chris@13 926 ) {
Chris@13 927 return ($fixup === self::FIXUP_VAR_BRACED_NAME ? '$' : '')
Chris@13 928 . '{' . $this->p($subNode) . '}';
Chris@13 929 }
Chris@13 930 break;
Chris@13 931 case self::FIXUP_ENCAPSED:
Chris@13 932 if (!$subNode instanceof Scalar\EncapsedStringPart
Chris@13 933 && !$this->origTokens->haveBraces($subStartPos, $subEndPos)
Chris@13 934 ) {
Chris@13 935 return '{' . $this->p($subNode) . '}';
Chris@13 936 }
Chris@13 937 break;
Chris@13 938 default:
Chris@13 939 throw new \Exception('Cannot happen');
Chris@13 940 }
Chris@13 941
Chris@13 942 // Nothing special to do
Chris@13 943 return $this->p($subNode);
Chris@13 944 }
Chris@13 945
Chris@13 946 /**
Chris@13 947 * Appends to a string, ensuring whitespace between label characters.
Chris@13 948 *
Chris@13 949 * Example: "echo" and "$x" result in "echo$x", but "echo" and "x" result in "echo x".
Chris@13 950 * Without safeAppend the result would be "echox", which does not preserve semantics.
Chris@13 951 *
Chris@13 952 * @param string $str
Chris@13 953 * @param string $append
Chris@13 954 */
Chris@13 955 protected function safeAppend(string &$str, string $append) {
Chris@13 956 if ($str === "") {
Chris@13 957 $str = $append;
Chris@13 958 return;
Chris@13 959 }
Chris@13 960
Chris@17 961 if ($append === "") {
Chris@17 962 return;
Chris@17 963 }
Chris@17 964
Chris@13 965 if (!$this->labelCharMap[$append[0]]
Chris@13 966 || !$this->labelCharMap[$str[\strlen($str) - 1]]) {
Chris@13 967 $str .= $append;
Chris@13 968 } else {
Chris@13 969 $str .= " " . $append;
Chris@13 970 }
Chris@13 971 }
Chris@13 972
Chris@13 973 /**
Chris@13 974 * Determines whether the LHS of a call must be wrapped in parenthesis.
Chris@13 975 *
Chris@13 976 * @param Node $node LHS of a call
Chris@13 977 *
Chris@13 978 * @return bool Whether parentheses are required
Chris@13 979 */
Chris@13 980 protected function callLhsRequiresParens(Node $node) : bool {
Chris@13 981 return !($node instanceof Node\Name
Chris@13 982 || $node instanceof Expr\Variable
Chris@13 983 || $node instanceof Expr\ArrayDimFetch
Chris@13 984 || $node instanceof Expr\FuncCall
Chris@13 985 || $node instanceof Expr\MethodCall
Chris@13 986 || $node instanceof Expr\StaticCall
Chris@13 987 || $node instanceof Expr\Array_);
Chris@13 988 }
Chris@13 989
Chris@13 990 /**
Chris@13 991 * Determines whether the LHS of a dereferencing operation must be wrapped in parenthesis.
Chris@13 992 *
Chris@13 993 * @param Node $node LHS of dereferencing operation
Chris@13 994 *
Chris@13 995 * @return bool Whether parentheses are required
Chris@13 996 */
Chris@13 997 protected function dereferenceLhsRequiresParens(Node $node) : bool {
Chris@13 998 return !($node instanceof Expr\Variable
Chris@13 999 || $node instanceof Node\Name
Chris@13 1000 || $node instanceof Expr\ArrayDimFetch
Chris@13 1001 || $node instanceof Expr\PropertyFetch
Chris@13 1002 || $node instanceof Expr\StaticPropertyFetch
Chris@13 1003 || $node instanceof Expr\FuncCall
Chris@13 1004 || $node instanceof Expr\MethodCall
Chris@13 1005 || $node instanceof Expr\StaticCall
Chris@13 1006 || $node instanceof Expr\Array_
Chris@13 1007 || $node instanceof Scalar\String_
Chris@13 1008 || $node instanceof Expr\ConstFetch
Chris@13 1009 || $node instanceof Expr\ClassConstFetch);
Chris@13 1010 }
Chris@13 1011
Chris@13 1012 /**
Chris@13 1013 * Print modifiers, including trailing whitespace.
Chris@13 1014 *
Chris@13 1015 * @param int $modifiers Modifier mask to print
Chris@13 1016 *
Chris@13 1017 * @return string Printed modifiers
Chris@13 1018 */
Chris@13 1019 protected function pModifiers(int $modifiers) {
Chris@13 1020 return ($modifiers & Stmt\Class_::MODIFIER_PUBLIC ? 'public ' : '')
Chris@13 1021 . ($modifiers & Stmt\Class_::MODIFIER_PROTECTED ? 'protected ' : '')
Chris@13 1022 . ($modifiers & Stmt\Class_::MODIFIER_PRIVATE ? 'private ' : '')
Chris@13 1023 . ($modifiers & Stmt\Class_::MODIFIER_STATIC ? 'static ' : '')
Chris@13 1024 . ($modifiers & Stmt\Class_::MODIFIER_ABSTRACT ? 'abstract ' : '')
Chris@13 1025 . ($modifiers & Stmt\Class_::MODIFIER_FINAL ? 'final ' : '');
Chris@13 1026 }
Chris@13 1027
Chris@13 1028 /**
Chris@13 1029 * Determine whether a list of nodes uses multiline formatting.
Chris@13 1030 *
Chris@13 1031 * @param (Node|null)[] $nodes Node list
Chris@13 1032 *
Chris@13 1033 * @return bool Whether multiline formatting is used
Chris@13 1034 */
Chris@13 1035 protected function isMultiline(array $nodes) : bool {
Chris@13 1036 if (\count($nodes) < 2) {
Chris@13 1037 return false;
Chris@13 1038 }
Chris@13 1039
Chris@13 1040 $pos = -1;
Chris@13 1041 foreach ($nodes as $node) {
Chris@13 1042 if (null === $node) {
Chris@13 1043 continue;
Chris@13 1044 }
Chris@13 1045
Chris@13 1046 $endPos = $node->getEndTokenPos() + 1;
Chris@13 1047 if ($pos >= 0) {
Chris@13 1048 $text = $this->origTokens->getTokenCode($pos, $endPos, 0);
Chris@13 1049 if (false === strpos($text, "\n")) {
Chris@13 1050 // We require that a newline is present between *every* item. If the formatting
Chris@13 1051 // is inconsistent, with only some items having newlines, we don't consider it
Chris@13 1052 // as multiline
Chris@13 1053 return false;
Chris@13 1054 }
Chris@13 1055 }
Chris@13 1056 $pos = $endPos;
Chris@13 1057 }
Chris@13 1058
Chris@13 1059 return true;
Chris@13 1060 }
Chris@13 1061
Chris@13 1062 /**
Chris@13 1063 * Lazily initializes label char map.
Chris@13 1064 *
Chris@13 1065 * The label char map determines whether a certain character may occur in a label.
Chris@13 1066 */
Chris@13 1067 protected function initializeLabelCharMap() {
Chris@13 1068 if ($this->labelCharMap) return;
Chris@13 1069
Chris@13 1070 $this->labelCharMap = [];
Chris@13 1071 for ($i = 0; $i < 256; $i++) {
Chris@13 1072 // Since PHP 7.1 The lower range is 0x80. However, we also want to support code for
Chris@13 1073 // older versions.
Chris@13 1074 $this->labelCharMap[chr($i)] = $i >= 0x7f || ctype_alnum($i);
Chris@13 1075 }
Chris@13 1076 }
Chris@13 1077
Chris@13 1078 /**
Chris@13 1079 * Lazily initializes node list differ.
Chris@13 1080 *
Chris@13 1081 * The node list differ is used to determine differences between two array subnodes.
Chris@13 1082 */
Chris@13 1083 protected function initializeNodeListDiffer() {
Chris@13 1084 if ($this->nodeListDiffer) return;
Chris@13 1085
Chris@13 1086 $this->nodeListDiffer = new Internal\Differ(function ($a, $b) {
Chris@13 1087 if ($a instanceof Node && $b instanceof Node) {
Chris@13 1088 return $a === $b->getAttribute('origNode');
Chris@13 1089 }
Chris@13 1090 // Can happen for array destructuring
Chris@13 1091 return $a === null && $b === null;
Chris@13 1092 });
Chris@13 1093 }
Chris@13 1094
Chris@13 1095 /**
Chris@13 1096 * Lazily initializes fixup map.
Chris@13 1097 *
Chris@13 1098 * The fixup map is used to determine whether a certain subnode of a certain node may require
Chris@13 1099 * some kind of "fixup" operation, e.g. the addition of parenthesis or braces.
Chris@13 1100 */
Chris@13 1101 protected function initializeFixupMap() {
Chris@13 1102 if ($this->fixupMap) return;
Chris@13 1103
Chris@13 1104 $this->fixupMap = [
Chris@13 1105 Expr\PreInc::class => ['var' => self::FIXUP_PREC_RIGHT],
Chris@13 1106 Expr\PreDec::class => ['var' => self::FIXUP_PREC_RIGHT],
Chris@13 1107 Expr\PostInc::class => ['var' => self::FIXUP_PREC_LEFT],
Chris@13 1108 Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT],
Chris@13 1109 Expr\Instanceof_::class => [
Chris@13 1110 'expr' => self::FIXUP_PREC_LEFT,
Chris@13 1111 'class' => self::FIXUP_PREC_RIGHT,
Chris@13 1112 ],
Chris@13 1113 Expr\Ternary::class => [
Chris@13 1114 'cond' => self::FIXUP_PREC_LEFT,
Chris@13 1115 'else' => self::FIXUP_PREC_RIGHT,
Chris@13 1116 ],
Chris@13 1117
Chris@13 1118 Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS],
Chris@13 1119 Expr\StaticCall::class => ['class' => self::FIXUP_DEREF_LHS],
Chris@13 1120 Expr\ArrayDimFetch::class => ['var' => self::FIXUP_DEREF_LHS],
Chris@13 1121 Expr\MethodCall::class => [
Chris@13 1122 'var' => self::FIXUP_DEREF_LHS,
Chris@13 1123 'name' => self::FIXUP_BRACED_NAME,
Chris@13 1124 ],
Chris@13 1125 Expr\StaticPropertyFetch::class => [
Chris@13 1126 'class' => self::FIXUP_DEREF_LHS,
Chris@13 1127 'name' => self::FIXUP_VAR_BRACED_NAME,
Chris@13 1128 ],
Chris@13 1129 Expr\PropertyFetch::class => [
Chris@13 1130 'var' => self::FIXUP_DEREF_LHS,
Chris@13 1131 'name' => self::FIXUP_BRACED_NAME,
Chris@13 1132 ],
Chris@13 1133 Scalar\Encapsed::class => [
Chris@13 1134 'parts' => self::FIXUP_ENCAPSED,
Chris@13 1135 ],
Chris@13 1136 ];
Chris@13 1137
Chris@13 1138 $binaryOps = [
Chris@13 1139 BinaryOp\Pow::class, BinaryOp\Mul::class, BinaryOp\Div::class, BinaryOp\Mod::class,
Chris@13 1140 BinaryOp\Plus::class, BinaryOp\Minus::class, BinaryOp\Concat::class,
Chris@13 1141 BinaryOp\ShiftLeft::class, BinaryOp\ShiftRight::class, BinaryOp\Smaller::class,
Chris@13 1142 BinaryOp\SmallerOrEqual::class, BinaryOp\Greater::class, BinaryOp\GreaterOrEqual::class,
Chris@13 1143 BinaryOp\Equal::class, BinaryOp\NotEqual::class, BinaryOp\Identical::class,
Chris@13 1144 BinaryOp\NotIdentical::class, BinaryOp\Spaceship::class, BinaryOp\BitwiseAnd::class,
Chris@13 1145 BinaryOp\BitwiseXor::class, BinaryOp\BitwiseOr::class, BinaryOp\BooleanAnd::class,
Chris@13 1146 BinaryOp\BooleanOr::class, BinaryOp\Coalesce::class, BinaryOp\LogicalAnd::class,
Chris@13 1147 BinaryOp\LogicalXor::class, BinaryOp\LogicalOr::class,
Chris@13 1148 ];
Chris@13 1149 foreach ($binaryOps as $binaryOp) {
Chris@13 1150 $this->fixupMap[$binaryOp] = [
Chris@13 1151 'left' => self::FIXUP_PREC_LEFT,
Chris@13 1152 'right' => self::FIXUP_PREC_RIGHT
Chris@13 1153 ];
Chris@13 1154 }
Chris@13 1155
Chris@13 1156 $assignOps = [
Chris@13 1157 Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class,
Chris@13 1158 AssignOp\Mul::class, AssignOp\Div::class, AssignOp\Concat::class, AssignOp\Mod::class,
Chris@13 1159 AssignOp\BitwiseAnd::class, AssignOp\BitwiseOr::class, AssignOp\BitwiseXor::class,
Chris@17 1160 AssignOp\ShiftLeft::class, AssignOp\ShiftRight::class, AssignOp\Pow::class, AssignOp\Coalesce::class
Chris@13 1161 ];
Chris@13 1162 foreach ($assignOps as $assignOp) {
Chris@13 1163 $this->fixupMap[$assignOp] = [
Chris@13 1164 'var' => self::FIXUP_PREC_LEFT,
Chris@13 1165 'expr' => self::FIXUP_PREC_RIGHT,
Chris@13 1166 ];
Chris@13 1167 }
Chris@13 1168
Chris@13 1169 $prefixOps = [
Chris@13 1170 Expr\BitwiseNot::class, Expr\BooleanNot::class, Expr\UnaryPlus::class, Expr\UnaryMinus::class,
Chris@13 1171 Cast\Int_::class, Cast\Double::class, Cast\String_::class, Cast\Array_::class,
Chris@13 1172 Cast\Object_::class, Cast\Bool_::class, Cast\Unset_::class, Expr\ErrorSuppress::class,
Chris@13 1173 Expr\YieldFrom::class, Expr\Print_::class, Expr\Include_::class,
Chris@13 1174 ];
Chris@13 1175 foreach ($prefixOps as $prefixOp) {
Chris@13 1176 $this->fixupMap[$prefixOp] = ['expr' => self::FIXUP_PREC_RIGHT];
Chris@13 1177 }
Chris@13 1178 }
Chris@13 1179
Chris@13 1180 /**
Chris@13 1181 * Lazily initializes the removal map.
Chris@13 1182 *
Chris@13 1183 * The removal map is used to determine which additional tokens should be returned when a
Chris@13 1184 * certain node is replaced by null.
Chris@13 1185 */
Chris@13 1186 protected function initializeRemovalMap() {
Chris@13 1187 if ($this->removalMap) return;
Chris@13 1188
Chris@13 1189 $stripBoth = ['left' => \T_WHITESPACE, 'right' => \T_WHITESPACE];
Chris@13 1190 $stripLeft = ['left' => \T_WHITESPACE];
Chris@13 1191 $stripRight = ['right' => \T_WHITESPACE];
Chris@13 1192 $stripDoubleArrow = ['right' => \T_DOUBLE_ARROW];
Chris@13 1193 $stripColon = ['left' => ':'];
Chris@13 1194 $stripEquals = ['left' => '='];
Chris@13 1195 $this->removalMap = [
Chris@13 1196 'Expr_ArrayDimFetch->dim' => $stripBoth,
Chris@13 1197 'Expr_ArrayItem->key' => $stripDoubleArrow,
Chris@13 1198 'Expr_Closure->returnType' => $stripColon,
Chris@13 1199 'Expr_Exit->expr' => $stripBoth,
Chris@13 1200 'Expr_Ternary->if' => $stripBoth,
Chris@13 1201 'Expr_Yield->key' => $stripDoubleArrow,
Chris@13 1202 'Expr_Yield->value' => $stripBoth,
Chris@13 1203 'Param->type' => $stripRight,
Chris@13 1204 'Param->default' => $stripEquals,
Chris@13 1205 'Stmt_Break->num' => $stripBoth,
Chris@13 1206 'Stmt_ClassMethod->returnType' => $stripColon,
Chris@13 1207 'Stmt_Class->extends' => ['left' => \T_EXTENDS],
Chris@13 1208 'Expr_PrintableNewAnonClass->extends' => ['left' => \T_EXTENDS],
Chris@13 1209 'Stmt_Continue->num' => $stripBoth,
Chris@13 1210 'Stmt_Foreach->keyVar' => $stripDoubleArrow,
Chris@13 1211 'Stmt_Function->returnType' => $stripColon,
Chris@13 1212 'Stmt_If->else' => $stripLeft,
Chris@13 1213 'Stmt_Namespace->name' => $stripLeft,
Chris@17 1214 'Stmt_Property->type' => $stripRight,
Chris@13 1215 'Stmt_PropertyProperty->default' => $stripEquals,
Chris@13 1216 'Stmt_Return->expr' => $stripBoth,
Chris@13 1217 'Stmt_StaticVar->default' => $stripEquals,
Chris@13 1218 'Stmt_TraitUseAdaptation_Alias->newName' => $stripLeft,
Chris@13 1219 'Stmt_TryCatch->finally' => $stripLeft,
Chris@13 1220 // 'Stmt_Case->cond': Replace with "default"
Chris@13 1221 // 'Stmt_Class->name': Unclear what to do
Chris@13 1222 // 'Stmt_Declare->stmts': Not a plain node
Chris@13 1223 // 'Stmt_TraitUseAdaptation_Alias->newModifier': Not a plain node
Chris@13 1224 ];
Chris@13 1225 }
Chris@13 1226
Chris@13 1227 protected function initializeInsertionMap() {
Chris@13 1228 if ($this->insertionMap) return;
Chris@13 1229
Chris@13 1230 // TODO: "yield" where both key and value are inserted doesn't work
Chris@13 1231 $this->insertionMap = [
Chris@17 1232 'Expr_ArrayDimFetch->dim' => ['[', false, null, null],
Chris@17 1233 'Expr_ArrayItem->key' => [null, false, null, ' => '],
Chris@17 1234 'Expr_Closure->returnType' => [')', false, ' : ', null],
Chris@17 1235 'Expr_Ternary->if' => ['?', false, ' ', ' '],
Chris@17 1236 'Expr_Yield->key' => [\T_YIELD, false, null, ' => '],
Chris@17 1237 'Expr_Yield->value' => [\T_YIELD, false, ' ', null],
Chris@17 1238 'Param->type' => [null, false, null, ' '],
Chris@17 1239 'Param->default' => [null, false, ' = ', null],
Chris@17 1240 'Stmt_Break->num' => [\T_BREAK, false, ' ', null],
Chris@17 1241 'Stmt_ClassMethod->returnType' => [')', false, ' : ', null],
Chris@17 1242 'Stmt_Class->extends' => [null, false, ' extends ', null],
Chris@13 1243 'Expr_PrintableNewAnonClass->extends' => [null, ' extends ', null],
Chris@17 1244 'Stmt_Continue->num' => [\T_CONTINUE, false, ' ', null],
Chris@17 1245 'Stmt_Foreach->keyVar' => [\T_AS, false, null, ' => '],
Chris@17 1246 'Stmt_Function->returnType' => [')', false, ' : ', null],
Chris@17 1247 'Stmt_If->else' => [null, false, ' ', null],
Chris@17 1248 'Stmt_Namespace->name' => [\T_NAMESPACE, false, ' ', null],
Chris@17 1249 'Stmt_Property->type' => [\T_VARIABLE, true, null, ' '],
Chris@17 1250 'Stmt_PropertyProperty->default' => [null, false, ' = ', null],
Chris@17 1251 'Stmt_Return->expr' => [\T_RETURN, false, ' ', null],
Chris@17 1252 'Stmt_StaticVar->default' => [null, false, ' = ', null],
Chris@17 1253 //'Stmt_TraitUseAdaptation_Alias->newName' => [T_AS, false, ' ', null], // TODO
Chris@17 1254 'Stmt_TryCatch->finally' => [null, false, ' ', null],
Chris@13 1255
Chris@13 1256 // 'Expr_Exit->expr': Complicated due to optional ()
Chris@13 1257 // 'Stmt_Case->cond': Conversion from default to case
Chris@13 1258 // 'Stmt_Class->name': Unclear
Chris@13 1259 // 'Stmt_Declare->stmts': Not a proper node
Chris@13 1260 // 'Stmt_TraitUseAdaptation_Alias->newModifier': Not a proper node
Chris@13 1261 ];
Chris@13 1262 }
Chris@13 1263
Chris@13 1264 protected function initializeListInsertionMap() {
Chris@13 1265 if ($this->listInsertionMap) return;
Chris@13 1266
Chris@13 1267 $this->listInsertionMap = [
Chris@13 1268 // special
Chris@13 1269 //'Expr_ShellExec->parts' => '', // TODO These need to be treated more carefully
Chris@13 1270 //'Scalar_Encapsed->parts' => '',
Chris@13 1271 'Stmt_Catch->types' => '|',
Chris@13 1272 'Stmt_If->elseifs' => ' ',
Chris@13 1273 'Stmt_TryCatch->catches' => ' ',
Chris@13 1274
Chris@13 1275 // comma-separated lists
Chris@13 1276 'Expr_Array->items' => ', ',
Chris@13 1277 'Expr_Closure->params' => ', ',
Chris@13 1278 'Expr_Closure->uses' => ', ',
Chris@13 1279 'Expr_FuncCall->args' => ', ',
Chris@13 1280 'Expr_Isset->vars' => ', ',
Chris@13 1281 'Expr_List->items' => ', ',
Chris@13 1282 'Expr_MethodCall->args' => ', ',
Chris@13 1283 'Expr_New->args' => ', ',
Chris@13 1284 'Expr_PrintableNewAnonClass->args' => ', ',
Chris@13 1285 'Expr_StaticCall->args' => ', ',
Chris@13 1286 'Stmt_ClassConst->consts' => ', ',
Chris@13 1287 'Stmt_ClassMethod->params' => ', ',
Chris@13 1288 'Stmt_Class->implements' => ', ',
Chris@13 1289 'Expr_PrintableNewAnonClass->implements' => ', ',
Chris@13 1290 'Stmt_Const->consts' => ', ',
Chris@13 1291 'Stmt_Declare->declares' => ', ',
Chris@13 1292 'Stmt_Echo->exprs' => ', ',
Chris@13 1293 'Stmt_For->init' => ', ',
Chris@13 1294 'Stmt_For->cond' => ', ',
Chris@13 1295 'Stmt_For->loop' => ', ',
Chris@13 1296 'Stmt_Function->params' => ', ',
Chris@13 1297 'Stmt_Global->vars' => ', ',
Chris@13 1298 'Stmt_GroupUse->uses' => ', ',
Chris@13 1299 'Stmt_Interface->extends' => ', ',
Chris@13 1300 'Stmt_Property->props' => ', ',
Chris@13 1301 'Stmt_StaticVar->vars' => ', ',
Chris@13 1302 'Stmt_TraitUse->traits' => ', ',
Chris@13 1303 'Stmt_TraitUseAdaptation_Precedence->insteadof' => ', ',
Chris@13 1304 'Stmt_Unset->vars' => ', ',
Chris@13 1305 'Stmt_Use->uses' => ', ',
Chris@13 1306
Chris@13 1307 // statement lists
Chris@13 1308 'Expr_Closure->stmts' => "\n",
Chris@13 1309 'Stmt_Case->stmts' => "\n",
Chris@13 1310 'Stmt_Catch->stmts' => "\n",
Chris@13 1311 'Stmt_Class->stmts' => "\n",
Chris@13 1312 'Expr_PrintableNewAnonClass->stmts' => "\n",
Chris@13 1313 'Stmt_Interface->stmts' => "\n",
Chris@13 1314 'Stmt_Trait->stmts' => "\n",
Chris@13 1315 'Stmt_ClassMethod->stmts' => "\n",
Chris@13 1316 'Stmt_Declare->stmts' => "\n",
Chris@13 1317 'Stmt_Do->stmts' => "\n",
Chris@13 1318 'Stmt_ElseIf->stmts' => "\n",
Chris@13 1319 'Stmt_Else->stmts' => "\n",
Chris@13 1320 'Stmt_Finally->stmts' => "\n",
Chris@13 1321 'Stmt_Foreach->stmts' => "\n",
Chris@13 1322 'Stmt_For->stmts' => "\n",
Chris@13 1323 'Stmt_Function->stmts' => "\n",
Chris@13 1324 'Stmt_If->stmts' => "\n",
Chris@13 1325 'Stmt_Namespace->stmts' => "\n",
Chris@13 1326 'Stmt_Switch->cases' => "\n",
Chris@13 1327 'Stmt_TraitUse->adaptations' => "\n",
Chris@13 1328 'Stmt_TryCatch->stmts' => "\n",
Chris@13 1329 'Stmt_While->stmts' => "\n",
Chris@13 1330 ];
Chris@13 1331 }
Chris@13 1332
Chris@13 1333 protected function initializeModifierChangeMap() {
Chris@13 1334 if ($this->modifierChangeMap) return;
Chris@13 1335
Chris@13 1336 $this->modifierChangeMap = [
Chris@13 1337 'Stmt_ClassConst->flags' => \T_CONST,
Chris@13 1338 'Stmt_ClassMethod->flags' => \T_FUNCTION,
Chris@13 1339 'Stmt_Class->flags' => \T_CLASS,
Chris@13 1340 'Stmt_Property->flags' => \T_VARIABLE,
Chris@13 1341 //'Stmt_TraitUseAdaptation_Alias->newModifier' => 0, // TODO
Chris@13 1342 ];
Chris@13 1343
Chris@13 1344 // List of integer subnodes that are not modifiers:
Chris@13 1345 // Expr_Include->type
Chris@13 1346 // Stmt_GroupUse->type
Chris@13 1347 // Stmt_Use->type
Chris@13 1348 // Stmt_UseUse->type
Chris@0 1349 }
Chris@0 1350 }