yield_from_array_to_yields.rst 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ===================================
  2. Rule ``yield_from_array_to_yields``
  3. ===================================
  4. Yield from array must be unpacked to series of yields.
  5. Description
  6. -----------
  7. The conversion will make the array in ``yield from`` changed in arrays of 1 less
  8. dimension.
  9. Warning
  10. -------
  11. Using this rule is risky
  12. ~~~~~~~~~~~~~~~~~~~~~~~~
  13. The rule is risky in case of ``yield from`` being used multiple times within
  14. single function scope, while using list-alike data sources (e.g. ``function
  15. foo() { yield from ["a"]; yield from ["b"]; }``). It only matters when consuming
  16. such iterator with key-value context, because set of yielded keys may be changed
  17. after applying this rule.
  18. Examples
  19. --------
  20. Example #1
  21. ~~~~~~~~~~
  22. .. code-block:: diff
  23. --- Original
  24. +++ New
  25. <?php function generate() {
  26. - yield from [
  27. - 1,
  28. - 2,
  29. - 3,
  30. - ];
  31. +
  32. + yield 1;
  33. + yield 2;
  34. + yield 3;
  35. +
  36. }
  37. Rule sets
  38. ---------
  39. The rule is part of the following rule set:
  40. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  41. References
  42. ----------
  43. - Fixer class: `PhpCsFixer\\Fixer\\ArrayNotation\\YieldFromArrayToYieldsFixer <./../../../src/Fixer/ArrayNotation/YieldFromArrayToYieldsFixer.php>`_
  44. - Test class: `PhpCsFixer\\Tests\\Fixer\\ArrayNotation\\YieldFromArrayToYieldsFixerTest <./../../../tests/Fixer/ArrayNotation/YieldFromArrayToYieldsFixerTest.php>`_
  45. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.