Skip to content

Commit 51d4580

Browse files
committed
EarlyExitSniff: Fixed internal error
1 parent 73612ee commit 51d4580

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ private function processElse(File $phpcsFile, int $elsePointer): void
165165
if (
166166
$previousConditionEarlyExitPointer !== null
167167
&& $tokens[$previousConditionEarlyExitPointer]['code'] === T_YIELD
168+
&& $elseEarlyExitPointer !== null
168169
&& $tokens[$elseEarlyExitPointer]['code'] === T_YIELD
169170
) {
170171
return;

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(64, $report->getErrorCount());
20+
self::assertSame(65, $report->getErrorCount());
2121

2222
foreach ([6, 15, 24, 33, 42, 50, 58, 66, 74, 82, 90, 98, 108, 149, 157, 165, 191, 199, 207, 376] 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, 398, 440, 462, 475, 503] as $line) {
30+
foreach ([173, 182, 328, 353, 398, 440, 462, 475, 505, 514] 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
@@ -569,6 +569,17 @@ function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
569569
}
570570
};
571571

572+
function yieldWithElse($handle)
573+
{
574+
while (($data = fgetcsv($handle, 0, ',')) !== false) {
575+
if (is_numeric($data[0])) {
576+
yield $data;
577+
}
578+
579+
echo 'skip';
580+
}
581+
};
582+
572583
// Simple else - needs to be last
573584
if (true) {
574585
return true;

tests/Sniffs/ControlStructures/data/earlyExitErrors.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
497497
}
498498
};
499499

500+
function yieldWithElse($handle)
501+
{
502+
while (($data = fgetcsv($handle, 0, ',')) !== false) {
503+
if (is_numeric($data[0])) {
504+
yield $data;
505+
} else {
506+
echo 'skip';
507+
}
508+
}
509+
};
510+
500511
// Simple else - needs to be last
501512
if (true) {
502513
return true;

0 commit comments

Comments
 (0)