Skip to content

Commit 5416644

Browse files
committed
BlockControlStructureSpacingSniff: Ignore if with alternative syntax
1 parent db3f132 commit 5416644

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
326326
throw new Exception('"if" without curly braces is not supported.');
327327
}
328328

329+
$pointerAfterParenthesisCloser = TokenHelper::findNextEffective(
330+
$phpcsFile,
331+
$tokens[$controlStructurePointer]['parenthesis_closer'] + 1
332+
);
333+
if ($pointerAfterParenthesisCloser !== null && $tokens[$pointerAfterParenthesisCloser]['code'] === T_COLON) {
334+
throw new Exception('"if" without curly braces is not supported.');
335+
}
336+
329337
$controlStructureEndPointer = $tokens[$controlStructurePointer]['scope_closer'];
330338
do {
331339
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $controlStructureEndPointer + 1);

tests/Sniffs/ControlStructures/BlockControlStructureSpacingSniffTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@ public function testElseWithoutCurlyBraces(): void
384384
self::assertSame(0, $report->getErrorCount());
385385
}
386386

387+
public function testIfWithAlternativeSyntax(): void
388+
{
389+
$report = self::checkFile(__DIR__ . '/data/blockControlStructureSpacingIfWithAlternativeSyntax.php');
390+
391+
self::assertSame(0, $report->getErrorCount());
392+
}
393+
387394
public function testAtTheEndOfFile(): void
388395
{
389396
$report = self::checkFile(__DIR__ . '/data/blockControlStructureSpacingAtTheEndOfFile.php');
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+
endif;
6+
min(0, 10);

0 commit comments

Comments
 (0)