LambdaNotUsedImportFixerTest.php 5.7 KB

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