Skip to content

Commit

Permalink
[Php80] Fix multiple cases and case mix up default for ChangeSwitchTo…
Browse files Browse the repository at this point in the history
…MatchRector (#231)

Co-authored-by: Do Pham Tuan <dptuan.itiu@gmail.com>
  • Loading branch information
samsonasik and tuandp committed Jun 16, 2021
1 parent bad9871 commit 5157414
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

class CasesMixUpDefault
{
public function run($value)
{
switch ($value) {
case 'foo':
$string = 'foo';
break;
case 'other':
default:
$string = 'bar';
break;
}
}
}

?>
-----
<?php

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

class CasesMixUpDefault
{
public function run($value)
{
$string = match ($value) {
'foo' => 'foo',
default => 'bar',
};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class MultipleCases
switch ($value) {
case 'v1':
case 'v2':
case 'v3':
return 100;
default:
return 1000;
Expand All @@ -27,7 +28,7 @@ final class MultipleCases
public function run($value)
{
return match ($value) {
'v1', 'v2' => 100,
'v1', 'v2', 'v3' => 100,
default => 1000,
};
}
Expand Down
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 MultipleCases2
{
public function run($value)
{
switch ($value) {
case 'v1':
case 'v2':
case 'v3':
return 100;
case 'v4':
case 'v5':
case 'v6':
return 200;
default:
return 1000;
}
}
}

?>
-----
<?php

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

final class MultipleCases2
{
public function run($value)
{
return match ($value) {
'v1', 'v2', 'v3' => 100,
'v4', 'v5', 'v6' => 200,
default => 1000,
};
}
}

?>
30 changes: 21 additions & 9 deletions rules/Php80/NodeResolver/SwitchExprsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ final class SwitchExprsResolver
*/
public function resolve(Switch_ $switch): array
{
$condAndExpr = [];
$condAndExpr = [];
$collectionEmptyCasesCond = [];

$previousCondExpr = null;
foreach ($switch->cases as $case) {
foreach ($switch->cases as $key => $case) {
if (! $this->isValidCase($case)) {
return [];
}

// prepend to previous one
if ($case->stmts === [] && $case->cond instanceof Expr) {
$collectionEmptyCasesCond[$key] = $case->cond;
}
}

foreach ($switch->cases as $key => $case) {
if ($case->stmts === []) {
$previousCondExpr = $case->cond;
continue;
}

Expand All @@ -42,12 +46,20 @@ public function resolve(Switch_ $switch): array
}

$condExprs = [];
if ($previousCondExpr instanceof Expr) {
$condExprs[] = $previousCondExpr;
$previousCondExpr = null;
}

if ($case->cond !== null) {
$emptyCasesCond = [];

foreach ($collectionEmptyCasesCond as $i => $collectionEmptyCaseCond) {
if ($i > $key) {
break;
}

$emptyCasesCond[$i] = $collectionEmptyCaseCond;
unset($collectionEmptyCasesCond[$i]);
}

$condExprs = $emptyCasesCond;
$condExprs[] = $case->cond;
}

Expand Down

0 comments on commit 5157414

Please sign in to comment.