Skip to content

Commit 3b26636

Browse files
committed
Fix Printer
1 parent b5e21fa commit 3b26636

File tree

1 file changed

+17
-46
lines changed

1 file changed

+17
-46
lines changed

src/Printer/Printer.php

+17-46
Original file line numberDiff line numberDiff line change
@@ -550,30 +550,23 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
550550

551551
foreach ($diff as $i => $diffElem) {
552552
$diffType = $diffElem->type;
553-
$arrItem = $diffElem->new;
554-
$origArrayItem = $diffElem->old;
553+
$newNode = $diffElem->new;
554+
$originalNode = $diffElem->old;
555555
if ($diffType === DiffElem::TYPE_KEEP || $diffType === DiffElem::TYPE_REPLACE) {
556556
$beforeFirstKeepOrReplace = false;
557-
if (!$arrItem instanceof Node || !$origArrayItem instanceof Node) {
557+
if (!$newNode instanceof Node || !$originalNode instanceof Node) {
558558
return null;
559559
}
560560

561561
/** @var int $itemStartPos */
562-
$itemStartPos = $origArrayItem->getAttribute(Attribute::START_INDEX);
562+
$itemStartPos = $originalNode->getAttribute(Attribute::START_INDEX);
563563

564564
/** @var int $itemEndPos */
565-
$itemEndPos = $origArrayItem->getAttribute(Attribute::END_INDEX);
566-
565+
$itemEndPos = $originalNode->getAttribute(Attribute::END_INDEX);
567566
if ($itemStartPos < 0 || $itemEndPos < 0 || $itemStartPos < $tokenIndex) {
568567
throw new LogicException();
569568
}
570569

571-
$comments = $arrItem->getAttribute(Attribute::COMMENTS) ?? [];
572-
$origComments = $origArrayItem->getAttribute(Attribute::COMMENTS) ?? [];
573-
574-
$commentStartPos = count($origComments) > 0 ? $origComments[0]->startIndex : $itemStartPos;
575-
assert($commentStartPos >= 0);
576-
577570
$result .= $originalTokens->getContentBetween($tokenIndex, $itemStartPos);
578571

579572
if (count($delayedAdd) > 0) {
@@ -583,15 +576,6 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
583576
if ($parenthesesNeeded) {
584577
$result .= '(';
585578
}
586-
587-
if ($insertNewline) {
588-
$delayedAddComments = $delayedAddNode->getAttribute(Attribute::COMMENTS) ?? [];
589-
if (count($delayedAddComments) > 0) {
590-
$result .= $this->printComments($delayedAddComments, $beforeAsteriskIndent, $afterAsteriskIndent);
591-
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
592-
}
593-
}
594-
595579
$result .= $this->printNodeFormatPreserving($delayedAddNode, $originalTokens);
596580
if ($parenthesesNeeded) {
597581
$result .= ')';
@@ -608,21 +592,14 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
608592
}
609593

610594
$parenthesesNeeded = isset($this->parenthesesListMap[$mapKey])
611-
&& in_array(get_class($arrItem), $this->parenthesesListMap[$mapKey], true)
612-
&& !in_array(get_class($origArrayItem), $this->parenthesesListMap[$mapKey], true);
595+
&& in_array(get_class($newNode), $this->parenthesesListMap[$mapKey], true)
596+
&& !in_array(get_class($originalNode), $this->parenthesesListMap[$mapKey], true);
613597
$addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($itemStartPos, $itemEndPos);
614598
if ($addParentheses) {
615599
$result .= '(';
616600
}
617601

618-
if ($comments !== $origComments) {
619-
if (count($comments) > 0) {
620-
$result .= $this->printComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent);
621-
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
622-
}
623-
}
624-
625-
$result .= $this->printNodeFormatPreserving($arrItem, $originalTokens);
602+
$result .= $this->printNodeFormatPreserving($newNode, $originalTokens);
626603
if ($addParentheses) {
627604
$result .= ')';
628605
}
@@ -632,58 +609,52 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
632609
if ($insertStr === null) {
633610
return null;
634611
}
635-
if (!$arrItem instanceof Node) {
612+
if (!$newNode instanceof Node) {
636613
return null;
637614
}
638615

639-
if ($insertStr === ', ' && $isMultiline || count($arrItem->getAttribute(Attribute::COMMENTS) ?? []) > 0) {
616+
if ($insertStr === ', ' && $isMultiline || count($newNode->getAttribute(Attribute::COMMENTS) ?? []) > 0) {
640617
$insertStr = ',';
641618
$insertNewline = true;
642619
}
643620

644621
if ($beforeFirstKeepOrReplace) {
645622
// Will be inserted at the next "replace" or "keep" element
646-
$delayedAdd[] = $arrItem;
623+
$delayedAdd[] = $newNode;
647624
continue;
648625
}
649626

650627
/** @var int $itemEndPos */
651628
$itemEndPos = $tokenIndex - 1;
652629
if ($insertNewline) {
653-
$comments = $arrItem->getAttribute(Attribute::COMMENTS) ?? [];
654-
$result .= $insertStr;
655-
if (count($comments) > 0) {
656-
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
657-
$result .= $this->printComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent);
658-
}
659-
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
630+
$result .= $insertStr . sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
660631
} else {
661632
$result .= $insertStr;
662633
}
663634

664635
$parenthesesNeeded = isset($this->parenthesesListMap[$mapKey])
665-
&& in_array(get_class($arrItem), $this->parenthesesListMap[$mapKey], true);
636+
&& in_array(get_class($newNode), $this->parenthesesListMap[$mapKey], true);
666637
if ($parenthesesNeeded) {
667638
$result .= '(';
668639
}
669640

670-
$result .= $this->printNodeFormatPreserving($arrItem, $originalTokens);
641+
$result .= $this->printNodeFormatPreserving($newNode, $originalTokens);
671642
if ($parenthesesNeeded) {
672643
$result .= ')';
673644
}
674645

675646
$tokenIndex = $itemEndPos + 1;
676647

677648
} elseif ($diffType === DiffElem::TYPE_REMOVE) {
678-
if (!$origArrayItem instanceof Node) {
649+
if (!$originalNode instanceof Node) {
679650
return null;
680651
}
681652

682653
/** @var int $itemStartPos */
683-
$itemStartPos = $origArrayItem->getAttribute(Attribute::START_INDEX);
654+
$itemStartPos = $originalNode->getAttribute(Attribute::START_INDEX);
684655

685656
/** @var int $itemEndPos */
686-
$itemEndPos = $origArrayItem->getAttribute(Attribute::END_INDEX);
657+
$itemEndPos = $originalNode->getAttribute(Attribute::END_INDEX);
687658
if ($itemStartPos < 0 || $itemEndPos < 0) {
688659
throw new LogicException();
689660
}

0 commit comments

Comments
 (0)