NoSpacesAfterFunctionNameFixerTest.php 4.9 KB

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