Skip to content

GetTypeMissUse ​

warning on by default quick-fix

Group: Control flow · PhpStorm name: GetTypeMissUseInspection

Comparing the result of gettype($v) with a type-name string is a roundabout (and typo-prone) way of calling the dedicated is_*() predicate. The rule suggests the predicate, and flags strings that gettype() can never return.

Example ​

php
<?php
function kinds($item, $mode = 'array') {
    $a = gettype($item->qty) === 'integer';
    $b = 'boolean' !== gettype($item);
    $c = gettype($item) == 'NULL';
    $d = gettype($item) != 'double';
    $e = gettype($item) === $mode;
    $f = gettype($item) === 'int';
    $g = gettype($item) === 'null';

    $h = gettype($item) === 'unknown type';
    $i = gettype($item) !== 'resource (closed)';
    $j = (gettype($item)) === 'string';
    $k = gettype($item) === ($item ? 'string' : 'object');
    return [$a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k];
}
php
<?php
function kinds($item, $mode = 'array') {
    $a = is_int($item->qty);
    $b = !is_bool($item);
    $c = is_null($item);
    $d = !is_float($item);
    $e = is_array($item);
    $f = gettype($item) === 'int';
    $g = gettype($item) === 'null';

    $h = gettype($item) === 'unknown type';
    $i = gettype($item) !== 'resource (closed)';
    $j = (gettype($item)) === 'string';
    $k = gettype($item) === ($item ? 'string' : 'object');
    return [$a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k];
}

Reported:

  • line 3: Use 'is_int($item->qty)' instead.
  • line 4: Use '!is_bool($item)' instead.
  • line 5: Use 'is_null($item)' instead.
  • line 6: Use '!is_float($item)' instead.
  • line 7: Use 'is_array($item)' instead.
  • line 8: gettype() never returns 'int'.
  • line 9: gettype() never returns 'null'.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore GetTypeMissUse

/**
 * @noinspection GetTypeMissUseInspection
 */

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