Skip to content

Commit

Permalink
Add metrics to the PSR2.ControlStructures.SwitchDeclaration sniff
Browse files Browse the repository at this point in the history
I've explicitly not added metrics for:
* lowercase keyword as those are already recorded by the `Generic.PHP.LowerCaseKeyword` sniff
* `break`/`return` not on a line by itself as that is already recorded by the `Generic.Formatting.DisallowMultipleStatements` sniff
  • Loading branch information
jrfnl committed Dec 26, 2023
1 parent 6be2da7 commit 0fdb253
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public function process(File $phpcsFile, $stackPtr)
&& ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE
|| $tokens[($nextCase + 1)]['content'] !== ' ')
) {
if ($tokens[($nextCase + 1)]['code'] === T_WHITESPACE) {
$phpcsFile->recordMetric($nextCase, 'Spaces following the "case" keyword', $tokens[($nextCase + 1)]['length']);
} else {
$phpcsFile->recordMetric($nextCase, 'Spaces following the "case" keyword', 'none');
}

$error = 'CASE keyword must be followed by a single space';
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase');
if ($fix === true) {
Expand All @@ -94,17 +100,24 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->replaceToken(($nextCase + 1), ' ');
}
}
} else if ($type === 'case') {
$phpcsFile->recordMetric($nextCase, 'Spaces following the "case" keyword', 1);
}

$opener = $tokens[$nextCase]['scope_opener'];
$nextCloser = $tokens[$nextCase]['scope_closer'];
if ($tokens[$opener]['code'] === T_COLON) {
$phpcsFile->recordMetric($opener, 'Default/case statement followed by colon', 'yes');

if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) {
$phpcsFile->recordMetric($opener, 'Spaces before colon after case/default keyword', $tokens[($opener - 1)]['length']);
$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), '');
}
} else {
$phpcsFile->recordMetric($opener, 'Spaces before colon after case/default keyword', 0);
}

for ($next = ($opener + 1); $next < $nextCloser; $next++) {
Expand All @@ -116,6 +129,8 @@ public function process(File $phpcsFile, $stackPtr)
}
}

$phpcsFile->recordMetric($opener, 'Blank lines at start of case/default body', ($tokens[$next]['line'] - ($tokens[$opener]['line'] + 1)));

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));
Expand Down Expand Up @@ -158,6 +173,7 @@ public function process(File $phpcsFile, $stackPtr)
} else {
$diff = ($tokens[$nextCase]['column'] + $this->indent - $tokens[$nextCloser]['column']);
if ($diff !== 0) {
$phpcsFile->recordMetric($nextCloser, 'Terminating statement aligned with body of case', 'no');
$error = 'Terminating statement must be indented to the same level as the CASE body';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');
if ($fix === true) {
Expand All @@ -167,10 +183,13 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff);
}
}
} else {
$phpcsFile->recordMetric($nextCloser, 'Terminating statement aligned with body of case', 'yes');
}
}//end if
}//end if
} else {
$phpcsFile->recordMetric($opener, 'Default/case statement followed by colon', 'no');
$error = strtoupper($type).' statements must be defined using a colon';
$phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type);
}//end if
Expand All @@ -193,11 +212,16 @@ public function process(File $phpcsFile, $stackPtr)
if (isset(Tokens::$commentTokens[$tokens[$prevCode]['code']]) === false
&& $this->findNestedTerminator($phpcsFile, ($opener + 1), $nextCode) === false
) {
$phpcsFile->recordMetric($nextCode, 'Case falls through to next', 'yes, without comment');
$error = 'There must be a comment when fall-through is intentional in a non-empty case body';
$phpcsFile->addError($error, $nextCase, 'TerminatingComment');
} else {
$phpcsFile->recordMetric($nextCode, 'Case falls through to next', 'yes, with comment');
}
} else {
$phpcsFile->recordMetric($nextCode, 'Case falls through to next', 'no');
}
}
}//end if
}//end while

}//end process()
Expand Down

0 comments on commit 0fdb253

Please sign in to comment.