UseArrowFunctionsFixerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Gregor Harlan
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\FunctionNotation\UseArrowFunctionsFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\UseArrowFunctionsFixer>
  22. */
  23. final class UseArrowFunctionsFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php foo(function () use ($a, &$b) { return 1; });',
  39. ];
  40. yield [
  41. '<?php foo(function () { bar(); return 1; });',
  42. ];
  43. yield [
  44. '<?php foo(fn()=> 1);',
  45. '<?php foo(function(){return 1;});',
  46. ];
  47. yield [
  48. '<?php foo(fn()=>$a);',
  49. '<?php foo(function()use($a){return$a;});',
  50. ];
  51. yield [
  52. '<?php foo( fn () => 1 );',
  53. '<?php foo( function () { return 1; } );',
  54. ];
  55. yield [
  56. '<?php $func = static fn &(array &$a, string ...$b): ?int => 1;',
  57. '<?php $func = static function &(array &$a, string ...$b): ?int { return 1; };',
  58. ];
  59. yield [
  60. <<<'EXPECTED'
  61. <?php
  62. foo(1, fn (int $a, Foo $b) => bar($a, $c), 2);
  63. EXPECTED,
  64. <<<'INPUT'
  65. <?php
  66. foo(1, function (int $a, Foo $b) use ($c, $d) {
  67. return bar($a, $c);
  68. }, 2);
  69. INPUT,
  70. ];
  71. yield [
  72. <<<'EXPECTED'
  73. <?php
  74. foo(fn () => 1);
  75. EXPECTED,
  76. <<<'INPUT'
  77. <?php
  78. foo(function () {
  79. return 1;
  80. });
  81. INPUT,
  82. ];
  83. yield [
  84. <<<'EXPECTED'
  85. <?php
  86. foo(fn ($a) => fn () => $a + 1);
  87. EXPECTED,
  88. <<<'INPUT'
  89. <?php
  90. foo(function ($a) {
  91. return function () use ($a) {
  92. return $a + 1;
  93. };
  94. });
  95. INPUT,
  96. ];
  97. yield [
  98. <<<'EXPECTED'
  99. <?php
  100. foo(function () {// comment
  101. return 1;
  102. });
  103. EXPECTED,
  104. ];
  105. yield [
  106. <<<'EXPECTED'
  107. <?php
  108. foo(function () {
  109. // comment
  110. return 1;
  111. });
  112. EXPECTED,
  113. ];
  114. yield [
  115. <<<'EXPECTED'
  116. <?php
  117. foo(function () {
  118. return 1; // comment
  119. });
  120. EXPECTED,
  121. ];
  122. yield [
  123. <<<'EXPECTED'
  124. <?php
  125. foo(function () {
  126. return 1;
  127. // comment
  128. });
  129. EXPECTED,
  130. ];
  131. yield [
  132. <<<'EXPECTED'
  133. <?php
  134. foo(function () {
  135. return
  136. 1;
  137. });
  138. EXPECTED,
  139. ];
  140. yield [
  141. <<<'EXPECTED'
  142. <?php
  143. $func = function (
  144. $a,
  145. $b
  146. ) {
  147. return 1;
  148. };
  149. EXPECTED,
  150. ];
  151. yield [
  152. <<<'EXPECTED'
  153. <?php
  154. $func = function () {
  155. return function () {
  156. foo();
  157. };
  158. };
  159. EXPECTED,
  160. ];
  161. yield [
  162. '<?php $testDummy = fn () => null;',
  163. '<?php $testDummy = function () { return; };',
  164. ];
  165. yield [
  166. '<?php $testDummy = fn () => null ;',
  167. '<?php $testDummy = function () { return ; };',
  168. ];
  169. yield [
  170. '<?php $testDummy = fn () => null/* foo */;',
  171. '<?php $testDummy = function () { return/* foo */; };',
  172. ];
  173. }
  174. }