Skip to content

StrTrUsageAsStrReplace ​

info on by default quick-fix

Group: Control flow · PhpStorm name: StrTrUsageAsStrReplaceInspection

strtr($subject, $from, $to) with single-character $from and $to is really a plain character replacement; str_replace($from, $to, $subject) states that intent more clearly.

Example ​

php
<?php
function slug($title, $glue = '+') {
    $a = strtr($title, '-', '_');
    $b = \strtr($title, "\t", ';');
    $c = strtr($title, '\'', '`');
    $d = strtr($title, "\$", 'S');
    $e = strtr($title, $glue, ' ');

    $f = strtr($title, '--', '_');
    $k = strtr($title, '-', '');
    $l = strtr($title, '$', 'USD');
    $g = strtr($title, '\r', '_');
    $h = strtr($title, "\0", '_');
    $i = strtr($title, ['-' => '_']);
    $j = strtr($title, '', '_');
    return [$a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l];
}
php
<?php
function slug($title, $glue = '+') {
    $a = str_replace('-', '_', $title);
    $b = \str_replace("\t", ';', $title);
    $c = str_replace('\'', '`', $title);
    $d = str_replace("\$", 'S', $title);
    $e = str_replace($glue, ' ', $title);

    $f = strtr($title, '--', '_');
    $k = strtr($title, '-', '');
    $l = strtr($title, '$', 'USD');
    $g = strtr($title, '\r', '_');
    $h = strtr($title, "\0", '_');
    $i = strtr($title, ['-' => '_']);
    $j = strtr($title, '', '_');
    return [$a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l];
}

Reported:

  • line 3: Use 'str_replace('-', '_', $title)' instead.
  • line 4: Use '\str_replace("\t", ';', $title)' instead.
  • line 5: Use 'str_replace('\'', '`', $title)' instead.
  • line 6: Use 'str_replace("\$", 'S', $title)' instead.
  • line 7: Use 'str_replace($glue, ' ', $title)' instead.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore StrTrUsageAsStrReplace

/**
 * @noinspection StrTrUsageAsStrReplaceInspection
 */

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