NoTrailingCommaInSinglelineFunctionCallFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\NoTrailingCommaInSinglelineFunctionCallFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoTrailingCommaInSinglelineFunctionCallFixer>
  20. */
  21. final class NoTrailingCommaInSinglelineFunctionCallFixerTest 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 var' => [
  36. '<?php $a(1);',
  37. '<?php $a(1,);',
  38. ];
  39. yield '&' => [
  40. '<?php $a = &foo($a);',
  41. '<?php $a = &foo($a,);',
  42. ];
  43. yield 'open' => [
  44. '<?php foo($a);',
  45. '<?php foo($a,);',
  46. ];
  47. yield '=' => [
  48. '<?php $b = foo($a);',
  49. '<?php $b = foo($a,);',
  50. ];
  51. yield '.' => [
  52. '<?php $c = $b . foo($a);',
  53. '<?php $c = $b . foo($a,);',
  54. ];
  55. yield '(' => [
  56. '<?php (foo($a/* 1 */ /* 2 */ ));',
  57. '<?php (foo($a /* 1 */ , /* 2 */ ));',
  58. ];
  59. yield '\\' => [
  60. '<?php \foo($a);',
  61. '<?php \foo($a,);',
  62. ];
  63. yield 'A\\' => [
  64. '<?php A\foo($a);',
  65. '<?php A\foo($a,);',
  66. ];
  67. yield '\A\\' => [
  68. '<?php \A\foo($a);',
  69. '<?php \A\foo($a,);',
  70. ];
  71. yield ';' => [
  72. '<?php ; foo($a);',
  73. '<?php ; foo($a,);',
  74. ];
  75. yield '}' => [
  76. '<?php if ($a) { echo 1;} foo($a);',
  77. '<?php if ($a) { echo 1;} foo($a,);',
  78. ];
  79. yield 'test method call' => [
  80. '<?php $o->abc($a);',
  81. '<?php $o->abc($a,);',
  82. ];
  83. yield 'nested call' => [
  84. '<?php $o->abc($a,foo(1));',
  85. '<?php $o->abc($a,foo(1,));',
  86. ];
  87. yield 'wrapped' => [
  88. '<?php echo (new Process())->getOutput(1);',
  89. '<?php echo (new Process())->getOutput(1,);',
  90. ];
  91. yield 'dynamic function and method calls' => [
  92. '<?php $b->$a(1); $c("");',
  93. '<?php $b->$a(1,); $c("",);',
  94. ];
  95. yield 'static function call' => [
  96. '<?php
  97. unset($foo->bar);
  98. $b = isset($foo->bar);
  99. list($a,$b) = $a;
  100. ',
  101. '<?php
  102. unset($foo->bar,);
  103. $b = isset($foo->bar,);
  104. list($a,$b,) = $a;
  105. ',
  106. ];
  107. yield 'unset' => [
  108. '<?php A::foo(1);',
  109. '<?php A::foo(1,);',
  110. ];
  111. yield 'anonymous_class construction' => [
  112. '<?php new class(1, 2) {};',
  113. '<?php new class(1, 2,) {};',
  114. ];
  115. yield 'array/property access call' => [
  116. '<?php
  117. $a = [
  118. "e" => static function(int $a): void{ echo $a;},
  119. "d" => [
  120. [2 => static function(int $a): void{ echo $a;}]
  121. ]
  122. ];
  123. $a["e"](1);
  124. $a["d"][0][2](1);
  125. $z = new class { public static function b(int $a): void {echo $a; }};
  126. $z::b(1);
  127. ${$e}(1);
  128. $$e(2);
  129. $f(0)(1);
  130. $g["e"](1); // foo',
  131. '<?php
  132. $a = [
  133. "e" => static function(int $a): void{ echo $a;},
  134. "d" => [
  135. [2 => static function(int $a): void{ echo $a;}]
  136. ]
  137. ];
  138. $a["e"](1,);
  139. $a["d"][0][2](1,);
  140. $z = new class { public static function b(int $a): void {echo $a; }};
  141. $z::b(1,);
  142. ${$e}(1,);
  143. $$e(2,);
  144. $f(0,)(1,);
  145. $g["e"](1,); // foo',
  146. ];
  147. yield 'do not fix' => [
  148. '<?php
  149. function someFunction ($p1){}
  150. function & foo($a,$b): array { return []; }
  151. foo (
  152. 1,
  153. 2,
  154. );
  155. $a = new class (
  156. $a,
  157. ) {};
  158. isset($a, $b);
  159. unset($a,$b);
  160. list($a,$b) = $a;
  161. $a = [1,2,3,];
  162. $a = array(1,2,3,);
  163. function foo1(string $param = null ): void
  164. {
  165. }
  166. ;',
  167. ];
  168. }
  169. /**
  170. * @dataProvider provideFix80Cases
  171. *
  172. * @requires PHP 8.0
  173. */
  174. public function testFix80(string $expected, ?string $input = null): void
  175. {
  176. $this->doTest($expected, $input);
  177. }
  178. /**
  179. * @return iterable<array{string}>
  180. */
  181. public static function provideFix80Cases(): iterable
  182. {
  183. yield [
  184. '<?php function foo(
  185. #[MyAttr(1, 2,)] Type $myParam,
  186. ) {}
  187. $foo1b = function() use ($bar, ) {};
  188. ',
  189. ];
  190. }
  191. /**
  192. * @dataProvider provideFix81Cases
  193. *
  194. * @requires PHP 8.1
  195. */
  196. public function testFix81(string $expected, ?string $input = null): void
  197. {
  198. $this->doTest($expected, $input);
  199. }
  200. /**
  201. * @return iterable<array{string, string}>
  202. */
  203. public static function provideFix81Cases(): iterable
  204. {
  205. yield [
  206. '<?php $object?->method(1); strlen(...);',
  207. '<?php $object?->method(1,); strlen(...);',
  208. ];
  209. }
  210. }