Skip to content

Commit f4afa10

Browse files
committed
Generic/InlineControlStructure: fix bug when handling elseif/if/foreach without body
In PHP, `elseif`, `if` and `foreach` can be declared without a body. The sniff was improperly flagging those as inline control structures. This commit changes that and now the sniff does not trigger an error anymore for elseif/if/foreach without a body. This is consistent with the behavior of the sniff for `while` and `for` statements without a body. It is likely that this problem was not found before because it is hard to imagine a case in which a `elseif`, `if` or `foreach` without a body would be useful.
1 parent 7d33868 commit f4afa10

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function process(File $phpcsFile, $stackPtr)
8080
}
8181
}
8282

83-
if ($tokens[$stackPtr]['code'] === T_WHILE || $tokens[$stackPtr]['code'] === T_FOR) {
84-
// This could be from a DO WHILE, which doesn't have an opening brace or a while/for without body.
83+
if (in_array($tokens[$stackPtr]['code'], [T_ELSEIF, T_IF, T_FOR, T_FOREACH, T_WHILE], true) === true) {
84+
// This could be from a DO WHILE, which doesn't have an opening brace, or a else/if/for/foreach/while without body.
8585
if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
8686
$afterParensCloser = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, true);
8787
if ($afterParensCloser === false) {

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,16 @@ function testFinally()
276276
if ($something) {
277277
echo 'hello';
278278
} else /* comment */ if ($somethingElse) echo 'hi';
279+
280+
if ($shouldNotTriggerSniff);
281+
282+
if (false) {
283+
} elseif ($shouldNotTriggerSniff);
284+
285+
if (false) {
286+
} else if ($shouldNotTriggerSniff);
287+
288+
if (false) {
289+
} else ($shouldTriggerSniff);
290+
291+
foreach ($array as $shouldNotTriggerSniff);

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,17 @@ if ($something) {
312312
echo 'hello';
313313
} else /* comment */ if ($somethingElse) { echo 'hi';
314314
}
315+
316+
if ($shouldNotTriggerSniff);
317+
318+
if (false) {
319+
} elseif ($shouldNotTriggerSniff);
320+
321+
if (false) {
322+
} else if ($shouldNotTriggerSniff);
323+
324+
if (false) {
325+
} else { ($shouldTriggerSniff);
326+
}
327+
328+
foreach ($array as $shouldNotTriggerSniff);

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function getErrorList($testFile='')
7979
260 => 1,
8080
269 => 1,
8181
278 => 1,
82+
289 => 1,
8283
];
8384

8485
case 'InlineControlStructureUnitTest.1.js':

0 commit comments

Comments
 (0)