Skip to content

Commit 961f158

Browse files
committed
Avoid negative indendation in formatting-preserving printer
Fixes #1015.
1 parent e50c67b commit 961f158

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ protected function p(
736736
$result .= $extraLeft;
737737

738738
$origIndentLevel = $this->indentLevel;
739-
$this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment);
739+
$this->setIndentLevel(max($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment, 0));
740740

741741
// If it's the same node that was previously in this position, it certainly doesn't
742742
// need fixup. It's important to check this here, because our fixup checks are more
@@ -839,7 +839,7 @@ protected function pArray(
839839
\assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos);
840840

841841
$origIndentLevel = $this->indentLevel;
842-
$lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
842+
$lastElemIndentLevel = max($this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment, 0);
843843
$this->setIndentLevel($lastElemIndentLevel);
844844

845845
$comments = $arrItem->getComments();

test/code/formatPreservation/indent.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,36 @@ if ($a) {
3535
@@{"\t"}@@$x;
3636
@@{"\t"}@@$y;
3737
}
38+
-----
39+
<?php
40+
array_merge(
41+
[
42+
$x,
43+
$y,
44+
]
45+
);
46+
-----
47+
$call = $stmts[0]->expr;
48+
$stmts[0]->expr = new Expr\StaticCall(new Node\Name\FullyQualified('Compat'), $call->name, $call->args);
49+
-----
50+
<?php
51+
\Compat::array_merge([
52+
$x,
53+
$y,
54+
]);
55+
-----
56+
<?php
57+
if ($a) {
58+
if (
59+
$b
60+
) {}
61+
}
62+
-----
63+
$stmts[0] = new Stmt\While_($stmts[0]->cond, $stmts[0]->stmts);
64+
-----
65+
<?php
66+
while ($a) {
67+
if (
68+
$b
69+
) {}
70+
}

0 commit comments

Comments
 (0)