Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;

class MergeComments
{
public function run()
{
// toplevel
if (true) {
// first innner
f();
// second inner
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;

class MergeComments
{
public function run()
{
// toplevel
// first innner
f();
// second inner
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\IntersectionType;
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -126,6 +127,12 @@ public function refactor(Node $node): int|null|array|If_
return NodeVisitor::REMOVE_NODE;
}

$node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge(
$node->getComments(),
$node->stmts[0]->getComments(),
));
$node->stmts[0]->setAttribute(AttributeKey::HAS_MERGED_COMMENTS, true);

return $node->stmts;
}

Expand Down
5 changes: 5 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,9 @@ final class AttributeKey
* @var string
*/
public const IS_FIRST_LEVEL_STATEMENT = 'first_level_stmt';

/**
* @var string
*/
public const HAS_MERGED_COMMENTS = 'has_merged_comments';
}
5 changes: 4 additions & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ private function postRefactorProcess(

if (is_array($refactoredNode)) {
$firstNode = current($refactoredNode);
$this->mirrorComments($firstNode, $originalNode);

if ($firstNode->getAttribute(AttributeKey::HAS_MERGED_COMMENTS, false) === false) {
$this->mirrorComments($firstNode, $originalNode);
}

$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);

Expand Down
Loading