YieldFromArrayToYieldsFixerTest.php 8.2 KB

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