LambdaNotUsedImportFixerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer>
  20. */
  21. final class LambdaNotUsedImportFixerTest 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. /**
  31. * @return iterable<string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'simple' => [
  36. '<?php $foo = function() {};',
  37. '<?php $foo = function() use ($bar) {};',
  38. ];
  39. yield 'simple, one of two' => [
  40. '<?php $foo = function & () use ( $foo) { echo $foo; };',
  41. '<?php $foo = function & () use ($bar, $foo) { echo $foo; };',
  42. ];
  43. yield 'simple, one used, one reference, two not used' => [
  44. '<?php $foo = function() use ($bar, &$foo ) { echo $bar; };',
  45. '<?php $foo = function() use ($bar, &$foo, $not1, $not2) { echo $bar; };',
  46. ];
  47. yield 'simple, but witch comments' => [
  48. '<?php $foo = function()
  49. # 1
  50. #2
  51. # 3
  52. #4
  53. # 5
  54. #6
  55. {};',
  56. '<?php $foo = function()
  57. use
  58. # 1
  59. ( #2
  60. # 3
  61. $bar #4
  62. # 5
  63. ) #6
  64. {};',
  65. ];
  66. yield 'nested lambda I' => [
  67. '<?php
  68. $f = function() {
  69. return function ($d) use ($c) {
  70. $b = 1; echo $c;
  71. };
  72. };
  73. ',
  74. '<?php
  75. $f = function() use ($b) {
  76. return function ($d) use ($c) {
  77. $b = 1; echo $c;
  78. };
  79. };
  80. ',
  81. ];
  82. yield 'nested lambda II' => [
  83. '<?php
  84. // do not fix
  85. $f = function() use ($a) { return function() use ($a) { return function() use ($a) { return function() use ($a) { echo $a; }; }; }; };
  86. $f = function() use ($b) { return function($b) { return function($b) { return function($b) { echo $b; }; }; }; };
  87. // do fix
  88. $f = function() { return function() { return function() { return function() { }; }; }; };
  89. ',
  90. '<?php
  91. // do not fix
  92. $f = function() use ($a) { return function() use ($a) { return function() use ($a) { return function() use ($a) { echo $a; }; }; }; };
  93. $f = function() use ($b) { return function($b) { return function($b) { return function($b) { echo $b; }; }; }; };
  94. // do fix
  95. $f = function() use ($a) { return function() use ($a) { return function() use ($a) { return function() use ($a) { }; }; }; };
  96. ',
  97. ];
  98. yield 'anonymous class' => [
  99. '<?php
  100. $a = function() use ($b) { new class($b){}; }; // do not fix
  101. $a = function() { new class(){ public function foo($b){echo $b;}}; }; // do fix
  102. ',
  103. '<?php
  104. $a = function() use ($b) { new class($b){}; }; // do not fix
  105. $a = function() use ($b) { new class(){ public function foo($b){echo $b;}}; }; // do fix
  106. ',
  107. ];
  108. yield 'anonymous class with a string argument' => [
  109. '<?php $function = function () {
  110. new class("bar") {};
  111. };',
  112. '<?php $function = function () use ($foo) {
  113. new class("bar") {};
  114. };',
  115. ];
  116. yield 'reference' => [
  117. '<?php $fn = function() use(&$b) {} ?>',
  118. ];
  119. yield 'compact 1' => [
  120. '<?php $foo = function() use ($b) { return compact(\'b\'); };',
  121. ];
  122. yield 'compact 2' => [
  123. '<?php $foo = function() use ($b) { return \compact(\'b\'); };',
  124. ];
  125. yield 'eval' => [
  126. '<?php $foo = function($c) use ($b) { eval($c); };',
  127. ];
  128. yield 'include' => [
  129. '<?php $foo = function($c) use ($b) { include __DIR__."/test3.php"; };',
  130. ];
  131. yield 'include_once' => [
  132. '<?php $foo = function($c) use ($b) { include_once __DIR__."/test3.php"; };',
  133. ];
  134. yield 'require' => [
  135. '<?php $foo = function($c) use ($b) { require __DIR__."/test3.php"; };',
  136. ];
  137. yield 'require_once' => [
  138. '<?php $foo = function($c) use ($b) { require_once __DIR__."/test3.php"; };',
  139. ];
  140. yield '${X}' => [
  141. '<?php $foo = function($g) use ($b) { $h = ${$g}; };',
  142. ];
  143. yield '$$c' => [
  144. '<?php $foo = function($g) use ($b) { $h = $$g; };',
  145. ];
  146. yield 'interpolation 1' => [
  147. '<?php $foo = function() use ($world) { echo "hello $world"; };',
  148. ];
  149. yield 'interpolation 2' => [
  150. '<?php $foo = function() use ($world) { echo "hello {$world}"; };',
  151. ];
  152. yield 'interpolation 3' => [
  153. '<?php $foo = function() use ($world) { echo "hello ${world}"; };',
  154. ];
  155. yield 'heredoc' => [
  156. '<?php
  157. $b = 123;
  158. $foo = function() use ($b) {
  159. echo
  160. <<<"TEST"
  161. Foo $b
  162. TEST;
  163. };
  164. $foo();
  165. ',
  166. ];
  167. }
  168. /**
  169. * @dataProvider provideFix80Cases
  170. *
  171. * @requires PHP 8.0
  172. */
  173. public function testFix80(string $expected, string $input): void
  174. {
  175. $this->doTest($expected, $input);
  176. }
  177. /**
  178. * @return iterable<string, array{string, string}>
  179. */
  180. public static function provideFix80Cases(): iterable
  181. {
  182. yield 'simple' => [
  183. '<?php $foo = function() {};',
  184. '<?php $foo = function() use ($bar,) {};',
  185. ];
  186. }
  187. }