Skip to content

StrStartsWithCanBeUsed ​

info on by default quick-fix PHP ≥ 8.0

Group: Language level migration · PhpStorm name: StrStartsWithCanBeUsedInspection

strpos($h, $n) === 0 is the pre-PHP 8 way to ask "does $h begin with $n". PHP 8 provides str_starts_with(), which says it directly and does not scan the whole haystack.

Example ​

php
<?php
function route(string $uri, string $prefix): array {
    return [
        strpos($uri, $prefix) === 0,
        0 === mb_strpos($uri, '/api'),
        \strpos(strtolower($uri), $prefix) !== 0,
        strpos($uri, $prefix, 1) === 0,
        strpos($uri, $prefix) == 0,
        strpos($uri, $prefix) === 00,
        strpos($uri, $prefix) === false,
    ];
}
php
<?php
function route(string $uri, string $prefix): array {
    return [
        str_starts_with($uri, $prefix),
        str_starts_with($uri, '/api'),
        !\str_starts_with(strtolower($uri), $prefix),
        strpos($uri, $prefix, 1) === 0,
        strpos($uri, $prefix) == 0,
        strpos($uri, $prefix) === 00,
        strpos($uri, $prefix) === false,
    ];
}

Reported:

  • line 4: Replace with 'str_starts_with($uri, $prefix)'.
  • line 5: Replace with 'str_starts_with($uri, '/api')'.
  • line 6: Replace with '!\str_starts_with(strtolower($uri), $prefix)'.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore StrStartsWithCanBeUsed

/**
 * @noinspection StrStartsWithCanBeUsedInspection
 */

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