Skip to content

Add metrics to the PSR2.ControlStructures.SwitchDeclaration sniff #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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