[Php80] Skip empty default on ChangeSwitchToMatchRector (#1980)

This commit is contained in:
Abdul Malik Ikhsan 2022-03-31 17:57:15 +07:00 committed by GitHub
parent a375ee2641
commit eba904f406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -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:
}
}
}

View File

@ -40,7 +40,7 @@ final class MatchSwitchAnalyzer
return false;
}
if ($this->switchAnalyzer->hasDefault($switch)) {
if ($this->switchAnalyzer->hasDefaultSingleStmt($switch)) {
return false;
}

View File

@ -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;
}
}