ReturnToYieldFromFixerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\ReturnToYieldFromFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\ReturnToYieldFromFixer>
  20. */
  21. final class ReturnToYieldFromFixerTest 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 ['<?php return [function() { return [1, 2, 3]; }];'];
  36. yield ['<?php return [fn() => [1, 2, 3]];'];
  37. yield [
  38. '<?php
  39. function foo(): iterable { return $z; }
  40. return [1,2] ?> X <?php { echo 2; }',
  41. ];
  42. yield ['<?php function foo() { return [1, 2, 3]; }'];
  43. yield ['<?php function foo(): MyAwesomeIterableType { return [1, 2, 3]; }'];
  44. yield ['<?php function foo(): iterable { if (true) { return [1]; } else { return [2]; } }'];
  45. yield ['<?php function foo(): ?iterable { return [1, 2, 3]; }'];
  46. yield ['<?php
  47. abstract class Foo {
  48. abstract public function bar(): iterable;
  49. public function baz(): array { return []; }
  50. }
  51. '];
  52. yield [
  53. '<?php return [function(): iterable { yield from [1, 2, 3]; }];',
  54. '<?php return [function(): iterable { return [1, 2, 3]; }];',
  55. ];
  56. yield [
  57. '<?php class Foo {
  58. function bar(): iterable { yield from [1, 2, 3]; }
  59. }',
  60. '<?php class Foo {
  61. function bar(): iterable { return [1, 2, 3]; }
  62. }',
  63. ];
  64. yield [
  65. '<?php function foo(): iterable { yield from [1, 2, 3];;;;;;;; }',
  66. '<?php function foo(): iterable { return [1, 2, 3];;;;;;;; }',
  67. ];
  68. yield [
  69. '<?php function foo(): iterable { yield from array(1, 2, 3); }',
  70. '<?php function foo(): iterable { return array(1, 2, 3); }',
  71. ];
  72. yield [
  73. '<?php function foo(): iterable { $x = 0; yield from [1, 2, 3]; }',
  74. '<?php function foo(): iterable { $x = 0; return [1, 2, 3]; }',
  75. ];
  76. yield [
  77. '<?php function foo(): iterable { $x = 0; yield from array(1, 2, 3); }',
  78. '<?php function foo(): iterable { $x = 0; return array(1, 2, 3); }',
  79. ];
  80. yield [
  81. '<?php function foo(): ITERABLE { yield from [1, 2, 3]; }',
  82. '<?php function foo(): ITERABLE { return [1, 2, 3]; }',
  83. ];
  84. yield [
  85. '<?php $f = function(): iterable { yield from [1, 2, 3]; };',
  86. '<?php $f = function(): iterable { return [1, 2, 3]; };',
  87. ];
  88. yield [
  89. '<?php
  90. function foo(): array { return [3, 4]; }
  91. function bar(): iterable { yield from [1, 2]; }
  92. function baz(): int { return 5; }
  93. ',
  94. '<?php
  95. function foo(): array { return [3, 4]; }
  96. function bar(): iterable { return [1, 2]; }
  97. function baz(): int { return 5; }
  98. ',
  99. ];
  100. }
  101. /**
  102. * @dataProvider provideFix80Cases
  103. *
  104. * @requires PHP 8.0
  105. */
  106. public function testFix80(string $expected, ?string $input = null): void
  107. {
  108. $this->doTest($expected, $input);
  109. }
  110. /**
  111. * @return iterable<array{0: string, 1?: string}>
  112. */
  113. public static function provideFix80Cases(): iterable
  114. {
  115. yield [
  116. '<?php function foo(): null|iterable { return [1, 2, 3]; }',
  117. ];
  118. yield [
  119. '<?php function foo(): iterable|null { return [1, 2, 3]; }',
  120. ];
  121. yield [
  122. '<?php function foo(): ITERABLE|null { return [1, 2, 3]; }',
  123. ];
  124. yield [
  125. '<?php function foo(): Bar|iterable { return [1, 2, 3]; }',
  126. ];
  127. }
  128. }