Skip to content

Commit 31eac88

Browse files
committed
EarlyExitSniff: Yield does not do early exit
1 parent b46c4c9 commit 31eac88

File tree

6 files changed

+8
-95
lines changed

6 files changed

+8
-95
lines changed

SlevomatCodingStandard/Helpers/TokenHelper.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
use const T_THROW;
3737
use const T_TRAIT;
3838
use const T_WHITESPACE;
39-
use const T_YIELD;
40-
use const T_YIELD_FROM;
4139

4240
class TokenHelper
4341
{
@@ -99,8 +97,6 @@ class TokenHelper
9997
T_CONTINUE,
10098
T_BREAK,
10199
T_THROW,
102-
T_YIELD,
103-
T_YIELD_FROM,
104100
T_EXIT,
105101
];
106102

SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use const T_SEMICOLON;
3030
use const T_WHILE;
3131
use const T_WHITESPACE;
32-
use const T_YIELD;
3332

3433
class EarlyExitSniff implements Sniff
3534
{
@@ -162,15 +161,6 @@ private function processElse(File $phpcsFile, int $elsePointer): void
162161
return;
163162
}
164163

165-
if (
166-
$previousConditionEarlyExitPointer !== null
167-
&& $tokens[$previousConditionEarlyExitPointer]['code'] === T_YIELD
168-
&& $elseEarlyExitPointer !== null
169-
&& $tokens[$elseEarlyExitPointer]['code'] === T_YIELD
170-
) {
171-
return;
172-
}
173-
174164
$pointerAfterElseCondition = TokenHelper::findNextEffective($phpcsFile, $tokens[$elsePointer]['scope_closer'] + 1);
175165
if ($pointerAfterElseCondition !== null && $tokens[$pointerAfterElseCondition]['code'] !== T_CLOSE_CURLY_BRACKET) {
176166
return;
@@ -218,33 +208,18 @@ private function processElseIf(File $phpcsFile, int $elseIfPointer): void
218208
return;
219209
}
220210

221-
$elseIfEarlyExitPointer = null;
222-
$previousConditionEarlyExitPointer = null;
223-
224211
foreach ($allConditionsPointers as $conditionPointer) {
225212
$conditionEarlyExitPointer = $this->findEarlyExitInScope($phpcsFile, $tokens[$conditionPointer]['scope_opener'], $tokens[$conditionPointer]['scope_closer']);
226213

227214
if ($conditionPointer === $elseIfPointer) {
228-
$elseIfEarlyExitPointer = $conditionEarlyExitPointer;
229215
break;
230216
}
231217

232-
$previousConditionEarlyExitPointer = $conditionEarlyExitPointer;
233-
234218
if ($conditionEarlyExitPointer === null) {
235219
return;
236220
}
237221
}
238222

239-
if (
240-
$previousConditionEarlyExitPointer !== null
241-
&& $tokens[$previousConditionEarlyExitPointer]['code'] === T_YIELD
242-
&& $elseIfEarlyExitPointer !== null
243-
&& $tokens[$elseIfEarlyExitPointer]['code'] === T_YIELD
244-
) {
245-
return;
246-
}
247-
248223
$fix = $phpcsFile->addFixableError(
249224
'Use "if" instead of "elseif".',
250225
$elseIfPointer,

tests/Sniffs/ControlStructures/EarlyExitSniffTest.php

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

20-
self::assertSame(65, $report->getErrorCount());
20+
self::assertSame(63, $report->getErrorCount());
2121

22-
foreach ([6, 15, 24, 33, 42, 50, 58, 66, 74, 82, 90, 98, 108, 149, 157, 165, 191, 199, 207, 376] as $line) {
22+
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".');
2424
}
2525

26-
foreach ([115, 122, 129, 135, 141, 213, 222, 229, 235, 241, 247, 256, 262, 271, 287, 305, 361, 368, 388, 415, 425, 450] as $line) {
26+
foreach ([115, 122, 129, 135, 141, 213, 222, 229, 235, 241, 247, 256, 262, 271, 287, 305, 361, 368, 380, 407, 417, 442] as $line) {
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, 505, 514] as $line) {
30+
foreach ([173, 182, 328, 353, 390, 432, 454, 467, 495] as $line) {
3131
self::assertSniffError($report, $line, EarlyExitSniff::CODE_USELESS_ELSE, 'Remove useless "else" to reduce code nesting.');
3232
}
3333

34-
foreach ([322, 324, 326, 336, 338, 340, 351, 396, 406, 436, 460, 473, 492] as $line) {
34+
foreach ([322, 324, 326, 336, 338, 340, 351, 388, 398, 428, 452, 465] as $line) {
3535
self::assertSniffError($report, $line, EarlyExitSniff::CODE_USELESS_ELSEIF, 'Use "if" instead of "elseif".');
3636
}
3737

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function () {
4646

4747
function greateThanOrEqualCondition() {
4848
if ($number < 0) {
49-
yield [];
49+
return [];
5050
}
5151

5252
// Something
@@ -422,14 +422,6 @@ function logicalVeryComplicatedCondition() {
422422
doSomething();
423423
}
424424

425-
function yieldFrom() {
426-
if ($number < 0) {
427-
yield from [];
428-
}
429-
430-
// Something
431-
}
432-
433425
function negativeInstanceOf($phpEditor, $jsonEditor) {
434426
$this->phpEditor = $phpEditor;
435427
$this->jsonEditor = $jsonEditor;
@@ -569,17 +561,6 @@ function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
569561
}
570562
};
571563

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-
583564
// Simple else - needs to be last
584565
if (true) {
585566
return true;

tests/Sniffs/ControlStructures/data/earlyExitErrors.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function greateThanOrEqualCondition() {
4848
if ($number >= 0) {
4949
// Something
5050
} else {
51-
yield [];
51+
return [];
5252
}
5353
}
5454

@@ -370,14 +370,6 @@ function logicalVeryComplicatedCondition() {
370370
}
371371
}
372372

373-
function yieldFrom() {
374-
if ($number >= 0) {
375-
// Something
376-
} else {
377-
yield from [];
378-
}
379-
}
380-
381373
function negativeInstanceOf($phpEditor, $jsonEditor) {
382374
$this->phpEditor = $phpEditor;
383375
$this->jsonEditor = $jsonEditor;
@@ -497,17 +489,6 @@ function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
497489
}
498490
};
499491

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-
511492
// Simple else - needs to be last
512493
if (true) {
513494
return true;

tests/Sniffs/ControlStructures/data/earlyExitNoErrors.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function () {
4242

4343
function () {
4444
if (false) {
45-
yield [];
45+
return [];
4646
}
4747

4848
// Something
@@ -191,26 +191,6 @@ function allConditionsWithEarlyExitButCodeAfter($dateTime) {
191191
somethingElse();
192192
}
193193

194-
function twoYields(bool $flag)
195-
{
196-
if ($flag) {
197-
yield 1;
198-
} else {
199-
yield 2;
200-
}
201-
}
202-
203-
function manyYields(bool $flag)
204-
{
205-
if ($flag) {
206-
yield 1;
207-
} elseif (!$flag) {
208-
yield 2;
209-
} else {
210-
yield 3;
211-
}
212-
}
213-
214194
function nestedIfWhenOneBranchDoesNotHaveEarlyExit($a, $b)
215195
{
216196
if ($a === 1) {

0 commit comments

Comments
 (0)