StaticLambdaFixerTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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\StaticLambdaFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer>
  20. */
  21. final class StaticLambdaFixerTest 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<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'sample' => [
  36. "<?php\n\$a = static function () use (\$b)\n{ echo \$b;\n};",
  37. "<?php\n\$a = function () use (\$b)\n{ echo \$b;\n};",
  38. ];
  39. yield 'minimal double fix case' => [
  40. '<?php $a=static function(){};$b=static function(){};',
  41. '<?php $a=function(){};$b=function(){};',
  42. ];
  43. yield [
  44. '<?php $a /**/ = /**/ static function(){};',
  45. '<?php $a /**/ = /**/ function(){};',
  46. ];
  47. yield [
  48. '<?php $a /**/ = /**/ static function(){};',
  49. '<?php $a /**/ = /**/ function(){};',
  50. ];
  51. yield [
  52. '<?php $a /**/ = /**/static function(){};',
  53. '<?php $a /**/ = /**/function(){};',
  54. ];
  55. yield [
  56. '<?php $a=static fn() => null;$b=static fn() => null;',
  57. '<?php $a=fn() => null;$b=fn() => null;',
  58. ];
  59. yield [
  60. '<?php $a /**/ = /**/ static fn() => null;',
  61. '<?php $a /**/ = /**/ fn() => null;',
  62. ];
  63. yield [
  64. '<?php $a /**/ = /**/ static fn() => null;',
  65. '<?php $a /**/ = /**/ fn() => null;',
  66. ];
  67. yield [
  68. '<?php $a /**/ = /**/static fn() => null; echo $this->foo();',
  69. '<?php $a /**/ = /**/fn() => null; echo $this->foo();',
  70. ];
  71. yield [
  72. '<?php $a /**/ = /**/ static fn() => null ?> <?php echo $this->foo();',
  73. '<?php $a /**/ = /**/ fn() => null ?> <?php echo $this->foo();',
  74. ];
  75. yield [
  76. '<?php
  77. class B
  78. {
  79. public function C()
  80. {
  81. $a = fn () => var_dump($this);
  82. $a();
  83. }
  84. }
  85. ',
  86. ];
  87. yield [
  88. '<?php static fn($a = ["foo" => "bar"]) => [];',
  89. '<?php fn($a = ["foo" => "bar"]) => [];',
  90. ];
  91. yield [
  92. '<?php class Foo {
  93. public function getNames()
  94. {
  95. return \array_map(
  96. static fn ($item) => $item->getName(),
  97. $this->getItems()
  98. );
  99. }
  100. }',
  101. '<?php class Foo {
  102. public function getNames()
  103. {
  104. return \array_map(
  105. fn ($item) => $item->getName(),
  106. $this->getItems()
  107. );
  108. }
  109. }',
  110. ];
  111. yield [
  112. '<?php class Foo {
  113. public function getNames()
  114. {
  115. return \array_map(
  116. fn ($item) => $item->getName(1, $this->foo()),
  117. $this->getItems()
  118. );
  119. }
  120. }',
  121. ];
  122. yield [
  123. '<?php
  124. class A
  125. {
  126. public function B()
  127. {
  128. $a = function () {
  129. $b = \'this\';
  130. var_dump($$b);
  131. };
  132. $a();
  133. }
  134. }
  135. ',
  136. ];
  137. yield [
  138. '<?php
  139. class B
  140. {
  141. public function C()
  142. {
  143. $a = function () {
  144. var_dump($THIS);
  145. };
  146. $a();
  147. }
  148. }
  149. ',
  150. ];
  151. yield [
  152. '<?php
  153. class D
  154. {
  155. public function E()
  156. {
  157. $a = function () {
  158. $c = include __DIR__.\'/return_this.php\';
  159. var_dump($c);
  160. };
  161. $a();
  162. }
  163. }
  164. ',
  165. ];
  166. yield [
  167. '<?php
  168. class F
  169. {
  170. public function G()
  171. {
  172. $a = function () {
  173. $d = include_once __DIR__.\'/return_this.php\';
  174. var_dump($d);
  175. };
  176. $a();
  177. }
  178. }
  179. ',
  180. ];
  181. yield [
  182. '<?php
  183. class H
  184. {
  185. public function I()
  186. {
  187. $a = function () {
  188. $e = require_once __DIR__.\'/return_this.php\';
  189. var_dump($e);
  190. };
  191. $a();
  192. }
  193. }
  194. ',
  195. ];
  196. yield [
  197. '<?php
  198. class J
  199. {
  200. public function K()
  201. {
  202. $a = function () {
  203. $f = require __DIR__.\'/return_this.php\';
  204. var_dump($f);
  205. };
  206. $a();
  207. }
  208. }
  209. ',
  210. ];
  211. yield [
  212. '<?php
  213. class L
  214. {
  215. public function M()
  216. {
  217. $a = function () {
  218. $g = \'this\';
  219. $h = ${$g};
  220. var_dump($h);
  221. };
  222. $a();
  223. }
  224. }
  225. ',
  226. ];
  227. yield [
  228. '<?php
  229. class N
  230. {
  231. public function O()
  232. {
  233. $a = function () {
  234. $a = [0 => \'this\'];
  235. var_dump(${$a[0]});
  236. };
  237. }
  238. }
  239. ',
  240. ];
  241. yield [
  242. '<?php function test(){} test();',
  243. ];
  244. yield [
  245. '<?php abstract class P
  246. {
  247. function test0(){}
  248. public function test1(){}
  249. protected function test2(){}
  250. protected abstract function test3();
  251. private function test4(){}
  252. }
  253. ',
  254. ];
  255. yield [
  256. '<?php
  257. class Q
  258. {
  259. public function abc()
  260. {
  261. $b = function () {
  262. $c = eval(\'return $this;\');
  263. var_dump($c);
  264. };
  265. $b();
  266. }
  267. }
  268. $b = new Q();
  269. $b->abc();
  270. ',
  271. ];
  272. yield [
  273. '<?php
  274. class A {}
  275. class B extends A {
  276. public function foo()
  277. {
  278. $c = function () {
  279. return parent::foo();
  280. };
  281. }
  282. }',
  283. ];
  284. yield [
  285. '<?php
  286. class B
  287. {
  288. public function C()
  289. {
  290. return array_map(
  291. fn () => $this,
  292. []
  293. );
  294. }
  295. }
  296. ',
  297. ];
  298. }
  299. }