Skip to content

Commit

Permalink
Squiz/NonExecutableCode: fix bug with alternative switch control stru…
Browse files Browse the repository at this point in the history
…ctures

The `T_ENDSWITCH` token wasn't being taken into account as a potential end token.

Includes unit tests.

Fixes 2512
  • Loading branch information
jrfnl committed May 23, 2019
1 parent 8b713e1 commit 19346c5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function process(File $phpcsFile, $stackPtr)
T_CASE,
T_DEFAULT,
T_CLOSE_CURLY_BRACKET,
T_ENDSWITCH,
],
($end + 1)
);
Expand Down
39 changes: 39 additions & 0 deletions src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,42 @@ switch ($var) {

end:
echo 'j hit 17';

// Issue 2512.
class TestAlternativeControlStructures {

public function alternative_switch_in_function( $var ) {

switch ( $var ) :
case 'value1':
do_something();
break;

default:
case 'value2':
do_something_else();
break;
endswitch;
}

public function various_alternative_control_structures() {
$_while = 1;

for ( $a = 0; $a++ < 1; ) :
foreach ( [ 1 ] as $b ) :
while ( $_while-- ) :
if ( 1 ) :
switch ( 1 ) :
default:
echo 'yay, we made it!';
break;
endswitch;
endif;
endwhile;
endforeach;
endfor;
}
}

$var_after_class_in_global_space = 1;
do_something_else();

0 comments on commit 19346c5

Please sign in to comment.