@@ -106,14 +106,14 @@ private function removeDuplicatedCases(Switch_ $switch): void
106106 $ result = [];
107107
108108 /** @var int[] */
109- $ processedCasesNumbers = [];
109+ $ processedCasesKeys = [];
110110
111111 foreach ($ switch ->cases as $ outerCaseKey => $ outerCase ) {
112- if (in_array ($ outerCaseKey , $ processedCasesNumbers )) {
112+ if (in_array ($ outerCaseKey , $ processedCasesKeys )) {
113113 continue ;
114114 }
115115
116- $ processedCasesNumbers [] = $ outerCaseKey ;
116+ $ processedCasesKeys [] = $ outerCaseKey ;
117117
118118 if ($ outerCase ->stmts === []) {
119119 $ result [] = $ outerCase ;
@@ -127,7 +127,7 @@ private function removeDuplicatedCases(Switch_ $switch): void
127127 $ equalCases = [];
128128
129129 foreach ($ switch ->cases as $ innerCaseKey => $ innerCase ) {
130- if (in_array ($ innerCaseKey , $ processedCasesNumbers )) {
130+ if (in_array ($ innerCaseKey , $ processedCasesKeys )) {
131131 continue ;
132132 }
133133
@@ -137,30 +137,26 @@ private function removeDuplicatedCases(Switch_ $switch): void
137137 }
138138
139139 if ($ this ->areSwitchStmtsEqualsAndWithBreak ($ outerCase , $ innerCase )) {
140- if ($ casesWithoutStmts !== []) {
141- foreach ($ casesWithoutStmts as $ caseWithoutStmtsKey => $ caseWithoutStmts ) {
142- $ equalCases [] = $ caseWithoutStmts ;
143- $ processedCasesNumbers [] = $ caseWithoutStmtsKey ;
144- }
145-
146- $ casesWithoutStmts = [];
140+ foreach ($ casesWithoutStmts as $ caseWithoutStmtsKey => $ caseWithoutStmts ) {
141+ $ equalCases [] = $ caseWithoutStmts ;
142+ $ processedCasesKeys [] = $ caseWithoutStmtsKey ;
147143 }
148144
149145 $ innerCase ->stmts = [];
150146 $ equalCases [] = $ innerCase ;
151- $ processedCasesNumbers [] = $ innerCaseKey ;
152-
153- $ this ->hasChanged = true ;
154- } else {
155- $ casesWithoutStmts = [];
147+ $ processedCasesKeys [] = $ innerCaseKey ;
156148 }
149+
150+ $ casesWithoutStmts = [];
157151 }
158152
159153 if ($ equalCases === []) {
160154 $ result [] = $ outerCase ;
161155 continue ;
162156 }
163157
158+ $ this ->hasChanged = true ;
159+
164160 $ equalCases [array_key_last ($ equalCases )]->stmts = $ outerCase ->stmts ;
165161 $ outerCase ->stmts = [];
166162
@@ -185,11 +181,7 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex
185181 }
186182
187183 foreach ($ currentCase ->stmts as $ stmt ) {
188- if ($ stmt instanceof Break_) {
189- return true ;
190- }
191-
192- if ($ stmt instanceof Return_) {
184+ if ($ stmt instanceof Break_ || $ stmt instanceof Return_) {
193185 return true ;
194186 }
195187 }
@@ -199,6 +191,9 @@ private function areSwitchStmtsEqualsAndWithBreak(Case_ $currentCase, Case_ $nex
199191
200192 private function areSwitchStmtsEqualsConsideringComments (Case_ $ currentCase , Case_ $ nextCase ): bool
201193 {
202- return $ this ->betterStandardPrinter ->print ($ currentCase ->stmts ) === $ this ->betterStandardPrinter ->print ($ nextCase ->stmts );
194+ $ currentCasePrintResult = $ this ->betterStandardPrinter ->print ($ currentCase ->stmts );
195+ $ nextCasePrintResult = $ this ->betterStandardPrinter ->print ($ nextCase ->stmts );
196+
197+ return $ currentCasePrintResult === $ nextCasePrintResult ;
203198 }
204199}
0 commit comments