Skip to content

Commit

Permalink
[EarlyReturn] Handle continue/break in foreach with next return on Ch…
Browse files Browse the repository at this point in the history
…angeAndIfToEarlyReturnRector (#946)

* [EarlyReturn] Handle continue in foreach with next return on ChangeAndIfToEarlyReturnRector

* update

* eol

* Fixed 🎉
  • Loading branch information
samsonasik committed Oct 1, 2021
1 parent 347b593 commit 0552b3a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;

class BreakInForeachNextReturn
{
public function run(array $data, bool $a, bool $b)
{
foreach ($data as $value) {
if ($a === $b && ! $value) {
break;
}

return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;

class BreakInForeachNextReturn
{
public function run(array $data, bool $a, bool $b)
{
foreach ($data as $value) {
if ($a !== $b) {
return true;
}
if ($value) {
return true;
}
break;
}

return false;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;

class ContinueInForeachNextReturn
{
public function run(array $data, bool $a, bool $b)
{
foreach ($data as $value) {
if ($a === $b && ! $value) {
continue;
}

return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture;

class ContinueInForeachNextReturn
{
public function run(array $data, bool $a, bool $b)
{
foreach ($data as $value) {
if ($a !== $b) {
return true;
}
if ($value) {
return true;
}
continue;
}

return false;
}
}

?>
19 changes: 18 additions & 1 deletion rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
Expand Down Expand Up @@ -119,13 +121,28 @@ public function refactor(Node $node): ?array
? clone $node->stmts[0]
: new Return_();

if ($this->contextAnalyzer->isInLoop($node)) {
if ($this->isInLoopWithoutContinueOrBreak($node)) {
$afters[] = new Return_();
}

return $this->processReplaceIfs($node, $booleanAndConditions, $ifNextReturnClone, $afters);
}

private function isInLoopWithoutContinueOrBreak(If_ $if): bool
{
if (!$this->contextAnalyzer->isInLoop(
$if
)) {
return false;
}

if ($if->stmts[0] instanceof Continue_) {
return false;
}

return ! $if->stmts[0] instanceof Break_;
}

/**
* @param Expr[] $conditions
* @param Node[] $afters
Expand Down

0 comments on commit 0552b3a

Please sign in to comment.