diff --git a/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/merge_comment.php.inc b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/merge_comment.php.inc new file mode 100644 index 00000000000..8cd0ba5f37f --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture/merge_comment.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php index 4e0bae1fe97..9f772a64473 100644 --- a/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php +++ b/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php @@ -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; @@ -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; } diff --git a/src/NodeTypeResolver/Node/AttributeKey.php b/src/NodeTypeResolver/Node/AttributeKey.php index 3f1ecd2312f..986bbf2973d 100644 --- a/src/NodeTypeResolver/Node/AttributeKey.php +++ b/src/NodeTypeResolver/Node/AttributeKey.php @@ -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'; } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index a65915b299b..050b56c21ff 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -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);