mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 10:43:35 +01:00
added failing ChangeIfElseValueAssignToEarlyReturnRector test, regarding lost comments (#4698)
This commit is contained in:
parent
04abc7af5c
commit
ae0d38e1f0
@ -105,7 +105,9 @@ CODE_SAMPLE
|
||||
/** @var Assign $assign */
|
||||
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($node->stmts);
|
||||
|
||||
$node->stmts[$lastIfStmtKey] = new Return_($assign->expr);
|
||||
$return = new Return_($assign->expr);
|
||||
$this->copyCommentIfExists($assign, $return);
|
||||
$node->stmts[$lastIfStmtKey] = $return;
|
||||
|
||||
/** @var Assign $assign */
|
||||
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($node->else->stmts);
|
||||
@ -113,7 +115,10 @@ CODE_SAMPLE
|
||||
$lastElseStmtKey = array_key_last($node->else->stmts);
|
||||
|
||||
$elseStmts = $node->else->stmts;
|
||||
$elseStmts[$lastElseStmtKey] = new Return_($assign->expr);
|
||||
|
||||
$return = new Return_($assign->expr);
|
||||
$this->copyCommentIfExists($assign, $return);
|
||||
$elseStmts[$lastElseStmtKey] = $return;
|
||||
|
||||
$node->else = null;
|
||||
$this->addNodesAfterNode($elseStmts, $node);
|
||||
@ -122,4 +127,10 @@ CODE_SAMPLE
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function copyCommentIfExists(Node $from, Node $to): void
|
||||
{
|
||||
$nodeComments = $from->getAttribute(AttributeKey::COMMENTS);
|
||||
$to->setAttribute(AttributeKey::COMMENTS, $nodeComments);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector\Fixture;
|
||||
|
||||
class LostComment
|
||||
{
|
||||
protected function replaceUrl($internalUrl, $publicUrl, $contenttext)
|
||||
{
|
||||
if (null === $publicUrl) {
|
||||
// parameters could not be resolved into a url
|
||||
$contenttext = preg_replace('@href=(["\'\s]*'.preg_quote($internalUrl, '@').'["\'\s]*)@', '', $contenttext);
|
||||
} else {
|
||||
// else comment
|
||||
$contenttext = str_replace($internalUrl, $publicUrl, $contenttext);
|
||||
}
|
||||
|
||||
return $contenttext;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\SOLID\Tests\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector\Fixture;
|
||||
|
||||
class LostComment
|
||||
{
|
||||
protected function replaceUrl($internalUrl, $publicUrl, $contenttext)
|
||||
{
|
||||
if (null === $publicUrl) {
|
||||
// parameters could not be resolved into a url
|
||||
return preg_replace('@href=(["\'\s]*'.preg_quote($internalUrl, '@').'["\'\s]*)@', '', $contenttext);
|
||||
}
|
||||
|
||||
// else comment
|
||||
return str_replace($internalUrl, $publicUrl, $contenttext);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user