NoSpacesAfterFunctionNameFixerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. final class NoSpacesAfterFunctionNameFixerTest 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. public static function provideFixCases(): iterable
  31. {
  32. yield 'test function call' => [
  33. '<?php abc($a);',
  34. '<?php abc ($a);',
  35. ];
  36. yield 'test method call' => [
  37. '<?php $o->abc($a);',
  38. '<?php $o->abc ($a);',
  39. ];
  40. yield 'test function-like constructs' => [
  41. '<?php
  42. include("something.php");
  43. include_once("something.php");
  44. require("something.php");
  45. require_once("something.php");
  46. print("hello");
  47. unset($hello);
  48. isset($hello);
  49. empty($hello);
  50. die($hello);
  51. echo("hello");
  52. array("hello");
  53. list($a, $b) = $c;
  54. eval("a");
  55. foo();
  56. $foo = &ref();
  57. ',
  58. '<?php
  59. include ("something.php");
  60. include_once ("something.php");
  61. require ("something.php");
  62. require_once ("something.php");
  63. print ("hello");
  64. unset ($hello);
  65. isset ($hello);
  66. empty ($hello);
  67. die ($hello);
  68. echo ("hello");
  69. array ("hello");
  70. list ($a, $b) = $c;
  71. eval ("a");
  72. foo ();
  73. $foo = &ref ();
  74. ',
  75. ];
  76. yield [
  77. '<?php echo foo(1) ? "y" : "n";',
  78. '<?php echo foo (1) ? "y" : "n";',
  79. ];
  80. yield [
  81. '<?php echo isset($name) ? "y" : "n";',
  82. '<?php echo isset ($name) ? "y" : "n";',
  83. ];
  84. yield [
  85. '<?php include (isHtml())? "1.html": "1.php";',
  86. '<?php include (isHtml ())? "1.html": "1.php";',
  87. ];
  88. // skip other language constructs
  89. yield [
  90. '<?php $a = 2 * (1 + 1);',
  91. ];
  92. yield [
  93. '<?php echo ($a == $b) ? "foo" : "bar";',
  94. ];
  95. yield [
  96. '<?php echo ($a == test($b)) ? "foo" : "bar";',
  97. ];
  98. yield [
  99. '<?php include ($html)? "custom.html": "custom.php";',
  100. ];
  101. yield 'don\'t touch function declarations' => [
  102. '<?php
  103. function TisMy ($p1)
  104. {
  105. print $p1;
  106. }
  107. ',
  108. ];
  109. yield [
  110. '<?php class A {
  111. function TisMy ($p1)
  112. {
  113. print $p1;
  114. }
  115. }',
  116. ];
  117. yield 'test dynamic by array' => [
  118. '<?php $a["e"](1); $a[2](1);',
  119. '<?php $a["e"] (1); $a[2] (1);',
  120. ];
  121. yield 'test variable variable' => [
  122. '<?php
  123. ${$e}(1);
  124. $$e(2);
  125. ',
  126. "<?php
  127. \${\$e}\t(1);
  128. \$\$e (2);
  129. ",
  130. ];
  131. yield 'test dynamic function and method calls' => [
  132. '<?php $b->$a(); $c();',
  133. '<?php $b->$a (); $c ();',
  134. ];
  135. yield 'test function call comment' => [
  136. '<?php abc#
  137. ($a);',
  138. ];
  139. yield [
  140. '<?php echo (new Process())->getOutput();',
  141. '<?php echo (new Process())->getOutput ();',
  142. ];
  143. yield [
  144. '<?php $a()(1);',
  145. '<?php $a () (1);',
  146. ];
  147. }
  148. /**
  149. * @dataProvider provideFixPre80Cases
  150. *
  151. * @requires PHP <8.0
  152. */
  153. public function testFixPre80(string $expected, string $input = null): void
  154. {
  155. $this->doTest($expected, $input);
  156. }
  157. public static function provideFixPre80Cases(): iterable
  158. {
  159. yield 'test dynamic by array, curly mix' => [
  160. '<?php $a["e"](1); $a{2}(1);',
  161. '<?php $a["e"] (1); $a{2} (1);',
  162. ];
  163. yield 'test dynamic by array, curly only' => [
  164. '<?php $a{"e"}(1); $a{2}(1);',
  165. '<?php $a{"e"} (1); $a{2} (1);',
  166. ];
  167. }
  168. /**
  169. * @dataProvider provideFix81Cases
  170. *
  171. * @requires PHP 8.1
  172. */
  173. public function testFix81(string $expected, ?string $input = null): void
  174. {
  175. $this->doTest($expected, $input);
  176. }
  177. public static function provideFix81Cases(): iterable
  178. {
  179. yield [
  180. '<?php strlen(...);',
  181. '<?php strlen (...);',
  182. ];
  183. }
  184. }