|
8 | 8 | use SlevomatCodingStandard\Helpers\TokenHelper; |
9 | 9 | use const T_ANON_CLASS; |
10 | 10 | use const T_BOOLEAN_NOT; |
| 11 | +use const T_CASE; |
11 | 12 | use const T_CLOSE_PARENTHESIS; |
12 | 13 | use const T_CLOSURE; |
| 14 | +use const T_COLON; |
13 | 15 | use const T_DOLLAR; |
14 | 16 | use const T_EMPTY; |
15 | 17 | use const T_EVAL; |
@@ -68,20 +70,21 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void |
68 | 70 | } |
69 | 71 |
|
70 | 72 | $this->checkParenthesesAroundConditionInTernaryOperator($phpcsFile, $parenthesisOpenerPointer); |
| 73 | + $this->checkParenthesesAroundCaseInSwitch($phpcsFile, $parenthesisOpenerPointer); |
71 | 74 | $this->checkParenthesesAroundVariableOrFunctionCall($phpcsFile, $parenthesisOpenerPointer); |
72 | 75 | } |
73 | 76 |
|
74 | 77 | private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFile, int $parenthesisOpenerPointer): void |
75 | 78 | { |
76 | 79 | $tokens = $phpcsFile->getTokens(); |
77 | 80 |
|
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) { |
80 | 83 | return; |
81 | 84 | } |
82 | 85 |
|
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) { |
85 | 88 | return; |
86 | 89 | } |
87 | 90 |
|
@@ -110,12 +113,49 @@ private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFil |
110 | 113 | $phpcsFile->fixer->endChangeset(); |
111 | 114 | } |
112 | 115 |
|
| 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 | + |
113 | 149 | private function checkParenthesesAroundVariableOrFunctionCall(File $phpcsFile, int $parenthesisOpenerPointer): void |
114 | 150 | { |
115 | 151 | $tokens = $phpcsFile->getTokens(); |
116 | 152 |
|
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 | + } |
118 | 157 |
|
| 158 | + $ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1); |
119 | 159 | if ($tokens[$ternaryOperatorPointer]['code'] === T_INLINE_THEN) { |
120 | 160 | return; |
121 | 161 | } |
|
0 commit comments