MethodArgumentSpaceFixerTest.php 25 KB

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