UseArrowFunctionsFixerTest.php 3.8 KB

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