1.2.714. 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.714.1. Connex PHP features¶
1.2.714.1.1. Suggestions¶
Use the yield from keyword
1.2.714.1.2. Specs¶
Short name |
Structures/MisusedYield |
Rulesets |
|
Exakat since |
2.5.2 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Available in |