Skip to content

Commit 19346c5

Browse files
committed
Squiz/NonExecutableCode: fix bug with alternative switch control structures
The `T_ENDSWITCH` token wasn't being taken into account as a potential end token. Includes unit tests. Fixes 2512
1 parent 8b713e1 commit 19346c5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function process(File $phpcsFile, $stackPtr)
105105
T_CASE,
106106
T_DEFAULT,
107107
T_CLOSE_CURLY_BRACKET,
108+
T_ENDSWITCH,
108109
],
109110
($end + 1)
110111
);

src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.1.inc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,42 @@ switch ($var) {
257257

258258
end:
259259
echo 'j hit 17';
260+
261+
// Issue 2512.
262+
class TestAlternativeControlStructures {
263+
264+
public function alternative_switch_in_function( $var ) {
265+
266+
switch ( $var ) :
267+
case 'value1':
268+
do_something();
269+
break;
270+
271+
default:
272+
case 'value2':
273+
do_something_else();
274+
break;
275+
endswitch;
276+
}
277+
278+
public function various_alternative_control_structures() {
279+
$_while = 1;
280+
281+
for ( $a = 0; $a++ < 1; ) :
282+
foreach ( [ 1 ] as $b ) :
283+
while ( $_while-- ) :
284+
if ( 1 ) :
285+
switch ( 1 ) :
286+
default:
287+
echo 'yay, we made it!';
288+
break;
289+
endswitch;
290+
endif;
291+
endwhile;
292+
endforeach;
293+
endfor;
294+
}
295+
}
296+
297+
$var_after_class_in_global_space = 1;
298+
do_something_else();

0 commit comments

Comments
 (0)