Skip to content

Commit 9f84eb7

Browse files
committed
ControlStructureSpacingSniff: Added T_CASE and T_DEFAULT support
1 parent 2c0ecbc commit 9f84eb7

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ private function checkLinesAfter(File $phpcsFile, int $controlStructurePointer):
228228
$hasCommentAfter = in_array($tokens[$notWhitespacePointerAfter]['code'], Tokens::$commentTokens, true) && $tokens[$notWhitespacePointerAfter]['line'] === $tokens[$controlStructureEndPointer]['line'];
229229
$pointerAfter = $hasCommentAfter ? TokenHelper::findNextEffective($phpcsFile, $controlStructureEndPointer + 1) : $notWhitespacePointerAfter;
230230

231-
$isLastControlStructure = in_array($tokens[$pointerAfter]['code'], [T_CLOSE_CURLY_BRACKET, T_CASE, T_DEFAULT], true);
231+
$isLastControlStructure = in_array($tokens[$controlStructurePointer]['code'], [T_CASE, T_DEFAULT], true)
232+
? $tokens[$pointerAfter]['code'] === T_CLOSE_CURLY_BRACKET
233+
: in_array($tokens[$pointerAfter]['code'], [T_CLOSE_CURLY_BRACKET, T_CASE, T_DEFAULT], true);
232234

233235
$requiredLinesCountAfter = SniffSettingsHelper::normalizeInteger($isLastControlStructure ? $this->linesCountAfterLastControlStructure : $this->linesCountAroundControlStructure);
234236
$actualLinesCountAfter = $tokens[$pointerAfter]['line'] - $tokens[$controlStructureEndPointer]['line'] - 1;
@@ -348,6 +350,17 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
348350
return $tokens[$controlStructurePointer]['scope_closer'];
349351
}
350352

353+
if (in_array($tokens[$controlStructurePointer]['code'], [T_CASE, T_DEFAULT], true)) {
354+
$switchPointer = TokenHelper::findPrevious($phpcsFile, T_SWITCH, $controlStructurePointer - 1);
355+
$pointerAfterControlStructureEnd = TokenHelper::findNext($phpcsFile, [T_CASE, T_DEFAULT], $controlStructurePointer + 1, $tokens[$switchPointer]['scope_closer']);
356+
357+
if ($pointerAfterControlStructureEnd === null) {
358+
return TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $tokens[$switchPointer]['scope_closer'] - 1);
359+
}
360+
361+
return TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointerAfterControlStructureEnd - 1);
362+
}
363+
351364
return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $controlStructurePointer + 1);
352365
}
353366

tests/Sniffs/ControlStructures/ControlStructureSpacingSniffTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public function testModifiedSettingsErrors(): void
118118
'T_YIELD_FROM',
119119
],
120120
]);
121-
122121
self::assertSame(23, $report->getErrorCount());
123122

124123
self::assertSniffError($report, 21, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 0 lines before "while", found 2.');
@@ -148,6 +147,26 @@ public function testModifiedSettingsErrors(): void
148147
self::assertAllFixedInFile($report);
149148
}
150149

150+
public function testSwitchErrors(): void
151+
{
152+
$report = self::checkFile(__DIR__ . '/data/controlStructureSpacingSwitchErrors.php', [
153+
'tokensToCheck' => [
154+
'T_CASE',
155+
'T_DEFAULT',
156+
],
157+
]);
158+
self::assertSame(6, $report->getErrorCount());
159+
160+
self::assertSniffError($report, 6, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "case", found 1.');
161+
self::assertSniffError($report, 6, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "case", found 2.');
162+
self::assertSniffError($report, 11, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "case", found 2.');
163+
self::assertSniffError($report, 11, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "case", found 0.');
164+
self::assertSniffError($report, 14, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "default", found 0.');
165+
self::assertSniffError($report, 14, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "default", found 1.');
166+
167+
self::assertAllFixedInFile($report);
168+
}
169+
151170
public function testThrowExceptionForUndefinedConstant(): void
152171
{
153172
try {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
switch ($condition)
4+
{
5+
case 'a':
6+
7+
return new A();
8+
9+
case 'b':
10+
11+
return new B();
12+
13+
default:
14+
15+
throw new InvalidArgumentException('...');
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
switch ($condition)
4+
{
5+
6+
case 'a':
7+
8+
return new A();
9+
10+
11+
case 'b':
12+
13+
return new B();
14+
default:
15+
16+
throw new InvalidArgumentException('...');
17+
18+
}

0 commit comments

Comments
 (0)