mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 11:50:51 +00:00
[Php80] Skip empty default on ChangeSwitchToMatchRector (#1980)
This commit is contained in:
parent
a375ee2641
commit
eba904f406
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;
|
||||
|
||||
class SkipEmptyDefault
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
switch ($value) {
|
||||
case 'a':
|
||||
$result = 'A';
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,7 +40,7 @@ final class MatchSwitchAnalyzer
|
|||
return false;
|
||||
}
|
||||
|
||||
if ($this->switchAnalyzer->hasDefault($switch)) {
|
||||
if ($this->switchAnalyzer->hasDefaultSingleStmt($switch)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,10 @@ final class SwitchAnalyzer
|
|||
public function hasEachCaseSingleStmt(Switch_ $switch): bool
|
||||
{
|
||||
foreach ($switch->cases as $case) {
|
||||
if ($case->cond === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$stmtsWithoutBreak = array_filter($case->stmts, fn (Node $node): bool => ! $node instanceof Break_);
|
||||
|
||||
if (count($stmtsWithoutBreak) !== 1) {
|
||||
|
@ -47,11 +51,12 @@ final class SwitchAnalyzer
|
|||
return true;
|
||||
}
|
||||
|
||||
public function hasDefault(Switch_ $switch): bool
|
||||
public function hasDefaultSingleStmt(Switch_ $switch): bool
|
||||
{
|
||||
foreach ($switch->cases as $case) {
|
||||
if ($case->cond === null) {
|
||||
return true;
|
||||
$stmtsWithoutBreak = array_filter($case->stmts, fn (Node $node): bool => ! $node instanceof Break_);
|
||||
return count($stmtsWithoutBreak) === 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user