Skip to content

Commit

Permalink
[Printer] Handle add Node after Nop not first stmt (#3312)
Browse files Browse the repository at this point in the history
* [Printer] Handle add Node after Nop not first stmt

* Fixed 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 28, 2023
1 parent 33527d4 commit 3555ee3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/NodeDecorator/MixPhpHtmlDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public function decorateInlineHTML(InlineHTML $inlineHTML, int $key, array $node
/**
* @param array<Node|null> $nodes
*/
public function decorateAfterNop(Nop $nop, array $nodes): void
public function decorateAfterNop(Nop $nop, int $key, array $nodes): void
{
if (! isset($nodes[1]) || $nodes[1] instanceof InlineHTML) {
if (! isset($nodes[$key + 1]) || $nodes[$key + 1] instanceof InlineHTML) {
return;
}

if (! $nodes[1] instanceof Stmt) {
if (! $nodes[$key + 1] instanceof Stmt) {
return;
}

$firstNodeAfterNop = $nodes[1];
$firstNodeAfterNop = $nodes[$key + 1];
if ($firstNodeAfterNop->getStartTokenPos() >= 0) {
return;
}
Expand Down
27 changes: 13 additions & 14 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,9 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
}

$content = $this->cleanEndWithPHPOpenTag($lastStmt, $content);
$content = str_replace('<?php <?php', '<?php', $content);

/** @var Node $firstStmt */
$isFirstStmtReprinted = $firstStmt->getAttribute(AttributeKey::ORIGINAL_NODE) === null;
if (! $isFirstStmtReprinted) {
return $this->cleanSurplusTag($content);
}

if (! $firstStmt instanceof InlineHTML) {
return str_replace('<?php <?php', '<?php', $content);
}

return $this->cleanSurplusTag($content);
return $this->cleanSurplusTag($firstStmt, $content);
}

/**
Expand Down Expand Up @@ -537,8 +528,16 @@ private function cleanEndWithPHPOpenTag(Node $node, string $content): string
return $content;
}

private function cleanSurplusTag(string $content): string
private function cleanSurplusTag(Node $node, string $content): string
{
if (! $node instanceof InlineHTML) {
return $content;
}

if (str_starts_with($content, "?>\n")) {
return substr($content, 3);
}

if (str_starts_with($content, "<?php\n\n?>")) {
return substr($content, 10);
}
Expand Down Expand Up @@ -612,8 +611,8 @@ private function decorateInlineHTMLOrNopAndUpdatePhpdocInfo(array $nodes): void
$this->mixPhpHtmlDecorator->decorateInlineHTML($node, $key, $nodes);
}

if ($node instanceof Nop && $key === 0 && $hasDiff) {
$this->mixPhpHtmlDecorator->decorateAfterNop($node, $nodes);
if ($node instanceof Nop && $hasDiff) {
$this->mixPhpHtmlDecorator->decorateAfterNop($node, $key, $nodes);
}

$this->docBlockUpdater->updateNodeWithPhpDocInfo($node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div></div>
<?php
/** @var Zend_View $this */
?>
<div></div>
-----
<div></div>
<?php
/** @var Zend_View $this */
/**
* @var string $container
*/
echo 'this is new stmt after Nop';?>
<div></div>

0 comments on commit 3555ee3

Please sign in to comment.