Skip to content

Commit

Permalink
[CodeQuality][Printer] Handle mix php+html on TernaryFalseExpressionT…
Browse files Browse the repository at this point in the history
…oIfRector (#3382)

Co-authored-by: kkmuffme <11071985+kkmuffme@users.noreply.github.com>
  • Loading branch information
samsonasik and kkmuffme committed Feb 15, 2023
1 parent f6d2041 commit a9e4d4b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture;

final class WithHTML
{
public function run( $foo, $bar )
{
?>
<input type="number" name="<?php echo $foo; ?>" value="<?php isset( $bar ) ? $bar : 0 ?>">
<?php
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture;

final class WithHTML
{
public function run( $foo, $bar )
{
?>
<input type="number" name="<?php echo $foo; ?>" value="<?php if (isset( $bar )) {
$bar;
}?>">
<?php
}
}

?>
10 changes: 10 additions & 0 deletions src/NodeDecorator/MixPhpHtmlDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ final class MixPhpHtmlDecorator
{
private NodeRemover $nodeRemover;

private bool $isRequireReprintInlineHTML = false;

#[Required]
public function autowire(NodeRemover $nodeRemover): void
{
$this->nodeRemover = $nodeRemover;
}

public function isRequireReprintInlineHTML(): bool
{
return $this->isRequireReprintInlineHTML;
}

/**
* @param array<Node|null> $nodes
*/
Expand Down Expand Up @@ -89,13 +96,16 @@ public function decorateAfterNop(Nop $nop, int $key, array $nodes): void

// remove Nop is marked as comment of Next Node
$this->nodeRemover->removeNode($nop);

$this->isRequireReprintInlineHTML = true;
}

private function rePrintInlineHTML(InlineHTML $inlineHTML, Stmt $stmt): void
{
// Token start = -1, just added
if ($stmt->getStartTokenPos() < 0) {
$inlineHTML->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->isRequireReprintInlineHTML = true;
}
}
}
33 changes: 7 additions & 26 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,12 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
$content .= $this->nl;
}

$currentFile = $this->currentFileProvider->getFile();
if ($currentFile instanceof File && ! $currentFile->getFileDiff() instanceof FileDiff) {
if (! $this->mixPhpHtmlDecorator->isRequireReprintInlineHTML()) {
return $content;
}

$firstStmt = current($newStmts);
$lastStmt = end($newStmts);

if ($firstStmt === $lastStmt) {
return $content;
}

if (! $firstStmt instanceof InlineHTML && ! $lastStmt instanceof InlineHTML) {
return $content;
}

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

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

/**
Expand Down Expand Up @@ -511,12 +497,8 @@ protected function pParam(Param $param): string
. ($param->default instanceof Expr ? ' = ' . $this->p($param->default) : '');
}

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

if (str_ends_with($content, "<?php \n")) {
return substr($content, 0, -7);
}
Expand All @@ -528,11 +510,10 @@ private function cleanEndWithPHPOpenTag(Node $node, string $content): string
return $content;
}

private function cleanSurplusTag(Node $node, string $content): string
private function cleanSurplusTag(string $content): string
{
if (! $node instanceof InlineHTML) {
return $content;
}
$content = str_replace('<?php <?php', '<?php', $content);
$content = str_replace('?>?>', '?>', $content);

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

0 comments on commit a9e4d4b

Please sign in to comment.