Skip to content

Commit 490f858

Browse files
committed
Squiz ComparisonOperatorUsageSniff now allows conditions like while(true)
1 parent 9a70b2f commit 490f858

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

CodeSniffer/Standards/Squiz/Sniffs/Operators/ComparisonOperatorUsageSniff.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
184184

185185
$requiredOps = 0;
186186
$foundOps = 0;
187+
$foundBools = 0;
187188

188189
for ($i = $start; $i <= $end; $i++) {
189190
$type = $tokens[$i]['code'];
@@ -199,36 +200,42 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
199200
$foundOps++;
200201
}
201202

202-
if ($phpcsFile->tokenizerType !== 'JS') {
203-
if ($tokens[$i]['code'] === T_BOOLEAN_AND || $tokens[$i]['code'] === T_BOOLEAN_OR) {
204-
$requiredOps++;
203+
if ($tokens[$i]['code'] === T_TRUE || $tokens[$i]['code'] === T_FALSE) {
204+
$foundBools++;
205+
}
205206

206-
// When the instanceof operator is used with another operator
207-
// like ===, you can get more ops than are required.
208-
if ($foundOps > $requiredOps) {
209-
$foundOps = $requiredOps;
210-
}
207+
if ($phpcsFile->tokenizerType !== 'JS'
208+
&& ($tokens[$i]['code'] === T_BOOLEAN_AND
209+
|| $tokens[$i]['code'] === T_BOOLEAN_OR)
210+
) {
211+
$requiredOps++;
211212

212-
// If we get to here and we have not found the right number of
213-
// comparison operators, then we must have had an implicit
214-
// true operation ie. if ($a) instead of the required
215-
// if ($a === true), so let's add an error.
216-
if ($requiredOps !== $foundOps) {
217-
$error = 'Implicit true comparisons prohibited; use === TRUE instead';
218-
$phpcsFile->addError($error, $stackPtr, 'ImplicitTrue');
219-
$foundOps++;
220-
}
213+
// When the instanceof operator is used with another operator
214+
// like ===, you can get more ops than are required.
215+
if ($foundOps > $requiredOps) {
216+
$foundOps = $requiredOps;
221217
}
222-
}//end if
218+
219+
// If we get to here and we have not found the right number of
220+
// comparison operators, then we must have had an implicit
221+
// true operation i.e., if ($a) instead of the required
222+
// if ($a === true), so let's add an error.
223+
if ($requiredOps !== $foundOps) {
224+
$error = 'Implicit true comparisons prohibited; use === TRUE instead';
225+
$phpcsFile->addError($error, $stackPtr, 'ImplicitTrue');
226+
$foundOps++;
227+
}
228+
}
223229
}//end for
224230

225231
$requiredOps++;
226232

227-
if ($phpcsFile->tokenizerType !== 'JS') {
228-
if ($foundOps < $requiredOps) {
229-
$error = 'Implicit true comparisons prohibited; use === TRUE instead';
230-
$phpcsFile->addError($error, $stackPtr, 'ImplicitTrue');
231-
}
233+
if ($phpcsFile->tokenizerType !== 'JS'
234+
&& $foundOps < $requiredOps
235+
&& ($requiredOps !== $foundBools)
236+
) {
237+
$error = 'Implicit true comparisons prohibited; use === TRUE instead';
238+
$phpcsFile->addError($error, $stackPtr, 'ImplicitTrue');
232239
}
233240

234241
}//end process()

CodeSniffer/Standards/Squiz/Tests/Operators/ComparisonOperatorUsageUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,9 @@ for ($var1 = 10; $var1 !== 0; $var1--) {
111111

112112
for ($var1 = ($var2 === 10); $var1; $var1--) {
113113
}
114+
115+
while (TRUE) {
116+
}
117+
118+
while (FALSE) {
119+
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5050
-- Please update your ruleset if you are referencing this error code directly
5151
- Squiz CSS IndentationSniff no longer assumes the class opening brace is at the end of a line
5252
- Squiz FunctionCommentThrowTagSniff now ignores non-docblock comments
53+
- Squiz ComparisonOperatorUsageSniff now allows conditions like while(true)
5354
- Fixed bug #872 : Incorrect detection of blank lines between CSS class names
5455
</notes>
5556
<contents>

0 commit comments

Comments
 (0)