Skip to content

Commit

Permalink
Merge branch 'feature/2822-generic-inlinecontrolstructures-bug-fix' of
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jan 30, 2020
2 parents 952426c + 13c803b commit 1141cb8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,26 @@ 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;
}
}

// In Javascript DO WHILE loops without curly braces are legal. This
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand All @@ -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++);
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1141cb8

Please sign in to comment.