Skip to content

Commit

Permalink
[CodeQuality] Handle mix HTML+PHP on ForRepeatedCountToOwnVariableRec…
Browse files Browse the repository at this point in the history
…tor (#3282)
  • Loading branch information
samsonasik committed Jan 14, 2023
1 parent 774de56 commit d9ee43c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div>
<?php
for ($i = 5; $i <= count($items); $i++) {
echo $items[$i];
}
?>
</div>
-----
<?php

?>
<div>
<?php
$itemsCount = count($items);
for ($i = 5; $i <= $itemsCount; $i++) {
echo $items[$i];
}
?>
</div>
<?php
13 changes: 13 additions & 0 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
Expand Down Expand Up @@ -421,6 +422,12 @@ private function connectNodes(array $nodes, Node $node): void
if (! $firstNodePreviousNode instanceof Node && $node->hasAttribute(AttributeKey::PREVIOUS_NODE)) {
/** @var Node $previousNode */
$previousNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE);

if ($previousNode instanceof InlineHTML && ! $firstNode instanceof InlineHTML) {
// re-print InlineHTML is safe
$previousNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

$nodes = [$previousNode, ...$nodes];
}

Expand All @@ -430,6 +437,12 @@ private function connectNodes(array $nodes, Node $node): void
if (! $lastNodeNextNode instanceof Node && $node->hasAttribute(AttributeKey::NEXT_NODE)) {
/** @var Node $nextNode */
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);

if ($nextNode instanceof InlineHTML && ! $lastNode instanceof InlineHTML) {
// re-print InlineHTML is safe
$nextNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

$nodes = [...$nodes, $nextNode];
}

Expand Down

0 comments on commit d9ee43c

Please sign in to comment.