NoSpacesAfterFunctionNameFixerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Varga Bence <vbence@czentral.org>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\FunctionNotation\NoSpacesAfterFunctionNameFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoSpacesAfterFunctionNameFixer>
  22. */
  23. final class NoSpacesAfterFunctionNameFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'test function call' => [
  38. '<?php abc($a);',
  39. '<?php abc ($a);',
  40. ];
  41. yield 'test method call' => [
  42. '<?php $o->abc($a);',
  43. '<?php $o->abc ($a);',
  44. ];
  45. yield 'test function-like constructs' => [
  46. '<?php
  47. include("something.php");
  48. include_once("something.php");
  49. require("something.php");
  50. require_once("something.php");
  51. print("hello");
  52. unset($hello);
  53. isset($hello);
  54. empty($hello);
  55. die($hello);
  56. echo("hello");
  57. array("hello");
  58. list($a, $b) = $c;
  59. eval("a");
  60. foo();
  61. $foo = &ref();
  62. ',
  63. '<?php
  64. include ("something.php");
  65. include_once ("something.php");
  66. require ("something.php");
  67. require_once ("something.php");
  68. print ("hello");
  69. unset ($hello);
  70. isset ($hello);
  71. empty ($hello);
  72. die ($hello);
  73. echo ("hello");
  74. array ("hello");
  75. list ($a, $b) = $c;
  76. eval ("a");
  77. foo ();
  78. $foo = &ref ();
  79. ',
  80. ];
  81. yield [
  82. '<?php echo foo(1) ? "y" : "n";',
  83. '<?php echo foo (1) ? "y" : "n";',
  84. ];
  85. yield [
  86. '<?php echo isset($name) ? "y" : "n";',
  87. '<?php echo isset ($name) ? "y" : "n";',
  88. ];
  89. yield [
  90. '<?php include (isHtml())? "1.html": "1.php";',
  91. '<?php include (isHtml ())? "1.html": "1.php";',
  92. ];
  93. // skip other language constructs
  94. yield [
  95. '<?php $a = 2 * (1 + 1);',
  96. ];
  97. yield [
  98. '<?php echo ($a == $b) ? "foo" : "bar";',
  99. ];
  100. yield [
  101. '<?php echo ($a == test($b)) ? "foo" : "bar";',
  102. ];
  103. yield [
  104. '<?php include ($html)? "custom.html": "custom.php";',
  105. ];
  106. yield 'don\'t touch function declarations' => [
  107. '<?php
  108. function TisMy ($p1)
  109. {
  110. print $p1;
  111. }
  112. ',
  113. ];
  114. yield [
  115. '<?php class A {
  116. function TisMy ($p1)
  117. {
  118. print $p1;
  119. }
  120. }',
  121. ];
  122. yield 'test dynamic by array' => [
  123. '<?php $a["e"](1); $a[2](1);',
  124. '<?php $a["e"] (1); $a[2] (1);',
  125. ];
  126. yield 'test variable variable' => [
  127. '<?php
  128. ${$e}(1);
  129. $$e(2);
  130. ',
  131. "<?php
  132. \${\$e}\t(1);
  133. \$\$e (2);
  134. ",
  135. ];
  136. yield 'test dynamic function and method calls' => [
  137. '<?php $b->$a(); $c();',
  138. '<?php $b->$a (); $c ();',
  139. ];
  140. yield 'test function call comment' => [
  141. '<?php abc#
  142. ($a);',
  143. ];
  144. yield [
  145. '<?php echo (new Process())->getOutput();',
  146. '<?php echo (new Process())->getOutput ();',
  147. ];
  148. yield [
  149. '<?php $a()(1);',
  150. '<?php $a () (1);',
  151. ];
  152. yield [
  153. '<?php
  154. echo (function () {})();
  155. echo ($propertyValue ? "TRUE" : "FALSE") . EOL;
  156. echo(FUNCTION_1);
  157. echo (EXPRESSION + CONST_1), CONST_2;
  158. ',
  159. '<?php
  160. echo (function () {})();
  161. echo ($propertyValue ? "TRUE" : "FALSE") . EOL;
  162. echo (FUNCTION_1);
  163. echo (EXPRESSION + CONST_1), CONST_2;
  164. ',
  165. ];
  166. yield [
  167. '<?php
  168. include(SOME_PATH_1);
  169. include_once(SOME_PATH_2);
  170. require(SOME_PATH_3);
  171. require_once(SOME_PATH_4);
  172. print(SOME_VALUE);
  173. print(FIRST_HALF_OF_STRING_1 . SECOND_HALF_OF_STRING_1);
  174. print((FIRST_HALF_OF_STRING_2) . (SECOND_HALF_OF_STRING_2));
  175. ',
  176. '<?php
  177. include (SOME_PATH_1);
  178. include_once (SOME_PATH_2);
  179. require (SOME_PATH_3);
  180. require_once (SOME_PATH_4);
  181. print (SOME_VALUE);
  182. print (FIRST_HALF_OF_STRING_1 . SECOND_HALF_OF_STRING_1);
  183. print ((FIRST_HALF_OF_STRING_2) . (SECOND_HALF_OF_STRING_2));
  184. ',
  185. ];
  186. yield [
  187. '<?php
  188. include (DIR) . FILENAME_1;
  189. include_once (DIR) . (FILENAME_2);
  190. require (DIR) . FILENAME_3;
  191. require_once (DIR) . (FILENAME_4);
  192. print (FIRST_HALF_OF_STRING_1) . SECOND_HALF_OF_STRING_1;
  193. print (FIRST_HALF_OF_STRING_2) . ((((SECOND_HALF_OF_STRING_2))));
  194. print ((FIRST_HALF_OF_STRING_3)) . ((SECOND_HALF_OF_STRING_3));
  195. print ((((FIRST_HALF_OF_STRING_4)))) . ((((SECOND_HALF_OF_STRING_4))));
  196. ',
  197. ];
  198. }
  199. /**
  200. * @dataProvider provideFixPre80Cases
  201. *
  202. * @requires PHP <8.0
  203. */
  204. public function testFixPre80(string $expected, ?string $input = null): void
  205. {
  206. $this->doTest($expected, $input);
  207. }
  208. /**
  209. * @return iterable<string, array{string, string}>
  210. */
  211. public static function provideFixPre80Cases(): iterable
  212. {
  213. yield 'test dynamic by array, curly mix' => [
  214. '<?php $a["e"](1); $a{2}(1);',
  215. '<?php $a["e"] (1); $a{2} (1);',
  216. ];
  217. yield 'test dynamic by array, curly only' => [
  218. '<?php $a{"e"}(1); $a{2}(1);',
  219. '<?php $a{"e"} (1); $a{2} (1);',
  220. ];
  221. }
  222. /**
  223. * @dataProvider provideFix81Cases
  224. *
  225. * @requires PHP 8.1
  226. */
  227. public function testFix81(string $expected, ?string $input = null): void
  228. {
  229. $this->doTest($expected, $input);
  230. }
  231. /**
  232. * @return iterable<array{string, string}>
  233. */
  234. public static function provideFix81Cases(): iterable
  235. {
  236. yield [
  237. '<?php strlen(...);',
  238. '<?php strlen (...);',
  239. ];
  240. }
  241. }