Skip to content

Commit 4bc8243

Browse files
committed
Only special-case brace handling for statement lists
Don't interpret the } before a catch clause as a block statement and trigger a pretty printing fallback.
1 parent fd6052e commit 4bc8243

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ protected function pArray(
704704

705705
$mapKey = $parentNodeType . '->' . $subNodeName;
706706
$insertStr = $this->listInsertionMap[$mapKey] ?? null;
707+
$isStmtList = $subNodeName === 'stmts';
707708

708709
$beforeFirstKeepOrReplace = true;
709710
$skipRemovedNode = false;
@@ -716,7 +717,7 @@ protected function pArray(
716717
$insertNewline = true;
717718
}
718719

719-
if ($subNodeName === 'stmts' && \count($origNodes) === 1 && \count($nodes) !== 1) {
720+
if ($isStmtList && \count($origNodes) === 1 && \count($nodes) !== 1) {
720721
$startPos = $origNodes[0]->getStartTokenPos();
721722
$endPos = $origNodes[0]->getEndTokenPos();
722723
\assert($startPos >= 0 && $endPos >= 0);
@@ -865,15 +866,19 @@ protected function pArray(
865866
$pos, $itemStartPos, $indentAdjustment);
866867
$skipRemovedNode = true;
867868

868-
if ($this->origTokens->haveTokenImmediatelyAfter($itemEndPos, '{')
869-
|| $this->origTokens->haveTokenImmediatelyAfter($itemEndPos, '}')) {
869+
if ($isStmtList
870+
&& ($this->origTokens->haveTokenImmediatelyAfter($itemEndPos, '{')
871+
|| $this->origTokens->haveTokenImmediatelyAfter($itemEndPos, '}'))
872+
) {
870873
// We'd remove the brace of a code block.
871874
// TODO: Preserve formatting.
872875
return null;
873876
}
874877
} else {
875-
if ($this->origTokens->haveTokenImmediatelyBefore($itemStartPos, '{')
876-
|| $this->origTokens->haveTokenImmediatelyBefore($itemStartPos, '}')) {
878+
if ($isStmtList
879+
&& ($this->origTokens->haveTokenImmediatelyBefore($itemStartPos, '{')
880+
|| $this->origTokens->haveTokenImmediatelyBefore($itemStartPos, '}'))
881+
) {
877882
// We'd remove the brace of a code block.
878883
// TODO: Preserve formatting.
879884
return null;

0 commit comments

Comments
 (0)