UseArrowFunctionsFixerTest.php 4.7 KB

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