Skip to content

Commit

Permalink
Squiz/ControlSignature: fix false positive with inline while()
Browse files Browse the repository at this point in the history
The `while()`control structure is special cased to deal with `do {} while();` syntax, but the special casing didn't allow for inline `while()` structures.

Includes unit tests.

Fixes 2562
  • Loading branch information
jrfnl committed Jul 21, 2019
1 parent f6fd848 commit 5cf397d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,26 @@ public function process(File $phpcsFile, $stackPtr)
}
}//end if
} else if ($tokens[$stackPtr]['code'] === T_WHILE) {
// Zero spaces after parenthesis closer.
$closer = $tokens[$stackPtr]['parenthesis_closer'];
$found = 0;
if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) {
$found = 'newline';
} else {
$found = $tokens[($closer + 1)]['length'];
// Zero spaces after parenthesis closer, but only if followed by a semicolon.
$closer = $tokens[$stackPtr]['parenthesis_closer'];
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closer + 1), null, true);
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_SEMICOLON) {
$found = 0;
if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) {
$found = 'newline';
} else {
$found = $tokens[($closer + 1)]['length'];
}
}
}

if ($found !== 0) {
$error = 'Expected 0 spaces before semicolon; %s found';
$data = [$found];
$fix = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($closer + 1), '');
if ($found !== 0) {
$error = 'Expected 0 spaces before semicolon; %s found';
$data = [$found];
$fix = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($closer + 1), '');
}
}
}
}//end if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ if ($this) {
foo(${$a[$b]});
}

while ( $level-- ) ob_end_clean();

while ( $level-- )
ob_end_clean();

while ( $level-- ):
ob_end_clean();
endwhile;

// Intentional parse error. This should be the last test in the file.
foreach
// Some unrelated comment.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ if ($this) {
foo(${$a[$b]});
}

while ( $level-- ) ob_end_clean();

while ( $level-- )
ob_end_clean();

while ( $level-- ):
ob_end_clean();
endwhile;

// Intentional parse error. This should be the last test in the file.
foreach
// Some unrelated comment.

0 comments on commit 5cf397d

Please sign in to comment.