Skip to content

StrContainsCanBeUsed ​

info on by default quick-fix PHP ≥ 8.0

Group: Language level migration · PhpStorm name: StrContainsCanBeUsedInspection

Comparing a strpos()/mb_strpos() result strictly against false is the pre-PHP 8 idiom for "does the string contain this substring". PHP 8 has str_contains() for exactly that.

Example ​

php
<?php
function scan(string $line, string $tag) {
    $hit  = strpos($line, $tag) !== false;
    $hit2 = FALSE !== mb_strpos($line, '#');
    $miss = \strpos(trim($line), $tag) === false;
    $from = strpos($line, $tag, 3) !== false;
    $loose = strpos($line, $tag) != false;
    $zero = strpos($line, $tag) === 0;
    $wrapped = (strpos($line, $tag)) !== false;
    return [$hit, $hit2, $miss, $from, $loose, $zero, $wrapped];
}
php
<?php
function scan(string $line, string $tag) {
    $hit  = str_contains($line, $tag);
    $hit2 = str_contains($line, '#');
    $miss = !\str_contains(trim($line), $tag);
    $from = strpos($line, $tag, 3) !== false;
    $loose = strpos($line, $tag) != false;
    $zero = strpos($line, $tag) === 0;
    $wrapped = (strpos($line, $tag)) !== false;
    return [$hit, $hit2, $miss, $from, $loose, $zero, $wrapped];
}

Reported:

  • line 3: Replace with 'str_contains($line, $tag)'.
  • line 4: Replace with 'str_contains($line, '#')'.
  • line 5: Replace with '!\str_contains(trim($line), $tag)'.

Configure ​

In custos.json:

json
{
  "rules": {
    "StrContainsCanBeUsed": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore StrContainsCanBeUsed

/**
 * @noinspection StrContainsCanBeUsedInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.