Skip to content

Commit

Permalink
[Printer][EarlyReturn] Enable InlineHTML on RemoveAlwaysElseRector (#…
Browse files Browse the repository at this point in the history
…3387)

* enable inlineHTML on RemoveAlwaysElseRector

* FIx

* FIx

* FIx

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fix

* Fix

* Fix

* early return

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 16, 2023
1 parent 5db390c commit d8aa22b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\Fixture;

final class ElseInlineHTML
{
public function run()
{
if (rand(0, 5) > 2) {
return;
} else { ?>
this is <?php echo "hello"; ?> world
<?php }
}
}

?>
-----
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\Fixture;

final class ElseInlineHTML
{
public function run()
{
if (rand(0, 5) > 2) {
return;
}
?>
this is <?php
echo "hello";
?> world
<?php
}
}

?>

This file was deleted.

12 changes: 2 additions & 10 deletions rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use Rector\Core\NodeAnalyzer\InlineHTMLAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,11 +22,6 @@
*/
final class RemoveAlwaysElseRector extends AbstractRector
{
public function __construct(
private readonly InlineHTMLAnalyzer $inlineHTMLAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -98,17 +92,15 @@ public function refactor(Node $node): ?array
$this->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]);
}

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;

Expand Down
18 changes: 18 additions & 0 deletions src/NodeDecorator/MixPhpHtmlDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
1 change: 1 addition & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ private function cleanSurplusTag(string $content): string
{
$content = str_replace('<?php <?php', '<?php', $content);
$content = str_replace('?>?>', '?>', $content);
$content = str_replace("?>\n?>", '?>', $content);

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

0 comments on commit d8aa22b

Please sign in to comment.