Skip to content

TypesCastingCanBeUsed ​

info on by default quick-fix

Group: Language level migration · PhpStorm name: TypesCastingCanBeUsedInspection

Conversion helpers (intval(), floatval(), strval(), boolval(), settype()), a string made of a single interpolated expression ("$x"), and explicit ->__toString() calls all perform a plain type conversion. A cast expresses the same thing directly and avoids a function call.

Example ​

Options: REPORT_INLINES = true, REPORT_TO_STRING_METHOD_CALLS = true.

php
<?php
namespace Store;

function normalize($qty, $ratio, $label, $item, $price) {
    $a = intval($qty);
    $b = floatval($ratio * 2);
    $c = \boolval($qty ?: $ratio);
    $d = intval($label, 10);
    $e = intval($label, 8);
    $f = strval($price);

    settype($qty, 'integer');
    settype($item['tags'], "array");
    settype($ratio, 'null');
    $ok = settype($ratio, 'int');

    $g = "$label";
    $h = "{$item->name}";
    $i = "#$label";
    $j = "{$label}{$qty}";
    $k = $price->__toString();
    return compact('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'ok');
}

class Tag extends Base {
    public function __toString()
    {
        return 'tag:' . parent::__toString();
    }
}
php
<?php
namespace Store;

function normalize($qty, $ratio, $label, $item, $price) {
    $a = (int)$qty;
    $b = (float)($ratio * 2);
    $c = (bool)($qty ?: $ratio);
    $d = (int)$label;
    $e = intval($label, 8);
    $f = (string)$price;

    $qty = (int)$qty;
    $item['tags'] = (array)$item['tags'];
    settype($ratio, 'null');
    $ok = settype($ratio, 'int');

    $g = (string)$label;
    $h = (string)($item->name);
    $i = "#$label";
    $j = "{$label}{$qty}";
    $k = (string)$price;
    return compact('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'ok');
}

class Tag extends Base {
    public function __toString()
    {
        return 'tag:' . parent::__toString();
    }
}

Reported:

  • line 5: Use '(int)$qty' instead (a cast is clearer and faster).
  • line 6: Use '(float)($ratio * 2)' instead (a cast is clearer and faster).
  • line 7: Use '(bool)($qty ?: $ratio)' instead (a cast is clearer and faster).
  • line 8: Use '(int)$label' instead (a cast is clearer and faster).
  • line 10: Use '(string)$price' instead (a cast is clearer and faster).
  • line 12: Use '$qty = (int)$qty' instead (a cast is clearer and faster).
  • line 13: Use '$item['tags'] = (array)$item['tags']' instead (a cast is clearer and faster).
  • line 17: Use '(string)$label' to make the string conversion explicit.
  • line 18: Use '(string)($item->name)' to make the string conversion explicit.
  • line 21: Use '(string)$price' instead of calling __toString() directly.

Options ​

OptionTypeDefaultEffect
REPORT_INLINESbooltrueEnables the single-interpolation string check (D8–D9).
REPORT_TO_STRING_METHOD_CALLSboolfalseEnables the __toString() call check (D10–D11).

The upstream fixture enables both.

Configure ​

In custos.json:

json
{
  "rules": {
    "TypesCastingCanBeUsed": {
      "enabled": false,
      "options": {
        "REPORT_INLINES": true,
        "REPORT_TO_STRING_METHOD_CALLS": false
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore TypesCastingCanBeUsed

/**
 * @noinspection TypesCastingCanBeUsedInspection
 */

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