1.2.300. Could Use Yield From

Yield from can be applied to an array or another generator <https://www.php.net/`generator>`_. It replaces a loop and a yield call. The resulting syntax is shorter and faster.

<?php

foreach(foo() as $f) {
     doSomething($f);
}

// using yield and a loop to yield all elements
function foo() {
     foreach(goo() as $g) {
             yield $g;
     }
}

// using yield from to yield all elements
function foo2() {
     yield from goo();
}

function goo() : array {
     return [1,2,3];
}

?>

1.2.300.1. Suggestions

  • Use yield from keyword and shorten the syntax

1.2.300.2. Specs

Short name

Structures/CouldUseYieldFrom

Rulesets

All, Analyze, Changed Behavior, Performances

Exakat since

2.5.3

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

yield-from

Available in

Entreprise Edition, Exakat Cloud