Skip to content

Commit

Permalink
EarlyExitSniff: Fixed false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed May 13, 2020
1 parent 31eac88 commit 2999540
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SlevomatCodingStandard\Helpers\TokenHelper;
use Throwable;
use function array_key_exists;
use function count;
use function in_array;
use function range;
use function sort;
Expand Down Expand Up @@ -106,17 +107,17 @@ private function processElse(File $phpcsFile, int $elsePointer): void
continue;
}

if (count($allConditionsPointers) > 2 && $conditionEarlyExitPointer === null) {
return;
}

$previousConditionPointer = $conditionPointer;
$previousConditionEarlyExitPointer = $conditionEarlyExitPointer;

if ($conditionPointer === $ifPointer) {
$ifEarlyExitPointer = $conditionEarlyExitPointer;
continue;
}

if ($conditionEarlyExitPointer === null) {
return;
}
}

if ($ifEarlyExitPointer === null && $elseEarlyExitPointer === null) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Sniffs/ControlStructures/data/earlyExitNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,14 @@ function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
doElse();
}
};

function oneConditionWithoutEarlyExitWithElse()
{
if (true) {
doAnything();
} elseif (false) {
return;
} else {
throw new \Exception('');
}
};

0 comments on commit 2999540

Please sign in to comment.