ReturnToYieldFromFixerTest.php 3.8 KB

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