Skip to content

Commit cadf1fd

Browse files
committed
ControlStructureSpacingSniff: Fixed internal error
1 parent 32e1ca2 commit cadf1fd

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SlevomatCodingStandard\Sniffs\ControlStructures;
44

5+
use Exception;
56
use PHP_CodeSniffer\Files\File;
67
use PHP_CodeSniffer\Sniffs\Sniff;
78
use PHP_CodeSniffer\Util\Tokens;
@@ -260,6 +261,10 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
260261
}
261262

262263
if ($tokens[$nextPointer]['code'] === T_ELSE) {
264+
if (!array_key_exists('scope_closer', $tokens[$nextPointer])) {
265+
throw new Exception('"else" without curly braces is not supported.');
266+
}
267+
263268
return $tokens[$nextPointer]['scope_closer'];
264269
}
265270

tests/Sniffs/ControlStructures/ControlStructureSpacingSniffTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use SlevomatCodingStandard\Sniffs\Namespaces\UndefinedKeywordTokenException;
66
use SlevomatCodingStandard\Sniffs\TestCase;
7+
use Throwable;
78

89
class ControlStructureSpacingSniffTest extends TestCase
910
{
@@ -153,4 +154,11 @@ public function testThrowExceptionForUndefinedConstant(): void
153154
}
154155
}
155156

157+
public function testElseWithoutCurlyBraces(): void
158+
{
159+
self::expectException(Throwable::class);
160+
self::expectExceptionMessage('"else" without curly braces is not supported.');
161+
self::checkFile(__DIR__ . '/data/controlStructureSpacingElseWithoutCurlyBraces.php');
162+
}
163+
156164
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
if (true) {
4+
return true;
5+
} else return false;
6+

0 commit comments

Comments
 (0)