Skip to content

Commit 03bd481

Browse files
committed
Merge branch 'comparison-operator-usage' of https://github.com/aboks/PHP_CodeSniffer
2 parents 7be34ee + cb6bd6e commit 03bd481

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

CodeSniffer/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function processTokenWithinScope(
106106

107107
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
108108
$startIndex = $stackPtr;
109-
while ($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) {
109+
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
110110
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
111111
&& $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
112112
) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public function register()
103103
T_IF,
104104
T_ELSEIF,
105105
T_INLINE_THEN,
106+
T_WHILE,
107+
T_FOR,
106108
);
107109

108110
}//end register()
@@ -158,6 +160,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
158160

159161
$start = $tokens[$end]['parenthesis_opener'];
160162
}//end if
163+
} else if ($tokens[$stackPtr]['code'] === T_FOR) {
164+
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
165+
return;
166+
}
167+
168+
$openingBracket = $tokens[$stackPtr]['parenthesis_opener'];
169+
$closingBracket = $tokens[$stackPtr]['parenthesis_closer'];
170+
171+
$start = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);
172+
$end = $phpcsFile->findNext(T_SEMICOLON, ($start + 1), $closingBracket);
161173
} else {
162174
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
163175
return;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,26 @@ if (false === ($parent instanceof Foo) && ($parent instanceof Bar) === false) {
8888

8989
if (false === ($parent instanceof Foo) && $foo) {
9090
}
91+
92+
while ($var1) {
93+
}
94+
95+
while ($var1 === TRUE) {
96+
}
97+
98+
do {
99+
100+
} while ($var1);
101+
102+
do {
103+
104+
} while ($var1 === TRUE);
105+
106+
for ($var1 = 10; $var1; $var1--) {
107+
}
108+
109+
for ($var1 = 10; $var1 !== 0; $var1--) {
110+
}
111+
112+
for ($var1 = ($var2 === 10); $var1; $var1--) {
113+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,21 @@ if (one === TRUE || two === TRUE || three === FALSE || four === TRUE) {
3636

3737
if (one || two || !three || four) {
3838
}
39+
40+
while (one == true) {
41+
}
42+
43+
while (one === true) {
44+
}
45+
46+
do {
47+
} while (one == true);
48+
49+
do {
50+
} while (one === true);
51+
52+
for (one = 10; one != 0; one--) {
53+
}
54+
55+
for (one = 10; one !== 0; one--) {
56+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function getErrorList($testFile='ComparisonOperatorUsageUnitTest.inc')
6767
82 => 1,
6868
83 => 1,
6969
89 => 1,
70+
92 => 1,
71+
100 => 1,
72+
106 => 1,
73+
112 => 1,
7074
);
7175
break;
7276
case 'ComparisonOperatorUsageUnitTest.js':
@@ -76,6 +80,9 @@ public function getErrorList($testFile='ComparisonOperatorUsageUnitTest.inc')
7680
17 => 1,
7781
18 => 1,
7882
28 => 2,
83+
40 => 1,
84+
47 => 1,
85+
52 => 1,
7986
);
8087
break;
8188
default:

0 commit comments

Comments
 (0)