Fork me on GitHub
a Sensio Labs Product

7th Gear (v3.59.3) edition

Rule yield_from_array_to_yields

Yield from array must be unpacked to series of yields.


The conversion will make the array in yield from changed in arrays of 1 less dimension.


Using this rule is risky

The rule is risky in case of yield from being used multiple times within single function scope, while using list-alike data sources (e.g. function foo() { yield from ["a"]; yield from ["b"]; }). It only matters when consuming such iterator with key-value context, because set of yielded keys may be changed after applying this rule.


Example #1

--- Original
+++ New
 <?php function generate() {
-    yield from [
-        1,
-        2,
-        3,
-    ];
+        yield 1;
+        yield 2;
+        yield 3;

Rule sets

The rule is part of the following rule set:


The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.