YieldFromArrayToYieldsFixerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\ArrayNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\ArrayNotation\YieldFromArrayToYieldsFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\YieldFromArrayToYieldsFixer>
  20. */
  21. final class YieldFromArrayToYieldsFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php function f() { yield from foo(); }',
  37. ];
  38. yield [
  39. '<?php function f() { yield 1; yield 2; yield 3; }',
  40. '<?php function f() { yield from [1, 2, 3]; }',
  41. ];
  42. yield [
  43. '<?php function f() { yield 11; yield 22; yield 33; }',
  44. '<?php function f() { yield from array(11, 22, 33); }',
  45. ];
  46. yield [
  47. '<?php function f() { yield 11; yield 22; yield 33; }',
  48. '<?php function f() { yield from array (11, 22, 33); }',
  49. ];
  50. yield [
  51. '<?php function f() { /* ugly comment */yield 11; yield 22; yield 33; }',
  52. '<?php function f() { yield from array/* ugly comment */(11, 22, 33); }',
  53. ];
  54. yield [
  55. '<?php function f() { /** ugly doc */yield 11; yield 22; yield 33; }',
  56. '<?php function f() { yield from array/** ugly doc */(11, 22, 33); }',
  57. ];
  58. yield [
  59. '<?php function f() { yield 111; yield 222; yield 333; }',
  60. '<?php function f() { yield from [111, 222, 333,]; }',
  61. ];
  62. yield [
  63. '<?php function f() {
  64. '.'
  65. yield [1, 2];
  66. yield [3, 4];
  67. '.'
  68. }',
  69. '<?php function f() {
  70. yield from [
  71. [1, 2],
  72. [3, 4],
  73. ];
  74. }',
  75. ];
  76. yield [
  77. '<?php function f() {
  78. '.'
  79. yield array(1, 2);
  80. yield array(3, 4);
  81. '.'
  82. }',
  83. '<?php function f() {
  84. yield from [
  85. array(1, 2),
  86. array(3, 4),
  87. ];
  88. }',
  89. ];
  90. yield [
  91. '<?php function f() {
  92. '.'
  93. yield 1;
  94. yield 2;
  95. yield 3;
  96. '.'
  97. }',
  98. '<?php function f() {
  99. yield from [
  100. 1,
  101. 2,
  102. 3,
  103. ];
  104. }',
  105. ];
  106. yield [
  107. '<?php function f() {
  108. '.'
  109. // uno
  110. yield 1;
  111. // dos
  112. yield 2;
  113. // tres
  114. yield 3;
  115. '.'
  116. }',
  117. '<?php function f() {
  118. yield from [
  119. // uno
  120. 1,
  121. // dos
  122. 2,
  123. // tres
  124. 3,
  125. ];
  126. }',
  127. ];
  128. yield [
  129. '<?php function f() {
  130. '.'
  131. yield random_key() => true;
  132. yield "foo" => foo(1, 2);
  133. yield "bar" => function ($x, $y) { return max($x, $y); };
  134. yield "baz" => function () { yield [1, 2]; };
  135. '.'
  136. }',
  137. '<?php function f() {
  138. yield from [
  139. random_key() => true,
  140. "foo" => foo(1, 2),
  141. "bar" => function ($x, $y) { return max($x, $y); },
  142. "baz" => function () { yield [1, 2]; },
  143. ];
  144. }',
  145. ];
  146. yield [
  147. '<?php
  148. function f1() { yield 0; yield 1; yield 2; }
  149. function f2() { yield 3; yield 4; yield 5; }
  150. function f3() { yield 6; yield 7; yield 8; }
  151. ',
  152. '<?php
  153. function f1() { yield from [0, 1, 2]; }
  154. function f2() { yield from [3, 4, 5]; }
  155. function f3() { yield from [6, 7, 8]; }
  156. ',
  157. ];
  158. yield [
  159. '<?php
  160. function f1() { yield 0; yield 1; }
  161. function f2() { yield 2; yield 3; }
  162. function f3() { yield 4; yield 5; }
  163. function f4() { yield 6; yield 7; }
  164. function f5() { yield 8; yield 9; }
  165. ',
  166. '<?php
  167. function f1() { yield from array(0, 1); }
  168. function f2() { yield from [2, 3]; }
  169. function f3() { yield from array(4, 5); }
  170. function f4() { yield from [6, 7]; }
  171. function f5() { yield from array(8, 9); }
  172. ',
  173. ];
  174. yield [
  175. '<?php
  176. function foo() {
  177. return [
  178. 1,
  179. yield from [2, 3],
  180. 4,
  181. ];
  182. }
  183. ',
  184. ];
  185. yield [
  186. '<?php
  187. function foo() {
  188. yield from [
  189. "this element is regular string",
  190. yield from ["here", "are", "nested", "strings"],
  191. "next elements will be an arrow function reference",
  192. fn() => [yield 1, yield from [2, 3]],
  193. fn() => [yield from [1, 2], yield 3],
  194. fn() => [yield from array(1, 2), yield 3]
  195. ];
  196. }
  197. ',
  198. ];
  199. yield [
  200. '<?php function foo() {
  201. yield 0; yield 1;
  202. yield 2; yield 3;
  203. yield from [4, yield from [5, 6], 7];
  204. yield 8; yield 9;
  205. }',
  206. '<?php function foo() {
  207. yield from [0, 1];
  208. yield from [2, 3];
  209. yield from [4, yield from [5, 6], 7];
  210. yield from [8, 9];
  211. }',
  212. ];
  213. yield [
  214. '<?php function foo()
  215. {
  216. foreach ([] as $x) {}
  217. yield 1; yield 2; yield 3;
  218. }',
  219. '<?php function foo()
  220. {
  221. foreach ([] as $x) {}
  222. yield from [1, 2, 3];
  223. }',
  224. ];
  225. yield 'skip empty arrays' => [
  226. '<?php
  227. function foo1()
  228. {
  229. yield from [/*empty*/ ];
  230. }
  231. function foo2()
  232. {
  233. yield from [
  234. // Inline comment,
  235. # and another one
  236. ];
  237. }
  238. function foo3()
  239. {
  240. yield from array(/*empty*/ );
  241. }
  242. function bar()
  243. {
  244. yield from [];
  245. yield from array();
  246. }
  247. function baz()
  248. {
  249. yield from [];
  250. yield 1; yield 2;
  251. yield from [];
  252. }',
  253. '<?php
  254. function foo1()
  255. {
  256. yield from [/*empty*/ ];
  257. }
  258. function foo2()
  259. {
  260. yield from [
  261. // Inline comment,
  262. # and another one
  263. ];
  264. }
  265. function foo3()
  266. {
  267. yield from array(/*empty*/ );
  268. }
  269. function bar()
  270. {
  271. yield from [];
  272. yield from array();
  273. }
  274. function baz()
  275. {
  276. yield from [];
  277. yield from [1, 2];
  278. yield from [];
  279. }',
  280. ];
  281. }
  282. }