Skip to content

Commit 742e60b

Browse files
committed
EarlyExitSniff: Improved internal errors reporting
1 parent f9fd126 commit 742e60b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ private function getAllConditionsPointers(File $phpcsFile, int $conditionPointer
382382
if ($currentConditionPointer !== null) {
383383
while (in_array($tokens[$currentConditionPointer]['code'], [T_ELSEIF, T_ELSE], true)) {
384384
$conditionsPointers[] = $currentConditionPointer;
385+
386+
if (!array_key_exists('scope_closer', $tokens[$currentConditionPointer])) {
387+
throw new Exception(sprintf('"%s" without curly braces is not supported.', $tokens[$currentConditionPointer]['content']));
388+
}
389+
385390
$currentConditionPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$currentConditionPointer]['scope_closer'] + 1);
386391
}
387392
}

tests/Sniffs/ControlStructures/EarlyExitSniffTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function testElseifWithoutCurlyBraces(): void
5353
self::checkFile(__DIR__ . '/data/earlyExitElseifWithoutCurlyBraces.php', [], [EarlyExitSniff::CODE_USELESS_ELSEIF]);
5454
}
5555

56+
public function testElseifWithSpace(): void
57+
{
58+
self::expectException(Throwable::class);
59+
self::expectExceptionMessage('"else" without curly braces is not supported.');
60+
self::checkFile(__DIR__ . '/data/earlyExitElseifWithSpace.php', [], [EarlyExitSniff::CODE_USELESS_ELSEIF]);
61+
}
62+
5663
public function testElseWithoutCurlyBraces(): void
5764
{
5865
self::expectException(Throwable::class);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
function ($a) {
4+
if (true) {
5+
return false;
6+
} elseif ($a) {
7+
return false;
8+
} else if (false) {
9+
return false;
10+
}
11+
};

0 commit comments

Comments
 (0)