Skip to content

Commit

Permalink
[Php80] Handle crash on multi next stmts on ChangeSwitchToMatchRector (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Dec 16, 2024
1 parent 8017dac commit 654d5a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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

final class MultiNextStmt
{
public function run($value)
{
switch ($value) {
case 'number':
case 'percentage':
case 'integer':
case 'price':
case 'decimal':
$sort = SORT_NUMERIC;
break;
case 'string':
case 'list':
case 'text':
$sort = SORT_NATURAL | SORT_FLAG_CASE;
break;
default:
$sort = SORT_REGULAR;
}
}
}

?>
-----
<?php

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

final class MultiNextStmt
{
public function run($value)
{
$sort = match ($value) {
'number', 'percentage', 'integer', 'price', 'decimal' => SORT_NUMERIC,
'string', 'list', 'text' => SORT_NATURAL | SORT_FLAG_CASE,
default => SORT_REGULAR,
};
}
}

?>
6 changes: 6 additions & 0 deletions rules/Php80/NodeFactory/MatchArmsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function createFromCondAndExprs(array $condAndExprs): array
]);
}

foreach ($matchArms as $matchArm) {
if (is_array($matchArm->conds)) {
$matchArm->conds = array_values($matchArm->conds);
}
}

return $matchArms;
}
}

0 comments on commit 654d5a2

Please sign in to comment.