mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-27 05:03:31 +00:00
[EarlyReturn] Move comment in If_ stmt for ChangeAndIfToEarlyReturnRector (#648)
This commit is contained in:
parent
90fd207cf6
commit
e35724edae
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;
|
||||||
|
|
||||||
|
class MoveCommentInIfStmt
|
||||||
|
{
|
||||||
|
public function canDrive(Car $car)
|
||||||
|
{
|
||||||
|
if ($car->hasWheels && $car->hasFuel) {
|
||||||
|
// a comment
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;
|
||||||
|
|
||||||
|
class MoveCommentInIfStmt
|
||||||
|
{
|
||||||
|
public function canDrive(Car $car)
|
||||||
|
{
|
||||||
|
if (!$car->hasWheels) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!$car->hasFuel) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// a comment
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;
|
||||||
|
|
||||||
|
class MoveCommentInIfStmt2
|
||||||
|
{
|
||||||
|
public function canDrive(Car $car)
|
||||||
|
{
|
||||||
|
if ($car->hasWheels && $car->hasFuel) {
|
||||||
|
// a comment
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// another comment
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;
|
||||||
|
|
||||||
|
class MoveCommentInIfStmt2
|
||||||
|
{
|
||||||
|
public function canDrive(Car $car)
|
||||||
|
{
|
||||||
|
if (!$car->hasWheels) {
|
||||||
|
// another comment
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!$car->hasFuel) {
|
||||||
|
// another comment
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// a comment
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -30,10 +30,15 @@ final class InvertedIfFactory
|
||||||
public function createFromConditions(If_ $if, array $conditions, Return_ $return): array
|
public function createFromConditions(If_ $if, array $conditions, Return_ $return): array
|
||||||
{
|
{
|
||||||
$ifs = [];
|
$ifs = [];
|
||||||
$stmt = $this->contextAnalyzer->isInLoop($if) && ! $this->getIfNextReturn($if)
|
$ifNextReturn = $this->getIfNextReturn($if);
|
||||||
|
$stmt = $this->contextAnalyzer->isInLoop($if) && ! $ifNextReturn
|
||||||
? [new Continue_()]
|
? [new Continue_()]
|
||||||
: [$return];
|
: [$return];
|
||||||
|
|
||||||
|
if ($ifNextReturn instanceof Return_) {
|
||||||
|
$stmt[0]->setAttribute(AttributeKey::COMMENTS, $ifNextReturn->getAttribute(AttributeKey::COMMENTS));
|
||||||
|
}
|
||||||
|
|
||||||
$getNextReturnExpr = $this->getNextReturnExpr($if);
|
$getNextReturnExpr = $this->getNextReturnExpr($if);
|
||||||
if ($getNextReturnExpr instanceof Return_) {
|
if ($getNextReturnExpr instanceof Return_) {
|
||||||
$return->expr = $getNextReturnExpr->expr;
|
$return->expr = $getNextReturnExpr->expr;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user