diff --git a/lib/PhpParser/Internal/TokenStream.php b/lib/PhpParser/Internal/TokenStream.php index 84c0175e..7e0a5de0 100644 --- a/lib/PhpParser/Internal/TokenStream.php +++ b/lib/PhpParser/Internal/TokenStream.php @@ -206,6 +206,11 @@ class TokenStream || $this->haveTokenInRange($startPos, $endPos, '}'); } + public function haveTagInRange(int $startPos, int $endPos): bool { + return $this->haveTokenInRange($startPos, $endPos, \T_OPEN_TAG) + || $this->haveTokenInRange($startPos, $endPos, \T_CLOSE_TAG); + } + /** * Get indentation before token position. * diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 82b1e355..6ed936fe 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -774,7 +774,8 @@ abstract class PrettyPrinterAbstract } if ($skipRemovedNode) { - if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) { + if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) || + $this->origTokens->haveTagInRange($pos, $itemStartPos))) { // We'd remove the brace of a code block. // TODO: Preserve formatting. $this->setIndentLevel($origIndentLevel); @@ -877,7 +878,8 @@ abstract class PrettyPrinterAbstract $pos, $itemStartPos, $indentAdjustment); $skipRemovedNode = true; } else { - if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) { + if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) || + $this->origTokens->haveTagInRange($pos, $itemStartPos))) { // We'd remove the brace of a code block. // TODO: Preserve formatting. return null; diff --git a/test/code/formatPreservation/inlineHtml.test b/test/code/formatPreservation/inlineHtml.test index ae25cc64..0b131fed 100644 --- a/test/code/formatPreservation/inlineHtml.test +++ b/test/code/formatPreservation/inlineHtml.test @@ -42,13 +42,14 @@ function test() { baz(); } ----- -// TODO Fix broken result +// TODO Preserve formatting $stmts[0]->stmts[1] = $stmts[0]->stmts[2]; ----- <?php -function test() { - foo();<?php +function test() +{ + foo(); baz(); baz(); } @@ -61,14 +62,15 @@ function test() { baz(); } ----- -// TODO Fix broken result +// TODO Preserve formatting unset($stmts[0]->stmts[2]); ----- <?php -function test() { +function test() +{ foo(); - ?>Bar + ?>Bar<?php } ----- <?php @@ -79,13 +81,14 @@ function test() { baz(); } ----- -// TODO Fix broken result +// TODO Preserve formatting array_splice($stmts[0]->stmts, 0, 1, []); ----- <?php -function test() { - Bar<?php +function test() +{ + ?>Bar<?php baz(); } ----- @@ -97,12 +100,13 @@ function test() { baz(); } ----- -// TODO Fix broken result +// TODO Preserve formatting array_splice($stmts[0]->stmts, 1, 1, []); ----- <?php -function test() { - foo();<?php +function test() +{ + foo(); baz(); }