Skip to content

Commit 9d036f8

Browse files
committed
Fixed bug #3075 : PSR12.ControlStructures.BooleanOperatorPlacement false positive when operator is the only content on line
1 parent 7c1df7d commit 9d036f8

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6161
-- Thanks to Sergei Morozov for the patch
6262
- Fixed bug #3066 : No support for namespace operator used in type declarations
6363
-- Thanks to Juliette Reinders Folmer for the patch
64+
- Fixed bug #3075 : PSR12.ControlStructures.BooleanOperatorPlacement false positive when operator is the only content on line
6465
- Fixed bug #3099 : Squiz.WhiteSpace.OperatorSpacing false positive when exiting with negative number
6566
-- Thanks to Sergei Morozov for the patch
6667
- Fixed bug #3124 : PSR-12 not reporting error for empty lines with only whitespace

src/Standards/PSR12/Sniffs/ControlStructures/BooleanOperatorPlacementSniff.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,44 +90,63 @@ public function process(File $phpcsFile, $stackPtr)
9090
break;
9191
}
9292

93-
$operators[] = $operator;
94-
9593
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($operator - 1), $parenOpener, true);
9694
if ($prev === false) {
9795
// Parse error.
9896
return;
9997
}
10098

99+
$next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true);
100+
if ($next === false) {
101+
// Parse error.
102+
return;
103+
}
104+
105+
$firstOnLine = false;
106+
$lastOnLine = false;
107+
101108
if ($tokens[$prev]['line'] < $tokens[$operator]['line']) {
102109
// The boolean operator is the first content on the line.
103-
if ($position === null) {
104-
$position = 'first';
105-
}
110+
$firstOnLine = true;
111+
}
106112

107-
if ($position !== 'first') {
108-
$error = true;
109-
}
113+
if ($tokens[$next]['line'] > $tokens[$operator]['line']) {
114+
// The boolean operator is the last content on the line.
115+
$lastOnLine = true;
116+
}
110117

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

114-
$next = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), $parenCloser, true);
115-
if ($next === false) {
116-
// Parse error.
117-
return;
125+
$operators[] = $operator;
126+
127+
if ($firstOnLine === false && $lastOnLine === false) {
128+
// It's in the middle of content, so we can't determine
129+
// placement information from looking at it, but we may
130+
// still need to process it.
131+
continue;
118132
}
119133

120-
if ($tokens[$next]['line'] > $tokens[$operator]['line']) {
121-
// The boolean operator is the last content on the line.
134+
if ($firstOnLine === true) {
135+
if ($position === null) {
136+
$position = 'first';
137+
}
138+
139+
if ($position !== 'first') {
140+
$error = true;
141+
}
142+
} else {
122143
if ($position === null) {
123144
$position = 'last';
124145
}
125146

126147
if ($position !== 'last') {
127148
$error = true;
128149
}
129-
130-
continue;
131150
}
132151
} while ($operator !== false);
133152

src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,13 @@ if (
108108
) {
109109
// elseif body
110110
}
111+
112+
if (
113+
($value == 1 ||
114+
$value == 2)
115+
&&
116+
($value == 3 ||
117+
$value == 4)
118+
) {
119+
return 5;
120+
}

src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,13 @@ if (
118118
) {
119119
// elseif body
120120
}
121+
122+
if (
123+
($value == 1 ||
124+
$value == 2)
125+
&&
126+
($value == 3 ||
127+
$value == 4)
128+
) {
129+
return 5;
130+
}

0 commit comments

Comments
 (0)