diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/else_inline_html.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/else_inline_html.php.inc new file mode 100644 index 00000000000..70bb41e94c7 --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/else_inline_html.php.inc @@ -0,0 +1,38 @@ + 2) { + return; + } else { ?> + this is world + +----- + 2) { + return; + } + ?> + this is world + diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/skip_else_inline_html.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/skip_else_inline_html.php.inc deleted file mode 100644 index f8d7a950a40..00000000000 --- a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/skip_else_inline_html.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - 2 ) { - return; - } else { ?> - this is world - mirrorComments($node, $firstElseIf); $nodesToReturnAfterNode = $this->getStatementsElseIfs($node); - if ($originalNode->else instanceof Else_ && ! $this->inlineHTMLAnalyzer->hasInlineHTML( - $originalNode->else - )) { + if ($originalNode->else instanceof Else_) { $node->else = null; $nodesToReturnAfterNode = array_merge($nodesToReturnAfterNode, [$originalNode->else]); } @@ -108,7 +100,7 @@ public function refactor(Node $node): ?array return [$if, $node, ...$nodesToReturnAfterNode]; } - if ($node->else instanceof Else_ && ! $this->inlineHTMLAnalyzer->hasInlineHTML($node->else)) { + if ($node->else instanceof Else_) { $stmts = $node->else->stmts; $node->else = null; diff --git a/src/NodeDecorator/MixPhpHtmlDecorator.php b/src/NodeDecorator/MixPhpHtmlDecorator.php index 53d30d6016c..f72722a8bbb 100644 --- a/src/NodeDecorator/MixPhpHtmlDecorator.php +++ b/src/NodeDecorator/MixPhpHtmlDecorator.php @@ -111,6 +111,24 @@ private function rePrintInlineHTML(InlineHTML $inlineHTML, Stmt $stmt): void if ($stmt->getStartTokenPos() < 0) { $inlineHTML->setAttribute(AttributeKey::ORIGINAL_NODE, null); $this->isRequireReprintInlineHTML = true; + + return; + } + + $originalNode = $stmt->getAttribute(AttributeKey::ORIGINAL_NODE); + if (! $originalNode instanceof Node) { + return; + } + + $node = $originalNode->getAttribute(AttributeKey::PARENT_NODE); + if (! $node instanceof Stmt) { + return; + } + + // last Stmt that connected to InlineHTML just removed + if ($inlineHTML->getAttribute(AttributeKey::PARENT_NODE) !== $node) { + $inlineHTML->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $this->isRequireReprintInlineHTML = true; } } } diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 8f2cb2e3396..3b8b91a2998 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -517,6 +517,7 @@ private function cleanSurplusTag(string $content): string { $content = str_replace('?>', '?>', $content); + $content = str_replace("?>\n?>", '?>', $content); if (str_starts_with($content, "?>\n")) { return substr($content, 3);