Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Rector\DeadCode\Rector\Switch_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -80,7 +82,7 @@ public function refactor(Node $node): ?Node
/** @var Case_|null $previousCase */
$previousCase = null;
foreach ($node->cases as $case) {
if ($previousCase && $this->areNodesEqual($case->stmts, $previousCase->stmts)) {
if ($previousCase && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
$previousCase->stmts = [];
}

Expand All @@ -89,4 +91,23 @@ public function refactor(Node $node): ?Node

return $node;
}

private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $previousCase): bool
{
if (! $this->areNodesEqual($currentCase->stmts, $previousCase->stmts)) {
return false;
}

foreach ($currentCase->stmts as $stmt) {
if ($stmt instanceof Break_) {
return true;
}

if ($stmt instanceof Return_) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\DeadCode\Tests\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector\Fixture;

class SkipNoBreak
{
public function run($max)
{
$letter = 't';

switch ($letter) {
case 't':
$max *= 1024;
// no break
case 'g':
$max *= 1024;
// no break
}

return $max;
}
}