Skip to content

Commit

Permalink
Merge pull request #16 from webimpress/hotfix/boolean-operator-at-the…
Browse files Browse the repository at this point in the history
…-end-of-the-line

Hotfix ScopeIndent sniff - boolean operator at the end of the line
  • Loading branch information
michalbundyra committed May 13, 2019
2 parents d0f7f42 + fd083d2 commit b5cdaa4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,23 @@ public function process(File $phpcsFile, $stackPtr)
$fix = $phpcsFile->addFixableError($error, $i, 'BooleanOperatorAtTheEnd');

if ($fix) {
$lastNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, $next - 1, null, true);
$string = $phpcsFile->getTokensAsString($i, $lastNonEmpty - $i + 1);
$lastNonEmpty = $i;
while ($tokens[$lastNonEmpty + 1]['line'] === $tokens[$i]['line']) {
++$lastNonEmpty;
}
while (in_array($tokens[$lastNonEmpty]['code'], Tokens::$emptyTokens, true)) {
--$lastNonEmpty;
}

$string = $phpcsFile->getTokensAsString($i, $lastNonEmpty - $i + 1);
if (substr($string, -1) !== '(') {
$string .= ' ';
}

while ($bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, $next - 1, $lastNonEmpty + 1)) {
$next = $bracket;
}

$phpcsFile->fixer->beginChangeset();
$j = $i - 1;
while ($tokens[$j]['code'] === T_WHITESPACE) {
Expand Down
15 changes: 15 additions & 0 deletions test/Sniffs/WhiteSpace/ScopeIndentUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,18 @@ if ($a
|| ($f && $g))
) {
}

if ($a ||
($b && $c)
) {
}

if ($a || // comment
($b && $c)
) {
}

if ($a ||
((($b && $c)))
) {
}
15 changes: 15 additions & 0 deletions test/Sniffs/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,18 @@ if ($a
|| ($f && $g))
) {
}

if ($a
|| ($b && $c)
) {
}

if ($a // comment
|| ($b && $c)
) {
}

if ($a
|| ((($b && $c)))
) {
}
6 changes: 6 additions & 0 deletions test/Sniffs/WhiteSpace/ScopeIndentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ protected function getErrorList(string $testFile = '') : array
138 => 1,
146 => 1,
149 => 1,
230 => 1,
231 => 1,
235 => 1,
236 => 1,
240 => 1,
241 => 1,
];
case 'ScopeIndentUnitTest.2.inc':
return [
Expand Down

0 comments on commit b5cdaa4

Please sign in to comment.