Skip to content

Commit ca4c747

Browse files
committed
SlevomatCodingStandard.PHP.UselessParentheses: Fix of fix
1 parent a7b07d4 commit ca4c747

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,6 @@ private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFil
234234
return;
235235
}
236236

237-
if (in_array($tokens[$pointerBeforeParenthesisOpener]['code'], self::OPERATORS, true)) {
238-
return;
239-
}
240-
241237
if (in_array($tokens[$pointerBeforeParenthesisOpener]['code'], Tokens::$comparisonTokens, true)) {
242238
return;
243239
}
@@ -328,12 +324,16 @@ private function checkParenthesesAroundVariableOrFunctionCall(File $phpcsFile, i
328324
{
329325
$tokens = $phpcsFile->getTokens();
330326

331-
$newPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
332-
if ($tokens[$newPointer]['code'] === T_NEW) {
327+
$pointerAfterParenthesis = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
328+
if ($tokens[$pointerAfterParenthesis]['code'] === T_NEW) {
333329
// Check in other method
334330
return;
335331
}
336332

333+
if ($tokens[$pointerAfterParenthesis]['code'] === T_OPEN_PARENTHESIS) {
334+
return;
335+
}
336+
337337
$operatorsPointers = TokenHelper::findNextAll(
338338
$phpcsFile,
339339
self::OPERATORS,

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(44, $report->getErrorCount());
20+
self::assertSame(46, $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, 41, 42, 43, 44, 49, 53, 55, 56, 57, 58, 62, 64, 67, 72] 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, 49, 53, 55, 56, 57, 58, 62, 63, 65, 67, 70, 75] as $line) {
2323
self::assertSniffError($report, $line, UselessParenthesesSniff::CODE_USELESS_PARENTHESES);
2424
}
2525

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ function () {
5959
$x = $b + 100 - $c;
6060
$x = $b * 100 / $c;
6161

62+
echo 'Hello' . $foo ? ' There' : $fn();
63+
echo 'Hello' . ($foo ? ' There' : $fn());
64+
6265
$remoteAddress = new RemoteAddress();
6366
$remotedAddresses = [
6467
new RemoteAddress()

tests/Sniffs/PHP/data/uselessParenthesesErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ function () {
5959
$x = $b + (100 - $c);
6060
$x = $b * (100 / $c);
6161

62+
echo 'Hello' . ($foo) ? ' There' : $fn();
63+
echo 'Hello' . (($foo) ? ' There' : $fn());
64+
6265
$remoteAddress = (new RemoteAddress());
6366
$remotedAddresses = [
6467
(new RemoteAddress())

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function ($value) {
175175

176176
$anotherObject = new ($object->getClassName());
177177

178-
echo 'Hello' . ($foo) ? ' There' : $fn();
178+
echo 'Hello' . ($foo ? ' There' : $fn());
179179

180180
$response = (new Response())->withStatus(200);
181181
$ip = (new RemoteAddress())?->getIpAddress();

0 commit comments

Comments
 (0)