Skip to content

StringCaseManipulation ​

info on by default quick-fix

Group: Control flow · PhpStorm name: StringCaseManipulationInspection

Lower- or upper-casing the haystack and/or needle only to make a substring search case-insensitive costs extra string copies; PHP already ships case-insensitive variants of the position functions (stripos, strripos, mb_stripos, mb_strripos). Use them on the raw strings instead.

Example ​

php
<?php
function lookup(string $text, string $term, array $row) {
    $a = strpos(strtoupper($text), $term);
    $b = strrpos($row['k'], strtolower('abc'));
    $c = \mb_strpos(mb_strtolower($text), mb_strtoupper($term));
    $d = mb_strrpos(strtoupper(trim($text)), mb_strtolower($term));

    $e = strpos(strtolower($text), $term, 3);
    $f = mb_strpos(mb_strtolower($text, 'UTF-8'), $term);
    $g = strpos(ucfirst($text), $term);
    $h = $row->strpos(strtolower($text), $term);
    $i = strpos($text, $row->strtolower($term));
    return [$a, $b, $c, $d, $e, $f, $g, $h, $i];
}
php
<?php
function lookup(string $text, string $term, array $row) {
    $a = stripos($text, $term);
    $b = strripos($row['k'], 'abc');
    $c = mb_stripos($text, $term);
    $d = mb_strripos(trim($text), $term);

    $e = strpos(strtolower($text), $term, 3);
    $f = mb_strpos(mb_strtolower($text, 'UTF-8'), $term);
    $g = strpos(ucfirst($text), $term);
    $h = $row->strpos(strtolower($text), $term);
    $i = strpos($text, $row->strtolower($term));
    return [$a, $b, $c, $d, $e, $f, $g, $h, $i];
}

Reported:

  • line 3: Use 'stripos($text, $term)' instead of changing the case.
  • line 4: Use 'strripos($row['k'], 'abc')' instead of changing the case.
  • line 5: Use 'mb_stripos($text, $term)' instead of changing the case.
  • line 6: Use 'mb_strripos(trim($text), $term)' instead of changing the case.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore StringCaseManipulation

/**
 * @noinspection StringCaseManipulationInspection
 */

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