Skip to content

Commit 9103d79

Browse files
committed
Add metrics to the PSR2.ControlStructures.SwitchDeclaration sniff
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
1 parent f83010f commit 9103d79

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public function process(File $phpcsFile, $stackPtr)
8585
&& ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE
8686
|| $tokens[($nextCase + 1)]['content'] !== ' ')
8787
) {
88+
if ($tokens[($nextCase + 1)]['code'] === T_WHITESPACE) {
89+
$phpcsFile->recordMetric($nextCase, 'Spaces following the "case" keyword', $tokens[($nextCase + 1)]['length']);
90+
} else {
91+
$phpcsFile->recordMetric($nextCase, 'Spaces following the "case" keyword', 'none');
92+
}
93+
8894
$error = 'CASE keyword must be followed by a single space';
8995
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase');
9096
if ($fix === true) {
@@ -94,17 +100,24 @@ public function process(File $phpcsFile, $stackPtr)
94100
$phpcsFile->fixer->replaceToken(($nextCase + 1), ' ');
95101
}
96102
}
103+
} else if ($type === 'case') {
104+
$phpcsFile->recordMetric($nextCase, 'Spaces following the "case" keyword', 1);
97105
}
98106

99107
$opener = $tokens[$nextCase]['scope_opener'];
100108
$nextCloser = $tokens[$nextCase]['scope_closer'];
101109
if ($tokens[$opener]['code'] === T_COLON) {
110+
$phpcsFile->recordMetric($opener, 'Default/case statement followed by colon', 'yes');
111+
102112
if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) {
113+
$phpcsFile->recordMetric($opener, 'Spaces before colon after case/default keyword', $tokens[($opener - 1)]['length']);
103114
$error = 'There must be no space before the colon in a '.strtoupper($type).' statement';
104115
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.strtoupper($type));
105116
if ($fix === true) {
106117
$phpcsFile->fixer->replaceToken(($opener - 1), '');
107118
}
119+
} else {
120+
$phpcsFile->recordMetric($opener, 'Spaces before colon after case/default keyword', 0);
108121
}
109122

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

132+
$phpcsFile->recordMetric($opener, 'Blank lines at start of case/default body', ($tokens[$next]['line'] - ($tokens[$opener]['line'] + 1)));
133+
119134
if ($tokens[$next]['line'] !== ($tokens[$opener]['line'] + 1)) {
120135
$error = 'The '.strtoupper($type).' body must start on the line following the statement';
121136
$fix = $phpcsFile->addFixableError($error, $nextCase, 'BodyOnNextLine'.strtoupper($type));
@@ -158,6 +173,7 @@ public function process(File $phpcsFile, $stackPtr)
158173
} else {
159174
$diff = ($tokens[$nextCase]['column'] + $this->indent - $tokens[$nextCloser]['column']);
160175
if ($diff !== 0) {
176+
$phpcsFile->recordMetric($nextCloser, 'Terminating statement aligned with body of case', 'no');
161177
$error = 'Terminating statement must be indented to the same level as the CASE body';
162178
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');
163179
if ($fix === true) {
@@ -167,10 +183,13 @@ public function process(File $phpcsFile, $stackPtr)
167183
$phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff);
168184
}
169185
}
186+
} else {
187+
$phpcsFile->recordMetric($nextCloser, 'Terminating statement aligned with body of case', 'yes');
170188
}
171189
}//end if
172190
}//end if
173191
} else {
192+
$phpcsFile->recordMetric($opener, 'Default/case statement followed by colon', 'no');
174193
$error = strtoupper($type).' statements must be defined using a colon';
175194
$phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type);
176195
}//end if
@@ -193,11 +212,16 @@ public function process(File $phpcsFile, $stackPtr)
193212
if (isset(Tokens::$commentTokens[$tokens[$prevCode]['code']]) === false
194213
&& $this->findNestedTerminator($phpcsFile, ($opener + 1), $nextCode) === false
195214
) {
215+
$phpcsFile->recordMetric($nextCode, 'Case falls through to next', 'yes, without comment');
196216
$error = 'There must be a comment when fall-through is intentional in a non-empty case body';
197217
$phpcsFile->addError($error, $nextCase, 'TerminatingComment');
218+
} else {
219+
$phpcsFile->recordMetric($nextCode, 'Case falls through to next', 'yes, with comment');
198220
}
221+
} else {
222+
$phpcsFile->recordMetric($nextCode, 'Case falls through to next', 'no');
199223
}
200-
}
224+
}//end if
201225
}//end while
202226

203227
}//end process()

0 commit comments

Comments
 (0)