Skip to content

Commit

Permalink
Fix no return in ChangeSwitchToMatchRector (#3188)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Langlet <VincentLanglet@users.noreply.github.com>
  • Loading branch information
TomasVotruba and VincentLanglet committed Dec 11, 2022
1 parent 4a03eb9 commit 809467e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

final class UnrelatedReturn
{
public function run($data, $proxyQuery, $alias): bool
{
switch ($data->getValue()) {
case ProductService::SPECIAL_GESTION_CAPSULE:
$proxyQuery->getQueryBuilder()->andWhere($alias . '.collectionCapsule = true');
break;
case ProductService::SPECIAL_GESTION_PRESTIGE:
$proxyQuery->getQueryBuilder()->andWhere($alias . '.editionPrestige = true');
break;
}

return true;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

final class UnrelatedReturn
{
public function run($data, $proxyQuery, $alias): bool
{
match ($data->getValue()) {
ProductService::SPECIAL_GESTION_CAPSULE => $proxyQuery->getQueryBuilder()->andWhere($alias . '.collectionCapsule = true'),
ProductService::SPECIAL_GESTION_PRESTIGE => $proxyQuery->getQueryBuilder()->andWhere($alias . '.editionPrestige = true'),
default => true,
};

return true;
}
}

?>
1 change: 1 addition & 0 deletions rules/Php80/NodeFactory/MatchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function createFromCondAndExprs(Expr $condExpr, array $condAndExprs, ?Stm
}

$shouldRemoteNextStmt = ! $expr instanceof Expr;

$condAndExprs[] = new CondAndExpr([], $nextStmt->expr, MatchKind::RETURN);
}
}
Expand Down
2 changes: 1 addition & 1 deletion rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function refactor(Node $node): ?Node
}

$match = $matchResult->getMatch();
if ($matchResult->shouldRemoveNextStmt()) {
if ($matchResult->shouldRemoveNextStmt() && $isReturn) {
unset($node->stmts[$key + 1]);
}

Expand Down

0 comments on commit 809467e

Please sign in to comment.