Skip to content

Commit 2999540

Browse files
committed
EarlyExitSniff: Fixed false positive
1 parent 31eac88 commit 2999540

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use SlevomatCodingStandard\Helpers\TokenHelper;
1212
use Throwable;
1313
use function array_key_exists;
14+
use function count;
1415
use function in_array;
1516
use function range;
1617
use function sort;
@@ -106,17 +107,17 @@ private function processElse(File $phpcsFile, int $elsePointer): void
106107
continue;
107108
}
108109

110+
if (count($allConditionsPointers) > 2 && $conditionEarlyExitPointer === null) {
111+
return;
112+
}
113+
109114
$previousConditionPointer = $conditionPointer;
110115
$previousConditionEarlyExitPointer = $conditionEarlyExitPointer;
111116

112117
if ($conditionPointer === $ifPointer) {
113118
$ifEarlyExitPointer = $conditionEarlyExitPointer;
114119
continue;
115120
}
116-
117-
if ($conditionEarlyExitPointer === null) {
118-
return;
119-
}
120121
}
121122

122123
if ($ifEarlyExitPointer === null && $elseEarlyExitPointer === null) {

tests/Sniffs/ControlStructures/data/earlyExitNoErrors.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,14 @@ function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
207207
doElse();
208208
}
209209
};
210+
211+
function oneConditionWithoutEarlyExitWithElse()
212+
{
213+
if (true) {
214+
doAnything();
215+
} elseif (false) {
216+
return;
217+
} else {
218+
throw new \Exception('');
219+
}
220+
};

0 commit comments

Comments
 (0)