Skip to content

Commit

Permalink
Detect and remove useless parentheses (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
asispts committed Nov 10, 2023
1 parent 27180c7 commit b04cc67
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
48 changes: 24 additions & 24 deletions ptscs/Sniffs/PSR12/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr): ?int
break;
}

$next = $phpcsFile->findNext($searchFor, ($openTag + 1));
$next = $phpcsFile->findNext($searchFor, $openTag + 1);
if (isset(Tokens::$ooScopeTokens[$tokens[$next]['code']]) === true) {
// Once we find an OO token, the file content has
// definitely started.
Expand Down Expand Up @@ -94,8 +94,8 @@ public function process(File $phpcsFile, $stackPtr): ?int
if ($openTag !== 0) {
// Allow for hashbang lines.
$hashbang = false;
if ($tokens[($openTag - 1)]['code'] === \T_INLINE_HTML) {
$content = \trim($tokens[($openTag - 1)]['content']);
if ($tokens[$openTag - 1]['code'] === \T_INLINE_HTML) {
$content = \trim($tokens[$openTag - 1]['content']);
if (\substr($content, 0, 2) === '#!') {
$hashbang = true;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr): array
{
$tokens = $phpcsFile->getTokens();

$next = $phpcsFile->findNext(\T_WHITESPACE, ($stackPtr + 1), null, true);
$next = $phpcsFile->findNext(\T_WHITESPACE, $stackPtr + 1, null, true);
if ($next === false) {
return [];
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr): array

// Make sure this is not a code-level docblock.
$end = $tokens[$next]['comment_closer'];
for ($docToken = ($end + 1); $docToken < $phpcsFile->numTokens; $docToken++) {
for ($docToken = $end + 1; $docToken < $phpcsFile->numTokens; $docToken++) {
if (isset(Tokens::$emptyTokens[$tokens[$docToken]['code']]) === true) {
continue;
}
Expand Down Expand Up @@ -230,7 +230,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr): array
break;
case \T_USE:
$type = 'use';
$useType = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
$useType = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true);
if ($useType !== false && $tokens[$useType]['code'] === \T_STRING) {
$content = \strtolower($tokens[$useType]['content']);
if ($content === 'function' || $content === 'const') {
Expand All @@ -251,7 +251,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr): array
default:
// Skip comments as PSR-12 doesn't say if these are allowed or not.
if (isset(Tokens::$commentTokens[$tokens[$next]['code']]) === true) {
$next = $phpcsFile->findNext(Tokens::$commentTokens, ($next + 1), null, true);
$next = $phpcsFile->findNext(Tokens::$commentTokens, $next + 1, null, true);
if ($next === false) {
// We reached the end of the file.
break(2);
Expand All @@ -265,7 +265,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr): array
break(2);
}//end switch

$next = $phpcsFile->findNext(\T_WHITESPACE, ($next + 1), null, true);
$next = $phpcsFile->findNext(\T_WHITESPACE, $next + 1, null, true);
} while ($next !== false);

return $headerLines;
Expand All @@ -285,25 +285,25 @@ public function processHeaderLines(File $phpcsFile, $headerLines): void

foreach ($headerLines as $i => $line) {
if (
isset($headerLines[($i + 1)]) === false
|| $headerLines[($i + 1)]['type'] !== $line['type']
isset($headerLines[$i + 1]) === false
|| $headerLines[$i + 1]['type'] !== $line['type']
) {
// We're at the end of the current header block.
// Make sure there is a single blank line after
// this block.
$next = $phpcsFile->findNext(\T_WHITESPACE, ($line['end'] + 1), null, true);
if ($next !== false && $tokens[$next]['line'] !== ($tokens[$line['end']]['line'] + 2)) {
$next = $phpcsFile->findNext(\T_WHITESPACE, $line['end'] + 1, null, true);
if ($next !== false && $tokens[$next]['line'] !== $tokens[$line['end']]['line'] + 2) {
$error = 'Header blocks must be separated by a single blank line';
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingAfterBlock');
if ($fix === true) {
if ($tokens[$next]['line'] === $tokens[$line['end']]['line']) {
$phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar . $phpcsFile->eolChar);
} elseif ($tokens[$next]['line'] === ($tokens[$line['end']]['line'] + 1)) {
} elseif ($tokens[$next]['line'] === $tokens[$line['end']]['line'] + 1) {
$phpcsFile->fixer->addNewline($line['end']);
} else {
$phpcsFile->fixer->beginChangeset();
for ($i = ($line['end'] + 1); $i < $next; $i++) {
if ($tokens[$i]['line'] === ($tokens[$line['end']]['line'] + 2)) {
for ($i = $line['end'] + 1; $i < $next; $i++) {
if ($tokens[$i]['line'] === $tokens[$line['end']]['line'] + 2) {
break;
}

Expand All @@ -317,27 +317,27 @@ public function processHeaderLines(File $phpcsFile, $headerLines): void

// Make sure we haven't seen this next block before.
if (
isset($headerLines[($i + 1)]) === true
&& isset($found[$headerLines[($i + 1)]['type']]) === true
isset($headerLines[$i + 1]) === true
&& isset($found[$headerLines[$i + 1]['type']]) === true
) {
$error = 'Similar statements must be grouped together inside header blocks; ';
$error .= 'the first "%s" statement was found on line %s';
$data = [
$headerLines[($i + 1)]['type'],
$tokens[$found[$headerLines[($i + 1)]['type']]['start']]['line'],
$headerLines[$i + 1]['type'],
$tokens[$found[$headerLines[$i + 1]['type']]['start']]['line'],
];
$phpcsFile->addError($error, $headerLines[($i + 1)]['start'], 'IncorrectGrouping', $data);
$phpcsFile->addError($error, $headerLines[$i + 1]['start'], 'IncorrectGrouping', $data);
}
} elseif ($headerLines[($i + 1)]['type'] === $line['type']) {
} elseif ($headerLines[$i + 1]['type'] === $line['type']) {
// Still in the same block, so make sure there is no
// blank line after this statement.
$next = $phpcsFile->findNext(\T_WHITESPACE, ($line['end'] + 1), null, true);
if ($tokens[$next]['line'] > ($tokens[$line['end']]['line'] + 1)) {
$next = $phpcsFile->findNext(\T_WHITESPACE, $line['end'] + 1, null, true);
if ($tokens[$next]['line'] > $tokens[$line['end']]['line'] + 1) {
$error = 'Header blocks must not contain blank lines';
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingInsideBlock');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($line['end'] + 1); $i < $next; $i++) {
for ($i = $line['end'] + 1; $i < $next; $i++) {
if ($tokens[$i]['line'] === $tokens[$line['end']]['line']) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions ptscs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation" />

<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators" />
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses" />

<!--++++++++++++++++++++++++++++++++++++
Code analysis
Expand Down
2 changes: 1 addition & 1 deletion tests/Sniffs/Generic/_data/ForbiddenFunctions.php.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

sizeof([]);
print($var);
print $var;
each([]);
is_null($var);
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
Expand Down
19 changes: 19 additions & 0 deletions tests/Sniffs/Slevomat/PHP/UselessParenthesesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Ptscs\Tests\Sniffs\Slevomat\PHP;

use Iterator;
use Ptscs\Tests\SniffTestCase;
use Ptscs\Tests\Utils\ErrorData;

final class UselessParenthesesTest extends SniffTestCase
{
public static function provideTestData(): Iterator
{
yield[
[
new ErrorData(3, 'SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses'),
],
];
}
}
3 changes: 3 additions & 0 deletions tests/Sniffs/Slevomat/PHP/_data/UselessParentheses.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php declare(strict_types=1);

! in_array($foo, ['bar', 'foo']);
3 changes: 3 additions & 0 deletions tests/Sniffs/Slevomat/PHP/_data/UselessParentheses.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php declare(strict_types=1);

! (in_array($foo, ['bar', 'foo']));

0 comments on commit b04cc67

Please sign in to comment.