StrStrUsedAsStrPos
warning on by default quick-fixGroup: Performance · PhpStorm name: StrStrUsedAsStrPosInspection
strstr() / stristr() build and return a substring. When the result is only used as a yes/no answer ("does the haystack contain the needle?"), that copy is wasted work: a position search (strpos() / stripos()) compared strictly against false answers the same question without allocating.
Example
php
<?php
function scan($text, $word, $flag) {
if (strstr($text, $word)) {}
if (!stristr($text, '#')) {}
while ($flag and strstr($text, $word)) { $flag = false; }
$a = strstr($text, $word) ? 1 : 2;
$b = !(strstr($text, $word));
$c = FALSE == \strstr($text, $word, true);
$d = stristr($text, $word) != false;
$e = strstr($text, $word);
$f = true == strstr($text, $word);
$g = (strstr($text, $word)) === false;
$h = strstr($text, $word) ?: 'none';
$i = strstr($text);
return [$a, $b, $c, $d, $e, $f, $g, $h, $i];
}php
<?php
function scan($text, $word, $flag) {
if (strpos($text, $word) !== false) {}
if (stripos($text, '#') === false) {}
while ($flag and strpos($text, $word) !== false) { $flag = false; }
$a = strpos($text, $word) !== false ? 1 : 2;
$b = !(strpos($text, $word) !== false);
$c = \strpos($text, $word) === false;
$d = stripos($text, $word) !== false;
$e = strstr($text, $word);
$f = true == strstr($text, $word);
$g = (strstr($text, $word)) === false;
$h = strstr($text, $word) ?: 'none';
$i = strstr($text);
return [$a, $b, $c, $d, $e, $f, $g, $h, $i];
}Reported:
- line 3: Use 'strpos($text, $word) !== false' instead; it avoids building a substring.
- line 4: Use 'stripos($text, '#') === false' instead; it avoids building a substring.
- line 5: Use 'strpos($text, $word) !== false' instead; it avoids building a substring.
- line 6: Use 'strpos($text, $word) !== false' instead; it avoids building a substring.
- line 7: Use 'strpos($text, $word) !== false' instead; it avoids building a substring.
- line 8: Use '\strpos($text, $word) === false' instead; it avoids building a substring.
- line 9: Use 'stripos($text, $word) !== false' instead; it avoids building a substring.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
| (none) | Operand order in the fix follows the global comparison style (regular default / yoda). |
Configure
In custos.json:
json
{
"rules": {
"StrStrUsedAsStrPos": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore StrStrUsedAsStrPos
/**
* @noinspection StrStrUsedAsStrPosInspection
*/