Skip to content

Commit c280c14

Browse files
committed
Girlscouting: Use strict comparisons when using in_array()
Prevent quirks caused by type juggling when using the default of `$strict = false`. Ref: http://php.net/manual/en/function.in-array.php
1 parent 219d3da commit c280c14

20 files changed

+39
-39
lines changed

src/Files/File.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,9 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
895895
// due to the use of the --sniffs command line argument.
896896
if ($includeAll === false
897897
&& ((empty($this->configCache['sniffs']) === false
898-
&& in_array(strtolower($listenerCode), $this->configCache['sniffs']) === false)
898+
&& in_array(strtolower($listenerCode), $this->configCache['sniffs'], true) === false)
899899
|| (empty($this->configCache['exclude']) === false
900-
&& in_array(strtolower($listenerCode), $this->configCache['exclude']) === true))
900+
&& in_array(strtolower($listenerCode), $this->configCache['exclude'], true) === true))
901901
) {
902902
return false;
903903
}
@@ -2346,7 +2346,7 @@ public function hasCondition($stackPtr, $types)
23462346
$conditions = $this->tokens[$stackPtr]['conditions'];
23472347

23482348
foreach ($types as $type) {
2349-
if (in_array($type, $conditions) === true) {
2349+
if (in_array($type, $conditions, true) === true) {
23502350
// We found a token with the required type.
23512351
return true;
23522352
}

src/Ruleset.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public function processRuleset($rulesetPath, $depth=0)
410410
}
411411
} else if (empty($newSniffs) === false) {
412412
$newSniff = $newSniffs[0];
413-
if (in_array($newSniff, $ownSniffs) === false) {
413+
if (in_array($newSniff, $ownSniffs, true) === false) {
414414
// Including a sniff that hasn't been included higher up, but
415415
// only including a single message from it. So turn off all messages in
416416
// the sniff, except this one.
@@ -578,7 +578,7 @@ public function processRuleset($rulesetPath, $depth=0)
578578
// sniff list, but filter out any excluded sniffs.
579579
$files = [];
580580
foreach ($includedSniffs as $sniff) {
581-
if (in_array($sniff, $excludedSniffs) === true) {
581+
if (in_array($sniff, $excludedSniffs, true) === true) {
582582
continue;
583583
} else {
584584
$files[] = Util\Common::realpath($sniff);

src/Sniffs/AbstractPatternSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ final public function process(File $phpcsFile, $stackPtr)
195195

196196
$tokens = $phpcsFile->getTokens();
197197

198-
if (in_array($tokens[$stackPtr]['code'], $this->supplementaryTokens) === true) {
198+
if (in_array($tokens[$stackPtr]['code'], $this->supplementaryTokens, true) === true) {
199199
$this->processSupplementary($phpcsFile, $stackPtr);
200200
}
201201

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function process(File $phpcsFile, $stackPtr)
153153
break;
154154
}
155155

156-
if (in_array($tokens[$end]['code'], $fixableScopeOpeners) === true
156+
if (in_array($tokens[$end]['code'], $fixableScopeOpeners, true) === true
157157
&& isset($tokens[$end]['scope_opener']) === false
158158
) {
159159
// The best way to fix nested inline scopes is middle-out.

src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function process(File $phpcsFile, $stackPtr)
173173
// Remove the pattern delimiters and modifier.
174174
$pattern = substr($pattern, 1, -2);
175175
} else {
176-
if (in_array($function, $this->forbiddenFunctionNames) === false) {
176+
if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
177177
return;
178178
}
179179
}//end if

src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ public function process(File $phpcsFile, $stackPtr)
776776

777777
if ($checkToken !== null
778778
&& isset(Tokens::$scopeOpeners[$tokens[$checkToken]['code']]) === true
779-
&& in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes) === false
779+
&& in_array($tokens[$checkToken]['code'], $this->nonIndentingScopes, true) === false
780780
&& isset($tokens[$checkToken]['scope_opener']) === true
781781
) {
782782
$exact = true;
@@ -1133,7 +1133,7 @@ public function process(File $phpcsFile, $stackPtr)
11331133

11341134
$condition = $tokens[$tokens[$i]['scope_condition']]['code'];
11351135
if (isset(Tokens::$scopeOpeners[$condition]) === true
1136-
&& in_array($condition, $this->nonIndentingScopes) === false
1136+
&& in_array($condition, $this->nonIndentingScopes, true) === false
11371137
) {
11381138
if ($this->debug === true) {
11391139
$line = $tokens[$i]['line'];

src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function process(File $phpcsFile, $stackPtr)
161161
T_PROPERTY,
162162
];
163163

164-
if (in_array($tokens[$nextToken]['code'], $ignore) === true) {
164+
if (in_array($tokens[$nextToken]['code'], $ignore, true) === true) {
165165
$phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing');
166166
$phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no');
167167
return ($phpcsFile->numTokens + 1);

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
7171
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
7272
$spaces = strlen($blankSpace);
7373

74-
if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL]) === true
74+
if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL], true) === true
7575
&& $spaces !== 1
7676
) {
7777
$prevContent = strtolower($tokens[($stackPtr - 2)]['content']);

src/Standards/Squiz/Sniffs/CSS/ForbiddenStylesSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function process(File $phpcsFile, $stackPtr)
115115
// Remove the pattern delimiters and modifier.
116116
$pattern = substr($pattern, 1, -2);
117117
} else {
118-
if (in_array($style, $this->forbiddenStyleNames) === false) {
118+
if (in_array($style, $this->forbiddenStyleNames, true) === false) {
119119
return;
120120
}
121121
}//end if

src/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function process(File $phpcsFile, $stackPtr)
9999
T_REQUIRE_ONCE,
100100
];
101101

102-
if (in_array($tokens[$nextToken]['code'], $ignore) === true) {
102+
if (in_array($tokens[$nextToken]['code'], $ignore, true) === true) {
103103
$phpcsFile->addError('Missing file doc comment', $stackPtr, 'Missing');
104104
$phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'no');
105105
return ($phpcsFile->numTokens + 1);
@@ -133,7 +133,7 @@ public function process(File $phpcsFile, $stackPtr)
133133
$name = $tokens[$tag]['content'];
134134
$isRequired = isset($required[$name]);
135135

136-
if ($isRequired === true && in_array($name, $foundTags) === true) {
136+
if ($isRequired === true && in_array($name, $foundTags, true) === true) {
137137
$error = 'Only one %s tag is allowed in a file comment';
138138
$data = [$name];
139139
$phpcsFile->addError($error, $tag, 'Duplicate'.ucfirst(substr($name, 1)).'Tag', $data);
@@ -183,7 +183,7 @@ public function process(File $phpcsFile, $stackPtr)
183183
// Check if the tags are in the correct position.
184184
$pos = 0;
185185
foreach ($required as $tag => $true) {
186-
if (in_array($tag, $foundTags) === false) {
186+
if (in_array($tag, $foundTags, true) === false) {
187187
$error = 'Missing %s tag in file comment';
188188
$data = [$tag];
189189
$phpcsFile->addError($error, $commentEnd, 'Missing'.ucfirst(substr($tag, 1)).'Tag', $data);

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
7878
$suggestedNames = [];
7979
foreach ($typeNames as $i => $typeName) {
8080
$suggestedName = Common::suggestType($typeName);
81-
if (in_array($suggestedName, $suggestedNames) === false) {
81+
if (in_array($suggestedName, $suggestedNames, true) === false) {
8282
$suggestedNames[] = $suggestedName;
8383
}
8484
}
@@ -397,7 +397,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
397397
$suggestedTypeHint = 'callable';
398398
} else if (strpos($suggestedName, 'callback') !== false) {
399399
$suggestedTypeHint = 'callable';
400-
} else if (in_array($suggestedName, Common::$allowedTypes) === false) {
400+
} else if (in_array($suggestedName, Common::$allowedTypes, true) === false) {
401401
$suggestedTypeHint = $suggestedName;
402402
}
403403

src/Standards/Squiz/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function process(File $phpcsFile, $stackPtr)
8686
T_REQUIRE_ONCE,
8787
];
8888

89-
if (in_array($tokens[$nextToken]['code'], $ignore) === true) {
89+
if (in_array($tokens[$nextToken]['code'], $ignore, true) === true) {
9090
return;
9191
}
9292

@@ -266,7 +266,7 @@ public function process(File $phpcsFile, $stackPtr)
266266
'or question marks' => '?',
267267
];
268268

269-
if (in_array($commentCloser, $acceptedClosers) === false) {
269+
if (in_array($commentCloser, $acceptedClosers, true) === false) {
270270
$error = 'Inline comments must end in %s';
271271
$ender = '';
272272
foreach ($acceptedClosers as $closerName => $symbol) {

src/Standards/Squiz/Sniffs/Commenting/LongConditionClosingCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function process(File $phpcsFile, $stackPtr)
9393
$endBrace = $tokens[$stackPtr];
9494

9595
// We are only interested in some code blocks.
96-
if (in_array($startCondition['code'], self::$openers) === false) {
96+
if (in_array($startCondition['code'], self::$openers, true) === false) {
9797
return;
9898
}
9999

src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
111111
$suggestedNames = [];
112112
foreach ($typeNames as $i => $typeName) {
113113
$suggestedName = Common::suggestType($typeName);
114-
if (in_array($suggestedName, $suggestedNames) === false) {
114+
if (in_array($suggestedName, $suggestedNames, true) === false) {
115115
$suggestedNames[] = $suggestedName;
116116
}
117117
}

src/Standards/Squiz/Sniffs/Formatting/OperatorBracketSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function process(File $phpcsFile, $stackPtr)
166166
// We allow simple operations to not be bracketed.
167167
// For example, ceil($one / $two).
168168
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
169-
if (in_array($tokens[$prev]['code'], $allowed) === true) {
169+
if (in_array($tokens[$prev]['code'], $allowed, true) === true) {
170170
continue;
171171
}
172172

@@ -182,7 +182,7 @@ public function process(File $phpcsFile, $stackPtr)
182182
}
183183

184184
for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
185-
if (in_array($tokens[$next]['code'], $allowed) === true) {
185+
if (in_array($tokens[$next]['code'], $allowed, true) === true) {
186186
continue;
187187
}
188188

@@ -198,7 +198,7 @@ public function process(File $phpcsFile, $stackPtr)
198198
}
199199
}//end if
200200

201-
if (in_array($prevCode, Tokens::$scopeOpeners) === true) {
201+
if (in_array($prevCode, Tokens::$scopeOpeners, true) === true) {
202202
// This operation is inside a control structure like FOREACH
203203
// or IF, but has no bracket of it's own.
204204
// The only control structure allowed to do this is SWITCH.

src/Standards/Squiz/Sniffs/Functions/FunctionDuplicateArgumentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function process(File $phpcsFile, $stackPtr)
4848
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
4949
if ($tokens[$i]['code'] === T_VARIABLE) {
5050
$variable = $tokens[$i]['content'];
51-
if (in_array($variable, $foundVariables) === true) {
51+
if (in_array($variable, $foundVariables, true) === true) {
5252
$error = 'Variable "%s" appears more than once in function declaration';
5353
$data = [$variable];
5454
$phpcsFile->addError($error, $i, 'Found', $data);

src/Tokenizers/PHP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ protected function tokenize($string)
10011001
$newToken['code'] = T_NULLABLE;
10021002
$newToken['type'] = 'T_NULLABLE';
10031003
break;
1004-
} else if (in_array($tokenType, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, '=', '{', ';']) === true) {
1004+
} else if (in_array($tokenType, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, '=', '{', ';'], true) === true) {
10051005
$newToken['code'] = T_INLINE_THEN;
10061006
$newToken['type'] = 'T_INLINE_THEN';
10071007

src/Util/Common.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,31 +226,31 @@ public static function isStdinATTY()
226226
public static function prepareForOutput($content, $exclude=[])
227227
{
228228
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
229-
if (in_array("\r", $exclude) === false) {
229+
if (in_array("\r", $exclude, true) === false) {
230230
$content = str_replace("\r", '\r', $content);
231231
}
232232

233-
if (in_array("\n", $exclude) === false) {
233+
if (in_array("\n", $exclude, true) === false) {
234234
$content = str_replace("\n", '\n', $content);
235235
}
236236

237-
if (in_array("\t", $exclude) === false) {
237+
if (in_array("\t", $exclude, true) === false) {
238238
$content = str_replace("\t", '\t', $content);
239239
}
240240
} else {
241-
if (in_array("\r", $exclude) === false) {
241+
if (in_array("\r", $exclude, true) === false) {
242242
$content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
243243
}
244244

245-
if (in_array("\n", $exclude) === false) {
245+
if (in_array("\n", $exclude, true) === false) {
246246
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
247247
}
248248

249-
if (in_array("\t", $exclude) === false) {
249+
if (in_array("\t", $exclude, true) === false) {
250250
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
251251
}
252252

253-
if (in_array(' ', $exclude) === false) {
253+
if (in_array(' ', $exclude, true) === false) {
254254
$content = str_replace(' ', "\033[30;1m·\033[0m", $content);
255255
}
256256
}//end if
@@ -399,7 +399,7 @@ public static function suggestType($varType)
399399
return '';
400400
}
401401

402-
if (in_array($varType, self::$allowedTypes) === true) {
402+
if (in_array($varType, self::$allowedTypes, true) === true) {
403403
return $varType;
404404
} else {
405405
$lowerVarType = strtolower($varType);
@@ -445,7 +445,7 @@ public static function suggestType($varType)
445445
} else {
446446
return 'array';
447447
}//end if
448-
} else if (in_array($lowerVarType, self::$allowedTypes) === true) {
448+
} else if (in_array($lowerVarType, self::$allowedTypes, true) === true) {
449449
// A valid type, but not lower cased.
450450
return $lowerVarType;
451451
} else {

tests/Standards/AbstractSniffUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ public function generateFailureMessages(LocalFile $file)
265265
$errorsTemp[] = $foundError['message'].' ('.$foundError['source'].')';
266266

267267
$source = $foundError['source'];
268-
if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES']) === false) {
268+
if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'], true) === false) {
269269
$GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'][] = $source;
270270
}
271271

272272
if ($foundError['fixable'] === true
273-
&& in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES']) === false
273+
&& in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'], true) === false
274274
) {
275275
$GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'][] = $source;
276276
}

tests/Standards/AllSniffs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function suite()
7070
$testPath = $details['path'];
7171
}
7272

73-
if (in_array($standard, $ignoreTestsForStandards) === true) {
73+
if (in_array($standard, $ignoreTestsForStandards, true) === true) {
7474
continue;
7575
}
7676

0 commit comments

Comments
 (0)