Skip to content

Commit

Permalink
Fixed bug #3075 : PSR12.ControlStructures.BooleanOperatorPlacement fa…
Browse files Browse the repository at this point in the history
…lse positive when operator is the only content on line
  • Loading branch information
gsherwood committed Sep 27, 2020
1 parent 7c1df7d commit 9d036f8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Sergei Morozov for the patch
- Fixed bug #3066 : No support for namespace operator used in type declarations
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3075 : PSR12.ControlStructures.BooleanOperatorPlacement false positive when operator is the only content on line
- Fixed bug #3099 : Squiz.WhiteSpace.OperatorSpacing false positive when exiting with negative number
-- Thanks to Sergei Morozov for the patch
- Fixed bug #3124 : PSR-12 not reporting error for empty lines with only whitespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,44 +90,63 @@ public function process(File $phpcsFile, $stackPtr)
break;
}

$operators[] = $operator;

$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($operator - 1), $parenOpener, true);
if ($prev === false) {
// Parse error.
return;
}

$next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true);
if ($next === false) {
// Parse error.
return;
}

$firstOnLine = false;
$lastOnLine = false;

if ($tokens[$prev]['line'] < $tokens[$operator]['line']) {
// The boolean operator is the first content on the line.
if ($position === null) {
$position = 'first';
}
$firstOnLine = true;
}

if ($position !== 'first') {
$error = true;
}
if ($tokens[$next]['line'] > $tokens[$operator]['line']) {
// The boolean operator is the last content on the line.
$lastOnLine = true;
}

if ($firstOnLine === true && $lastOnLine === true) {
// The operator is the only content on the line.
// Don't record it because we can't determine
// placement information from looking at it.
continue;
}

$next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true);
if ($next === false) {
// Parse error.
return;
$operators[] = $operator;

if ($firstOnLine === false && $lastOnLine === false) {
// It's in the middle of content, so we can't determine
// placement information from looking at it, but we may
// still need to process it.
continue;
}

if ($tokens[$next]['line'] > $tokens[$operator]['line']) {
// The boolean operator is the last content on the line.
if ($firstOnLine === true) {
if ($position === null) {
$position = 'first';
}

if ($position !== 'first') {
$error = true;
}
} else {
if ($position === null) {
$position = 'last';
}

if ($position !== 'last') {
$error = true;
}

continue;
}
} while ($operator !== false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,13 @@ if (
) {
// elseif body
}

if (
($value == 1 ||
$value == 2)
&&
($value == 3 ||
$value == 4)
) {
return 5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ if (
) {
// elseif body
}

if (
($value == 1 ||
$value == 2)
&&
($value == 3 ||
$value == 4)
) {
return 5;
}

0 comments on commit 9d036f8

Please sign in to comment.