UseArrowFunctionsFixerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. <<<'PHP'
  133. <?php
  134. foo(fn () =>
  135. 1);
  136. PHP,
  137. <<<'PHP'
  138. <?php
  139. foo(function () {
  140. return
  141. 1;
  142. });
  143. PHP,
  144. ];
  145. yield [
  146. <<<'PHP'
  147. <?php
  148. $func = fn (
  149. $a,
  150. $b
  151. ) => 1;
  152. PHP,
  153. <<<'PHP'
  154. <?php
  155. $func = function (
  156. $a,
  157. $b
  158. ) {
  159. return 1;
  160. };
  161. PHP,
  162. ];
  163. yield [
  164. <<<'EXPECTED'
  165. <?php
  166. $func = function () {
  167. return function () {
  168. foo();
  169. };
  170. };
  171. EXPECTED,
  172. ];
  173. yield [
  174. '<?php $testDummy = fn () => null;',
  175. '<?php $testDummy = function () { return; };',
  176. ];
  177. yield [
  178. '<?php $testDummy = fn () => null ;',
  179. '<?php $testDummy = function () { return ; };',
  180. ];
  181. yield [
  182. '<?php $testDummy = fn () => null/* foo */;',
  183. '<?php $testDummy = function () { return/* foo */; };',
  184. ];
  185. yield [
  186. <<<'PHP'
  187. <?php return fn () => [
  188. CONST_A,
  189. CONST_B,
  190. ];
  191. PHP,
  192. <<<'PHP'
  193. <?php return function () {
  194. return [
  195. CONST_A,
  196. CONST_B,
  197. ];
  198. };
  199. PHP,
  200. ];
  201. }
  202. }