Skip to content

Commit f819492

Browse files
committed
NoBreakCommentFixer - fix throw detect
1 parent 2473c7b commit f819492

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/Fixer/ControlStructure/NoBreakCommentFixer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ private function fixCase(Tokens $tokens, $casePosition)
131131
$empty = true;
132132
$fallThrough = true;
133133
$commentPosition = null;
134+
$caseColonIndex = $tokens->getNextTokenOfKind($casePosition, [':', ';']);
134135

135-
for ($i = $tokens->getNextTokenOfKind($casePosition, [':', ';']) + 1, $max = \count($tokens); $i < $max; ++$i) {
136+
for ($i = $caseColonIndex + 1, $max = \count($tokens); $i < $max; ++$i) {
136137
if ($tokens[$i]->isGivenKind([T_SWITCH, T_IF, T_ELSE, T_ELSEIF, T_FOR, T_FOREACH, T_WHILE, T_DO, T_FUNCTION, T_CLASS])) {
137138
$empty = false;
138139
$i = $this->getStructureEnd($tokens, $i);
@@ -149,7 +150,7 @@ private function fixCase(Tokens $tokens, $casePosition)
149150
if ($tokens[$i]->isGivenKind([T_THROW])) {
150151
$previousIndex = $tokens->getPrevMeaningfulToken($i);
151152

152-
if ($previousIndex === $casePosition || $tokens[$previousIndex]->equalsAny(['{', ';', [T_OPEN_TAG]])) {
153+
if ($previousIndex === $caseColonIndex || $tokens[$previousIndex]->equalsAny(['{', ';', [T_OPEN_TAG]])) {
153154
$fallThrough = false;
154155
}
155156

tests/Fixer/ControlStructure/NoBreakCommentFixerTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,24 @@ public function provideTestFixCases()
858858
bar();
859859
}',
860860
],
861+
[
862+
'<?php
863+
switch ($a) {
864+
case 1:
865+
throw new \Exception("");
866+
case 2;
867+
throw new \Exception("");
868+
case 3:
869+
throw new \Exception("");
870+
case 4;
871+
throw new \Exception("");
872+
case 5:
873+
throw new \Exception("");
874+
case 6;
875+
throw new \Exception("");
876+
}
877+
',
878+
],
861879
];
862880
}
863881

@@ -938,6 +956,39 @@ public function foo($bar)
938956
bar();
939957
}',
940958
],
959+
[
960+
'<?php
961+
switch($a) {
962+
case 1:
963+
$a = function () { throw new \Exception(""); };
964+
// no break
965+
case 2:
966+
$a = new class(){
967+
public function foo () { throw new \Exception(""); }
968+
};
969+
// no break
970+
case 3:
971+
echo 5;
972+
// no break
973+
default:
974+
echo 1;
975+
}
976+
',
977+
'<?php
978+
switch($a) {
979+
case 1:
980+
$a = function () { throw new \Exception(""); };
981+
case 2:
982+
$a = new class(){
983+
public function foo () { throw new \Exception(""); }
984+
};
985+
case 3:
986+
echo 5;
987+
default:
988+
echo 1;
989+
}
990+
',
991+
],
941992
];
942993
}
943994

0 commit comments

Comments
 (0)