Skip to content

Commit

Permalink
EarlyExitSniff: Fixed false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 26, 2020
1 parent 79386c1 commit 23de77f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ private function processElse(File $phpcsFile, int $elsePointer): void
return;
}

if (TokenHelper::findNext(
$phpcsFile,
T_FUNCTION,
$tokens[$elsePointer]['scope_opener'] + 1,
$tokens[$elsePointer]['scope_closer']
) !== null) {
return;
}

$ifPointer = $allConditionsPointers[0];
$ifEarlyExitPointer = null;
$elseEarlyExitPointer = null;
Expand Down Expand Up @@ -220,6 +229,15 @@ private function processElseIf(File $phpcsFile, int $elseIfPointer): void
return;
}

if (TokenHelper::findNext(
$phpcsFile,
T_FUNCTION,
$tokens[$elseIfPointer]['scope_opener'] + 1,
$tokens[$elseIfPointer]['scope_closer']
) !== null) {
return;
}

foreach ($allConditionsPointers as $conditionPointer) {
$conditionEarlyExitPointer = $this->findEarlyExitInScope(
$phpcsFile,
Expand Down
34 changes: 34 additions & 0 deletions tests/Sniffs/ControlStructures/data/earlyExitNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,37 @@ function oneConditionWithoutEarlyExitWithElse()
throw new \Exception('');
}
};

function functionInElse()
{
if (rand(0, 1)) {
function test()
{
echo 1;
}

return;
} else {
function test()
{
echo 0;
}
}
};

function functionInElseIf()
{
if (rand(0, 1)) {
function test()
{
echo 1;
}

return;
} elseif (false) {
function test()
{
echo 0;
}
}
};

0 comments on commit 23de77f

Please sign in to comment.