Skip to content

Commit

Permalink
EarlyExitSniff: fixed fixing of negative logical conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 7, 2018
1 parent 4b52495 commit ea5fe67
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
Expand Up @@ -230,6 +230,12 @@ private function getNegativeCondition(\PHP_CodeSniffer\Files\File $phpcsFile, in

$condition = TokenHelper::getContent($phpcsFile, $startPointer, $endPointer);

$booleanNotPointer = TokenHelper::findNextEffective($phpcsFile, $startPointer);
if ($tokens[$booleanNotPointer]['code'] === T_BOOLEAN_NOT) {
$negativeCondition = preg_replace('~^!\\s*~', '', $condition);
return preg_replace('~^\(\\s*(.+?)\\s*\)\\s*$~', '\\1', $negativeCondition);
}

$booleanPointers = TokenHelper::findNextAll($phpcsFile, \PHP_CodeSniffer\Util\Tokens::$booleanOperators, $startPointer, $endPointer + 1);
if (count($booleanPointers) > 0) {
$uniqueBooleanOperators = array_unique(array_map(function (int $pointer) use ($tokens) {
Expand Down Expand Up @@ -274,12 +280,6 @@ private function getNegativeCondition(\PHP_CodeSniffer\Files\File $phpcsFile, in
return $negativeCondition;
}

$booleanNotPointer = TokenHelper::findNextEffective($phpcsFile, $startPointer);
if ($tokens[$booleanNotPointer]['code'] === T_BOOLEAN_NOT) {
$negativeCondition = preg_replace('~^!\\s*~', '', $condition);
return preg_replace('~^\(\\s*(.+?)\\s*\)\\s*~', '\\1', $negativeCondition);
}

if (TokenHelper::findNext($phpcsFile, [T_INSTANCEOF, T_BITWISE_AND], $startPointer, $endPointer + 1) !== null) {
return sprintf('!(%s)', $condition);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Sniffs/ControlStructures/EarlyExitSniffTest.php
Expand Up @@ -15,13 +15,13 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/earlyExitErrors.php');

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

foreach ([6, 15, 24, 33, 42, 50, 58, 66, 74, 82, 90, 98, 108, 149, 157, 165, 191, 199, 207] as $line) {
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit instead of else.');
}

foreach ([115, 122, 129, 135, 141, 213, 222, 229] as $line) {
foreach ([115, 122, 129, 135, 141, 213, 222, 229, 235, 241] as $line) {
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit to reduce code nesting.');
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Sniffs/ControlStructures/data/earlyExitErrors.fixed.php
Expand Up @@ -248,3 +248,19 @@ function binaryAndCondition() {

$this->eventManager->dispatchEvent($eventName, $event);
}

function negativeLogicalAndCondition() {
if (isset($parameterMappings[$parameterName]) && array_key_exists($parameterName, $parameterMappings)) {
return;
}

unset($parameters[$key]);
}

function negativeLogicalOrCondition() {
if (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings)) {
return;
}

unset($parameters[$key]);
}
12 changes: 12 additions & 0 deletions tests/Sniffs/ControlStructures/data/earlyExitErrors.php
Expand Up @@ -230,3 +230,15 @@ function binaryAndCondition() {
$this->eventManager->dispatchEvent($eventName, $event);
}
}

function negativeLogicalAndCondition() {
if (! (isset($parameterMappings[$parameterName]) && array_key_exists($parameterName, $parameterMappings))) {
unset($parameters[$key]);
}
}

function negativeLogicalOrCondition() {
if (! (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings))) {
unset($parameters[$key]);
}
}

0 comments on commit ea5fe67

Please sign in to comment.