function SwitchDeclarationSniff::process
Same name in this branch
- 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\SwitchDeclarationSniff::process()
Processes this test, when one of its tokens is encountered.
Parameters
\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:
int $stackPtr The position of the current token in the: stack passed in $tokens.
Return value
void
Overrides Sniff::process
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ PSR2/ Sniffs/ ControlStructures/ SwitchDeclarationSniff.php, line 48
Class
Namespace
PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructuresCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
// We can't process SWITCH statements unless we know where they start and end.
if (isset($tokens[$stackPtr]['scope_opener']) === false || isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
$switch = $tokens[$stackPtr];
$nextCase = $stackPtr;
$caseAlignment = $switch['column'] + $this->indent;
while (($nextCase = $this->findNextCase($phpcsFile, $nextCase + 1, $switch['scope_closer'])) !== false) {
if ($tokens[$nextCase]['code'] === T_DEFAULT) {
$type = 'default';
}
else {
$type = 'case';
}
if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) {
$expected = strtolower($tokens[$nextCase]['content']);
$error = strtoupper($type) . ' keyword must be lowercase; expected "%s" but found "%s"';
$data = [
$expected,
$tokens[$nextCase]['content'],
];
$fix = $phpcsFile->addFixableError($error, $nextCase, $type . 'NotLower', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($nextCase, $expected);
}
}
if ($type === 'case' && ($tokens[$nextCase + 1]['code'] !== T_WHITESPACE || $tokens[$nextCase + 1]['content'] !== ' ')) {
$error = 'CASE keyword must be followed by a single space';
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase');
if ($fix === true) {
if ($tokens[$nextCase + 1]['code'] !== T_WHITESPACE) {
$phpcsFile->fixer
->addContent($nextCase, ' ');
}
else {
$phpcsFile->fixer
->replaceToken($nextCase + 1, ' ');
}
}
}
$opener = $tokens[$nextCase]['scope_opener'];
$nextCloser = $tokens[$nextCase]['scope_closer'];
if ($tokens[$opener]['code'] === T_COLON) {
if ($tokens[$opener - 1]['code'] === T_WHITESPACE) {
$error = 'There must be no space before the colon in a ' . strtoupper($type) . ' statement';
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon' . strtoupper($type));
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($opener - 1, '');
}
}
for ($next = $opener + 1; $next < $nextCloser; $next++) {
if (isset(Tokens::$emptyTokens[$tokens[$next]['code']]) === false || isset(Tokens::$commentTokens[$tokens[$next]['code']]) === true && $tokens[$next]['line'] !== $tokens[$opener]['line']) {
break;
}
}
if ($tokens[$next]['line'] !== $tokens[$opener]['line'] + 1) {
$error = 'The ' . strtoupper($type) . ' body must start on the line following the statement';
$fix = $phpcsFile->addFixableError($error, $nextCase, 'BodyOnNextLine' . strtoupper($type));
if ($fix === true) {
if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
$padding = str_repeat(' ', $caseAlignment + $this->indent - 1);
$phpcsFile->fixer
->addContentBefore($next, $phpcsFile->eolChar . $padding);
}
else {
$phpcsFile->fixer
->beginChangeset();
for ($i = $opener + 1; $i < $next; $i++) {
if ($tokens[$i]['line'] === $tokens[$opener]['line']) {
// Ignore trailing comments.
continue;
}
if ($tokens[$i]['line'] === $tokens[$next]['line']) {
break;
}
$phpcsFile->fixer
->replaceToken($i, '');
}
$phpcsFile->fixer
->endChangeset();
}
}
//end if
}
//end if
if ($tokens[$nextCloser]['scope_condition'] === $nextCase) {
// Only need to check some things once, even if the
// closer is shared between multiple case statements, or even
// the default case.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $nextCloser - 1, $nextCase, true);
if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) {
$error = 'Terminating statement must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine');
if ($fix === true) {
$phpcsFile->fixer
->addNewLine($prev);
$phpcsFile->fixer
->replaceToken($nextCloser, trim($tokens[$nextCloser]['content']));
}
}
else {
$diff = $tokens[$nextCase]['column'] + $this->indent - $tokens[$nextCloser]['column'];
if ($diff !== 0) {
$error = 'Terminating statement must be indented to the same level as the CASE body';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');
if ($fix === true) {
if ($diff > 0) {
$phpcsFile->fixer
->addContentBefore($nextCloser, str_repeat(' ', $diff));
}
else {
$phpcsFile->fixer
->substrToken($nextCloser - 1, 0, $diff);
}
}
}
}
//end if
}
//end if
}
else {
$error = strtoupper($type) . ' statements must be defined using a colon';
$phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type);
}
//end if
// We only want cases from here on in.
if ($type !== 'case') {
continue;
}
$nextCode = $phpcsFile->findNext(T_WHITESPACE, $opener + 1, $nextCloser, true);
if ($tokens[$nextCode]['code'] !== T_CASE && $tokens[$nextCode]['code'] !== T_DEFAULT) {
// This case statement has content. If the next case or default comes
// before the closer, it means we don't have an obvious terminating
// statement and need to make some more effort to find one. If we
// don't, we do need a comment.
$nextCode = $this->findNextCase($phpcsFile, $opener + 1, $nextCloser);
if ($nextCode !== false) {
$prevCode = $phpcsFile->findPrevious(T_WHITESPACE, $nextCode - 1, $nextCase, true);
if (isset(Tokens::$commentTokens[$tokens[$prevCode]['code']]) === false && $this->findNestedTerminator($phpcsFile, $opener + 1, $nextCode) === false) {
$error = 'There must be a comment when fall-through is intentional in a non-empty case body';
$phpcsFile->addError($error, $nextCase, 'TerminatingComment');
}
}
}
}
//end while
}