diff --git a/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php index 9922b8f846..d48a477ce1 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php @@ -81,16 +81,18 @@ public function process(File $phpcsFile, $stackPtr) } } - if ($tokens[$stackPtr]['code'] === T_WHILE) { - // This could be from a DO WHILE, which doesn't have an opening brace. - $lastContent = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); - if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) { - $brace = $tokens[$lastContent]; - if (isset($brace['scope_condition']) === true) { - $condition = $tokens[$brace['scope_condition']]; - if ($condition['code'] === T_DO) { - return; - } + if ($tokens[$stackPtr]['code'] === T_WHILE || $tokens[$stackPtr]['code'] === T_FOR) { + // This could be from a DO WHILE, which doesn't have an opening brace or a while/for without body. + if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) { + $afterParensCloser = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, true); + if ($afterParensCloser === false) { + // Live coding. + return; + } + + if ($tokens[$afterParensCloser]['code'] === T_SEMICOLON) { + $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no'); + return; } } @@ -98,7 +100,7 @@ public function process(File $phpcsFile, $stackPtr) // is only valid if a single statement is present between the DO and // the WHILE. We can detect this by checking only a single semicolon // is present between them. - if ($phpcsFile->tokenizerType === 'JS') { + if ($tokens[$stackPtr]['code'] === T_WHILE && $phpcsFile->tokenizerType === 'JS') { $lastDo = $phpcsFile->findPrevious(T_DO, ($stackPtr - 1)); $lastSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1)); if ($lastDo !== false && $lastSemicolon !== false && $lastDo < $lastSemicolon) { diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc index 20eeafc91d..422efb7e47 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc @@ -244,3 +244,9 @@ echo 'bar'; { echo 'baz'; } + +// Issue 2822. +$i = 10; +while ($i > 0 && --$i); + +for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed index 05260f3982..a37a109bcd 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed @@ -46,10 +46,9 @@ foreach ($array as $element) : echo 'hello'; endforeach; -while (!$this->readLine($tokens, $tag)) {} -while (!$this->readLine($tokens, $tag)) { -//skip to end of file -} +while (!$this->readLine($tokens, $tag)); +while (!$this->readLine($tokens, $tag)); //skip to end of file + foreach ($cookies as $cookie) { if ($cookie->match($uri, $matchSessionCookies, $now)) { $ret[] = $cookie; @@ -254,14 +253,12 @@ if ($this) { } } -while (!$this->readLine($tokens, $tag)) {} //phpcs:ignore Standard.Category.Sniff +while (!$this->readLine($tokens, $tag)); //phpcs:ignore Standard.Category.Sniff -while (!$this->readLine($tokens, $tag)) { -// comment -} -while (!$this->readLine($tokens, $tag)) { /* comment */ +while (!$this->readLine($tokens, $tag)); // comment + +while (!$this->readLine($tokens, $tag)); /* comment */ -} foreach ($stringParade as $hit) { $hitParade[] = $hit + 0; // phpcs:ignore Standard.Category.Sniff } @@ -280,3 +277,9 @@ echo 'bar'; { echo 'baz'; } + +// Issue 2822. +$i = 10; +while ($i > 0 && --$i); + +for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php index 48f1903861..92888b8581 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php @@ -37,8 +37,6 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.1.inc') 15 => 1, 17 => 1, 23 => 1, - 42 => 1, - 43 => 1, 45 => 1, 46 => 1, 49 => 1, @@ -68,9 +66,6 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.1.inc') 198 => 1, 206 => 1, 222 => 1, - 226 => 1, - 228 => 1, - 230 => 1, 232 => 1, 235 => 1, 236 => 1,