Skip to content

StringNormalization ​

info on by default quick-fix

Group: Control flow · PhpStorm name: StringNormalizationInspection

Two smells when chaining string normalisation calls:

  1. Changing the case of a string and then trimming / cutting it converts characters that are thrown away right after; cut first, convert the case of the (shorter) result.
  2. Wrapping a case conversion inside another case conversion is pointless when the outer call overrides what the inner one did (or repeats it); the inner call can be dropped.

Example ​

php
<?php
function tidy($code, $mask, $o) {
    $r = [];
    $r[] = ltrim(strtoupper($code));
    $r[] = substr(mb_strtolower($code), 0, 3);
    $r[] = rtrim(mb_convert_case($code, MB_CASE_TITLE), '-');
    $r[] = \trim(lcfirst($code), '#/');
    $r[] = mb_substr(ucwords($code), -2);

    $r[] = trim(strtoupper($code), 'xyz');
    $r[] = trim(strtoupper($code), " \n");
    $r[] = trim(strtoupper($code), $mask);
    $r[] = $o->rtrim(strtoupper($code));
    $r[] = rtrim($o->strtoupper($code));
    $r[] = strtoupper(rtrim($code));

    $r[] = mb_strtoupper(mb_strtoupper($code));
    $r[] = lcfirst(lcfirst($code));
    $r[] = strtoupper(ucfirst($code));
    $r[] = mb_strtolower(ucwords($code));
    $r[] = ucfirst(lcfirst($code));

    $r[] = ucwords(strtolower($code));
    $r[] = lcfirst(mb_strtolower($code));
    $r[] = strtolower(mb_strtoupper($code));
    $r[] = strtoupper(ucwords($code, '-'));
    return $r;
}
php
<?php
function tidy($code, $mask, $o) {
    $r = [];
    $r[] = strtoupper(ltrim($code));
    $r[] = mb_strtolower(substr($code, 0, 3));
    $r[] = mb_convert_case(rtrim($code, '-'), MB_CASE_TITLE);
    $r[] = \trim(lcfirst($code), '#/');
    $r[] = mb_substr(ucwords($code), -2);

    $r[] = trim(strtoupper($code), 'xyz');
    $r[] = trim(strtoupper($code), " \n");
    $r[] = trim(strtoupper($code), $mask);
    $r[] = $o->rtrim(strtoupper($code));
    $r[] = rtrim($o->strtoupper($code));
    $r[] = strtoupper(rtrim($code));

    $r[] = mb_strtoupper($code);
    $r[] = lcfirst($code);
    $r[] = strtoupper($code);
    $r[] = mb_strtolower($code);
    $r[] = ucfirst(lcfirst($code));

    $r[] = ucwords(strtolower($code));
    $r[] = lcfirst(mb_strtolower($code));
    $r[] = strtolower(mb_strtoupper($code));
    $r[] = strtoupper(ucwords($code, '-'));
    return $r;
}

Reported:

  • line 4: Cut first, then change the case: 'strtoupper(ltrim($code))'.
  • line 5: Cut first, then change the case: 'mb_strtolower(substr($code, 0, 3))'.
  • line 6: Cut first, then change the case: 'mb_convert_case(rtrim($code, '-'), MB_CASE_TITLE)'.
  • line 17: The inner 'mb_strtoupper(...)' call has no effect here.
  • line 18: The inner 'lcfirst(...)' call has no effect here.
  • line 19: The inner 'ucfirst(...)' call has no effect here.
  • line 20: The inner 'ucwords(...)' call has no effect here.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore StringNormalization

/**
 * @noinspection StringNormalizationInspection
 */

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