PrettyPrinterAbstract.php 62 KB

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