MethodArgumentSpaceFixerTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Kuanhung Chen <ericj.tw@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer
  21. */
  22. final class MethodArgumentSpaceFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param array<string, mixed> $configuration
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  30. {
  31. $indent = ' ';
  32. $lineEnding = "\n";
  33. if (str_contains($expected, "\t")) {
  34. $indent = "\t";
  35. } elseif (preg_match('/\n \S/', $expected)) {
  36. $indent = ' ';
  37. }
  38. if (str_contains($expected, "\r")) {
  39. $lineEnding = "\r\n";
  40. }
  41. $this->fixer->configure($configuration);
  42. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(
  43. $indent,
  44. $lineEnding
  45. ));
  46. $this->doTest($expected, $input);
  47. }
  48. /**
  49. * @param array<string, mixed> $configuration
  50. *
  51. * @dataProvider provideFixCases
  52. */
  53. public function testFixWithDifferentLineEndings(string $expected, ?string $input = null, array $configuration = []): void
  54. {
  55. if (null !== $input) {
  56. $input = str_replace("\n", "\r\n", $input);
  57. }
  58. $this->testFix(
  59. str_replace("\n", "\r\n", $expected),
  60. $input,
  61. $configuration
  62. );
  63. }
  64. public static function provideFixCases(): iterable
  65. {
  66. return [
  67. [
  68. '<?php
  69. // space '.'
  70. $var1 = $a->some_method(
  71. $var2
  72. );
  73. // space '.'
  74. $var2 = some_function(
  75. $var2
  76. );
  77. // space '.'
  78. $var2a = $z[1](
  79. $var2a
  80. );
  81. '.'
  82. $var3 = function( $a, $b ) { };
  83. ',
  84. '<?php
  85. // space '.'
  86. $var1 = $a->some_method(
  87. $var2);
  88. // space '.'
  89. $var2 = some_function(
  90. $var2);
  91. // space '.'
  92. $var2a = $z[1](
  93. $var2a
  94. );
  95. '.'
  96. $var3 = function( $a , $b ) { };
  97. ',
  98. [
  99. 'on_multiline' => 'ensure_fully_multiline',
  100. ],
  101. ],
  102. 'default' => [
  103. '<?php xyz("", "", "", "");',
  104. '<?php xyz("","","","");',
  105. ],
  106. 'test method arguments' => [
  107. '<?php function xyz($a=10, $b=20, $c=30) {}',
  108. '<?php function xyz($a=10,$b=20,$c=30) {}',
  109. ],
  110. 'test method arguments with multiple spaces' => [
  111. '<?php function xyz($a=10, $b=20, $c=30) {}',
  112. '<?php function xyz($a=10, $b=20 , $c=30) {}',
  113. ],
  114. 'test method arguments with multiple spaces (kmsac)' => [
  115. '<?php function xyz($a=10, $b=20, $c=30) {}',
  116. '<?php function xyz($a=10, $b=20 , $c=30) {}',
  117. ['keep_multiple_spaces_after_comma' => true],
  118. ],
  119. 'test method call (I)' => [
  120. '<?php xyz($a=10, $b=20, $c=30);',
  121. '<?php xyz($a=10 ,$b=20,$c=30);',
  122. ],
  123. 'test method call (II)' => [
  124. '<?php xyz($a=10, $b=20, $this->foo(), $c=30);',
  125. '<?php xyz($a=10,$b=20 ,$this->foo() ,$c=30);',
  126. ],
  127. 'test method call with multiple spaces (I)' => [
  128. '<?php xyz($a=10, $b=20, $c=30);',
  129. '<?php xyz($a=10 , $b=20 , $c=30);',
  130. ],
  131. 'test method call with multiple spaces (I) (kmsac)' => [
  132. '<?php xyz($a=10, $b=20, $c=30);',
  133. '<?php xyz($a=10 , $b=20 , $c=30);',
  134. ['keep_multiple_spaces_after_comma' => true],
  135. ],
  136. 'test method call with tab' => [
  137. '<?php xyz($a=10, $b=20, $c=30);',
  138. "<?php xyz(\$a=10 , \$b=20 ,\t \$c=30);",
  139. ],
  140. 'test method call with tab (kmsac)' => [
  141. "<?php xyz(\$a=10, \$b=20,\t \$c=30);",
  142. "<?php xyz(\$a=10 , \$b=20 ,\t \$c=30);",
  143. ['keep_multiple_spaces_after_comma' => true],
  144. ],
  145. 'test method call with multiple spaces (II)' => [
  146. '<?php xyz($a=10, $b=20, $this->foo(), $c=30);',
  147. '<?php xyz($a=10,$b=20 , $this->foo() ,$c=30);',
  148. ],
  149. 'test method call with multiple spaces (II) (kmsac)' => [
  150. '<?php xyz($a=10, $b=20, $this->foo(), $c=30);',
  151. '<?php xyz($a=10,$b=20 , $this->foo() ,$c=30);',
  152. ['keep_multiple_spaces_after_comma' => true],
  153. ],
  154. 'test named class constructor call' => [
  155. '<?php new Foo($a=10, $b=20, $this->foo(), $c=30);',
  156. '<?php new Foo($a=10,$b=20 ,$this->foo() ,$c=30);',
  157. ],
  158. 'test named class constructor call with multiple spaces' => [
  159. '<?php new Foo($a=10, $b=20, $c=30);',
  160. '<?php new Foo($a=10 , $b=20 , $c=30);',
  161. ],
  162. 'test named class constructor call with multiple spaces (kmsac)' => [
  163. '<?php new Foo($a=10, $b=20, $c=30);',
  164. '<?php new Foo($a=10 , $b=20 , $c=30);',
  165. ['keep_multiple_spaces_after_comma' => true],
  166. ],
  167. 'test anonymous class constructor call' => [
  168. '<?php new class ($a=10, $b=20, $this->foo(), $c=30) {};',
  169. '<?php new class ($a=10,$b=20 ,$this->foo() ,$c=30) {};',
  170. ],
  171. 'test anonymous class constructor call with multiple spaces' => [
  172. '<?php new class ($a=10, $b=20, $c=30) extends Foo {};',
  173. '<?php new class ($a=10 , $b=20 , $c=30) extends Foo {};',
  174. ],
  175. 'test anonymous class constructor call with multiple spaces (kmsac)' => [
  176. '<?php new class ($a=10, $b=20, $c=30) {};',
  177. '<?php new class ($a=10 , $b=20 , $c=30) {};',
  178. ['keep_multiple_spaces_after_comma' => true],
  179. ],
  180. 'test receiving data in list context with omitted values' => [
  181. '<?php list($a, $b, , , $c) = foo();',
  182. '<?php list($a, $b,, ,$c) = foo();',
  183. ],
  184. 'test receiving data in list context with omitted values and multiple spaces' => [
  185. '<?php list($a, $b, , , $c) = foo();',
  186. '<?php list($a, $b,, ,$c) = foo();',
  187. ],
  188. 'test receiving data in list context with omitted values and multiple spaces (kmsac)' => [
  189. '<?php list($a, $b, , , $c) = foo();',
  190. '<?php list($a, $b,, ,$c) = foo();',
  191. ['keep_multiple_spaces_after_comma' => true],
  192. ],
  193. 'skip array' => [
  194. '<?php array(10 , 20 ,30); $foo = [ 10,50 , 60 ] ?>',
  195. ],
  196. 'list call with trailing comma' => [
  197. '<?php list($path, $mode, ) = foo();',
  198. '<?php list($path, $mode,) = foo();',
  199. ],
  200. 'list call with trailing comma multi line' => [
  201. '<?php
  202. list(
  203. $a,
  204. $b,
  205. ) = foo();
  206. ',
  207. '<?php
  208. list(
  209. $a ,
  210. $b ,
  211. ) = foo();
  212. ',
  213. ],
  214. 'inline comments with spaces' => [
  215. '<?php xyz($a=10, /*comment1*/ $b=2000, /*comment2*/ $c=30);',
  216. '<?php xyz($a=10, /*comment1*/ $b=2000,/*comment2*/ $c=30);',
  217. ],
  218. 'inline comments with spaces (kmsac)' => [
  219. '<?php xyz($a=10, /*comment1*/ $b=2000, /*comment2*/ $c=30);',
  220. '<?php xyz($a=10, /*comment1*/ $b=2000,/*comment2*/ $c=30);',
  221. ['keep_multiple_spaces_after_comma' => true],
  222. ],
  223. 'multi line testing method call' => [
  224. '<?php if (1) {
  225. xyz(
  226. $a=10,
  227. $b=20,
  228. $c=30
  229. );
  230. }',
  231. '<?php if (1) {
  232. xyz(
  233. $a=10 ,
  234. $b=20,
  235. $c=30
  236. );
  237. }',
  238. ],
  239. 'multi line anonymous class constructor call' => [
  240. '<?php if (1) {
  241. new class (
  242. $a=10,
  243. $b=20,
  244. $c=30
  245. ) {};
  246. }',
  247. '<?php if (1) {
  248. new class (
  249. $a=10 ,
  250. $b=20,$c=30) {};
  251. }',
  252. ],
  253. 'skip arrays but replace arg methods' => [
  254. '<?php fnc(1, array(2, func2(6, 7) ,4), 5);',
  255. '<?php fnc(1,array(2, func2(6, 7) ,4), 5);',
  256. ],
  257. 'skip arrays but replace arg methods (kmsac)' => [
  258. '<?php fnc(1, array(2, func2(6, 7) ,4), 5);',
  259. '<?php fnc(1,array(2, func2(6, 7) ,4), 5);',
  260. ['keep_multiple_spaces_after_comma' => true],
  261. ],
  262. 'ignore commas inside call argument' => [
  263. '<?php fnc(1, array(2, 3 ,4), 5);',
  264. ],
  265. 'skip multi line array' => [
  266. '<?php
  267. array(
  268. 10 ,
  269. 20,
  270. 30
  271. );',
  272. ],
  273. 'skip short array' => [
  274. '<?php
  275. $foo = ["a"=>"apple", "b"=>"bed" ,"c"=>"car"];
  276. $bar = ["a" ,"b" ,"c"];
  277. ',
  278. ],
  279. 'don\'t change HEREDOC and NOWDOC' => [
  280. "<?php if (1) {
  281. \$this->foo(
  282. <<<EOTXTa
  283. heredoc
  284. EOTXTa
  285. ,
  286. <<<'EOTXTb'
  287. nowdoc
  288. EOTXTb
  289. ,
  290. 'foo'
  291. );
  292. }",
  293. ],
  294. 'with_random_comments on_multiline:ignore' => [
  295. '<?php xyz#
  296. (#
  297. ""#
  298. ,#
  299. $a#
  300. );',
  301. null,
  302. ['on_multiline' => 'ignore'],
  303. ],
  304. 'with_random_comments on_multiline:ensure_single_line' => [
  305. '<?php xyz#
  306. (#
  307. ""#
  308. ,#
  309. $a#
  310. );',
  311. null,
  312. ['on_multiline' => 'ensure_single_line'],
  313. ],
  314. 'with_random_comments on_multiline:ensure_fully_multiline' => [
  315. '<?php xyz#
  316. (#
  317. ""#
  318. ,#
  319. $a#
  320. );',
  321. '<?php xyz#
  322. (#
  323. ""#
  324. ,#
  325. $a#
  326. );',
  327. ['on_multiline' => 'ensure_fully_multiline'],
  328. ],
  329. 'test half-multiline function becomes fully-multiline' => [
  330. <<<'EXPECTED'
  331. <?php
  332. functionCall(
  333. 'a',
  334. 'b',
  335. 'c'
  336. );
  337. EXPECTED
  338. ,
  339. <<<'INPUT'
  340. <?php
  341. functionCall(
  342. 'a', 'b',
  343. 'c'
  344. );
  345. INPUT
  346. ,
  347. ],
  348. 'test wrongly formatted half-multiline function becomes fully-multiline' => [
  349. '<?php
  350. f(
  351. 1,
  352. 2,
  353. 3
  354. );',
  355. '<?php
  356. f(1,2,
  357. 3);',
  358. ],
  359. 'function calls with here doc cannot be anything but multiline' => [
  360. <<<'EXPECTED'
  361. <?php
  362. str_replace(
  363. "\n",
  364. PHP_EOL,
  365. <<<'TEXT'
  366. 1) someFile.php
  367. TEXT
  368. );
  369. EXPECTED
  370. ,
  371. <<<'INPUT'
  372. <?php
  373. str_replace("\n", PHP_EOL, <<<'TEXT'
  374. 1) someFile.php
  375. TEXT
  376. );
  377. INPUT
  378. ,
  379. ],
  380. 'test barely multiline function with blank lines becomes fully-multiline' => [
  381. <<<'EXPECTED'
  382. <?php
  383. functionCall(
  384. 'a',
  385. 'b',
  386. 'c'
  387. );
  388. EXPECTED
  389. ,
  390. <<<'INPUT'
  391. <?php
  392. functionCall('a', 'b',
  393. 'c');
  394. INPUT
  395. ,
  396. ],
  397. 'test indentation is preserved' => [
  398. <<<'EXPECTED'
  399. <?php
  400. if (true) {
  401. functionCall(
  402. 'a',
  403. 'b',
  404. 'c'
  405. );
  406. }
  407. EXPECTED
  408. ,
  409. <<<'INPUT'
  410. <?php
  411. if (true) {
  412. functionCall(
  413. 'a', 'b',
  414. 'c'
  415. );
  416. }
  417. INPUT
  418. ,
  419. ],
  420. 'test multiline array arguments do not trigger multiline' => [
  421. <<<'EXPECTED'
  422. <?php
  423. defraculate(1, array(
  424. 'a',
  425. 'b',
  426. 'c',
  427. ), 42);
  428. EXPECTED
  429. ,
  430. ],
  431. 'test multiline function arguments do not trigger multiline' => [
  432. <<<'EXPECTED'
  433. <?php
  434. defraculate(1, function () {
  435. $a = 42;
  436. }, 42);
  437. EXPECTED
  438. ,
  439. ],
  440. 'test violation after opening parenthesis' => [
  441. <<<'EXPECTED'
  442. <?php
  443. defraculate(
  444. 1,
  445. 2,
  446. 3
  447. );
  448. EXPECTED
  449. ,
  450. <<<'INPUT'
  451. <?php
  452. defraculate(
  453. 1, 2, 3);
  454. INPUT
  455. ,
  456. ],
  457. 'test violation after opening parenthesis, indented with two spaces' => [
  458. <<<'EXPECTED'
  459. <?php
  460. defraculate(
  461. 1,
  462. 2,
  463. 3
  464. );
  465. EXPECTED
  466. ,
  467. <<<'INPUT'
  468. <?php
  469. defraculate(
  470. 1, 2, 3);
  471. INPUT
  472. ,
  473. ],
  474. 'test violation after opening parenthesis, indented with tabs' => [
  475. <<<'EXPECTED'
  476. <?php
  477. defraculate(
  478. 1,
  479. 2,
  480. 3
  481. );
  482. EXPECTED
  483. ,
  484. <<<'INPUT'
  485. <?php
  486. defraculate(
  487. 1, 2, 3);
  488. INPUT
  489. ,
  490. ],
  491. 'test violation before closing parenthesis' => [
  492. <<<'EXPECTED'
  493. <?php
  494. defraculate(
  495. 1,
  496. 2,
  497. 3
  498. );
  499. EXPECTED
  500. ,
  501. <<<'INPUT'
  502. <?php
  503. defraculate(1, 2, 3
  504. );
  505. INPUT
  506. ,
  507. ],
  508. 'test violation before closing parenthesis in nested call' => [
  509. <<<'EXPECTED'
  510. <?php
  511. getSchwifty('rick', defraculate(
  512. 1,
  513. 2,
  514. 3
  515. ), 'morty');
  516. EXPECTED
  517. ,
  518. <<<'INPUT'
  519. <?php
  520. getSchwifty('rick', defraculate(1, 2, 3
  521. ), 'morty');
  522. INPUT
  523. ,
  524. ],
  525. 'test with comment between arguments' => [
  526. <<<'EXPECTED'
  527. <?php
  528. functionCall(
  529. 'a', /* comment */
  530. 'b',
  531. 'c'
  532. );
  533. EXPECTED
  534. ,
  535. <<<'INPUT'
  536. <?php
  537. functionCall(
  538. 'a',/* comment */'b',
  539. 'c'
  540. );
  541. INPUT
  542. ,
  543. ],
  544. 'test with deeply nested arguments' => [
  545. <<<'EXPECTED'
  546. <?php
  547. foo(
  548. 'a',
  549. 'b',
  550. [
  551. 'c',
  552. 'd', bar('e', 'f'),
  553. baz(
  554. 'g',
  555. ['h',
  556. 'i',
  557. ]
  558. ),
  559. ]
  560. );
  561. EXPECTED
  562. ,
  563. <<<'INPUT'
  564. <?php
  565. foo('a',
  566. 'b',
  567. [
  568. 'c',
  569. 'd', bar('e', 'f'),
  570. baz('g',
  571. ['h',
  572. 'i',
  573. ]),
  574. ]);
  575. INPUT
  576. ,
  577. ],
  578. 'multiline string argument' => [
  579. <<<'UNAFFECTED'
  580. <?php
  581. $this->with('<?php
  582. %s
  583. class FooClass
  584. {
  585. }', $comment, false);
  586. UNAFFECTED
  587. ,
  588. ],
  589. 'arrays with whitespace inside' => [
  590. <<<'UNAFFECTED'
  591. <?php
  592. $a = array/**/( 1);
  593. $a = array/**/( 12,
  594. 7);
  595. $a = array/***/(123, 7);
  596. $a = array ( 1,
  597. 2);
  598. UNAFFECTED
  599. ,
  600. ],
  601. 'test code that should not be affected (because not a function nor a method)' => [
  602. <<<'UNAFFECTED'
  603. <?php
  604. if (true &&
  605. true
  606. ) {
  607. // do whatever
  608. }
  609. UNAFFECTED
  610. ,
  611. ],
  612. 'test ungodly code' => [
  613. <<<'EXPECTED'
  614. <?php
  615. $a = function#
  616. (#
  617. #
  618. $a#
  619. #
  620. ,#
  621. #
  622. $b,
  623. $c#
  624. #
  625. )#
  626. use (
  627. $b1,
  628. $c1,
  629. $d1
  630. ) {
  631. };
  632. EXPECTED
  633. ,
  634. <<<'INPUT'
  635. <?php
  636. $a = function#
  637. (#
  638. #
  639. $a#
  640. #
  641. ,#
  642. #
  643. $b,$c#
  644. #
  645. )#
  646. use ($b1,
  647. $c1,$d1) {
  648. };
  649. INPUT
  650. ,
  651. ],
  652. 'test list' => [
  653. <<<'UNAFFECTED'
  654. <?php
  655. // no fix
  656. list($a,
  657. $b, $c) = $a;
  658. isset($a,
  659. $b, $c);
  660. unset($a,
  661. $b, $c);
  662. array(1,
  663. 2,3
  664. );
  665. UNAFFECTED
  666. ,
  667. ],
  668. 'test function argument with multiline echo in it' => [
  669. <<<'UNAFFECTED'
  670. <?php
  671. call_user_func(function ($arguments) {
  672. echo 'a',
  673. 'b';
  674. }, $argv);
  675. UNAFFECTED
  676. ,
  677. ],
  678. 'test function argument with oneline echo in it' => [
  679. <<<'EXPECTED'
  680. <?php
  681. call_user_func(
  682. function ($arguments) {
  683. echo 'a', 'b';
  684. },
  685. $argv
  686. );
  687. EXPECTED
  688. ,
  689. <<<'INPUT'
  690. <?php
  691. call_user_func(function ($arguments) {
  692. echo 'a', 'b';
  693. },
  694. $argv);
  695. INPUT
  696. ,
  697. ],
  698. 'ensure_single_line' => [
  699. <<<'EXPECTED'
  700. <?php
  701. function foo($a, $b) {
  702. // foo
  703. }
  704. foo($a, $b);
  705. EXPECTED
  706. ,
  707. <<<'INPUT'
  708. <?php
  709. function foo(
  710. $a,
  711. $b
  712. ) {
  713. // foo
  714. }
  715. foo(
  716. $a,
  717. $b
  718. );
  719. INPUT
  720. ,
  721. ['on_multiline' => 'ensure_single_line'],
  722. ],
  723. 'ensure_single_line_with_random_comments' => [
  724. <<<'EXPECTED'
  725. <?php
  726. function foo(/* foo */// bar
  727. $a, /* foo */// bar
  728. $b#foo
  729. ) {
  730. // foo
  731. }
  732. foo(/* foo */// bar
  733. $a, /* foo */// bar
  734. $b#foo
  735. );
  736. EXPECTED
  737. ,
  738. null,
  739. ['on_multiline' => 'ensure_single_line'],
  740. ],
  741. 'ensure_single_line_with_consecutive_newlines' => [
  742. <<<'EXPECTED'
  743. <?php
  744. function foo($a, $b) {
  745. // foo
  746. }
  747. foo($a, $b);
  748. EXPECTED
  749. ,
  750. <<<'INPUT'
  751. <?php
  752. function foo(
  753. $a,
  754. $b
  755. ) {
  756. // foo
  757. }
  758. foo(
  759. $a,
  760. $b
  761. );
  762. INPUT
  763. ,
  764. ['on_multiline' => 'ensure_single_line'],
  765. ],
  766. 'ensure_single_line_methods' => [
  767. <<<'EXPECTED'
  768. <?php
  769. class Foo {
  770. public static function foo1($a, $b, $c) {}
  771. private function foo2($a, $b, $c) {}
  772. }
  773. EXPECTED
  774. ,
  775. <<<'INPUT'
  776. <?php
  777. class Foo {
  778. public static function foo1(
  779. $a,
  780. $b,
  781. $c
  782. ) {}
  783. private function foo2(
  784. $a,
  785. $b,
  786. $c
  787. ) {}
  788. }
  789. INPUT
  790. ,
  791. ['on_multiline' => 'ensure_single_line'],
  792. ],
  793. 'ensure_single_line_methods_in_anonymous_class' => [
  794. <<<'EXPECTED'
  795. <?php
  796. new class {
  797. public static function foo1($a, $b, $c) {}
  798. private function foo2($a, $b, $c) {}
  799. };
  800. EXPECTED
  801. ,
  802. <<<'INPUT'
  803. <?php
  804. new class {
  805. public static function foo1(
  806. $a,
  807. $b,
  808. $c
  809. ) {}
  810. private function foo2(
  811. $a,
  812. $b,
  813. $c
  814. ) {}
  815. };
  816. INPUT
  817. ,
  818. ['on_multiline' => 'ensure_single_line'],
  819. ],
  820. 'ensure_single_line_keep_spaces_after_comma' => [
  821. <<<'EXPECTED'
  822. <?php
  823. function foo($a, $b) {
  824. // foo
  825. }
  826. foo($a, $b);
  827. EXPECTED
  828. ,
  829. <<<'INPUT'
  830. <?php
  831. function foo(
  832. $a,
  833. $b
  834. ) {
  835. // foo
  836. }
  837. foo(
  838. $a,
  839. $b
  840. );
  841. INPUT
  842. ,
  843. [
  844. 'on_multiline' => 'ensure_single_line',
  845. 'keep_multiple_spaces_after_comma' => true,
  846. ],
  847. ],
  848. 'fix closing parenthesis (without trailing comma)' => [
  849. '<?php
  850. if (true) {
  851. execute(
  852. $foo,
  853. $bar
  854. );
  855. }',
  856. '<?php
  857. if (true) {
  858. execute(
  859. $foo,
  860. $bar
  861. );
  862. }',
  863. [
  864. 'on_multiline' => 'ensure_fully_multiline',
  865. ],
  866. ],
  867. 'test anonymous functions' => [
  868. '<?php
  869. $example = function () use ($message1, $message2) {
  870. };',
  871. '<?php
  872. $example = function () use ($message1,$message2) {
  873. };',
  874. ],
  875. 'test first element in same line, space before comma and inconsistent indent' => [
  876. '<?php foo(
  877. "aaa
  878. bbb",
  879. $c,
  880. $d,
  881. $e,
  882. $f
  883. );
  884. ',
  885. '<?php foo("aaa
  886. bbb",
  887. $c, $d ,
  888. $e,
  889. $f);
  890. ',
  891. ],
  892. 'test first element in same line, space before comma and inconsistent indent with comments' => [
  893. '<?php foo(
  894. "aaa
  895. bbb", // comment1
  896. $c, /** comment2 */
  897. $d,
  898. $e/* comment3 */,
  899. $f
  900. );# comment4
  901. ',
  902. '<?php foo("aaa
  903. bbb", // comment1
  904. $c, /** comment2 */$d ,
  905. $e/* comment3 */,
  906. $f);# comment4
  907. ',
  908. ],
  909. ];
  910. }
  911. /**
  912. * @param array<string, mixed> $config
  913. *
  914. * @dataProvider provideFix2Cases
  915. */
  916. public function testFix2(string $expected, ?string $input = null, array $config = []): void
  917. {
  918. $this->fixer->configure($config);
  919. $this->doTest($expected, $input);
  920. }
  921. public static function provideFix2Cases(): iterable
  922. {
  923. yield [
  924. '<?php function A($c, ...$a){}',
  925. '<?php function A($c ,...$a){}',
  926. ];
  927. yield [
  928. <<<'EXPECTED'
  929. <?php
  930. foo(
  931. <<<'EOD'
  932. bar
  933. EOD,
  934. 'baz'
  935. );
  936. EXPECTED
  937. ,
  938. <<<'INPUT'
  939. <?php
  940. foo(
  941. <<<'EOD'
  942. bar
  943. EOD
  944. ,
  945. 'baz'
  946. );
  947. INPUT
  948. ,
  949. ['after_heredoc' => true],
  950. ];
  951. yield [
  952. <<<'EXPECTED'
  953. <?php
  954. foo(
  955. $bar,
  956. $baz,
  957. );
  958. EXPECTED
  959. ,
  960. null,
  961. ['on_multiline' => 'ensure_fully_multiline'],
  962. ];
  963. yield [
  964. '<?php
  965. functionCall(
  966. 1,
  967. 2,
  968. 3,
  969. );',
  970. '<?php
  971. functionCall(
  972. 1, 2,
  973. 3,
  974. );',
  975. [
  976. 'on_multiline' => 'ensure_fully_multiline',
  977. ],
  978. ];
  979. yield [
  980. '<?php foo(1, 2, 3, );',
  981. '<?php foo(1,2,3,);',
  982. ];
  983. yield [
  984. '<?php
  985. $fn = fn(
  986. $test1,
  987. $test2
  988. ) => null;',
  989. '<?php
  990. $fn = fn(
  991. $test1, $test2
  992. ) => null;',
  993. [
  994. 'on_multiline' => 'ensure_fully_multiline',
  995. ],
  996. ];
  997. }
  998. /**
  999. * @dataProvider provideFix81Cases
  1000. *
  1001. * @requires PHP 8.1
  1002. */
  1003. public function testFix81(string $expected, ?string $input = null): void
  1004. {
  1005. $this->doTest($expected, $input);
  1006. }
  1007. public static function provideFix81Cases(): iterable
  1008. {
  1009. yield [
  1010. '<?php
  1011. [Foo::class, \'method\'](
  1012. ...
  1013. ) ?>',
  1014. '<?php
  1015. [Foo::class, \'method\']( ...
  1016. ) ?>',
  1017. ];
  1018. }
  1019. }