diff --git a/rules/code-quality/src/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php b/rules/code-quality/src/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php index 7912c764fa6..5fdda35ac25 100644 --- a/rules/code-quality/src/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php +++ b/rules/code-quality/src/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php @@ -84,7 +84,7 @@ CODE_SAMPLE // keep comments of first line if ($position === 1) { - $assignExpression->setAttribute(AttributeKey::COMMENTS, $node->getComments()); + $this->mirrorComments($assignExpression, $node); } $this->addNodeAfterNode($assignExpression, $node); diff --git a/rules/early-return/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php b/rules/early-return/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php index d283308ae36..e4c7c818a89 100644 --- a/rules/early-return/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php +++ b/rules/early-return/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php @@ -128,7 +128,7 @@ CODE_SAMPLE $conditions = $this->getBooleanAndConditions($expr); $ifs = $this->createInvertedIfNodesFromConditions($node, $conditions); - $this->keepCommentIfExists($node, $ifs); + $this->mirrorComments($ifs[0], $node); $this->addNodesAfterNode($ifs, $node); $this->addNodeAfterNode($ifReturn, $node); @@ -233,15 +233,6 @@ CODE_SAMPLE return $ifs; } - /** - * @param If_[] $ifs - */ - private function keepCommentIfExists(If_ $if, array $ifs): void - { - $nodeComments = $if->getAttribute(AttributeKey::COMMENTS); - $ifs[0]->setAttribute(AttributeKey::COMMENTS, $nodeComments); - } - private function getIfNextReturn(If_ $if): ?Return_ { $nextNode = $if->getAttribute(AttributeKey::NEXT_NODE); diff --git a/rules/early-return/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php b/rules/early-return/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php index df8096451ed..16fcf441ffe 100644 --- a/rules/early-return/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php +++ b/rules/early-return/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php @@ -106,7 +106,7 @@ CODE_SAMPLE $assign = $this->stmtsManipulator->getUnwrappedLastStmt($node->stmts); $return = new Return_($assign->expr); - $this->copyCommentIfExists($assign, $return); + $this->mirrorComments($return, $assign); $node->stmts[$lastIfStmtKey] = $return; /** @var Assign $assign */ @@ -117,7 +117,7 @@ CODE_SAMPLE $elseStmts = $node->else->stmts; $return = new Return_($assign->expr); - $this->copyCommentIfExists($assign, $return); + $this->mirrorComments($return, $assign); $elseStmts[$lastElseStmtKey] = $return; $node->else = null; @@ -127,10 +127,4 @@ CODE_SAMPLE return $node; } - - private function copyCommentIfExists(Node $from, Node $to): void - { - $nodeComments = $from->getAttribute(AttributeKey::COMMENTS); - $to->setAttribute(AttributeKey::COMMENTS, $nodeComments); - } } diff --git a/rules/early-return/src/Rector/If_/RemoveAlwaysElseRector.php b/rules/early-return/src/Rector/If_/RemoveAlwaysElseRector.php index 324a0150e6f..a14ac43d61d 100644 --- a/rules/early-return/src/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/early-return/src/Rector/If_/RemoveAlwaysElseRector.php @@ -87,8 +87,7 @@ CODE_SAMPLE $firstElseIf = array_shift($node->elseifs); $node->cond = $firstElseIf->cond; $node->stmts = $firstElseIf->stmts; - - $this->copyCommentIfExists($firstElseIf, $node); + $this->mirrorComments($node, $firstElseIf); return $node; } @@ -112,9 +111,4 @@ CODE_SAMPLE || ($lastStmt instanceof Expression && $lastStmt->expr instanceof Exit_)); } - private function copyCommentIfExists(ElseIf_ $elseIf, If_ $if): void - { - $nodeComments = $elseIf->getAttribute(AttributeKey::COMMENTS); - $if->setAttribute(AttributeKey::COMMENTS, $nodeComments); - } } diff --git a/rules/early-return/src/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php b/rules/early-return/src/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php index cfaaa637ee6..d43e4be76a2 100644 --- a/rules/early-return/src/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php +++ b/rules/early-return/src/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php @@ -75,11 +75,10 @@ CODE_SAMPLE $left = $node->expr->left; $ifNegations = $this->createMultipleIfsNegation($left, $node, []); - $nodeComments = $node->getAttribute(AttributeKey::COMMENTS); foreach ($ifNegations as $key => $ifNegation) { if ($key === 0) { - $ifNegation->setAttribute(AttributeKey::COMMENTS, $nodeComments); + $this->mirrorComments($ifNegation, $node); } $this->addNodeBeforeNode($ifNegation, $node); diff --git a/rules/php72/src/Rector/While_/WhileEachToForeachRector.php b/rules/php72/src/Rector/While_/WhileEachToForeachRector.php index fa9b659aaa3..648f1dce165 100644 --- a/rules/php72/src/Rector/While_/WhileEachToForeachRector.php +++ b/rules/php72/src/Rector/While_/WhileEachToForeachRector.php @@ -110,9 +110,7 @@ CODE_SAMPLE 'stmts' => $node->stmts, ]); - /** @var Comment[] $comments */ - $comments = $node->getAttribute(AttributeKey::COMMENTS); - $foreach->setAttribute(AttributeKey::COMMENTS, $comments); + $this->mirrorComments($foreach, $node); // is key included? add it to foreach if ($listNode->items !== []) {