Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
use const T_MODULUS;
use const T_MULTIPLY;
use const T_NEW;
use const T_NULLSAFE_OBJECT_OPERATOR;
use const T_OBJECT_CAST;
use const T_OBJECT_OPERATOR;
use const T_OPEN_PARENTHESIS;
use const T_PARENT;
use const T_PLUS;
Expand Down Expand Up @@ -103,6 +105,8 @@ class UselessParenthesesSniff implements Sniff

public bool $ignoreComplexTernaryConditions = false;

public bool $enableCheckAroundNew = false;

/**
* @return array<int, (int|string)>
*/
Expand Down Expand Up @@ -611,7 +615,19 @@ private function checkParenthesesAroundNew(File $phpcsFile, int $parenthesisOpen
$tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1,
);
if (!in_array($tokens[$pointerAfterParenthesisCloser]['code'], [T_COMMA, T_SEMICOLON, T_CLOSE_SHORT_ARRAY], true)) {
return;
if (!in_array($tokens[$pointerAfterParenthesisCloser]['code'], [T_OBJECT_OPERATOR, T_NULLSAFE_OBJECT_OPERATOR], true)) {
return;
}
if (!$this->enableCheckAroundNew) {
return;
}
$pointerBeforeParenthesisCloser = TokenHelper::findPreviousEffective(
$phpcsFile,
$tokens[$parenthesisOpenerPointer]['parenthesis_closer'] - 1,
);
if ($tokens[$pointerBeforeParenthesisCloser]['type'] !== 'T_CLOSE_PARENTHESIS') {
return;
}
}

$fix = $phpcsFile->addFixableError('Useless parentheses.', $parenthesisOpenerPointer, self::CODE_USELESS_PARENTHESES);
Expand Down
2 changes: 2 additions & 0 deletions doc/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Looks for useless parentheses.
Sniff provides the following settings:

* `ignoreComplexTernaryConditions` (default: `false`): ignores complex ternary conditions - condition must contain `&&`, `||` etc. or end of line.
* `enableCheckAroundNew` (default: `false`): enables check of useless parentheses around `(new class())->call()`.


#### SlevomatCodingStandard.PHP.UselessSemicolon 🔧

Expand Down
24 changes: 24 additions & 0 deletions tests/Sniffs/PHP/UselessParenthesesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ public function testErrors(): void
self::assertAllFixedInFile($report);
}

public function testCheckAroundNewDisabled(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesCheckAroundNewErrors.php', [
'enableCheckAroundNew' => false,
]);

self::assertNoSniffErrorInFile($report);
}

public function testCheckAroundNewEnabled(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesCheckAroundNewErrors.php', [
'enableCheckAroundNew' => true,
]);

self::assertSame(2, $report->getErrorCount());

foreach ([3, 5] as $line) {
self::assertSniffError($report, $line, UselessParenthesesSniff::CODE_USELESS_PARENTHESES);
}

self::assertAllFixedInFile($report);
}

public function testNoErrorsWithIgnoredComplexTernaryConditions(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesNoErrorsWithIgnoredComplexTernaryConditions.php', [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php // lint >= 8.4

$response = new Response()->withStatus(200);
$response = (new Response)->withStatus(200);
$ip = new RemoteAddress()?->getIpAddress();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php // lint >= 8.4

$response = (new Response())->withStatus(200);
$response = (new Response)->withStatus(200);
$ip = (new RemoteAddress())?->getIpAddress();
3 changes: 1 addition & 2 deletions tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ function ($value) {

echo 'Hello' . ($foo ? ' There' : $fn());

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

echo ~(1 - 1);

Expand Down
Loading