1.2.1186. This Could Be Iterable

An argument that is both array and traversable <https://www.php.net/`traversable>`_ may be typed iterable. Iterable is a more generic type than array, and allows the usage of iterators too.

<?php

// parameter and return type might be iterable
function foo($a) {
    foreach($a as $b) {
     // do something
    }

    return $a;
}

class x {
     private $b;

     function foo() {
     foreach($this->b as $c) {
             // do something
         }
     }
}

?>

See also iterable.

1.2.1186.1. Suggestions

  • Add the iterable typehint

1.2.1186.2. Specs

Short name

Classes/CouldBeIterable

Rulesets

All, Changed Behavior, Suggestions, Typechecks

Exakat since

2.3.3

PHP Version

With PHP 7.1 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

iterable

Available in

Entreprise Edition, Exakat Cloud