TypeDeclarationSpacesFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. * @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer
  25. */
  26. final class TypeDeclarationSpacesFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @dataProvider provideFixCases
  30. */
  31. public function testFix(string $expected, ?string $input = null): void
  32. {
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<array{string, 1?: ?string}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. yield [
  41. '<?php function foo(bool /**bla bla*/$param) {}',
  42. '<?php function foo(bool/**bla bla*/$param) {}',
  43. ];
  44. yield [
  45. '<?php function foo(bool /**bla bla*/$param) {}',
  46. '<?php function foo(bool /**bla bla*/$param) {}',
  47. ];
  48. yield [
  49. '<?php function foo(callable $param) {}',
  50. '<?php function foo(callable$param) {}',
  51. ];
  52. yield [
  53. '<?php function foo(callable $param) {}',
  54. '<?php function foo(callable $param) {}',
  55. ];
  56. yield [
  57. '<?php function foo(array &$param) {}',
  58. '<?php function foo(array&$param) {}',
  59. ];
  60. yield [
  61. '<?php function foo(array &$param) {}',
  62. '<?php function foo(array &$param) {}',
  63. ];
  64. yield [
  65. '<?php function foo(array & $param) {}',
  66. '<?php function foo(array& $param) {}',
  67. ];
  68. yield [
  69. '<?php function foo(array & $param) {}',
  70. '<?php function foo(array & $param) {}',
  71. ];
  72. yield [
  73. '<?php function foo(Bar $param) {}',
  74. '<?php function foo(Bar$param) {}',
  75. ];
  76. yield [
  77. '<?php function foo(Bar $param) {}',
  78. '<?php function foo(Bar $param) {}',
  79. ];
  80. yield [
  81. '<?php function foo(Bar\Baz $param) {}',
  82. '<?php function foo(Bar\Baz$param) {}',
  83. ];
  84. yield [
  85. '<?php function foo(Bar\Baz $param) {}',
  86. '<?php function foo(Bar\Baz $param) {}',
  87. ];
  88. yield [
  89. '<?php function foo(Bar\Baz &$param) {}',
  90. '<?php function foo(Bar\Baz&$param) {}',
  91. ];
  92. yield [
  93. '<?php function foo(Bar\Baz &$param) {}',
  94. '<?php function foo(Bar\Baz &$param) {}',
  95. ];
  96. yield [
  97. '<?php function foo(Bar\Baz & $param) {}',
  98. '<?php function foo(Bar\Baz& $param) {}',
  99. ];
  100. yield [
  101. '<?php function foo(Bar\Baz & $param) {}',
  102. '<?php function foo(Bar\Baz & $param) {}',
  103. ];
  104. yield [
  105. '<?php $foo = function(Bar\Baz $param) {};',
  106. '<?php $foo = function(Bar\Baz$param) {};',
  107. ];
  108. yield [
  109. '<?php $foo = function(Bar\Baz $param) {};',
  110. '<?php $foo = function(Bar\Baz $param) {};',
  111. ];
  112. yield [
  113. '<?php $foo = function(Bar\Baz &$param) {};',
  114. '<?php $foo = function(Bar\Baz&$param) {};',
  115. ];
  116. yield [
  117. '<?php $foo = function(Bar\Baz &$param) {};',
  118. '<?php $foo = function(Bar\Baz &$param) {};',
  119. ];
  120. yield [
  121. '<?php $foo = function(Bar\Baz & $param) {};',
  122. '<?php $foo = function(Bar\Baz& $param) {};',
  123. ];
  124. yield [
  125. '<?php $foo = function(Bar\Baz & $param) {};',
  126. '<?php $foo = function(Bar\Baz & $param) {};',
  127. ];
  128. yield [
  129. '<?php class Test { public function foo(Bar\Baz $param) {} }',
  130. '<?php class Test { public function foo(Bar\Baz$param) {} }',
  131. ];
  132. yield [
  133. '<?php class Test { public function foo(Bar\Baz $param) {} }',
  134. '<?php class Test { public function foo(Bar\Baz $param) {} }',
  135. ];
  136. yield [
  137. '<?php $foo = function(array $a,
  138. array $b, array $c, array $d) {};',
  139. '<?php $foo = function(array $a,
  140. array$b, array $c, array
  141. $d) {};',
  142. ];
  143. yield [
  144. '<?php $foo = fn(Bar\Baz $param) => null;',
  145. '<?php $foo = fn(Bar\Baz$param) => null;',
  146. ];
  147. yield [
  148. '<?php $foo = fn(Bar\Baz $param) => null;',
  149. '<?php $foo = fn(Bar\Baz $param) => null;',
  150. ];
  151. yield [
  152. '<?php $foo = fn(Bar\Baz &$param) => null;',
  153. '<?php $foo = fn(Bar\Baz&$param) => null;',
  154. ];
  155. yield [
  156. '<?php $foo = fn(Bar\Baz &$param) => null;',
  157. '<?php $foo = fn(Bar\Baz &$param) => null;',
  158. ];
  159. yield [
  160. '<?php $foo = fn(Bar\Baz & $param) => null;',
  161. '<?php $foo = fn(Bar\Baz& $param) => null;',
  162. ];
  163. yield [
  164. '<?php $foo = fn(Bar\Baz & $param) => null;',
  165. '<?php $foo = fn(Bar\Baz & $param) => null;',
  166. ];
  167. yield [
  168. '<?php $foo = fn(array $a,
  169. array $b, array $c, array $d) => null;',
  170. '<?php $foo = fn(array $a,
  171. array$b, array $c, array
  172. $d) => null;',
  173. ];
  174. yield [
  175. '<?php function foo(array ...$param) {}',
  176. '<?php function foo(array...$param) {}',
  177. ];
  178. yield [
  179. '<?php function foo(array & ...$param) {}',
  180. '<?php function foo(array& ...$param) {}',
  181. ];
  182. yield [
  183. '<?php class Foo { public int $x; }',
  184. '<?php class Foo { public int$x; }',
  185. ];
  186. yield [
  187. '<?php class Foo { public bool $x; }',
  188. '<?php class Foo { public bool $x; }',
  189. ];
  190. yield [
  191. '<?php class Foo { protected \Bar\Baz $c; }',
  192. '<?php class Foo { protected \Bar\Baz$c; }',
  193. ];
  194. yield [
  195. '<?php class Foo { protected \Bar\Baz $c; }',
  196. '<?php class Foo { protected \Bar\Baz $c; }',
  197. ];
  198. yield [
  199. '<?php class Foo { private array $x; }',
  200. '<?php class Foo { private array$x; }',
  201. ];
  202. yield [
  203. '<?php class Foo { private array $x; }',
  204. '<?php class Foo { private array
  205. $x; }',
  206. ];
  207. yield [
  208. '<?php
  209. class Point
  210. {
  211. public \DateTime $x;
  212. protected bool $y = true;
  213. private array $z = [];
  214. public int $a = 0;
  215. protected string $b = \'\';
  216. private float $c = 0.0;
  217. }
  218. ',
  219. '<?php
  220. class Point
  221. {
  222. public \DateTime $x;
  223. protected bool $y = true;
  224. private array $z = [];
  225. public int $a = 0;
  226. protected string $b = \'\';
  227. private float $c = 0.0;
  228. }
  229. ',
  230. ];
  231. yield [
  232. '<?php function foo($param) {}',
  233. ];
  234. yield [
  235. '<?php function foo($param1,$param2) {}',
  236. ];
  237. yield [
  238. '<?php function foo(&$param) {}',
  239. ];
  240. yield [
  241. '<?php function foo(& $param) {}',
  242. ];
  243. yield [
  244. '<?php function foo(/**int*/$param) {}',
  245. ];
  246. yield [
  247. '<?php function foo(bool /**bla bla*/ $param) {}',
  248. ];
  249. yield [
  250. '<?php $foo = function(
  251. array $a,
  252. $b
  253. ) {};',
  254. ];
  255. yield [
  256. '<?php $foo = function(
  257. $a,
  258. array $b
  259. ) {};',
  260. ];
  261. yield [
  262. '<?php function foo(...$param) {}',
  263. ];
  264. yield [
  265. '<?php function foo(&...$param) {}',
  266. ];
  267. yield [
  268. '<?php use function some\test\{fn_a, fn_b, fn_c};',
  269. ];
  270. yield [
  271. '<?php use function some\test\{fn_a, fn_b, fn_c} ?>',
  272. ];
  273. yield [
  274. '<?php $foo = fn(
  275. array $a,
  276. $b
  277. ) => null;',
  278. ];
  279. yield [
  280. '<?php $foo = fn(
  281. $a,
  282. array $b
  283. ) => null;',
  284. ];
  285. yield [
  286. '<?php class Foo { public $p; }',
  287. ];
  288. yield [
  289. '<?php class Foo { protected /* int */ $a; }',
  290. ];
  291. yield [
  292. '<?php class Foo { private int $a; }',
  293. ];
  294. }
  295. /**
  296. * @dataProvider provideFix80Cases
  297. *
  298. * @requires PHP 8.0
  299. */
  300. public function testFix80(string $expected, string $input): void
  301. {
  302. $this->doTest($expected, $input);
  303. }
  304. /**
  305. * @return iterable<array{string, 1?: ?string}>
  306. */
  307. public static function provideFix80Cases(): iterable
  308. {
  309. yield [
  310. '<?php function foo(mixed $a) {}',
  311. '<?php function foo(mixed$a) {}',
  312. ];
  313. yield [
  314. '<?php function foo(mixed $a) {}',
  315. '<?php function foo(mixed $a) {}',
  316. ];
  317. yield [
  318. '<?php
  319. class Foo
  320. {
  321. public function __construct(
  322. public int $a,
  323. protected bool $b,
  324. private Bar\Baz $c,
  325. ) {}
  326. }
  327. ',
  328. '<?php
  329. class Foo
  330. {
  331. public function __construct(
  332. public int $a,
  333. protected bool$b,
  334. private Bar\Baz $c,
  335. ) {}
  336. }
  337. ',
  338. ];
  339. }
  340. /**
  341. * @dataProvider provideFix81Cases
  342. *
  343. * @requires PHP 8.1
  344. */
  345. public function testFix81(string $expected, ?string $input = null): void
  346. {
  347. $this->doTest($expected, $input);
  348. }
  349. /**
  350. * @return iterable<array{string, 1?: ?string}>
  351. */
  352. public static function provideFix81Cases(): iterable
  353. {
  354. yield [
  355. '<?php class Foo { private readonly int $bar; }',
  356. '<?php class Foo { private readonly int$bar; }',
  357. ];
  358. yield [
  359. '<?php class Foo { private readonly int $bar; }',
  360. '<?php class Foo { private readonly int $bar; }',
  361. ];
  362. }
  363. /**
  364. * @dataProvider provideFix82Cases
  365. *
  366. * @requires PHP 8.2
  367. */
  368. public function testFix82(string $expected, ?string $input = null): void
  369. {
  370. $this->doTest($expected, $input);
  371. }
  372. /**
  373. * @return iterable<array{string, 1?: ?string}>
  374. */
  375. public static function provideFix82Cases(): iterable
  376. {
  377. yield [
  378. '<?php class Foo { public (A&B)|C $bar; }',
  379. '<?php class Foo { public (A&B)|C$bar; }',
  380. ];
  381. yield [
  382. '<?php class Foo { public (A&B)|C $bar; }',
  383. '<?php class Foo { public (A&B)|C $bar; }',
  384. ];
  385. }
  386. }