SpacesInsideParenthesesFixerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Marc Aubé
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer
  25. */
  26. final class SpacesInsideParenthesesFixerTest extends AbstractFixerTestCase
  27. {
  28. public function testInvalidConfigMissingKey(): void
  29. {
  30. $this->expectException(InvalidFixerConfigurationException::class);
  31. $this->expectExceptionMessageMatches('#^\[spaces_inside_parentheses\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#');
  32. $this->fixer->configure(['a' => 1]);
  33. }
  34. public function testInvalidConfigValue(): void
  35. {
  36. $this->expectException(InvalidFixerConfigurationException::class);
  37. $this->expectExceptionMessageMatches('#^\[spaces_inside_parentheses\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#');
  38. $this->fixer->configure(['space' => 'double']); // @phpstan-ignore-line
  39. }
  40. /**
  41. * @dataProvider provideDefaultFixCases
  42. */
  43. public function testDefaultFix(string $expected, ?string $input = null): void
  44. {
  45. $this->doTest($expected, $input);
  46. }
  47. /**
  48. * @dataProvider provideSpacesFixCases
  49. */
  50. public function testSpacesFix(string $expected, ?string $input = null): void
  51. {
  52. $this->fixer->configure(['space' => 'single']);
  53. $this->doTest($expected, $input);
  54. }
  55. /**
  56. * @return iterable<array{0: string, 1?: string}>
  57. */
  58. public static function provideDefaultFixCases(): iterable
  59. {
  60. // default leaves new lines alone
  61. yield [
  62. "<?php
  63. class Foo
  64. {
  65. private function bar()
  66. {
  67. if (foo(
  68. 'foo' ,
  69. 'bar' ,
  70. [1, 2, 3],
  71. 'baz' // a comment just to mix things up
  72. )) {
  73. return 1;
  74. };
  75. }
  76. }
  77. ",
  78. ];
  79. yield [
  80. '<?php foo();',
  81. '<?php foo( );',
  82. ];
  83. yield [
  84. '<?php
  85. if (true) {
  86. // if body
  87. }',
  88. '<?php
  89. if ( true ) {
  90. // if body
  91. }',
  92. ];
  93. yield [
  94. '<?php
  95. if (true) {
  96. // if body
  97. }',
  98. '<?php
  99. if ( true ) {
  100. // if body
  101. }',
  102. ];
  103. yield [
  104. '<?php
  105. function foo($bar, $baz)
  106. {
  107. // function body
  108. }',
  109. '<?php
  110. function foo( $bar, $baz )
  111. {
  112. // function body
  113. }',
  114. ];
  115. yield [
  116. '<?php
  117. $foo->bar($arg1, $arg2);',
  118. '<?php
  119. $foo->bar( $arg1, $arg2 );',
  120. ];
  121. yield [
  122. '<?php
  123. $var = array( 1, 2, 3 );
  124. ',
  125. ];
  126. yield [
  127. '<?php
  128. $var = [ 1, 2, 3 ];
  129. ',
  130. ];
  131. // list call with trailing comma - need to leave alone
  132. yield [
  133. '<?php list($path, $mode, ) = foo();',
  134. ];
  135. yield [
  136. '<?php list($path, $mode,) = foo();',
  137. ];
  138. yield [
  139. '<?php
  140. $a = $b->test( // do not remove space
  141. $e // between `(` and `)`
  142. // and this comment
  143. );',
  144. ];
  145. yield [
  146. '<?php
  147. function hello($value) {
  148. // code...
  149. }',
  150. '<?php
  151. function hello( $value ) {
  152. // code...
  153. }',
  154. ];
  155. yield [
  156. '<?php
  157. $code = function ($hello, $there) use ($ami, $tumi) {
  158. // code
  159. };
  160. ',
  161. '<?php
  162. $code = function ( $hello, $there ) use ( $ami, $tumi ) {
  163. // code
  164. };
  165. ',
  166. ];
  167. yield [
  168. '<?php
  169. for ($i = 0; $i < 42; $i++) {
  170. // code...
  171. }
  172. ',
  173. '<?php
  174. for ( $i = 0; $i < 42; $i++ ) {
  175. // code...
  176. }
  177. ',
  178. ];
  179. yield [
  180. '<?php
  181. explode($a, $b);
  182. ',
  183. '<?php
  184. explode( $a, $b );
  185. ',
  186. ];
  187. yield [
  188. '<?php
  189. if ($something) {
  190. // code
  191. }
  192. ',
  193. '<?php
  194. if ( $something ) {
  195. // code
  196. }
  197. ',
  198. ];
  199. yield [
  200. '<?php
  201. multiply((2 + 3) * 4);
  202. ',
  203. '<?php
  204. multiply( ( 2 + 3 ) * 4 );
  205. ',
  206. ];
  207. yield [
  208. '<?php $x = (new Foo())->bar();',
  209. '<?php $x = ( new Foo() )->bar();',
  210. ];
  211. yield [
  212. '<?php $x = (new Foo)->bar;',
  213. '<?php $x = ( new Foo )->bar;',
  214. ];
  215. }
  216. /**
  217. * @return iterable<array{0: string, 1?: string}>
  218. */
  219. public static function provideSpacesFixCases(): iterable
  220. {
  221. // Leaves new lines alone
  222. yield [
  223. "<?php
  224. class Foo
  225. {
  226. private function bar()
  227. {
  228. if ( foo(
  229. 'foo' ,
  230. 'bar' ,
  231. [1, 2, 3],
  232. 'baz' // a comment just to mix things up
  233. ) ) {
  234. return 1;
  235. };
  236. }
  237. }",
  238. ];
  239. yield [
  240. '<?php foo();',
  241. '<?php foo( );',
  242. ];
  243. yield [
  244. '<?php
  245. if ( true ) {
  246. // if body
  247. }',
  248. '<?php
  249. if (true) {
  250. // if body
  251. }',
  252. ];
  253. yield [
  254. '<?php
  255. if ( true ) {
  256. // if body
  257. }',
  258. '<?php
  259. if ( true ) {
  260. // if body
  261. }',
  262. ];
  263. yield [
  264. '<?php
  265. function foo( $bar, $baz )
  266. {
  267. // function body
  268. }',
  269. '<?php
  270. function foo($bar, $baz)
  271. {
  272. // function body
  273. }',
  274. ];
  275. yield [
  276. '<?php
  277. $foo->bar( $arg1, $arg2 );',
  278. '<?php
  279. $foo->bar( $arg1, $arg2 );',
  280. ];
  281. yield [
  282. '<?php
  283. $var = array( 1, 2, 3 );
  284. ',
  285. '<?php
  286. $var = array(1, 2, 3);
  287. ',
  288. ];
  289. yield [
  290. '<?php
  291. $var = [ 1, 2, 3 ];
  292. ',
  293. ];
  294. yield [
  295. '<?php list( $path, $mode, ) = foo();',
  296. '<?php list($path, $mode,) = foo();',
  297. ];
  298. yield [
  299. '<?php
  300. $a = $b->test( // do not remove space
  301. $e // between `(` and `)`
  302. // and this comment
  303. );',
  304. ];
  305. yield [
  306. '<?php
  307. function hello( $value ) {
  308. // code...
  309. }',
  310. '<?php
  311. function hello($value) {
  312. // code...
  313. }',
  314. ];
  315. yield [
  316. '<?php
  317. $code = function ( $hello, $there ) use ( $ami, $tumi ) {
  318. // code
  319. };
  320. ',
  321. '<?php
  322. $code = function ($hello, $there) use ($ami, $tumi) {
  323. // code
  324. };
  325. ',
  326. ];
  327. yield [
  328. '<?php
  329. for ( $i = 0; $i < 42; $i++ ) {
  330. // code...
  331. }
  332. ',
  333. '<?php
  334. for ($i = 0; $i < 42; $i++) {
  335. // code...
  336. }
  337. ',
  338. ];
  339. yield [
  340. '<?php
  341. explode( $a, $b );
  342. ',
  343. '<?php
  344. explode($a, $b);
  345. ',
  346. ];
  347. yield [
  348. '<?php
  349. if ( $something ) {
  350. // code
  351. }
  352. ',
  353. '<?php
  354. if ( $something ) {
  355. // code
  356. }
  357. ',
  358. ];
  359. yield [
  360. '<?php
  361. multiply( ( 2 + 3 ) * 4 );
  362. ',
  363. '<?php
  364. multiply((2 + 3) * 4);
  365. ',
  366. ];
  367. yield [
  368. '<?php $x = ( new Foo() )->bar();',
  369. '<?php $x = (new Foo())->bar();',
  370. ];
  371. }
  372. /**
  373. * @dataProvider provideDefaultFix80Cases
  374. *
  375. * @requires PHP 8.0
  376. */
  377. public function testDefaultFix80(string $expected, string $input): void
  378. {
  379. $this->doTest($expected, $input);
  380. }
  381. /**
  382. * @return iterable<string, array{0: string, 1?: string}>
  383. */
  384. public static function provideDefaultFix80Cases(): iterable
  385. {
  386. yield 'mixed argument' => [
  387. '<?php function foo(mixed $a){}',
  388. '<?php function foo( mixed $a ){}',
  389. ];
  390. }
  391. /**
  392. * @dataProvider provideSpacesFix80Cases
  393. *
  394. * @requires PHP 8.0
  395. */
  396. public function testSpacesFix80(string $expected, string $input): void
  397. {
  398. $this->fixer->configure(['space' => 'single']);
  399. $this->doTest($expected, $input);
  400. }
  401. /**
  402. * @return iterable<string, array{0: string, 1?: string}>
  403. */
  404. public static function provideSpacesFix80Cases(): iterable
  405. {
  406. yield 'mixed argument' => [
  407. '<?php function foo( mixed $a ){}',
  408. '<?php function foo(mixed $a){}',
  409. ];
  410. }
  411. }