1.2.302. 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.302.1. Connex PHP features¶
1.2.302.1.1. Suggestions¶
Use
yield from
keyword and shorten the syntax
1.2.302.1.2. Specs¶
Short name |
Structures/CouldUseYieldFrom |
Rulesets |
|
Exakat since |
2.5.3 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |