Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect and remove useless parentheses #22

Merged
merged 1 commit into from
Nov 10, 2023
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
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']));