1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | use PhpParser\Internal\DiffElem; |
6: | use PhpParser\Internal\PrintableNewAnonClassNode; |
7: | use PhpParser\Internal\TokenStream; |
8: | use PhpParser\Node\Expr; |
9: | use PhpParser\Node\Expr\AssignOp; |
10: | use PhpParser\Node\Expr\BinaryOp; |
11: | use PhpParser\Node\Expr\Cast; |
12: | use PhpParser\Node\Scalar; |
13: | use PhpParser\Node\Stmt; |
14: | |
15: | abstract class PrettyPrinterAbstract |
16: | { |
17: | const FIXUP_PREC_LEFT = 0; |
18: | const FIXUP_PREC_RIGHT = 1; |
19: | const FIXUP_CALL_LHS = 2; |
20: | const FIXUP_DEREF_LHS = 3; |
21: | const FIXUP_BRACED_NAME = 4; |
22: | const FIXUP_VAR_BRACED_NAME = 5; |
23: | const FIXUP_ENCAPSED = 6; |
24: | const FIXUP_NEW = 7; |
25: | const FIXUP_STATIC_DEREF_LHS = 8; |
26: | |
27: | protected $precedenceMap = [ |
28: | |
29: | |
30: | BinaryOp\Pow::class => [ 0, 1], |
31: | Expr\BitwiseNot::class => [ 10, 1], |
32: | Expr\PreInc::class => [ 10, 1], |
33: | Expr\PreDec::class => [ 10, 1], |
34: | Expr\PostInc::class => [ 10, -1], |
35: | Expr\PostDec::class => [ 10, -1], |
36: | Expr\UnaryPlus::class => [ 10, 1], |
37: | Expr\UnaryMinus::class => [ 10, 1], |
38: | Cast\Int_::class => [ 10, 1], |
39: | Cast\Double::class => [ 10, 1], |
40: | Cast\String_::class => [ 10, 1], |
41: | Cast\Array_::class => [ 10, 1], |
42: | Cast\Object_::class => [ 10, 1], |
43: | Cast\Bool_::class => [ 10, 1], |
44: | Cast\Unset_::class => [ 10, 1], |
45: | Expr\ErrorSuppress::class => [ 10, 1], |
46: | Expr\Instanceof_::class => [ 20, 0], |
47: | Expr\BooleanNot::class => [ 30, 1], |
48: | BinaryOp\Mul::class => [ 40, -1], |
49: | BinaryOp\Div::class => [ 40, -1], |
50: | BinaryOp\Mod::class => [ 40, -1], |
51: | BinaryOp\Plus::class => [ 50, -1], |
52: | BinaryOp\Minus::class => [ 50, -1], |
53: | BinaryOp\Concat::class => [ 50, -1], |
54: | BinaryOp\ShiftLeft::class => [ 60, -1], |
55: | BinaryOp\ShiftRight::class => [ 60, -1], |
56: | BinaryOp\Smaller::class => [ 70, 0], |
57: | BinaryOp\SmallerOrEqual::class => [ 70, 0], |
58: | BinaryOp\Greater::class => [ 70, 0], |
59: | BinaryOp\GreaterOrEqual::class => [ 70, 0], |
60: | BinaryOp\Equal::class => [ 80, 0], |
61: | BinaryOp\NotEqual::class => [ 80, 0], |
62: | BinaryOp\Identical::class => [ 80, 0], |
63: | BinaryOp\NotIdentical::class => [ 80, 0], |
64: | BinaryOp\Spaceship::class => [ 80, 0], |
65: | BinaryOp\BitwiseAnd::class => [ 90, -1], |
66: | BinaryOp\BitwiseXor::class => [100, -1], |
67: | BinaryOp\BitwiseOr::class => [110, -1], |
68: | BinaryOp\BooleanAnd::class => [120, -1], |
69: | BinaryOp\BooleanOr::class => [130, -1], |
70: | BinaryOp\Coalesce::class => [140, 1], |
71: | Expr\Ternary::class => [150, 0], |
72: | |
73: | Expr\Assign::class => [160, 1], |
74: | Expr\AssignRef::class => [160, 1], |
75: | AssignOp\Plus::class => [160, 1], |
76: | AssignOp\Minus::class => [160, 1], |
77: | AssignOp\Mul::class => [160, 1], |
78: | AssignOp\Div::class => [160, 1], |
79: | AssignOp\Concat::class => [160, 1], |
80: | AssignOp\Mod::class => [160, 1], |
81: | AssignOp\BitwiseAnd::class => [160, 1], |
82: | AssignOp\BitwiseOr::class => [160, 1], |
83: | AssignOp\BitwiseXor::class => [160, 1], |
84: | AssignOp\ShiftLeft::class => [160, 1], |
85: | AssignOp\ShiftRight::class => [160, 1], |
86: | AssignOp\Pow::class => [160, 1], |
87: | AssignOp\Coalesce::class => [160, 1], |
88: | Expr\YieldFrom::class => [165, 1], |
89: | Expr\Print_::class => [168, 1], |
90: | BinaryOp\LogicalAnd::class => [170, -1], |
91: | BinaryOp\LogicalXor::class => [180, -1], |
92: | BinaryOp\LogicalOr::class => [190, -1], |
93: | Expr\Include_::class => [200, -1], |
94: | ]; |
95: | |
96: | |
97: | protected $indentLevel; |
98: | |
99: | protected $nl; |
100: | |
101: | protected $docStringEndToken; |
102: | |
103: | protected $canUseSemicolonNamespaces; |
104: | |
105: | protected $options; |
106: | |
107: | |
108: | protected $origTokens; |
109: | |
110: | protected $nodeListDiffer; |
111: | |
112: | protected $labelCharMap; |
113: | |
114: | |
115: | |
116: | |
117: | protected $fixupMap; |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | protected $removalMap; |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | protected $insertionMap; |
130: | |
131: | |
132: | |
133: | |
134: | protected $listInsertionMap; |
135: | protected $emptyListInsertionMap; |
136: | |
137: | |
138: | protected $modifierChangeMap; |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | public function __construct(array $options = []) { |
150: | $this->docStringEndToken = '_DOC_STRING_END_' . mt_rand(); |
151: | |
152: | $defaultOptions = ['shortArraySyntax' => false]; |
153: | $this->options = $options + $defaultOptions; |
154: | } |
155: | |
156: | |
157: | |
158: | |
159: | protected function resetState() { |
160: | $this->indentLevel = 0; |
161: | $this->nl = "\n"; |
162: | $this->origTokens = null; |
163: | } |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | protected function setIndentLevel(int $level) { |
171: | $this->indentLevel = $level; |
172: | $this->nl = "\n" . \str_repeat(' ', $level); |
173: | } |
174: | |
175: | |
176: | |
177: | |
178: | protected function indent() { |
179: | $this->indentLevel += 4; |
180: | $this->nl .= ' '; |
181: | } |
182: | |
183: | |
184: | |
185: | |
186: | protected function outdent() { |
187: | assert($this->indentLevel >= 4); |
188: | $this->indentLevel -= 4; |
189: | $this->nl = "\n" . str_repeat(' ', $this->indentLevel); |
190: | } |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | public function prettyPrint(array $stmts) : string { |
200: | $this->resetState(); |
201: | $this->preprocessNodes($stmts); |
202: | |
203: | return ltrim($this->handleMagicTokens($this->pStmts($stmts, false))); |
204: | } |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | public function prettyPrintExpr(Expr $node) : string { |
214: | $this->resetState(); |
215: | return $this->handleMagicTokens($this->p($node)); |
216: | } |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: | |
225: | public function prettyPrintFile(array $stmts) : string { |
226: | if (!$stmts) { |
227: | return "<?php\n\n"; |
228: | } |
229: | |
230: | $p = "<?php\n\n" . $this->prettyPrint($stmts); |
231: | |
232: | if ($stmts[0] instanceof Stmt\InlineHTML) { |
233: | $p = preg_replace('/^<\?php\s+\?>\n?/', '', $p); |
234: | } |
235: | if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) { |
236: | $p = preg_replace('/<\?php$/', '', rtrim($p)); |
237: | } |
238: | |
239: | return $p; |
240: | } |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | protected function preprocessNodes(array $nodes) { |
248: | |
249: | $this->canUseSemicolonNamespaces = true; |
250: | foreach ($nodes as $node) { |
251: | if ($node instanceof Stmt\Namespace_ && null === $node->name) { |
252: | $this->canUseSemicolonNamespaces = false; |
253: | break; |
254: | } |
255: | } |
256: | } |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | protected function handleMagicTokens(string $str) : string { |
265: | |
266: | $str = str_replace($this->docStringEndToken . ";\n", ";\n", $str); |
267: | $str = str_replace($this->docStringEndToken, "\n", $str); |
268: | |
269: | return $str; |
270: | } |
271: | |
272: | |
273: | |
274: | |
275: | |
276: | |
277: | |
278: | |
279: | |
280: | protected function pStmts(array $nodes, bool $indent = true) : string { |
281: | if ($indent) { |
282: | $this->indent(); |
283: | } |
284: | |
285: | $result = ''; |
286: | foreach ($nodes as $node) { |
287: | $comments = $node->getComments(); |
288: | if ($comments) { |
289: | $result .= $this->nl . $this->pComments($comments); |
290: | if ($node instanceof Stmt\Nop) { |
291: | continue; |
292: | } |
293: | } |
294: | |
295: | $result .= $this->nl . $this->p($node); |
296: | } |
297: | |
298: | if ($indent) { |
299: | $this->outdent(); |
300: | } |
301: | |
302: | return $result; |
303: | } |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | protected function pInfixOp(string $class, Node $leftNode, string $operatorString, Node $rightNode) : string { |
316: | list($precedence, $associativity) = $this->precedenceMap[$class]; |
317: | |
318: | return $this->pPrec($leftNode, $precedence, $associativity, -1) |
319: | . $operatorString |
320: | . $this->pPrec($rightNode, $precedence, $associativity, 1); |
321: | } |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | |
328: | |
329: | |
330: | |
331: | |
332: | protected function pPrefixOp(string $class, string $operatorString, Node $node) : string { |
333: | list($precedence, $associativity) = $this->precedenceMap[$class]; |
334: | return $operatorString . $this->pPrec($node, $precedence, $associativity, 1); |
335: | } |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | |
344: | |
345: | |
346: | protected function pPostfixOp(string $class, Node $node, string $operatorString) : string { |
347: | list($precedence, $associativity) = $this->precedenceMap[$class]; |
348: | return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString; |
349: | } |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | |
361: | |
362: | |
363: | protected function pPrec(Node $node, int $parentPrecedence, int $parentAssociativity, int $childPosition) : string { |
364: | $class = \get_class($node); |
365: | if (isset($this->precedenceMap[$class])) { |
366: | $childPrecedence = $this->precedenceMap[$class][0]; |
367: | if ($childPrecedence > $parentPrecedence |
368: | || ($parentPrecedence === $childPrecedence && $parentAssociativity !== $childPosition) |
369: | ) { |
370: | return '(' . $this->p($node) . ')'; |
371: | } |
372: | } |
373: | |
374: | return $this->p($node); |
375: | } |
376: | |
377: | |
378: | |
379: | |
380: | |
381: | |
382: | |
383: | |
384: | |
385: | protected function pImplode(array $nodes, string $glue = '') : string { |
386: | $pNodes = []; |
387: | foreach ($nodes as $node) { |
388: | if (null === $node) { |
389: | $pNodes[] = ''; |
390: | } else { |
391: | $pNodes[] = $this->p($node); |
392: | } |
393: | } |
394: | |
395: | return implode($glue, $pNodes); |
396: | } |
397: | |
398: | |
399: | |
400: | |
401: | |
402: | |
403: | |
404: | |
405: | protected function pCommaSeparated(array $nodes) : string { |
406: | return $this->pImplode($nodes, ', '); |
407: | } |
408: | |
409: | |
410: | |
411: | |
412: | |
413: | |
414: | |
415: | |
416: | |
417: | |
418: | |
419: | protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma) : string { |
420: | $this->indent(); |
421: | |
422: | $result = ''; |
423: | $lastIdx = count($nodes) - 1; |
424: | foreach ($nodes as $idx => $node) { |
425: | if ($node !== null) { |
426: | $comments = $node->getComments(); |
427: | if ($comments) { |
428: | $result .= $this->nl . $this->pComments($comments); |
429: | } |
430: | |
431: | $result .= $this->nl . $this->p($node); |
432: | } else { |
433: | $result .= $this->nl; |
434: | } |
435: | if ($trailingComma || $idx !== $lastIdx) { |
436: | $result .= ','; |
437: | } |
438: | } |
439: | |
440: | $this->outdent(); |
441: | return $result; |
442: | } |
443: | |
444: | |
445: | |
446: | |
447: | |
448: | |
449: | |
450: | |
451: | protected function pComments(array $comments) : string { |
452: | $formattedComments = []; |
453: | |
454: | foreach ($comments as $comment) { |
455: | $formattedComments[] = str_replace("\n", $this->nl, $comment->getReformattedText()); |
456: | } |
457: | |
458: | return implode($this->nl, $formattedComments); |
459: | } |
460: | |
461: | |
462: | |
463: | |
464: | |
465: | |
466: | |
467: | |
468: | |
469: | |
470: | |
471: | |
472: | |
473: | |
474: | |
475: | |
476: | |
477: | |
478: | public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens) : string { |
479: | $this->initializeNodeListDiffer(); |
480: | $this->initializeLabelCharMap(); |
481: | $this->initializeFixupMap(); |
482: | $this->initializeRemovalMap(); |
483: | $this->initializeInsertionMap(); |
484: | $this->initializeListInsertionMap(); |
485: | $this->initializeEmptyListInsertionMap(); |
486: | $this->initializeModifierChangeMap(); |
487: | |
488: | $this->resetState(); |
489: | $this->origTokens = new TokenStream($origTokens); |
490: | |
491: | $this->preprocessNodes($stmts); |
492: | |
493: | $pos = 0; |
494: | $result = $this->pArray($stmts, $origStmts, $pos, 0, 'File', 'stmts', null); |
495: | if (null !== $result) { |
496: | $result .= $this->origTokens->getTokenCode($pos, count($origTokens), 0); |
497: | } else { |
498: | |
499: | |
500: | $result = "<?php\n" . $this->pStmts($stmts, false); |
501: | } |
502: | |
503: | return ltrim($this->handleMagicTokens($result)); |
504: | } |
505: | |
506: | protected function pFallback(Node $node) { |
507: | return $this->{'p' . $node->getType()}($node); |
508: | } |
509: | |
510: | |
511: | |
512: | |
513: | |
514: | |
515: | |
516: | |
517: | |
518: | |
519: | |
520: | protected function p(Node $node, $parentFormatPreserved = false) : string { |
521: | |
522: | if (!$this->origTokens) { |
523: | return $this->{'p' . $node->getType()}($node); |
524: | } |
525: | |
526: | |
527: | $origNode = $node->getAttribute('origNode'); |
528: | if (null === $origNode) { |
529: | return $this->pFallback($node); |
530: | } |
531: | |
532: | $class = \get_class($node); |
533: | \assert($class === \get_class($origNode)); |
534: | |
535: | $startPos = $origNode->getStartTokenPos(); |
536: | $endPos = $origNode->getEndTokenPos(); |
537: | \assert($startPos >= 0 && $endPos >= 0); |
538: | |
539: | $fallbackNode = $node; |
540: | if ($node instanceof Expr\New_ && $node->class instanceof Stmt\Class_) { |
541: | |
542: | $node = PrintableNewAnonClassNode::fromNewNode($node); |
543: | $origNode = PrintableNewAnonClassNode::fromNewNode($origNode); |
544: | } |
545: | |
546: | |
547: | |
548: | |
549: | if ($node instanceof Stmt\InlineHTML && !$parentFormatPreserved) { |
550: | return $this->pFallback($fallbackNode); |
551: | } |
552: | |
553: | $indentAdjustment = $this->indentLevel - $this->origTokens->getIndentationBefore($startPos); |
554: | |
555: | $type = $node->getType(); |
556: | $fixupInfo = $this->fixupMap[$class] ?? null; |
557: | |
558: | $result = ''; |
559: | $pos = $startPos; |
560: | foreach ($node->getSubNodeNames() as $subNodeName) { |
561: | $subNode = $node->$subNodeName; |
562: | $origSubNode = $origNode->$subNodeName; |
563: | |
564: | if ((!$subNode instanceof Node && $subNode !== null) |
565: | || (!$origSubNode instanceof Node && $origSubNode !== null) |
566: | ) { |
567: | if ($subNode === $origSubNode) { |
568: | |
569: | continue; |
570: | } |
571: | |
572: | if (is_array($subNode) && is_array($origSubNode)) { |
573: | |
574: | $listResult = $this->pArray( |
575: | $subNode, $origSubNode, $pos, $indentAdjustment, $type, $subNodeName, |
576: | $fixupInfo[$subNodeName] ?? null |
577: | ); |
578: | if (null === $listResult) { |
579: | return $this->pFallback($fallbackNode); |
580: | } |
581: | |
582: | $result .= $listResult; |
583: | continue; |
584: | } |
585: | |
586: | if (is_int($subNode) && is_int($origSubNode)) { |
587: | |
588: | $key = $type . '->' . $subNodeName; |
589: | if (!isset($this->modifierChangeMap[$key])) { |
590: | return $this->pFallback($fallbackNode); |
591: | } |
592: | |
593: | $findToken = $this->modifierChangeMap[$key]; |
594: | $result .= $this->pModifiers($subNode); |
595: | $pos = $this->origTokens->findRight($pos, $findToken); |
596: | continue; |
597: | } |
598: | |
599: | |
600: | |
601: | |
602: | return $this->pFallback($fallbackNode); |
603: | } |
604: | |
605: | $extraLeft = ''; |
606: | $extraRight = ''; |
607: | if ($origSubNode !== null) { |
608: | $subStartPos = $origSubNode->getStartTokenPos(); |
609: | $subEndPos = $origSubNode->getEndTokenPos(); |
610: | \assert($subStartPos >= 0 && $subEndPos >= 0); |
611: | } else { |
612: | if ($subNode === null) { |
613: | |
614: | continue; |
615: | } |
616: | |
617: | |
618: | $key = $type . '->' . $subNodeName; |
619: | if (!isset($this->insertionMap[$key])) { |
620: | return $this->pFallback($fallbackNode); |
621: | } |
622: | |
623: | list($findToken, $beforeToken, $extraLeft, $extraRight) = $this->insertionMap[$key]; |
624: | if (null !== $findToken) { |
625: | $subStartPos = $this->origTokens->findRight($pos, $findToken) |
626: | + (int) !$beforeToken; |
627: | } else { |
628: | $subStartPos = $pos; |
629: | } |
630: | |
631: | if (null === $extraLeft && null !== $extraRight) { |
632: | |
633: | $subStartPos = $this->origTokens->skipRightWhitespace($subStartPos); |
634: | } |
635: | $subEndPos = $subStartPos - 1; |
636: | } |
637: | |
638: | if (null === $subNode) { |
639: | |
640: | $key = $type . '->' . $subNodeName; |
641: | if (!isset($this->removalMap[$key])) { |
642: | return $this->pFallback($fallbackNode); |
643: | } |
644: | |
645: | |
646: | $removalInfo = $this->removalMap[$key]; |
647: | if (isset($removalInfo['left'])) { |
648: | $subStartPos = $this->origTokens->skipLeft($subStartPos - 1, $removalInfo['left']) + 1; |
649: | } |
650: | if (isset($removalInfo['right'])) { |
651: | $subEndPos = $this->origTokens->skipRight($subEndPos + 1, $removalInfo['right']) - 1; |
652: | } |
653: | } |
654: | |
655: | $result .= $this->origTokens->getTokenCode($pos, $subStartPos, $indentAdjustment); |
656: | |
657: | if (null !== $subNode) { |
658: | $result .= $extraLeft; |
659: | |
660: | $origIndentLevel = $this->indentLevel; |
661: | $this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment); |
662: | |
663: | |
664: | |
665: | |
666: | if (isset($fixupInfo[$subNodeName]) |
667: | && $subNode->getAttribute('origNode') !== $origSubNode |
668: | ) { |
669: | $fixup = $fixupInfo[$subNodeName]; |
670: | $res = $this->pFixup($fixup, $subNode, $class, $subStartPos, $subEndPos); |
671: | } else { |
672: | $res = $this->p($subNode, true); |
673: | } |
674: | |
675: | $this->safeAppend($result, $res); |
676: | $this->setIndentLevel($origIndentLevel); |
677: | |
678: | $result .= $extraRight; |
679: | } |
680: | |
681: | $pos = $subEndPos + 1; |
682: | } |
683: | |
684: | $result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment); |
685: | return $result; |
686: | } |
687: | |
688: | |
689: | |
690: | |
691: | |
692: | |
693: | |
694: | |
695: | |
696: | |
697: | |
698: | |
699: | |
700: | |
701: | protected function pArray( |
702: | array $nodes, array $origNodes, int &$pos, int $indentAdjustment, |
703: | string $parentNodeType, string $subNodeName, $fixup |
704: | ) { |
705: | $diff = $this->nodeListDiffer->diffWithReplacements($origNodes, $nodes); |
706: | |
707: | $mapKey = $parentNodeType . '->' . $subNodeName; |
708: | $insertStr = $this->listInsertionMap[$mapKey] ?? null; |
709: | $isStmtList = $subNodeName === 'stmts'; |
710: | |
711: | $beforeFirstKeepOrReplace = true; |
712: | $skipRemovedNode = false; |
713: | $delayedAdd = []; |
714: | $lastElemIndentLevel = $this->indentLevel; |
715: | |
716: | $insertNewline = false; |
717: | if ($insertStr === "\n") { |
718: | $insertStr = ''; |
719: | $insertNewline = true; |
720: | } |
721: | |
722: | if ($isStmtList && \count($origNodes) === 1 && \count($nodes) !== 1) { |
723: | $startPos = $origNodes[0]->getStartTokenPos(); |
724: | $endPos = $origNodes[0]->getEndTokenPos(); |
725: | \assert($startPos >= 0 && $endPos >= 0); |
726: | if (!$this->origTokens->haveBraces($startPos, $endPos)) { |
727: | |
728: | |
729: | |
730: | |
731: | return null; |
732: | } |
733: | } |
734: | |
735: | $result = ''; |
736: | foreach ($diff as $i => $diffElem) { |
737: | $diffType = $diffElem->type; |
738: | |
739: | $arrItem = $diffElem->new; |
740: | |
741: | $origArrItem = $diffElem->old; |
742: | |
743: | if ($diffType === DiffElem::TYPE_KEEP || $diffType === DiffElem::TYPE_REPLACE) { |
744: | $beforeFirstKeepOrReplace = false; |
745: | |
746: | if ($origArrItem === null || $arrItem === null) { |
747: | |
748: | if ($origArrItem === $arrItem) { |
749: | continue; |
750: | } |
751: | return null; |
752: | } |
753: | |
754: | if (!$arrItem instanceof Node || !$origArrItem instanceof Node) { |
755: | |
756: | return null; |
757: | } |
758: | |
759: | $itemStartPos = $origArrItem->getStartTokenPos(); |
760: | $itemEndPos = $origArrItem->getEndTokenPos(); |
761: | \assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos); |
762: | |
763: | $origIndentLevel = $this->indentLevel; |
764: | $lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment; |
765: | $this->setIndentLevel($lastElemIndentLevel); |
766: | |
767: | $comments = $arrItem->getComments(); |
768: | $origComments = $origArrItem->getComments(); |
769: | $commentStartPos = $origComments ? $origComments[0]->getStartTokenPos() : $itemStartPos; |
770: | \assert($commentStartPos >= 0); |
771: | |
772: | if ($commentStartPos < $pos) { |
773: | |
774: | |
775: | $commentStartPos = $itemStartPos; |
776: | } |
777: | |
778: | if ($skipRemovedNode) { |
779: | if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) || |
780: | $this->origTokens->haveTagInRange($pos, $itemStartPos))) { |
781: | |
782: | |
783: | $this->setIndentLevel($origIndentLevel); |
784: | return null; |
785: | } |
786: | } else { |
787: | $result .= $this->origTokens->getTokenCode( |
788: | $pos, $commentStartPos, $indentAdjustment); |
789: | } |
790: | |
791: | if (!empty($delayedAdd)) { |
792: | |
793: | foreach ($delayedAdd as $delayedAddNode) { |
794: | if ($insertNewline) { |
795: | $delayedAddComments = $delayedAddNode->getComments(); |
796: | if ($delayedAddComments) { |
797: | $result .= $this->pComments($delayedAddComments) . $this->nl; |
798: | } |
799: | } |
800: | |
801: | $this->safeAppend($result, $this->p($delayedAddNode, true)); |
802: | |
803: | if ($insertNewline) { |
804: | $result .= $insertStr . $this->nl; |
805: | } else { |
806: | $result .= $insertStr; |
807: | } |
808: | } |
809: | |
810: | $delayedAdd = []; |
811: | } |
812: | |
813: | if ($comments !== $origComments) { |
814: | if ($comments) { |
815: | $result .= $this->pComments($comments) . $this->nl; |
816: | } |
817: | } else { |
818: | $result .= $this->origTokens->getTokenCode( |
819: | $commentStartPos, $itemStartPos, $indentAdjustment); |
820: | } |
821: | |
822: | |
823: | $skipRemovedNode = false; |
824: | } elseif ($diffType === DiffElem::TYPE_ADD) { |
825: | if (null === $insertStr) { |
826: | |
827: | return null; |
828: | } |
829: | |
830: | |
831: | |
832: | if ($insertStr === ', ' && |
833: | ($this->isMultiline($origNodes) || $arrItem->getComments()) |
834: | ) { |
835: | $insertStr = ','; |
836: | $insertNewline = true; |
837: | } |
838: | |
839: | if ($beforeFirstKeepOrReplace) { |
840: | |
841: | $delayedAdd[] = $arrItem; |
842: | continue; |
843: | } |
844: | |
845: | $itemStartPos = $pos; |
846: | $itemEndPos = $pos - 1; |
847: | |
848: | $origIndentLevel = $this->indentLevel; |
849: | $this->setIndentLevel($lastElemIndentLevel); |
850: | |
851: | if ($insertNewline) { |
852: | $result .= $insertStr . $this->nl; |
853: | $comments = $arrItem->getComments(); |
854: | if ($comments) { |
855: | $result .= $this->pComments($comments) . $this->nl; |
856: | } |
857: | } else { |
858: | $result .= $insertStr; |
859: | } |
860: | } elseif ($diffType === DiffElem::TYPE_REMOVE) { |
861: | if (!$origArrItem instanceof Node) { |
862: | |
863: | return null; |
864: | } |
865: | |
866: | $itemStartPos = $origArrItem->getStartTokenPos(); |
867: | $itemEndPos = $origArrItem->getEndTokenPos(); |
868: | \assert($itemStartPos >= 0 && $itemEndPos >= 0); |
869: | |
870: | |
871: | $origComments = $origArrItem->getComments(); |
872: | if ($origComments) { |
873: | $itemStartPos = $origComments[0]->getStartTokenPos(); |
874: | } |
875: | |
876: | if ($i === 0) { |
877: | |
878: | |
879: | $result .= $this->origTokens->getTokenCode( |
880: | $pos, $itemStartPos, $indentAdjustment); |
881: | $skipRemovedNode = true; |
882: | } else { |
883: | if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) || |
884: | $this->origTokens->haveTagInRange($pos, $itemStartPos))) { |
885: | |
886: | |
887: | return null; |
888: | } |
889: | } |
890: | |
891: | $pos = $itemEndPos + 1; |
892: | continue; |
893: | } else { |
894: | throw new \Exception("Shouldn't happen"); |
895: | } |
896: | |
897: | if (null !== $fixup && $arrItem->getAttribute('origNode') !== $origArrItem) { |
898: | $res = $this->pFixup($fixup, $arrItem, null, $itemStartPos, $itemEndPos); |
899: | } else { |
900: | $res = $this->p($arrItem, true); |
901: | } |
902: | $this->safeAppend($result, $res); |
903: | |
904: | $this->setIndentLevel($origIndentLevel); |
905: | $pos = $itemEndPos + 1; |
906: | } |
907: | |
908: | if ($skipRemovedNode) { |
909: | |
910: | return null; |
911: | } |
912: | |
913: | if (!empty($delayedAdd)) { |
914: | if (!isset($this->emptyListInsertionMap[$mapKey])) { |
915: | return null; |
916: | } |
917: | |
918: | list($findToken, $extraLeft, $extraRight) = $this->emptyListInsertionMap[$mapKey]; |
919: | if (null !== $findToken) { |
920: | $insertPos = $this->origTokens->findRight($pos, $findToken) + 1; |
921: | $result .= $this->origTokens->getTokenCode($pos, $insertPos, $indentAdjustment); |
922: | $pos = $insertPos; |
923: | } |
924: | |
925: | $first = true; |
926: | $result .= $extraLeft; |
927: | foreach ($delayedAdd as $delayedAddNode) { |
928: | if (!$first) { |
929: | $result .= $insertStr; |
930: | if ($insertNewline) { |
931: | $result .= $this->nl; |
932: | } |
933: | } |
934: | $result .= $this->p($delayedAddNode, true); |
935: | $first = false; |
936: | } |
937: | $result .= $extraRight === "\n" ? $this->nl : $extraRight; |
938: | } |
939: | |
940: | return $result; |
941: | } |
942: | |
943: | |
944: | |
945: | |
946: | |
947: | |
948: | |
949: | |
950: | |
951: | |
952: | |
953: | |
954: | |
955: | |
956: | |
957: | |
958: | protected function pFixup(int $fixup, Node $subNode, $parentClass, int $subStartPos, int $subEndPos) : string { |
959: | switch ($fixup) { |
960: | case self::FIXUP_PREC_LEFT: |
961: | case self::FIXUP_PREC_RIGHT: |
962: | if (!$this->origTokens->haveParens($subStartPos, $subEndPos)) { |
963: | list($precedence, $associativity) = $this->precedenceMap[$parentClass]; |
964: | return $this->pPrec($subNode, $precedence, $associativity, |
965: | $fixup === self::FIXUP_PREC_LEFT ? -1 : 1); |
966: | } |
967: | break; |
968: | case self::FIXUP_CALL_LHS: |
969: | if ($this->callLhsRequiresParens($subNode) |
970: | && !$this->origTokens->haveParens($subStartPos, $subEndPos) |
971: | ) { |
972: | return '(' . $this->p($subNode) . ')'; |
973: | } |
974: | break; |
975: | case self::FIXUP_DEREF_LHS: |
976: | if ($this->dereferenceLhsRequiresParens($subNode) |
977: | && !$this->origTokens->haveParens($subStartPos, $subEndPos) |
978: | ) { |
979: | return '(' . $this->p($subNode) . ')'; |
980: | } |
981: | break; |
982: | case self::FIXUP_STATIC_DEREF_LHS: |
983: | if ($this->staticDereferenceLhsRequiresParens($subNode) |
984: | && !$this->origTokens->haveParens($subStartPos, $subEndPos) |
985: | ) { |
986: | return '(' . $this->p($subNode) . ')'; |
987: | } |
988: | break; |
989: | case self::FIXUP_NEW: |
990: | if ($this->newOperandRequiresParens($subNode) |
991: | && !$this->origTokens->haveParens($subStartPos, $subEndPos)) { |
992: | return '(' . $this->p($subNode) . ')'; |
993: | } |
994: | break; |
995: | case self::FIXUP_BRACED_NAME: |
996: | case self::FIXUP_VAR_BRACED_NAME: |
997: | if ($subNode instanceof Expr |
998: | && !$this->origTokens->haveBraces($subStartPos, $subEndPos) |
999: | ) { |
1000: | return ($fixup === self::FIXUP_VAR_BRACED_NAME ? '$' : '') |
1001: | . '{' . $this->p($subNode) . '}'; |
1002: | } |
1003: | break; |
1004: | case self::FIXUP_ENCAPSED: |
1005: | if (!$subNode instanceof Scalar\EncapsedStringPart |
1006: | && !$this->origTokens->haveBraces($subStartPos, $subEndPos) |
1007: | ) { |
1008: | return '{' . $this->p($subNode) . '}'; |
1009: | } |
1010: | break; |
1011: | default: |
1012: | throw new \Exception('Cannot happen'); |
1013: | } |
1014: | |
1015: | |
1016: | return $this->p($subNode); |
1017: | } |
1018: | |
1019: | |
1020: | |
1021: | |
1022: | |
1023: | |
1024: | |
1025: | |
1026: | |
1027: | |
1028: | protected function safeAppend(string &$str, string $append) { |
1029: | if ($str === "") { |
1030: | $str = $append; |
1031: | return; |
1032: | } |
1033: | |
1034: | if ($append === "") { |
1035: | return; |
1036: | } |
1037: | |
1038: | if (!$this->labelCharMap[$append[0]] |
1039: | || !$this->labelCharMap[$str[\strlen($str) - 1]]) { |
1040: | $str .= $append; |
1041: | } else { |
1042: | $str .= " " . $append; |
1043: | } |
1044: | } |
1045: | |
1046: | |
1047: | |
1048: | |
1049: | |
1050: | |
1051: | |
1052: | |
1053: | protected function callLhsRequiresParens(Node $node) : bool { |
1054: | return !($node instanceof Node\Name |
1055: | || $node instanceof Expr\Variable |
1056: | || $node instanceof Expr\ArrayDimFetch |
1057: | || $node instanceof Expr\FuncCall |
1058: | || $node instanceof Expr\MethodCall |
1059: | || $node instanceof Expr\NullsafeMethodCall |
1060: | || $node instanceof Expr\StaticCall |
1061: | || $node instanceof Expr\Array_); |
1062: | } |
1063: | |
1064: | |
1065: | |
1066: | |
1067: | |
1068: | |
1069: | |
1070: | |
1071: | protected function dereferenceLhsRequiresParens(Node $node) : bool { |
1072: | |
1073: | return $this->staticDereferenceLhsRequiresParens($node) |
1074: | && !$node instanceof Expr\ConstFetch; |
1075: | } |
1076: | |
1077: | |
1078: | |
1079: | |
1080: | |
1081: | |
1082: | |
1083: | |
1084: | protected function staticDereferenceLhsRequiresParens(Node $node): bool { |
1085: | return !($node instanceof Expr\Variable |
1086: | || $node instanceof Node\Name |
1087: | || $node instanceof Expr\ArrayDimFetch |
1088: | || $node instanceof Expr\PropertyFetch |
1089: | || $node instanceof Expr\NullsafePropertyFetch |
1090: | || $node instanceof Expr\StaticPropertyFetch |
1091: | || $node instanceof Expr\FuncCall |
1092: | || $node instanceof Expr\MethodCall |
1093: | || $node instanceof Expr\NullsafeMethodCall |
1094: | || $node instanceof Expr\StaticCall |
1095: | || $node instanceof Expr\Array_ |
1096: | || $node instanceof Scalar\String_ |
1097: | || $node instanceof Expr\ClassConstFetch); |
1098: | } |
1099: | |
1100: | |
1101: | |
1102: | |
1103: | |
1104: | |
1105: | |
1106: | |
1107: | protected function newOperandRequiresParens(Node $node): bool { |
1108: | if ($node instanceof Node\Name || $node instanceof Expr\Variable) { |
1109: | return false; |
1110: | } |
1111: | if ($node instanceof Expr\ArrayDimFetch || $node instanceof Expr\PropertyFetch || |
1112: | $node instanceof Expr\NullsafePropertyFetch |
1113: | ) { |
1114: | return $this->newOperandRequiresParens($node->var); |
1115: | } |
1116: | if ($node instanceof Expr\StaticPropertyFetch) { |
1117: | return $this->newOperandRequiresParens($node->class); |
1118: | } |
1119: | return true; |
1120: | } |
1121: | |
1122: | |
1123: | |
1124: | |
1125: | |
1126: | |
1127: | |
1128: | |
1129: | protected function pModifiers(int $modifiers) { |
1130: | return ($modifiers & Stmt\Class_::MODIFIER_PUBLIC ? 'public ' : '') |
1131: | . ($modifiers & Stmt\Class_::MODIFIER_PROTECTED ? 'protected ' : '') |
1132: | . ($modifiers & Stmt\Class_::MODIFIER_PRIVATE ? 'private ' : '') |
1133: | . ($modifiers & Stmt\Class_::MODIFIER_STATIC ? 'static ' : '') |
1134: | . ($modifiers & Stmt\Class_::MODIFIER_ABSTRACT ? 'abstract ' : '') |
1135: | . ($modifiers & Stmt\Class_::MODIFIER_FINAL ? 'final ' : '') |
1136: | . ($modifiers & Stmt\Class_::MODIFIER_READONLY ? 'readonly ' : ''); |
1137: | } |
1138: | |
1139: | |
1140: | |
1141: | |
1142: | |
1143: | |
1144: | |
1145: | |
1146: | protected function isMultiline(array $nodes) : bool { |
1147: | if (\count($nodes) < 2) { |
1148: | return false; |
1149: | } |
1150: | |
1151: | $pos = -1; |
1152: | foreach ($nodes as $node) { |
1153: | if (null === $node) { |
1154: | continue; |
1155: | } |
1156: | |
1157: | $endPos = $node->getEndTokenPos() + 1; |
1158: | if ($pos >= 0) { |
1159: | $text = $this->origTokens->getTokenCode($pos, $endPos, 0); |
1160: | if (false === strpos($text, "\n")) { |
1161: | |
1162: | |
1163: | |
1164: | return false; |
1165: | } |
1166: | } |
1167: | $pos = $endPos; |
1168: | } |
1169: | |
1170: | return true; |
1171: | } |
1172: | |
1173: | |
1174: | |
1175: | |
1176: | |
1177: | |
1178: | protected function initializeLabelCharMap() { |
1179: | if ($this->labelCharMap) return; |
1180: | |
1181: | $this->labelCharMap = []; |
1182: | for ($i = 0; $i < 256; $i++) { |
1183: | |
1184: | |
1185: | $chr = chr($i); |
1186: | $this->labelCharMap[$chr] = $i >= 0x7f || ctype_alnum($chr); |
1187: | } |
1188: | } |
1189: | |
1190: | |
1191: | |
1192: | |
1193: | |
1194: | |
1195: | protected function initializeNodeListDiffer() { |
1196: | if ($this->nodeListDiffer) return; |
1197: | |
1198: | $this->nodeListDiffer = new Internal\Differ(function ($a, $b) { |
1199: | if ($a instanceof Node && $b instanceof Node) { |
1200: | return $a === $b->getAttribute('origNode'); |
1201: | } |
1202: | |
1203: | return $a === null && $b === null; |
1204: | }); |
1205: | } |
1206: | |
1207: | |
1208: | |
1209: | |
1210: | |
1211: | |
1212: | |
1213: | protected function initializeFixupMap() { |
1214: | if ($this->fixupMap) return; |
1215: | |
1216: | $this->fixupMap = [ |
1217: | Expr\PreInc::class => ['var' => self::FIXUP_PREC_RIGHT], |
1218: | Expr\PreDec::class => ['var' => self::FIXUP_PREC_RIGHT], |
1219: | Expr\PostInc::class => ['var' => self::FIXUP_PREC_LEFT], |
1220: | Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT], |
1221: | Expr\Instanceof_::class => [ |
1222: | 'expr' => self::FIXUP_PREC_LEFT, |
1223: | 'class' => self::FIXUP_NEW, |
1224: | ], |
1225: | Expr\Ternary::class => [ |
1226: | 'cond' => self::FIXUP_PREC_LEFT, |
1227: | 'else' => self::FIXUP_PREC_RIGHT, |
1228: | ], |
1229: | |
1230: | Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS], |
1231: | Expr\StaticCall::class => ['class' => self::FIXUP_STATIC_DEREF_LHS], |
1232: | Expr\ArrayDimFetch::class => ['var' => self::FIXUP_DEREF_LHS], |
1233: | Expr\ClassConstFetch::class => [ |
1234: | 'class' => self::FIXUP_STATIC_DEREF_LHS, |
1235: | 'name' => self::FIXUP_BRACED_NAME, |
1236: | ], |
1237: | Expr\New_::class => ['class' => self::FIXUP_NEW], |
1238: | Expr\MethodCall::class => [ |
1239: | 'var' => self::FIXUP_DEREF_LHS, |
1240: | 'name' => self::FIXUP_BRACED_NAME, |
1241: | ], |
1242: | Expr\NullsafeMethodCall::class => [ |
1243: | 'var' => self::FIXUP_DEREF_LHS, |
1244: | 'name' => self::FIXUP_BRACED_NAME, |
1245: | ], |
1246: | Expr\StaticPropertyFetch::class => [ |
1247: | 'class' => self::FIXUP_STATIC_DEREF_LHS, |
1248: | 'name' => self::FIXUP_VAR_BRACED_NAME, |
1249: | ], |
1250: | Expr\PropertyFetch::class => [ |
1251: | 'var' => self::FIXUP_DEREF_LHS, |
1252: | 'name' => self::FIXUP_BRACED_NAME, |
1253: | ], |
1254: | Expr\NullsafePropertyFetch::class => [ |
1255: | 'var' => self::FIXUP_DEREF_LHS, |
1256: | 'name' => self::FIXUP_BRACED_NAME, |
1257: | ], |
1258: | Scalar\Encapsed::class => [ |
1259: | 'parts' => self::FIXUP_ENCAPSED, |
1260: | ], |
1261: | ]; |
1262: | |
1263: | $binaryOps = [ |
1264: | BinaryOp\Pow::class, BinaryOp\Mul::class, BinaryOp\Div::class, BinaryOp\Mod::class, |
1265: | BinaryOp\Plus::class, BinaryOp\Minus::class, BinaryOp\Concat::class, |
1266: | BinaryOp\ShiftLeft::class, BinaryOp\ShiftRight::class, BinaryOp\Smaller::class, |
1267: | BinaryOp\SmallerOrEqual::class, BinaryOp\Greater::class, BinaryOp\GreaterOrEqual::class, |
1268: | BinaryOp\Equal::class, BinaryOp\NotEqual::class, BinaryOp\Identical::class, |
1269: | BinaryOp\NotIdentical::class, BinaryOp\Spaceship::class, BinaryOp\BitwiseAnd::class, |
1270: | BinaryOp\BitwiseXor::class, BinaryOp\BitwiseOr::class, BinaryOp\BooleanAnd::class, |
1271: | BinaryOp\BooleanOr::class, BinaryOp\Coalesce::class, BinaryOp\LogicalAnd::class, |
1272: | BinaryOp\LogicalXor::class, BinaryOp\LogicalOr::class, |
1273: | ]; |
1274: | foreach ($binaryOps as $binaryOp) { |
1275: | $this->fixupMap[$binaryOp] = [ |
1276: | 'left' => self::FIXUP_PREC_LEFT, |
1277: | 'right' => self::FIXUP_PREC_RIGHT |
1278: | ]; |
1279: | } |
1280: | |
1281: | $assignOps = [ |
1282: | Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class, |
1283: | AssignOp\Mul::class, AssignOp\Div::class, AssignOp\Concat::class, AssignOp\Mod::class, |
1284: | AssignOp\BitwiseAnd::class, AssignOp\BitwiseOr::class, AssignOp\BitwiseXor::class, |
1285: | AssignOp\ShiftLeft::class, AssignOp\ShiftRight::class, AssignOp\Pow::class, AssignOp\Coalesce::class |
1286: | ]; |
1287: | foreach ($assignOps as $assignOp) { |
1288: | $this->fixupMap[$assignOp] = [ |
1289: | 'var' => self::FIXUP_PREC_LEFT, |
1290: | 'expr' => self::FIXUP_PREC_RIGHT, |
1291: | ]; |
1292: | } |
1293: | |
1294: | $prefixOps = [ |
1295: | Expr\BitwiseNot::class, Expr\BooleanNot::class, Expr\UnaryPlus::class, Expr\UnaryMinus::class, |
1296: | Cast\Int_::class, Cast\Double::class, Cast\String_::class, Cast\Array_::class, |
1297: | Cast\Object_::class, Cast\Bool_::class, Cast\Unset_::class, Expr\ErrorSuppress::class, |
1298: | Expr\YieldFrom::class, Expr\Print_::class, Expr\Include_::class, |
1299: | ]; |
1300: | foreach ($prefixOps as $prefixOp) { |
1301: | $this->fixupMap[$prefixOp] = ['expr' => self::FIXUP_PREC_RIGHT]; |
1302: | } |
1303: | } |
1304: | |
1305: | |
1306: | |
1307: | |
1308: | |
1309: | |
1310: | |
1311: | protected function initializeRemovalMap() { |
1312: | if ($this->removalMap) return; |
1313: | |
1314: | $stripBoth = ['left' => \T_WHITESPACE, 'right' => \T_WHITESPACE]; |
1315: | $stripLeft = ['left' => \T_WHITESPACE]; |
1316: | $stripRight = ['right' => \T_WHITESPACE]; |
1317: | $stripDoubleArrow = ['right' => \T_DOUBLE_ARROW]; |
1318: | $stripColon = ['left' => ':']; |
1319: | $stripEquals = ['left' => '=']; |
1320: | $this->removalMap = [ |
1321: | 'Expr_ArrayDimFetch->dim' => $stripBoth, |
1322: | 'Expr_ArrayItem->key' => $stripDoubleArrow, |
1323: | 'Expr_ArrowFunction->returnType' => $stripColon, |
1324: | 'Expr_Closure->returnType' => $stripColon, |
1325: | 'Expr_Exit->expr' => $stripBoth, |
1326: | 'Expr_Ternary->if' => $stripBoth, |
1327: | 'Expr_Yield->key' => $stripDoubleArrow, |
1328: | 'Expr_Yield->value' => $stripBoth, |
1329: | 'Param->type' => $stripRight, |
1330: | 'Param->default' => $stripEquals, |
1331: | 'Stmt_Break->num' => $stripBoth, |
1332: | 'Stmt_Catch->var' => $stripLeft, |
1333: | 'Stmt_ClassConst->type' => $stripRight, |
1334: | 'Stmt_ClassMethod->returnType' => $stripColon, |
1335: | 'Stmt_Class->extends' => ['left' => \T_EXTENDS], |
1336: | 'Stmt_Enum->scalarType' => $stripColon, |
1337: | 'Stmt_EnumCase->expr' => $stripEquals, |
1338: | 'Expr_PrintableNewAnonClass->extends' => ['left' => \T_EXTENDS], |
1339: | 'Stmt_Continue->num' => $stripBoth, |
1340: | 'Stmt_Foreach->keyVar' => $stripDoubleArrow, |
1341: | 'Stmt_Function->returnType' => $stripColon, |
1342: | 'Stmt_If->else' => $stripLeft, |
1343: | 'Stmt_Namespace->name' => $stripLeft, |
1344: | 'Stmt_Property->type' => $stripRight, |
1345: | 'Stmt_PropertyProperty->default' => $stripEquals, |
1346: | 'Stmt_Return->expr' => $stripBoth, |
1347: | 'Stmt_StaticVar->default' => $stripEquals, |
1348: | 'Stmt_TraitUseAdaptation_Alias->newName' => $stripLeft, |
1349: | 'Stmt_TryCatch->finally' => $stripLeft, |
1350: | |
1351: | |
1352: | |
1353: | |
1354: | ]; |
1355: | } |
1356: | |
1357: | protected function initializeInsertionMap() { |
1358: | if ($this->insertionMap) return; |
1359: | |
1360: | |
1361: | |
1362: | $this->insertionMap = [ |
1363: | 'Expr_ArrayDimFetch->dim' => ['[', false, null, null], |
1364: | 'Expr_ArrayItem->key' => [null, false, null, ' => '], |
1365: | 'Expr_ArrowFunction->returnType' => [')', false, ' : ', null], |
1366: | 'Expr_Closure->returnType' => [')', false, ' : ', null], |
1367: | 'Expr_Ternary->if' => ['?', false, ' ', ' '], |
1368: | 'Expr_Yield->key' => [\T_YIELD, false, null, ' => '], |
1369: | 'Expr_Yield->value' => [\T_YIELD, false, ' ', null], |
1370: | 'Param->type' => [null, false, null, ' '], |
1371: | 'Param->default' => [null, false, ' = ', null], |
1372: | 'Stmt_Break->num' => [\T_BREAK, false, ' ', null], |
1373: | 'Stmt_Catch->var' => [null, false, ' ', null], |
1374: | 'Stmt_ClassMethod->returnType' => [')', false, ' : ', null], |
1375: | 'Stmt_ClassConst->type' => [\T_CONST, false, ' ', null], |
1376: | 'Stmt_Class->extends' => [null, false, ' extends ', null], |
1377: | 'Stmt_Enum->scalarType' => [null, false, ' : ', null], |
1378: | 'Stmt_EnumCase->expr' => [null, false, ' = ', null], |
1379: | 'Expr_PrintableNewAnonClass->extends' => [null, ' extends ', null], |
1380: | 'Stmt_Continue->num' => [\T_CONTINUE, false, ' ', null], |
1381: | 'Stmt_Foreach->keyVar' => [\T_AS, false, null, ' => '], |
1382: | 'Stmt_Function->returnType' => [')', false, ' : ', null], |
1383: | 'Stmt_If->else' => [null, false, ' ', null], |
1384: | 'Stmt_Namespace->name' => [\T_NAMESPACE, false, ' ', null], |
1385: | 'Stmt_Property->type' => [\T_VARIABLE, true, null, ' '], |
1386: | 'Stmt_PropertyProperty->default' => [null, false, ' = ', null], |
1387: | 'Stmt_Return->expr' => [\T_RETURN, false, ' ', null], |
1388: | 'Stmt_StaticVar->default' => [null, false, ' = ', null], |
1389: | |
1390: | 'Stmt_TryCatch->finally' => [null, false, ' ', null], |
1391: | |
1392: | |
1393: | |
1394: | |
1395: | |
1396: | |
1397: | ]; |
1398: | } |
1399: | |
1400: | protected function initializeListInsertionMap() { |
1401: | if ($this->listInsertionMap) return; |
1402: | |
1403: | $this->listInsertionMap = [ |
1404: | |
1405: | |
1406: | |
1407: | 'Stmt_Catch->types' => '|', |
1408: | 'UnionType->types' => '|', |
1409: | 'IntersectionType->types' => '&', |
1410: | 'Stmt_If->elseifs' => ' ', |
1411: | 'Stmt_TryCatch->catches' => ' ', |
1412: | |
1413: | |
1414: | 'Expr_Array->items' => ', ', |
1415: | 'Expr_ArrowFunction->params' => ', ', |
1416: | 'Expr_Closure->params' => ', ', |
1417: | 'Expr_Closure->uses' => ', ', |
1418: | 'Expr_FuncCall->args' => ', ', |
1419: | 'Expr_Isset->vars' => ', ', |
1420: | 'Expr_List->items' => ', ', |
1421: | 'Expr_MethodCall->args' => ', ', |
1422: | 'Expr_NullsafeMethodCall->args' => ', ', |
1423: | 'Expr_New->args' => ', ', |
1424: | 'Expr_PrintableNewAnonClass->args' => ', ', |
1425: | 'Expr_StaticCall->args' => ', ', |
1426: | 'Stmt_ClassConst->consts' => ', ', |
1427: | 'Stmt_ClassMethod->params' => ', ', |
1428: | 'Stmt_Class->implements' => ', ', |
1429: | 'Stmt_Enum->implements' => ', ', |
1430: | 'Expr_PrintableNewAnonClass->implements' => ', ', |
1431: | 'Stmt_Const->consts' => ', ', |
1432: | 'Stmt_Declare->declares' => ', ', |
1433: | 'Stmt_Echo->exprs' => ', ', |
1434: | 'Stmt_For->init' => ', ', |
1435: | 'Stmt_For->cond' => ', ', |
1436: | 'Stmt_For->loop' => ', ', |
1437: | 'Stmt_Function->params' => ', ', |
1438: | 'Stmt_Global->vars' => ', ', |
1439: | 'Stmt_GroupUse->uses' => ', ', |
1440: | 'Stmt_Interface->extends' => ', ', |
1441: | 'Stmt_Match->arms' => ', ', |
1442: | 'Stmt_Property->props' => ', ', |
1443: | 'Stmt_StaticVar->vars' => ', ', |
1444: | 'Stmt_TraitUse->traits' => ', ', |
1445: | 'Stmt_TraitUseAdaptation_Precedence->insteadof' => ', ', |
1446: | 'Stmt_Unset->vars' => ', ', |
1447: | 'Stmt_Use->uses' => ', ', |
1448: | 'MatchArm->conds' => ', ', |
1449: | 'AttributeGroup->attrs' => ', ', |
1450: | |
1451: | |
1452: | 'Expr_Closure->stmts' => "\n", |
1453: | 'Stmt_Case->stmts' => "\n", |
1454: | 'Stmt_Catch->stmts' => "\n", |
1455: | 'Stmt_Class->stmts' => "\n", |
1456: | 'Stmt_Enum->stmts' => "\n", |
1457: | 'Expr_PrintableNewAnonClass->stmts' => "\n", |
1458: | 'Stmt_Interface->stmts' => "\n", |
1459: | 'Stmt_Trait->stmts' => "\n", |
1460: | 'Stmt_ClassMethod->stmts' => "\n", |
1461: | 'Stmt_Declare->stmts' => "\n", |
1462: | 'Stmt_Do->stmts' => "\n", |
1463: | 'Stmt_ElseIf->stmts' => "\n", |
1464: | 'Stmt_Else->stmts' => "\n", |
1465: | 'Stmt_Finally->stmts' => "\n", |
1466: | 'Stmt_Foreach->stmts' => "\n", |
1467: | 'Stmt_For->stmts' => "\n", |
1468: | 'Stmt_Function->stmts' => "\n", |
1469: | 'Stmt_If->stmts' => "\n", |
1470: | 'Stmt_Namespace->stmts' => "\n", |
1471: | 'Stmt_Class->attrGroups' => "\n", |
1472: | 'Stmt_Enum->attrGroups' => "\n", |
1473: | 'Stmt_EnumCase->attrGroups' => "\n", |
1474: | 'Stmt_Interface->attrGroups' => "\n", |
1475: | 'Stmt_Trait->attrGroups' => "\n", |
1476: | 'Stmt_Function->attrGroups' => "\n", |
1477: | 'Stmt_ClassMethod->attrGroups' => "\n", |
1478: | 'Stmt_ClassConst->attrGroups' => "\n", |
1479: | 'Stmt_Property->attrGroups' => "\n", |
1480: | 'Expr_PrintableNewAnonClass->attrGroups' => ' ', |
1481: | 'Expr_Closure->attrGroups' => ' ', |
1482: | 'Expr_ArrowFunction->attrGroups' => ' ', |
1483: | 'Param->attrGroups' => ' ', |
1484: | 'Stmt_Switch->cases' => "\n", |
1485: | 'Stmt_TraitUse->adaptations' => "\n", |
1486: | 'Stmt_TryCatch->stmts' => "\n", |
1487: | 'Stmt_While->stmts' => "\n", |
1488: | |
1489: | |
1490: | 'File->stmts' => "\n", |
1491: | ]; |
1492: | } |
1493: | |
1494: | protected function initializeEmptyListInsertionMap() { |
1495: | if ($this->emptyListInsertionMap) return; |
1496: | |
1497: | |
1498: | |
1499: | |
1500: | $this->emptyListInsertionMap = [ |
1501: | 'Expr_ArrowFunction->params' => ['(', '', ''], |
1502: | 'Expr_Closure->uses' => [')', ' use(', ')'], |
1503: | 'Expr_Closure->params' => ['(', '', ''], |
1504: | 'Expr_FuncCall->args' => ['(', '', ''], |
1505: | 'Expr_MethodCall->args' => ['(', '', ''], |
1506: | 'Expr_NullsafeMethodCall->args' => ['(', '', ''], |
1507: | 'Expr_New->args' => ['(', '', ''], |
1508: | 'Expr_PrintableNewAnonClass->args' => ['(', '', ''], |
1509: | 'Expr_PrintableNewAnonClass->implements' => [null, ' implements ', ''], |
1510: | 'Expr_StaticCall->args' => ['(', '', ''], |
1511: | 'Stmt_Class->implements' => [null, ' implements ', ''], |
1512: | 'Stmt_Enum->implements' => [null, ' implements ', ''], |
1513: | 'Stmt_ClassMethod->params' => ['(', '', ''], |
1514: | 'Stmt_Interface->extends' => [null, ' extends ', ''], |
1515: | 'Stmt_Function->params' => ['(', '', ''], |
1516: | 'Stmt_Interface->attrGroups' => [null, '', "\n"], |
1517: | 'Stmt_Class->attrGroups' => [null, '', "\n"], |
1518: | 'Stmt_ClassConst->attrGroups' => [null, '', "\n"], |
1519: | 'Stmt_ClassMethod->attrGroups' => [null, '', "\n"], |
1520: | 'Stmt_Function->attrGroups' => [null, '', "\n"], |
1521: | 'Stmt_Property->attrGroups' => [null, '', "\n"], |
1522: | 'Stmt_Trait->attrGroups' => [null, '', "\n"], |
1523: | 'Expr_ArrowFunction->attrGroups' => [null, '', ' '], |
1524: | 'Expr_Closure->attrGroups' => [null, '', ' '], |
1525: | 'Expr_PrintableNewAnonClass->attrGroups' => [\T_NEW, ' ', ''], |
1526: | |
1527: | |
1528: | |
1529: | |
1530: | |
1531: | |
1532: | |
1533: | |
1534: | |
1535: | |
1536: | |
1537: | |
1538: | |
1539: | |
1540: | |
1541: | |
1542: | |
1543: | |
1544: | |
1545: | |
1546: | |
1547: | |
1548: | |
1549: | |
1550: | |
1551: | |
1552: | |
1553: | |
1554: | ]; |
1555: | } |
1556: | |
1557: | protected function initializeModifierChangeMap() { |
1558: | if ($this->modifierChangeMap) return; |
1559: | |
1560: | $this->modifierChangeMap = [ |
1561: | 'Stmt_ClassConst->flags' => \T_CONST, |
1562: | 'Stmt_ClassMethod->flags' => \T_FUNCTION, |
1563: | 'Stmt_Class->flags' => \T_CLASS, |
1564: | 'Stmt_Property->flags' => \T_VARIABLE, |
1565: | 'Expr_PrintableNewAnonClass->flags' => \T_CLASS, |
1566: | 'Param->flags' => \T_VARIABLE, |
1567: | |
1568: | ]; |
1569: | |
1570: | |
1571: | |
1572: | |
1573: | |
1574: | |
1575: | } |
1576: | } |
1577: | |