Using $this->mirrorComments() from AbstractRector to keep comment (#4923)

* Using $this->mirrorComments() from AbstractRector to keep comment

* fix

* fix

* clean up

Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
This commit is contained in:
Abdul Malik Ikhsan 2020-12-20 19:56:43 +07:00 committed by GitHub
parent 574c7cd174
commit 7f674d7c8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 7 additions and 31 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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 !== []) {