1.2.286. Could Type With Iterable

Suggest using iterable typehint for arguments.

iterable represents both array and objects that implements Iterator interface. Both types are coerced, and usable here.

<?php

// $s may be both an array or an iterator
function foo($s) : int {
    $t = 0;
    foreach($s as $v) {
        $t += (int) $v;
    }

    return $t;
}

?>

See also Iterables.

1.2.286.1. Suggestions

  • Add the iterable type

1.2.286.2. Specs

Short name

Functions/CouldTypeWithIterable

Rulesets

All

Exakat since

1.9.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

iterable

Available in

Entreprise Edition, Exakat Cloud