Skip to content

IsCountableCanBeUsed ​

info on by default PHP ≥ 7.4

Group: Language level migration · PhpStorm name: IsCountableCanBeUsedInspection

is_array($v) || $v instanceof Countable is exactly what is_countable($v) checks. On a language level where is_countable() is available, the one call is shorter and makes the intent obvious.

Example ​

php
<?php
function sizeOf($bag, $other) {
    $a = is_array($bag) || $bag instanceof \Countable;
    $b = ($bag instanceof Countable) || is_array($bag->items) || $bag->items instanceof Countable;
    $c = $other === null || (is_array($other) || ($other instanceof \Countable));

    $d = is_array($bag) || $other instanceof Countable;           // different subject
    $e = (is_array($bag)) || $bag instanceof Countable;           // call is parenthesised
    $f = is_array($bag) || $bag instanceof Traversable;           // other interface
    $g = is_array($bag) || ($bag instanceof Countable && $other); // inside &&
    $h = is_array($bag) or $bag instanceof Countable;             // keyword or
    $i = is_array($bag) && $x || $bag instanceof Countable;       // direct parent is &&
}

Reported:

  • line 3: Use 'is_countable($bag)' instead of the is_array()/instanceof Countable pair.
  • line 4: Use 'is_countable($bag->items)' instead of the is_array()/instanceof Countable pair.
  • line 5: Use 'is_countable($other)' instead of the is_array()/instanceof Countable pair.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore IsCountableCanBeUsed

/**
 * @noinspection IsCountableCanBeUsedInspection
 */

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