UseArrowFunctionsFixerTest.php 3.9 KB

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