Skip to content

Commit

Permalink
[EarlyReturn] Do not move up variable up next foreach on ChangeAndIfT…
Browse files Browse the repository at this point in the history
…oEarlyReturnRector (#852)

* [EarlyReturn] Do not move up variable up next foreach

* update fixture

* Fixed 🎉

* cs
  • Loading branch information
samsonasik committed Sep 8, 2021
1 parent 5b17710 commit 70e4b3c
Show file tree
Hide file tree
Showing 2 changed files with 48 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 DoNotMoveVariableUpForeach
{
public function run(array $data, $a, $b)
{
foreach ($data as $value) {
if ($a && $b) {
unset($value);
}
}

$targets = [];
return $targets;
}
}

?>
-----
<?php

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

class DoNotMoveVariableUpForeach
{
public function run(array $data, $a, $b)
{
foreach ($data as $value) {
if (!$a) {
continue;
}
if (!$b) {
continue;
}
unset($value);
}

$targets = [];
return $targets;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ private function processReplaceIfs(If_ $node, array $conditions, Return_ $ifNext

$this->removeNode($node);

if (! $node->stmts[0] instanceof Return_ && $ifNextReturnClone->expr instanceof Expr) {
if (! $node->stmts[0] instanceof Return_ && $ifNextReturnClone->expr instanceof Expr && ! $this->contextAnalyzer->isInLoop(
$node
)) {
return [$node, $ifNextReturnClone];
}

Expand Down

0 comments on commit 70e4b3c

Please sign in to comment.