Skip to content

Commit

Permalink
UselessIfConditionWithReturnSniff: Don't remove comments automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jun 22, 2020
1 parent cf0627a commit de41b2a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\ConditionHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_key_exists;
Expand Down Expand Up @@ -60,14 +61,6 @@ public function process(File $phpcsFile, $ifPointer): void
: ConditionHelper::getNegativeCondition($phpcsFile, $tokens[$ifPointer]['parenthesis_opener'] + 1, $tokens[$ifPointer]['parenthesis_closer'] - 1);
};

$isFixable = function (int $ifPointer) use ($phpcsFile, $tokens): bool {
if ($this->assumeAllConditionExpressionsAreAlreadyBoolean) {
return true;
}

return ConditionHelper::conditionReturnsBoolean($phpcsFile, $tokens[$ifPointer]['parenthesis_opener'] + 1, $tokens[$ifPointer]['parenthesis_closer'] - 1);
};

$elsePointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$ifPointer]['scope_closer'] + 1);

$errorParameters = [
Expand All @@ -90,7 +83,7 @@ public function process(File $phpcsFile, $ifPointer): void
return;
}

if (!$isFixable($ifPointer)) {
if (!$this->isFixable($phpcsFile, $ifPointer, $tokens[$elsePointer]['scope_closer'])) {
$phpcsFile->addError(...$errorParameters);
return;
}
Expand Down Expand Up @@ -119,7 +112,7 @@ public function process(File $phpcsFile, $ifPointer): void
return;
}

if (!$isFixable($ifPointer)) {
if (!$this->isFixable($phpcsFile, $ifPointer, $semicolonPointer)) {
$phpcsFile->addError(...$errorParameters);
return;
}
Expand All @@ -139,6 +132,21 @@ public function process(File $phpcsFile, $ifPointer): void
}
}

private function isFixable(File $phpcsFile, int $ifPointer, int $endPointer): bool
{
$tokens = $phpcsFile->getTokens();

if (TokenHelper::findNext($phpcsFile, Tokens::$commentTokens, $ifPointer + 1, $endPointer) !== null) {
return false;
}

if ($this->assumeAllConditionExpressionsAreAlreadyBoolean) {
return true;
}

return ConditionHelper::conditionReturnsBoolean($phpcsFile, $tokens[$ifPointer]['parenthesis_opener'] + 1, $tokens[$ifPointer]['parenthesis_closer'] - 1);
}

private function findBooleanAfterReturnInScope(File $phpcsFile, int $scopeOpenerPointer): ?int
{
$tokens = $phpcsFile->getTokens();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessIfConditionWithReturnErrors.php');

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

self::assertSniffError($report, 4, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
self::assertSniffError($report, 12, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
Expand All @@ -28,6 +28,9 @@ public function testErrors(): void
self::assertSniffError($report, 52, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
self::assertSniffError($report, 61, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
self::assertSniffError($report, 70, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
self::assertSniffError($report, 82, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
self::assertSniffError($report, 91, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
self::assertSniffError($report, 100, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);

self::assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,30 @@ function () {
empty($day['from']) || !empty($day['to'])
);
};

function () {
if (true) {
// Comment in if
return true;
}

return false;
};

function () {
if (true) {
return false;
} else {
// Comment in else
return true;
}
};

function () {
if (true) {
return true;
}

// Comment before return
return false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,30 @@ function () {

return true;
};

function () {
if (true) {
// Comment in if
return true;
}

return false;
};

function () {
if (true) {
return false;
} else {
// Comment in else
return true;
}
};

function () {
if (true) {
return true;
}

// Comment before return
return false;
};

0 comments on commit de41b2a

Please sign in to comment.