Skip to content

Commit

Permalink
[FunctionLike] Skip return by ref with switch on SimplifyUselessVaria…
Browse files Browse the repository at this point in the history
…bleRector (#6623)
  • Loading branch information
samsonasik authored Dec 20, 2024
1 parent 8a49d9d commit df255c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ function & notAnonymous()
return $var;
}
}

function & notAnonymous2()
{
for ($i = 0; $i < 4; $i++) {
switch ($i) {
case 1:
$var = 42;
return $var;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -50,6 +51,14 @@ private function setByRefAttribute(array $stmts): void
continue;
}

if ($stmt instanceof Switch_) {
foreach ($stmt->cases as $case) {
$this->setByRefAttribute($case->stmts);
}

continue;
}

if ($stmt instanceof Return_) {
$stmt->setAttribute(AttributeKey::IS_BYREF_RETURN, true);
}
Expand Down

0 comments on commit df255c0

Please sign in to comment.