FunctionTypehintSpaceFixerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer>
  22. */
  23. final class FunctionTypehintSpaceFixerTest 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<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php function foo($param) {}',
  39. ];
  40. yield [
  41. '<?php function foo($param1,$param2) {}',
  42. ];
  43. yield [
  44. '<?php function foo(&$param) {}',
  45. ];
  46. yield [
  47. '<?php function foo(& $param) {}',
  48. ];
  49. yield [
  50. '<?php function foo(/**int*/$param) {}',
  51. ];
  52. yield [
  53. '<?php function foo(bool /**bla bla*/ $param) {}',
  54. ];
  55. yield [
  56. '<?php function foo(bool /**bla bla*/$param) {}',
  57. '<?php function foo(bool/**bla bla*/$param) {}',
  58. ];
  59. yield [
  60. '<?php function foo(bool /**bla bla*/$param) {}',
  61. '<?php function foo(bool /**bla bla*/$param) {}',
  62. ];
  63. yield [
  64. '<?php function foo(callable $param) {}',
  65. '<?php function foo(callable$param) {}',
  66. ];
  67. yield [
  68. '<?php function foo(callable $param) {}',
  69. '<?php function foo(callable $param) {}',
  70. ];
  71. yield [
  72. '<?php function foo(array &$param) {}',
  73. '<?php function foo(array&$param) {}',
  74. ];
  75. yield [
  76. '<?php function foo(array &$param) {}',
  77. '<?php function foo(array &$param) {}',
  78. ];
  79. yield [
  80. '<?php function foo(array & $param) {}',
  81. '<?php function foo(array& $param) {}',
  82. ];
  83. yield [
  84. '<?php function foo(array & $param) {}',
  85. '<?php function foo(array & $param) {}',
  86. ];
  87. yield [
  88. '<?php function foo(Bar $param) {}',
  89. '<?php function foo(Bar$param) {}',
  90. ];
  91. yield [
  92. '<?php function foo(Bar $param) {}',
  93. '<?php function foo(Bar $param) {}',
  94. ];
  95. yield [
  96. '<?php function foo(Bar\Baz $param) {}',
  97. '<?php function foo(Bar\Baz$param) {}',
  98. ];
  99. yield [
  100. '<?php function foo(Bar\Baz $param) {}',
  101. '<?php function foo(Bar\Baz $param) {}',
  102. ];
  103. yield [
  104. '<?php function foo(Bar\Baz &$param) {}',
  105. '<?php function foo(Bar\Baz&$param) {}',
  106. ];
  107. yield [
  108. '<?php function foo(Bar\Baz &$param) {}',
  109. '<?php function foo(Bar\Baz &$param) {}',
  110. ];
  111. yield [
  112. '<?php function foo(Bar\Baz & $param) {}',
  113. '<?php function foo(Bar\Baz& $param) {}',
  114. ];
  115. yield [
  116. '<?php function foo(Bar\Baz & $param) {}',
  117. '<?php function foo(Bar\Baz & $param) {}',
  118. ];
  119. yield [
  120. '<?php $foo = function(Bar\Baz $param) {};',
  121. '<?php $foo = function(Bar\Baz$param) {};',
  122. ];
  123. yield [
  124. '<?php $foo = function(Bar\Baz $param) {};',
  125. '<?php $foo = function(Bar\Baz $param) {};',
  126. ];
  127. yield [
  128. '<?php $foo = function(Bar\Baz &$param) {};',
  129. '<?php $foo = function(Bar\Baz&$param) {};',
  130. ];
  131. yield [
  132. '<?php $foo = function(Bar\Baz &$param) {};',
  133. '<?php $foo = function(Bar\Baz &$param) {};',
  134. ];
  135. yield [
  136. '<?php $foo = function(Bar\Baz & $param) {};',
  137. '<?php $foo = function(Bar\Baz& $param) {};',
  138. ];
  139. yield [
  140. '<?php $foo = function(Bar\Baz & $param) {};',
  141. '<?php $foo = function(Bar\Baz & $param) {};',
  142. ];
  143. yield [
  144. '<?php class Test { public function foo(Bar\Baz $param) {} }',
  145. '<?php class Test { public function foo(Bar\Baz$param) {} }',
  146. ];
  147. yield [
  148. '<?php class Test { public function foo(Bar\Baz $param) {} }',
  149. '<?php class Test { public function foo(Bar\Baz $param) {} }',
  150. ];
  151. yield [
  152. '<?php $foo = function(array $a,
  153. array $b, array $c, array $d) {};',
  154. '<?php $foo = function(array $a,
  155. array$b, array $c, array
  156. $d) {};',
  157. ];
  158. yield [
  159. '<?php $foo = function(
  160. array $a,
  161. $b
  162. ) {};',
  163. ];
  164. yield [
  165. '<?php $foo = function(
  166. $a,
  167. array $b
  168. ) {};',
  169. ];
  170. yield [
  171. '<?php function foo(...$param) {}',
  172. ];
  173. yield [
  174. '<?php function foo(&...$param) {}',
  175. ];
  176. yield [
  177. '<?php function foo(array ...$param) {}',
  178. '<?php function foo(array...$param) {}',
  179. ];
  180. yield [
  181. '<?php function foo(array & ...$param) {}',
  182. '<?php function foo(array& ...$param) {}',
  183. ];
  184. yield ['<?php use function some\test\{fn_a, fn_b, fn_c};'];
  185. yield ['<?php use function some\test\{fn_a, fn_b, fn_c} ?>'];
  186. yield [
  187. '<?php $foo = fn(Bar\Baz $param) => null;',
  188. '<?php $foo = fn(Bar\Baz$param) => null;',
  189. ];
  190. yield [
  191. '<?php $foo = fn(Bar\Baz $param) => null;',
  192. '<?php $foo = fn(Bar\Baz $param) => null;',
  193. ];
  194. yield [
  195. '<?php $foo = fn(Bar\Baz &$param) => null;',
  196. '<?php $foo = fn(Bar\Baz&$param) => null;',
  197. ];
  198. yield [
  199. '<?php $foo = fn(Bar\Baz &$param) => null;',
  200. '<?php $foo = fn(Bar\Baz &$param) => null;',
  201. ];
  202. yield [
  203. '<?php $foo = fn(Bar\Baz & $param) => null;',
  204. '<?php $foo = fn(Bar\Baz& $param) => null;',
  205. ];
  206. yield [
  207. '<?php $foo = fn(Bar\Baz & $param) => null;',
  208. '<?php $foo = fn(Bar\Baz & $param) => null;',
  209. ];
  210. yield [
  211. '<?php $foo = fn(array $a,
  212. array $b, array $c, array $d) => null;',
  213. '<?php $foo = fn(array $a,
  214. array$b, array $c, array
  215. $d) => null;',
  216. ];
  217. yield [
  218. '<?php $foo = fn(
  219. array $a,
  220. $b
  221. ) => null;',
  222. ];
  223. yield [
  224. '<?php $foo = fn(
  225. $a,
  226. array $b
  227. ) => null;',
  228. ];
  229. }
  230. /**
  231. * @dataProvider provideFix80Cases
  232. *
  233. * @requires PHP 8.0
  234. */
  235. public function testFix80(string $expected, string $input): void
  236. {
  237. $this->doTest($expected, $input);
  238. }
  239. /**
  240. * @return iterable<array{string, string}>
  241. */
  242. public static function provideFix80Cases(): iterable
  243. {
  244. yield [
  245. '<?php function foo(mixed $a){}',
  246. '<?php function foo(mixed$a){}',
  247. ];
  248. }
  249. }