TypesSpacesFixerTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer
  22. */
  23. final class TypesSpacesFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param _AutogeneratedInputConfiguration $configuration
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  31. {
  32. $this->fixer->configure($configuration);
  33. $this->doTest($expected, $input);
  34. }
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php try {} catch (ErrorA|ErrorB $e) {}',
  39. '<?php try {} catch (ErrorA | ErrorB $e) {}',
  40. ];
  41. yield [
  42. '<?php try {} catch (ErrorA|ErrorB $e) {}',
  43. '<?php try {} catch (ErrorA | ErrorB $e) {}',
  44. ];
  45. yield [
  46. '<?php try {} catch (ErrorA | ErrorB $e) {}',
  47. '<?php try {} catch (ErrorA|ErrorB $e) {}',
  48. ['space' => 'single'],
  49. ];
  50. yield [
  51. '<?php try {} catch (ErrorA | ErrorB $e) {}',
  52. '<?php try {} catch (ErrorA | ErrorB $e) {}',
  53. ['space' => 'single'],
  54. ];
  55. yield [
  56. '<?php
  57. try {} catch (ErrorA | ErrorB $e) {}
  58. try {} catch (ErrorA | ErrorB $e) {}
  59. try {} catch (ErrorA | ErrorB $e) {}
  60. try {} catch (ErrorA | ErrorB $e) {}
  61. try {} catch (ErrorA | ErrorB $e) {}
  62. try {} catch (ErrorA | ErrorB $e) {}
  63. try {} catch (ErrorA | ErrorB $e) {}
  64. try {} catch (ErrorA | ErrorB $e) {}
  65. try {} catch (ErrorA | ErrorB $e) {}
  66. try {} catch (ErrorA | ErrorB $e) {}
  67. try {} catch (ErrorA | ErrorB $e) {}
  68. ',
  69. '<?php
  70. try {} catch (ErrorA|ErrorB $e) {}
  71. try {} catch (ErrorA|ErrorB $e) {}
  72. try {} catch (ErrorA|ErrorB $e) {}
  73. try {} catch (ErrorA|ErrorB $e) {}
  74. try {} catch (ErrorA|ErrorB $e) {}
  75. try {} catch (ErrorA|ErrorB $e) {}
  76. try {} catch (ErrorA|ErrorB $e) {}
  77. try {} catch (ErrorA|ErrorB $e) {}
  78. try {} catch (ErrorA|ErrorB $e) {}
  79. try {} catch (ErrorA|ErrorB $e) {}
  80. try {} catch (ErrorA|ErrorB $e) {}
  81. ',
  82. ['space' => 'single'],
  83. ];
  84. }
  85. /**
  86. * @param _AutogeneratedInputConfiguration $configuration
  87. *
  88. * @dataProvider provideFix80Cases
  89. *
  90. * @requires PHP 8.0
  91. */
  92. public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
  93. {
  94. $this->fixer->configure($configuration);
  95. $this->doTest($expected, $input);
  96. }
  97. public static function provideFix80Cases(): iterable
  98. {
  99. yield [
  100. '<?php function foo(TypeA|TypeB $x) {}',
  101. '<?php function foo(TypeA | TypeB $x) {}',
  102. ];
  103. yield [
  104. '<?php function foo(TypeA|TypeB|TypeC|TypeD $x, TypeE|TypeF $y, TypeA|TypeB $z) {}',
  105. '<?php function foo(TypeA | TypeB | TypeC | TypeD $x, TypeE | TypeF $y, TypeA| TypeB $z) {}',
  106. ];
  107. yield [
  108. '<?php function foo(): TypeA|TypeB {}',
  109. '<?php function foo(): TypeA | TypeB {}',
  110. ];
  111. yield [
  112. '<?php class Foo { private array|int $x; }',
  113. '<?php class Foo { private array | int $x; }',
  114. ];
  115. yield [
  116. '<?php function foo(TypeA
  117. |
  118. TypeB $x) {}',
  119. ];
  120. yield [
  121. '<?php function foo(TypeA
  122. |
  123. TypeB $x) {}',
  124. null,
  125. ['space' => 'single'],
  126. ];
  127. yield [
  128. '<?php function foo(TypeA/* not a space */|/* not a space */TypeB $x) {}',
  129. ];
  130. yield [
  131. '<?php function foo(TypeA/* not a space */ | /* not a space */TypeB $x) {}',
  132. '<?php function foo(TypeA/* not a space */|/* not a space */TypeB $x) {}',
  133. ['space' => 'single'],
  134. ];
  135. yield [
  136. '<?php function foo(TypeA// not a space
  137. |//not a space
  138. TypeB $x) {}',
  139. ];
  140. yield [
  141. '<?php function foo(TypeA// not a space
  142. | //not a space
  143. TypeB $x) {}',
  144. '<?php function foo(TypeA// not a space
  145. |//not a space
  146. TypeB $x) {}',
  147. ['space' => 'single'],
  148. ];
  149. yield [
  150. '<?php class Foo {
  151. public function __construct(
  152. public int|string $a,
  153. protected int|string $b,
  154. private int|string $c
  155. ) {}
  156. }',
  157. '<?php class Foo {
  158. public function __construct(
  159. public int | string $a,
  160. protected int | string $b,
  161. private int | string $c
  162. ) {}
  163. }',
  164. ];
  165. yield [
  166. '<?php
  167. function foo(TypeA | TypeB $x) {}
  168. try {} catch (ErrorA|ErrorB $e) {}
  169. ',
  170. '<?php
  171. function foo(TypeA |TypeB $x) {}
  172. try {} catch (ErrorA| ErrorB $e) {}
  173. ',
  174. [
  175. 'space' => 'single',
  176. 'space_multiple_catch' => 'none',
  177. ],
  178. ];
  179. yield [
  180. '<?php
  181. function foo(TypeA|TypeB $x) {}
  182. try {} catch (ErrorA | ErrorB $e) {}
  183. ',
  184. '<?php
  185. function foo(TypeA | TypeB $x) {}
  186. try {} catch (ErrorA|ErrorB $e) {}
  187. ',
  188. [
  189. 'space' => 'none',
  190. 'space_multiple_catch' => 'single',
  191. ],
  192. ];
  193. yield [
  194. '<?php
  195. function foo(TypeA|TypeB $x) {}
  196. try {} catch (ErrorA|ErrorB $e) {}
  197. ',
  198. '<?php
  199. function foo(TypeA| TypeB $x) {}
  200. try {} catch (ErrorA |ErrorB $e) {}
  201. ',
  202. [
  203. 'space' => 'none',
  204. 'space_multiple_catch' => 'none',
  205. ],
  206. ];
  207. yield [
  208. '<?php
  209. function foo(TypeA | TypeB $x) {}
  210. try {} catch (ErrorA | ErrorB $e) {}
  211. ',
  212. '<?php
  213. function foo(TypeA |TypeB $x) {}
  214. try {} catch (ErrorA|ErrorB $e) {}
  215. ',
  216. [
  217. 'space' => 'single',
  218. 'space_multiple_catch' => 'single',
  219. ],
  220. ];
  221. yield [
  222. '<?php
  223. function foo(TypeA | TypeB $x) {}
  224. try {} catch (ErrorA | ErrorB $e) {}
  225. ',
  226. '<?php
  227. function foo(TypeA|TypeB $x) {}
  228. try {} catch (ErrorA|ErrorB $e) {}
  229. ',
  230. [
  231. 'space' => 'single',
  232. ],
  233. ];
  234. yield [
  235. '<?php
  236. function foo(TypeA|TypeB $x) {}
  237. try {} catch (ErrorA|ErrorB $e) {}
  238. ',
  239. '<?php
  240. function foo(TypeA | TypeB $x) {}
  241. try {} catch (ErrorA | ErrorB $e) {}
  242. ',
  243. [
  244. 'space' => 'none',
  245. ],
  246. ];
  247. }
  248. /**
  249. * @dataProvider provideFix81Cases
  250. *
  251. * @requires PHP 8.1
  252. */
  253. public function testFix81(string $expected, string $input): void
  254. {
  255. $this->doTest($expected, $input);
  256. }
  257. /**
  258. * @return iterable<array{string, string}>
  259. */
  260. public static function provideFix81Cases(): iterable
  261. {
  262. yield [
  263. '<?php class Foo {
  264. public function __construct(
  265. public readonly int|string $a,
  266. protected readonly int|string $b,
  267. private readonly int|string $c
  268. ) {}
  269. }',
  270. '<?php class Foo {
  271. public function __construct(
  272. public readonly int | string $a,
  273. protected readonly int | string $b,
  274. private readonly int | string $c
  275. ) {}
  276. }',
  277. ];
  278. yield [
  279. '<?php function foo(): \Foo&Bar {}',
  280. '<?php function foo(): \Foo & Bar {}',
  281. ];
  282. }
  283. }