Skip to content

Commit

Permalink
SlevomatCodingStandard.PHP.UselessParentheses: Checks useless parenth…
Browse files Browse the repository at this point in the history
…eses around (new class)
  • Loading branch information
kukulich committed Feb 2, 2025
1 parent cae7dc6 commit a7b07d4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
51 changes: 50 additions & 1 deletion SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
use const T_CASE;
use const T_CLONE;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSE_SHORT_ARRAY;
use const T_CLOSURE;
use const T_COALESCE;
use const T_COLON;
use const T_COMMA;
use const T_CONSTANT_ENCAPSED_STRING;
use const T_DIVIDE;
use const T_DNUMBER;
Expand Down Expand Up @@ -169,7 +171,7 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
$pointerAfterParenthesisOpener = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
if (in_array(
$tokens[$pointerAfterParenthesisOpener]['code'],
[T_NEW, T_CLONE, T_YIELD, T_YIELD_FROM, T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE, T_ARRAY_CAST],
[T_CLONE, T_YIELD, T_YIELD_FROM, T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE, T_ARRAY_CAST],
true,
)) {
return;
Expand Down Expand Up @@ -204,6 +206,7 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
$this->checkParenthesesAroundVariableOrFunctionCall($phpcsFile, $parenthesisOpenerPointer);
$this->checkParenthesesAroundString($phpcsFile, $parenthesisOpenerPointer);
$this->checkParenthesesAroundOperators($phpcsFile, $parenthesisOpenerPointer);
$this->checkParenthesesAroundNew($phpcsFile, $parenthesisOpenerPointer);
}

private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFile, int $parenthesisOpenerPointer): void
Expand Down Expand Up @@ -325,6 +328,12 @@ private function checkParenthesesAroundVariableOrFunctionCall(File $phpcsFile, i
{
$tokens = $phpcsFile->getTokens();

$newPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
if ($tokens[$newPointer]['code'] === T_NEW) {
// Check in other method
return;
}

$operatorsPointers = TokenHelper::findNextAll(
$phpcsFile,
self::OPERATORS,
Expand Down Expand Up @@ -455,6 +464,12 @@ private function checkParenthesesAroundOperators(File $phpcsFile, int $parenthes
{
$tokens = $phpcsFile->getTokens();

$newPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
if ($tokens[$newPointer]['code'] === T_NEW) {
// Check in other method
return;
}

$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
$pointerAfterParenthesisCloser = TokenHelper::findNextEffective(
$phpcsFile,
Expand Down Expand Up @@ -585,4 +600,38 @@ private function checkParenthesesAroundOperators(File $phpcsFile, int $parenthes
$phpcsFile->fixer->endChangeset();
}

private function checkParenthesesAroundNew(File $phpcsFile, int $parenthesisOpenerPointer): void
{
$tokens = $phpcsFile->getTokens();

$newPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
if ($tokens[$newPointer]['code'] !== T_NEW) {
return;
}

$pointerAfterParenthesisCloser = TokenHelper::findNextEffective(
$phpcsFile,
$tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1,
);
if (!in_array($tokens[$pointerAfterParenthesisCloser]['code'], [T_COMMA, T_SEMICOLON, T_CLOSE_SHORT_ARRAY], true)) {
return;
}

$fix = $phpcsFile->addFixableError('Useless parentheses.', $parenthesisOpenerPointer, self::CODE_USELESS_PARENTHESES);

if (!$fix) {
return;
}

$contentStartPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
$contentEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] - 1);

$phpcsFile->fixer->beginChangeset();

FixerHelper::removeBetweenIncluding($phpcsFile, $parenthesisOpenerPointer, $contentStartPointer - 1);
FixerHelper::removeBetweenIncluding($phpcsFile, $contentEndPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']);

$phpcsFile->fixer->endChangeset();
}

}
4 changes: 2 additions & 2 deletions tests/Sniffs/PHP/UselessParenthesesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesErrors.php');

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

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, 64] as $line) {
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) {
self::assertSniffError($report, $line, UselessParenthesesSniff::CODE_USELESS_PARENTHESES);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Sniffs/PHP/data/uselessParenthesesErrors.fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ function () {
$x = $b + 100 - $c;
$x = $b * 100 / $c;

$remoteAddress = new RemoteAddress();
$remotedAddresses = [
new RemoteAddress()
];
$remotedAddresses = [
new RemoteAddress(),
];

function () {
return [
'a' => 'aa' . 'bb',
Expand Down
8 changes: 8 additions & 0 deletions tests/Sniffs/PHP/data/uselessParenthesesErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ function () {
$x = $b + (100 - $c);
$x = $b * (100 / $c);

$remoteAddress = (new RemoteAddress());
$remotedAddresses = [
(new RemoteAddress())
];
$remotedAddresses = [
(new RemoteAddress()),
];

function () {
return [
'a' => ('aa' . 'bb'),
Expand Down
5 changes: 3 additions & 2 deletions tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public function __construct()
}
}

$response = (new Response())->withStatus(200);

$a = $b / (100 + $c);
$a = (100 - $b) * $c;
$a = ('100' . '000') * $c;
Expand Down Expand Up @@ -179,6 +177,9 @@ function ($value) {

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

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

// Must be last
return true
? 100
Expand Down

0 comments on commit a7b07d4

Please sign in to comment.