Skip to content

Commit fe76868

Browse files
committed
SlevomatCodingStandard.ControlStructures.EarlyExit: Fixed detection of useless else
1 parent bdfd6a2 commit fe76868

File tree

5 files changed

+24
-32
lines changed

5 files changed

+24
-32
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,6 @@ private function processElse(File $phpcsFile, int $elsePointer): void
176176
return;
177177
}
178178

179-
$pointerAfterElseCondition = TokenHelper::findNextEffective($phpcsFile, $tokens[$elsePointer]['scope_closer'] + 1);
180-
if (
181-
$pointerAfterElseCondition !== null
182-
&& $tokens[$pointerAfterElseCondition]['code'] !== T_CLOSE_CURLY_BRACKET
183-
) {
184-
return;
185-
}
186-
187179
$fix = $phpcsFile->addFixableError('Remove useless "else" to reduce code nesting.', $elsePointer, self::CODE_USELESS_ELSE);
188180

189181
if (!$fix) {

tests/Sniffs/ControlStructures/EarlyExitSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/earlyExitErrors.php');
1919

20-
self::assertSame(73, $report->getErrorCount());
20+
self::assertSame(74, $report->getErrorCount());
2121

2222
foreach ([6, 15, 24, 33, 42, 50, 58, 66, 74, 82, 90, 98, 108, 149, 157, 165, 191, 199, 207] as $line) {
2323
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit instead of "else".');
@@ -27,7 +27,7 @@ public function testErrors(): void
2727
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit to reduce code nesting.');
2828
}
2929

30-
foreach ([173, 182, 328, 353, 390, 432, 454, 467, 572] as $line) {
30+
foreach ([173, 182, 328, 353, 390, 432, 454, 467, 573, 583] as $line) {
3131
self::assertSniffError($report, $line, EarlyExitSniff::CODE_USELESS_ELSE, 'Remove useless "else" to reduce code nesting.');
3232
}
3333

tests/Sniffs/ControlStructures/data/earlyExitErrors.fixed.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,17 @@ function booleanOperatorPriorities(bool $a, bool $b, bool $c): void
658658
echo 'OK';
659659
}
660660

661+
// With some text after if
662+
function () {
663+
if (true) {
664+
throw new \Exception('');
665+
}
666+
667+
echo 'world';
668+
669+
echo 'hello';
670+
};
671+
661672
// Simple else - needs to be last
662673
if (true) {
663674
return true;

tests/Sniffs/ControlStructures/data/earlyExitErrors.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ function booleanOperatorPriorities(bool $a, bool $b, bool $c): void
566566
}
567567
}
568568

569+
// With some text after if
570+
function () {
571+
if (true) {
572+
throw new \Exception('');
573+
} else {
574+
echo 'world';
575+
}
576+
577+
echo 'hello';
578+
};
579+
569580
// Simple else - needs to be last
570581
if (true) {
571582
return true;

tests/Sniffs/ControlStructures/data/earlyExitNoErrors.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,6 @@ function oneConditionsWithoutEarlyExit($dateTime) {
162162
throw new NotImplementedException();
163163
}
164164

165-
function allConditionsWithEarlyExitButCodeAfter($dateTime) {
166-
if ($dateTime instanceof DateTimeImmutable) {
167-
return true;
168-
}
169-
170-
if ($dateTime instanceof DateTime) {
171-
return true;
172-
}
173-
174-
if (is_numeric($dateTime)) {
175-
return true;
176-
}
177-
178-
if (is_string($dateTime)) {
179-
throw new NotImplementedException();
180-
} else {
181-
throw new NotImplementedException();
182-
}
183-
184-
doSomething();
185-
}
186-
187165
// Code in the end of file
188166
if (!empty($_SERVER['argv'])) {
189167
something();

0 commit comments

Comments
 (0)