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