From 4f92eef842a0e22449d9961c8ab1787d01d83140 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 12:13:10 +0700 Subject: [PATCH 1/6] [DeadCode] Handle different indirect duplicated on RemoveDuplicatedCaseInSwitchRector --- .../different_indirect_duplicated.php.inc | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc new file mode 100644 index 00000000000..ff07ba331ab --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated.php.inc @@ -0,0 +1,47 @@ + +----- + \ No newline at end of file From 64a57a77826a9067ba7e4346f22dd761af9ec1f1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 13:04:37 +0700 Subject: [PATCH 2/6] Fixed :tada: --- .../Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index 4658772ce96..c8c8a73dbc0 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -103,6 +103,7 @@ private function resolveInsertedByKeys(Switch_ $switch): array { $totalKeys = count($switch->cases); $insertByKeys = []; + $appendKey = 0; foreach ($switch->cases as $key => $case) { if ($case->stmts === []) { @@ -122,10 +123,12 @@ private function resolveInsertedByKeys(Switch_ $switch): array unset($switch->cases[$jumpToKey]); - $insertByKeys[$key][] = $nextCase; + $insertByKeys[$key + $appendKey][] = $nextCase; $this->hasChanged = true; } + + $appendKey = count($insertByKeys[$key] ?? []) ?? 0; } return $insertByKeys; From da1e766d7b75aa178d837654960e8e5e152ef990 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 13:08:51 +0700 Subject: [PATCH 3/6] add more fixture --- .../different_indirect_duplicated2.php.inc | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc new file mode 100644 index 00000000000..aa814e625ba --- /dev/null +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc @@ -0,0 +1,50 @@ + +----- + \ No newline at end of file From 4d1e14fd3f60ec84bf3e6e233c5d82620c9fb262 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 13:20:20 +0700 Subject: [PATCH 4/6] more fixture --- .../Switch_/RemoveDuplicatedCaseInSwitchRector.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index c8c8a73dbc0..80cb64a857c 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -143,10 +143,16 @@ private function insertCaseByKeys(Switch_ $switch, array $insertByKeys): void $nextKey = $key + 1; array_splice($switch->cases, $nextKey, 0, $insertByKey); + } - for ($jumpToKey = $key; $jumpToKey < $key + count($insertByKey); ++$jumpToKey) { - $switch->cases[$jumpToKey]->stmts = []; + /** @var Case_|null $previousCase */ + $previousCase = null; + foreach ($switch->cases as $case) { + if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) { + $previousCase->stmts = []; } + + $previousCase = $case; } } From af6cf1fbecda26c261183f5730adc1e754858606 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 13:21:14 +0700 Subject: [PATCH 5/6] phpstan --- .../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 80cb64a857c..11967e0a947 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -128,7 +128,7 @@ private function resolveInsertedByKeys(Switch_ $switch): array $this->hasChanged = true; } - $appendKey = count($insertByKeys[$key] ?? []) ?? 0; + $appendKey = isset($insertByKeys[$key]) ? count($insertByKeys[$key]) : 0; } return $insertByKeys; From f2e1650651eddd34220b4c9046025a7d6e1abb4b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 10 Nov 2023 13:54:25 +0700 Subject: [PATCH 6/6] fixed --- .../Fixture/different_indirect_duplicated2.php.inc | 2 +- .../Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc index aa814e625ba..e636eab2eb3 100644 --- a/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc +++ b/rules-tests/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture/different_indirect_duplicated2.php.inc @@ -40,7 +40,7 @@ class DiffrentIndirectDuplicated2 case 'b': case 'e': return 'B'; - case 'c'; + case 'c': case 'f': return 'C'; } diff --git a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php index 11967e0a947..78ec5a2fe40 100644 --- a/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php +++ b/rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php @@ -110,7 +110,7 @@ private function resolveInsertedByKeys(Switch_ $switch): array continue; } - for ($jumpToKey = $key + 1; $jumpToKey < $totalKeys; ++$jumpToKey) { + for ($jumpToKey = $key + 2; $jumpToKey < $totalKeys; ++$jumpToKey) { if (! isset($switch->cases[$jumpToKey])) { continue; }