diff --git a/Travelopia-WordPress/Sniffs/PHP/PreferBreakInsideSwitchOnlySniff.php b/Travelopia-WordPress/Sniffs/PHP/PreferBreakInsideSwitchOnlySniff.php new file mode 100644 index 0000000..20f1fec --- /dev/null +++ b/Travelopia-WordPress/Sniffs/PHP/PreferBreakInsideSwitchOnlySniff.php @@ -0,0 +1,72 @@ +getTokens(); + + // Set Start and End pointers. + $searchStartPtr = $stackPtr + 1; + $caseEndPtr = $tokens[ $stackPtr ]['scope_closer']; + + // Search for continue inside the case. + do { + // Find the next continue. + $continuePtr = $phpcsFile->findNext( T_CONTINUE, $searchStartPtr, $caseEndPtr + 1 ); + + // If continue is inside a loop, ignore it. + if ( false !== $continuePtr ) { + // Search for the loop in which the continue is present. + $loopStartPtr = $phpcsFile->findPrevious( [ T_WHILE, T_FOR, T_FOREACH, T_DO ], $continuePtr, $stackPtr ); + $loopEndPtr = $tokens[ $loopStartPtr ]['scope_closer']; + + // If the continue is inside a loop, ignore it. + if ( $loopStartPtr && $continuePtr > $loopStartPtr && $continuePtr < $loopEndPtr ) { + $searchStartPtr = $loopEndPtr + 1; + continue; + } + + // Display warning. + $phpcsFile->addWarningOnLine( + 'Use `break` or `continue x` where x is the nesting level to break out of the switch.', + $tokens[ $continuePtr ]['line'], + 'UseBreakInsideSwitch' + ); + + // Set the search pointer to the next token. + $searchStartPtr = $continuePtr + 1; + } + } while ( false !== $continuePtr ); + } +}