Skip to content

Commit bd0ee02

Browse files
committed
UselessParenthesesSniff: Fixed checks for parentheses around "case"
1 parent 85fdbef commit bd0ee02

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use SlevomatCodingStandard\Helpers\TokenHelper;
99
use const T_ANON_CLASS;
1010
use const T_BOOLEAN_NOT;
11+
use const T_CASE;
1112
use const T_CLOSE_PARENTHESIS;
1213
use const T_CLOSURE;
14+
use const T_COLON;
1315
use const T_DOLLAR;
1416
use const T_EMPTY;
1517
use const T_EVAL;
@@ -68,20 +70,21 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
6870
}
6971

7072
$this->checkParenthesesAroundConditionInTernaryOperator($phpcsFile, $parenthesisOpenerPointer);
73+
$this->checkParenthesesAroundCaseInSwitch($phpcsFile, $parenthesisOpenerPointer);
7174
$this->checkParenthesesAroundVariableOrFunctionCall($phpcsFile, $parenthesisOpenerPointer);
7275
}
7376

7477
private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFile, int $parenthesisOpenerPointer): void
7578
{
7679
$tokens = $phpcsFile->getTokens();
7780

78-
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
79-
if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_BOOLEAN_NOT) {
81+
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
82+
if ($tokens[$ternaryOperatorPointer]['code'] !== T_INLINE_THEN) {
8083
return;
8184
}
8285

83-
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
84-
if ($tokens[$ternaryOperatorPointer]['code'] !== T_INLINE_THEN) {
86+
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
87+
if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_BOOLEAN_NOT) {
8588
return;
8689
}
8790

@@ -110,12 +113,49 @@ private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFil
110113
$phpcsFile->fixer->endChangeset();
111114
}
112115

116+
private function checkParenthesesAroundCaseInSwitch(File $phpcsFile, int $parenthesisOpenerPointer): void
117+
{
118+
$tokens = $phpcsFile->getTokens();
119+
120+
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
121+
if ($tokens[$pointerBeforeParenthesisOpener]['code'] !== T_CASE) {
122+
return;
123+
}
124+
125+
$pointerAfterParenthesisCloser = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
126+
if ($tokens[$pointerAfterParenthesisCloser]['code'] !== T_COLON) {
127+
return;
128+
}
129+
130+
$fix = $phpcsFile->addFixableError('Useless parentheses.', $parenthesisOpenerPointer, self::CODE_USELESS_PARENTHESES);
131+
132+
if (!$fix) {
133+
return;
134+
}
135+
136+
$contentStartPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
137+
$contentEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] - 1);
138+
139+
$phpcsFile->fixer->beginChangeset();
140+
for ($i = $parenthesisOpenerPointer; $i < $contentStartPointer; $i++) {
141+
$phpcsFile->fixer->replaceToken($i, '');
142+
}
143+
for ($i = $contentEndPointer + 1; $i <= $tokens[$parenthesisOpenerPointer]['parenthesis_closer']; $i++) {
144+
$phpcsFile->fixer->replaceToken($i, '');
145+
}
146+
$phpcsFile->fixer->endChangeset();
147+
}
148+
113149
private function checkParenthesesAroundVariableOrFunctionCall(File $phpcsFile, int $parenthesisOpenerPointer): void
114150
{
115151
$tokens = $phpcsFile->getTokens();
116152

117-
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
153+
$casePointer = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
154+
if ($tokens[$casePointer]['code'] === T_CASE) {
155+
return;
156+
}
118157

158+
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
119159
if ($tokens[$ternaryOperatorPointer]['code'] === T_INLINE_THEN) {
120160
return;
121161
}

tests/Sniffs/PHP/UselessParenthesesSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesErrors.php');
1919

20-
self::assertSame(27, $report->getErrorCount());
20+
self::assertSame(31, $report->getErrorCount());
2121

22-
foreach ([8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38] as $line) {
22+
foreach ([8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38, 41, 42, 43, 44] as $line) {
2323
self::assertSniffError($report, $line, UselessParenthesesSniff::CODE_USELESS_PARENTHESES);
2424
}
2525

tests/Sniffs/PHP/data/uselessParenthesesErrors.fixed.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ public function __construct($parameter)
3636
$x = $y !== null ? true : false;
3737
$a = $b ? 1 : 0;
3838
$c = $d ? 1 : 0;
39+
40+
switch (true) {
41+
case $boo:
42+
case $boo === true:
43+
case $boo ? true : false:
44+
case $boo ? true : false:
45+
}

tests/Sniffs/PHP/data/uselessParenthesesErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ public function __construct($parameter)
3636
$x = ($y !== null) ? true : false;
3737
$a = ($b) ? 1 : 0;
3838
$c = ( $d ) ? 1 : 0;
39+
40+
switch (true) {
41+
case ($boo):
42+
case ($boo === true):
43+
case ($boo ? true : false):
44+
case ($boo) ? true : false:
45+
}

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ function doSomething($parameter) {
5959
eval($c);
6060

6161
list($c) = [];
62+
63+
switch (true) {
64+
case !($boo !== null):
65+
}

0 commit comments

Comments
 (0)