Skip to content

IsIterableCanBeUsed ​

info on by default PHP ≥ 7.1

Group: Language level migration · PhpStorm name: IsIterableCanBeUsedInspection

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

Example ​

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

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

Reported:

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

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore IsIterableCanBeUsed

/**
 * @noinspection IsIterableCanBeUsedInspection
 */

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