1.2.709. Misused Yield

When chaining generator <https://www.php.net/`generator>`_, one must use the yield from keyword.

Forgetting the yield from keyword cancels the generator <https://www.php.net/`generator>`_ nature of the functioncall and nothing is emited.

Using yield on a generator <https://www.php.net/`generator>`_, yields the generator <https://www.php.net/`generator>`_, not the values of the generator <https://www.php.net/`generator>`_.

It is legit to yield a generator <https://www.php.net/`generator>`_, for later usage. This is just very uncommon, and worth a check.

<?php

function foo() {
     yield 1;
     // Goo is called, but not run as a generator
     goo();
}

function hoo() {
     yield 1;
     // Goo is yield, but not run as a generator
     yield goo();
}

function goo() {
     yield 3;
}

?>

1.2.709.1. Suggestions

  • Use the yield from keyword

1.2.709.2. Specs

Short name

Structures/MisusedYield

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.5.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

yield-from

Available in

Entreprise Edition, Exakat Cloud