From 961f158f6da32565e7e3a33e23b8a38941068597 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 29 Sep 2024 15:34:59 +0200 Subject: [PATCH] Avoid negative indendation in formatting-preserving printer Fixes #1015. --- lib/PhpParser/PrettyPrinterAbstract.php | 4 +-- test/code/formatPreservation/indent.test | 33 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 4941be95f4..d32be24817 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -736,7 +736,7 @@ protected function p( $result .= $extraLeft; $origIndentLevel = $this->indentLevel; - $this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment); + $this->setIndentLevel(max($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment, 0)); // If it's the same node that was previously in this position, it certainly doesn't // need fixup. It's important to check this here, because our fixup checks are more @@ -839,7 +839,7 @@ protected function pArray( \assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos); $origIndentLevel = $this->indentLevel; - $lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment; + $lastElemIndentLevel = max($this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment, 0); $this->setIndentLevel($lastElemIndentLevel); $comments = $arrItem->getComments(); diff --git a/test/code/formatPreservation/indent.test b/test/code/formatPreservation/indent.test index c8113a07dd..8a66a7c944 100644 --- a/test/code/formatPreservation/indent.test +++ b/test/code/formatPreservation/indent.test @@ -35,3 +35,36 @@ if ($a) { @@{"\t"}@@$x; @@{"\t"}@@$y; } +----- +expr; +$stmts[0]->expr = new Expr\StaticCall(new Node\Name\FullyQualified('Compat'), $call->name, $call->args); +----- +cond, $stmts[0]->stmts); +----- +