Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Handle repetitive jump equal case stmts on RemoveDuplicatedCaseInSwitchRector #5239

Merged
merged 5 commits into from
Nov 10, 2023
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
@@ -0,0 +1,59 @@
<?php

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

class DiffrentIndirectDuplicated3
{
public function run($name)
{
switch ($name) {
case 'a':
return 'A';
case 'b':
return 'B';
case 'c';
return 'C';
case 'd':
return 'A';
case 'e':
return 'B';
case 'f':
return 'C';
case 'g':
return 'A';
case 'h':
return 'B';
case 'i':
return 'C';
}
}
}

?>
-----
<?php

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

class DiffrentIndirectDuplicated3
{
public function run($name)
{
switch ($name) {
case 'a':
case 'd':
case 'g':
return 'A';
case 'b':
case 'e':
case 'h':
return 'B';
case 'c':
case 'f':
case 'i':
return 'C';
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -86,72 +86,48 @@ public function refactor(Node $node): ?Node

$this->hasChanged = false;

$insertByKeys = $this->resolveInsertedByKeys($node);
$this->insertCaseByKeys($node, $insertByKeys);

$this->removeDuplicatedCases($node);
if (! $this->hasChanged) {
return null;
}

return $node;
}

/**
* @return array<int, array<int, Case_>>
*/
private function resolveInsertedByKeys(Switch_ $switch): array
private function removeDuplicatedCases(Switch_ $switch): void
{
$totalKeys = count($switch->cases);
$insertByKeys = [];
$appendKey = 0;
/** @var Case_|null $previousCase */
$previousCase = null;

foreach ($switch->cases as $key => $case) {
if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
$previousCase->stmts = [];
$this->hasChanged = true;
}

$previousCase = $case;

for ($jumpToKey = $key + 2; $jumpToKey < $totalKeys; ++$jumpToKey) {
foreach (array_keys($switch->cases) as $key) {
$nextCases = [];
for ($jumpToKey = $key + 1; $jumpToKey < $totalKeys; ++$jumpToKey) {
if (! isset($switch->cases[$jumpToKey])) {
continue;
}

if (! $this->areSwitchStmtsEqualsAndWithBreak($case, $switch->cases[$jumpToKey])) {
if (! $this->areSwitchStmtsEqualsAndWithBreak($switch->cases[$key], $switch->cases[$jumpToKey])) {
continue;
}

$nextCase = $switch->cases[$jumpToKey];

unset($switch->cases[$jumpToKey]);

$insertByKeys[$key + $appendKey][] = $nextCase;
$nextCases[] = $nextCase;

$this->hasChanged = true;
}

$appendKey = isset($insertByKeys[$key]) ? count($insertByKeys[$key]) : 0;
}

return $insertByKeys;
}

/**
* @param array<int, array<int, Case_>> $insertByKeys
*/
private function insertCaseByKeys(Switch_ $switch, array $insertByKeys): void
{
foreach ($insertByKeys as $key => $insertByKey) {
$nextKey = $key + 1;
if ($nextCases === []) {
continue;
}

array_splice($switch->cases, $nextKey, 0, $insertByKey);
array_splice($switch->cases, $key + 1, 0, $nextCases);

$switch->cases[$key]->stmts = [];
for ($jumpToKey = $key; $jumpToKey < $key + count($nextCases); ++$jumpToKey) {
$switch->cases[$jumpToKey]->stmts = [];
}

$this->hasChanged = true;
$key += count($nextCases);
}
}

Expand Down
4 changes: 4 additions & 0 deletions utils/Command/MissingInSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveSetMethodsMethodCallRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenCollectorRector;
use Rector\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector;
Expand Down Expand Up @@ -38,6 +39,9 @@ final class MissingInSetCommand extends Command
TypedPropertyFromStrictConstructorReadonlyClassRector::class,
BinarySwitchToIfElseRector::class,
CountOnNullRector::class,

// namespace to be moved from CodeQuality to PHPUnit100
RemoveSetMethodsMethodCallRector::class,
Comment on lines +42 to +44
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba this is added here temporary due to rector-phpunit PR for move RemoveSetMethodsMethodCallRector from phpunit code-quality to phpunit 100

to avoid notice:

bin/rector missing-in-set --ansi

We could not find there rules in configs
========================================

 * vendor/rector/rector-phpunit/config/sets/phpunit-code-quality.php

 * Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveSetMethodsMethodCallRector

];

public function __construct(
Expand Down