From 191fd4c751293d33944c030d6ffdd23107fdbc9d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 20:33:51 +0700 Subject: [PATCH 1/5] [DeadCode] Handle repetitive jump equal case stmts on RemoveDuplicatedCaseInSwitchRector --- .../different_indirect_duplicated3.php.inc | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc new file mode 100644 index 00000000000..756769d47bc --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated3.php.inc @@ -0,0 +1,59 @@ + +----- + \ No newline at end of file From 8046f963795a7dc8c6f870b3eff7ccd3a45ea795 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Nov 2023 01:53:58 +0700 Subject: [PATCH 2/5] Fixed :tada: --- .../RemoveDuplicatedCaseInSwitchRector.php | 53 ++++++------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index b71216a8f3f..9ab8db6dfee 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -86,9 +86,7 @@ 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; } @@ -99,28 +97,17 @@ public function refactor(Node $node): ?Node /** * @return array> */ - 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) { + $totalKeys = count($switch->cases); + $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; } @@ -128,30 +115,22 @@ private function resolveInsertedByKeys(Switch_ $switch): array 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> $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); } } From 6b4ca2190ea465015ca3dfd064715e7e020247d2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Nov 2023 01:59:16 +0700 Subject: [PATCH 3/5] fix missing-in-set config --- utils/Command/MissingInSetCommand.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utils/Command/MissingInSetCommand.php b/utils/Command/MissingInSetCommand.php index 623fedfe0d8..f10c42927a7 100644 --- a/utils/Command/MissingInSetCommand.php +++ b/utils/Command/MissingInSetCommand.php @@ -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; @@ -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, ]; public function __construct( From 9a1214499af5fb4f72a39678fcfef12fe75ab200 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Nov 2023 02:04:25 +0700 Subject: [PATCH 4/5] fix @return --- .../Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index 9ab8db6dfee..d0cd5c6e19c 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -94,9 +94,6 @@ public function refactor(Node $node): ?Node return $node; } - /** - * @return array> - */ private function removeDuplicatedCases(Switch_ $switch): void { foreach (array_keys($switch->cases) as $key) { From 4b02d3f1b3cf5af7d791ee88de2dcb9116e10684 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Nov 2023 02:04:57 +0700 Subject: [PATCH 5/5] clean up --- .../Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index d0cd5c6e19c..9b45274b8c4 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -96,8 +96,8 @@ public function refactor(Node $node): ?Node private function removeDuplicatedCases(Switch_ $switch): void { + $totalKeys = count($switch->cases); foreach (array_keys($switch->cases) as $key) { - $totalKeys = count($switch->cases); $nextCases = []; for ($jumpToKey = $key + 1; $jumpToKey < $totalKeys; ++$jumpToKey) { if (! isset($switch->cases[$jumpToKey])) {