MultilineWhitespaceBeforeSemicolonsFixerTest.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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\Semicolon;
  13. use PhpCsFixer\Fixer\Semicolon\MultilineWhitespaceBeforeSemicolonsFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @author John Kelly <wablam@gmail.com>
  18. * @author Graham Campbell <hello@gjcampbell.co.uk>
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. * @author Egidijus Girčys <e.gircys@gmail.com>
  21. *
  22. * @internal
  23. *
  24. * @covers \PhpCsFixer\Fixer\Semicolon\MultilineWhitespaceBeforeSemicolonsFixer
  25. */
  26. final class MultilineWhitespaceBeforeSemicolonsFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @dataProvider provideMultiLineWhitespaceFixCases
  30. */
  31. public function testFixMultiLineWhitespace(string $expected, ?string $input = null): void
  32. {
  33. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NO_MULTI_LINE]);
  34. $this->doTest($expected, $input);
  35. }
  36. public static function provideMultiLineWhitespaceFixCases(): array
  37. {
  38. return [
  39. [
  40. '<?php
  41. $foo->bar(); // test',
  42. '<?php
  43. $foo->bar() // test
  44. ;',
  45. ],
  46. [
  47. '<?php echo(1); // test',
  48. "<?php echo(1) // test\n;",
  49. ],
  50. [
  51. "<?php echo(1); // test\n",
  52. ],
  53. [
  54. '<?php
  55. $foo->bar(); # test',
  56. '<?php
  57. $foo->bar() # test
  58. ;',
  59. ],
  60. [
  61. '<?php
  62. $foo->bar();// test',
  63. '<?php
  64. $foo->bar()// test
  65. ;',
  66. ],
  67. [
  68. "<?php\n;",
  69. ],
  70. [
  71. '<?php
  72. $this
  73. ->setName(\'readme1\')
  74. ->setDescription(\'Generates the README\');
  75. ',
  76. '<?php
  77. $this
  78. ->setName(\'readme1\')
  79. ->setDescription(\'Generates the README\')
  80. ;
  81. ',
  82. ],
  83. [
  84. '<?php
  85. $this
  86. ->setName(\'readme2\')
  87. ->setDescription(\'Generates the README\');
  88. ',
  89. '<?php
  90. $this
  91. ->setName(\'readme2\')
  92. ->setDescription(\'Generates the README\')
  93. ;
  94. ',
  95. ],
  96. [
  97. '<?php echo "$this->foo(\'with param containing ;\') ;" ;',
  98. ],
  99. [
  100. '<?php $this->foo();',
  101. ],
  102. [
  103. '<?php $this->foo() ;',
  104. ],
  105. [
  106. '<?php $this->foo(\'with param containing ;\') ;',
  107. ],
  108. [
  109. '<?php $this->foo(\'with param containing ) ; \') ;',
  110. ],
  111. [
  112. '<?php $this->foo("with param containing ) ; ") ; ?>',
  113. ],
  114. [
  115. '<?php $this->foo("with semicolon in string) ; "); ?>',
  116. ],
  117. [
  118. '<?php
  119. $this
  120. ->example();',
  121. '<?php
  122. $this
  123. ->example()
  124. ;',
  125. ],
  126. [
  127. '<?php
  128. Foo::bar(); // test',
  129. '<?php
  130. Foo::bar() // test
  131. ;',
  132. ],
  133. [
  134. '<?php
  135. Foo::bar(); # test',
  136. '<?php
  137. Foo::bar() # test
  138. ;',
  139. ],
  140. [
  141. '<?php
  142. self
  143. ::setName(\'readme1\')
  144. ->setDescription(\'Generates the README\');
  145. ',
  146. '<?php
  147. self
  148. ::setName(\'readme1\')
  149. ->setDescription(\'Generates the README\')
  150. ;
  151. ',
  152. ],
  153. [
  154. '<?php
  155. self
  156. ::setName(\'readme2\')
  157. ->setDescription(\'Generates the README\');
  158. ',
  159. '<?php
  160. self
  161. ::setName(\'readme2\')
  162. ->setDescription(\'Generates the README\')
  163. ;
  164. ',
  165. ],
  166. [
  167. '<?php echo "self::foo(\'with param containing ;\') ;" ;',
  168. ],
  169. [
  170. '<?php self::foo();',
  171. ],
  172. [
  173. '<?php self::foo() ;',
  174. ],
  175. [
  176. '<?php self::foo(\'with param containing ;\') ;',
  177. ],
  178. [
  179. '<?php self::foo(\'with param containing ) ; \') ;',
  180. ],
  181. [
  182. '<?php self::foo("with param containing ) ; ") ; ?>',
  183. ],
  184. [
  185. '<?php self::foo("with semicolon in string) ; "); ?>',
  186. ],
  187. [
  188. '<?php
  189. self
  190. ::example();',
  191. '<?php
  192. self
  193. ::example()
  194. ;',
  195. ],
  196. [
  197. '<?php
  198. $seconds = $minutes
  199. * 60; // seconds in a minute',
  200. '<?php
  201. $seconds = $minutes
  202. * 60 // seconds in a minute
  203. ;',
  204. ],
  205. [
  206. '<?php
  207. $seconds = $minutes
  208. * (int) \'60\'; // seconds in a minute',
  209. '<?php
  210. $seconds = $minutes
  211. * (int) \'60\' // seconds in a minute
  212. ;',
  213. ],
  214. [
  215. '<?php
  216. $secondsPerMinute = 60;
  217. $seconds = $minutes
  218. * $secondsPerMinute; // seconds in a minute',
  219. '<?php
  220. $secondsPerMinute = 60;
  221. $seconds = $minutes
  222. * $secondsPerMinute // seconds in a minute
  223. ;',
  224. ],
  225. [
  226. '<?php
  227. $secondsPerMinute = 60;
  228. $seconds = $minutes
  229. * 60 * (int) true; // seconds in a minute',
  230. '<?php
  231. $secondsPerMinute = 60;
  232. $seconds = $minutes
  233. * 60 * (int) true // seconds in a minute
  234. ;',
  235. ],
  236. ];
  237. }
  238. /**
  239. * @dataProvider provideMessyWhitespacesMultiLineWhitespaceFixCases
  240. */
  241. public function testMessyWhitespacesMultiLineWhitespace(string $expected, ?string $input = null): void
  242. {
  243. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  244. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NO_MULTI_LINE]);
  245. $this->doTest($expected, $input);
  246. }
  247. public static function provideMessyWhitespacesMultiLineWhitespaceFixCases(): array
  248. {
  249. return [
  250. [
  251. '<?php echo(1); // test',
  252. "<?php echo(1) // test\r\n;",
  253. ],
  254. ];
  255. }
  256. /**
  257. * @dataProvider provideSemicolonForChainedCallsFixCases
  258. */
  259. public function testSemicolonForChainedCallsFix(string $expected, ?string $input = null): void
  260. {
  261. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS]);
  262. $this->doTest($expected, $input);
  263. }
  264. public static function provideSemicolonForChainedCallsFixCases(): array
  265. {
  266. return [
  267. [
  268. '<?php
  269. $this
  270. ->method1()
  271. ->method2()
  272. ;
  273. ?>',
  274. '<?php
  275. $this
  276. ->method1()
  277. ->method2();
  278. ?>',
  279. ],
  280. [
  281. '<?php
  282. $this
  283. ->method1()
  284. ->method2() // comment
  285. ;
  286. ',
  287. '<?php
  288. $this
  289. ->method1()
  290. ->method2(); // comment
  291. ',
  292. ],
  293. [
  294. '<?php
  295. $service->method1()
  296. ->method2()
  297. ;
  298. $service->method3();
  299. $this
  300. ->method1()
  301. ->method2()
  302. ;',
  303. '<?php
  304. $service->method1()
  305. ->method2()
  306. ;
  307. $service->method3();
  308. $this
  309. ->method1()
  310. ->method2();',
  311. ],
  312. [
  313. '<?php
  314. $service
  315. ->method2()
  316. ;
  317. ?>',
  318. '<?php
  319. $service
  320. ->method2();
  321. ?>',
  322. ],
  323. [
  324. '<?php
  325. $service->method1()
  326. ->method2()
  327. ->method3()
  328. ->method4()
  329. ;
  330. ?>',
  331. '<?php
  332. $service->method1()
  333. ->method2()
  334. ->method3()
  335. ->method4();
  336. ?>',
  337. ],
  338. [
  339. '<?php
  340. $this->service->method1()
  341. ->method2([1, 2])
  342. ->method3(
  343. "2",
  344. 2,
  345. [1, 2]
  346. )
  347. ->method4()
  348. ;
  349. ?>',
  350. '<?php
  351. $this->service->method1()
  352. ->method2([1, 2])
  353. ->method3(
  354. "2",
  355. 2,
  356. [1, 2]
  357. )
  358. ->method4();
  359. ?>',
  360. ],
  361. [
  362. '<?php
  363. $service
  364. ->method1()
  365. ->method2()
  366. ->method3()
  367. ->method4()
  368. ;
  369. ?>',
  370. '<?php
  371. $service
  372. ->method1()
  373. ->method2()
  374. ->method3()
  375. ->method4();
  376. ?>',
  377. ],
  378. [
  379. '<?php
  380. $f = "g";
  381. $service
  382. ->method1("a", true)
  383. ->method2(true, false)
  384. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  385. ->method4(1, "a", $f)
  386. ;
  387. ?>',
  388. '<?php
  389. $f = "g";
  390. $service
  391. ->method1("a", true)
  392. ->method2(true, false)
  393. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  394. ->method4(1, "a", $f);
  395. ?>',
  396. ],
  397. [
  398. '<?php
  399. $f = "g";
  400. $service
  401. ->method1("a", true) // this is a comment
  402. /* ->method2(true, false) */
  403. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  404. ->method4(1, "a", $f) /* this is a comment */
  405. ;
  406. ?>',
  407. '<?php
  408. $f = "g";
  409. $service
  410. ->method1("a", true) // this is a comment
  411. /* ->method2(true, false) */
  412. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  413. ->method4(1, "a", $f); /* this is a comment */
  414. ?>',
  415. ],
  416. [
  417. '<?php
  418. $service->method1();
  419. $service->method2()->method3();
  420. ?>',
  421. ],
  422. [
  423. '<?php
  424. $service->method1() ;
  425. $service->method2()->method3() ;
  426. ?>',
  427. ],
  428. [
  429. '<?php
  430. $service
  431. ->method2(function ($a) {
  432. $a->otherCall()
  433. ->a()
  434. ->b()
  435. ;
  436. })
  437. ;
  438. ?>',
  439. '<?php
  440. $service
  441. ->method2(function ($a) {
  442. $a->otherCall()
  443. ->a()
  444. ->b()
  445. ;
  446. });
  447. ?>',
  448. ],
  449. [
  450. '<?php
  451. $data = $service
  452. ->method2(function ($a) {
  453. $a->otherCall()
  454. ->a()
  455. ->b(array_merge([
  456. 1 => 1,
  457. 2 => 2,
  458. ], $this->getOtherArray()
  459. ))
  460. ;
  461. })
  462. ;
  463. ?>',
  464. '<?php
  465. $data = $service
  466. ->method2(function ($a) {
  467. $a->otherCall()
  468. ->a()
  469. ->b(array_merge([
  470. 1 => 1,
  471. 2 => 2,
  472. ], $this->getOtherArray()
  473. ));
  474. });
  475. ?>',
  476. ],
  477. [
  478. '<?php
  479. $service
  480. ->method1(null, null, [
  481. null => null,
  482. 1 => $data->getId() > 0,
  483. ])
  484. ->method2(4, Type::class)
  485. ;
  486. ',
  487. '<?php
  488. $service
  489. ->method1(null, null, [
  490. null => null,
  491. 1 => $data->getId() > 0,
  492. ])
  493. ->method2(4, Type::class);
  494. ',
  495. ],
  496. [
  497. '<?php
  498. $this
  499. ->method1()
  500. ->method2()
  501. ;
  502. ?>',
  503. '<?php
  504. $this
  505. ->method1()
  506. ->method2();
  507. ?>',
  508. ],
  509. [
  510. '<?php
  511. self
  512. ::method1()
  513. ->method2()
  514. ;
  515. ?>',
  516. '<?php
  517. self
  518. ::method1()
  519. ->method2();
  520. ?>',
  521. ],
  522. [
  523. '<?php
  524. self
  525. ::method1()
  526. ->method2() // comment
  527. ;
  528. ',
  529. '<?php
  530. self
  531. ::method1()
  532. ->method2(); // comment
  533. ',
  534. ],
  535. [
  536. '<?php
  537. Service::method1()
  538. ->method2()
  539. ;
  540. Service::method3();
  541. $this
  542. ->method1()
  543. ->method2()
  544. ;',
  545. '<?php
  546. Service::method1()
  547. ->method2()
  548. ;
  549. Service::method3();
  550. $this
  551. ->method1()
  552. ->method2();',
  553. ],
  554. [
  555. '<?php
  556. Service
  557. ::method2()
  558. ;
  559. ?>',
  560. '<?php
  561. Service
  562. ::method2();
  563. ?>',
  564. ],
  565. [
  566. '<?php
  567. Service::method1()
  568. ->method2()
  569. ->method3()
  570. ->method4()
  571. ;
  572. ?>',
  573. '<?php
  574. Service::method1()
  575. ->method2()
  576. ->method3()
  577. ->method4();
  578. ?>',
  579. ],
  580. [
  581. '<?php
  582. self::method1()
  583. ->method2([1, 2])
  584. ->method3(
  585. "2",
  586. 2,
  587. [1, 2]
  588. )
  589. ->method4()
  590. ;
  591. ?>',
  592. '<?php
  593. self::method1()
  594. ->method2([1, 2])
  595. ->method3(
  596. "2",
  597. 2,
  598. [1, 2]
  599. )
  600. ->method4();
  601. ?>',
  602. ],
  603. [
  604. '<?php
  605. Service
  606. ::method1()
  607. ->method2()
  608. ->method3()
  609. ->method4()
  610. ;
  611. ?>',
  612. '<?php
  613. Service
  614. ::method1()
  615. ->method2()
  616. ->method3()
  617. ->method4();
  618. ?>',
  619. ],
  620. [
  621. '<?php
  622. $f = "g";
  623. Service
  624. ::method1("a", true)
  625. ->method2(true, false)
  626. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  627. ->method4(1, "a", $f)
  628. ;
  629. ?>',
  630. '<?php
  631. $f = "g";
  632. Service
  633. ::method1("a", true)
  634. ->method2(true, false)
  635. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  636. ->method4(1, "a", $f);
  637. ?>',
  638. ],
  639. [
  640. '<?php
  641. $f = "g";
  642. Service
  643. ::method1("a", true) // this is a comment
  644. /* ->method2(true, false) */
  645. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  646. ->method4(1, "a", $f) /* this is a comment */
  647. ;
  648. ?>',
  649. '<?php
  650. $f = "g";
  651. Service
  652. ::method1("a", true) // this is a comment
  653. /* ->method2(true, false) */
  654. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  655. ->method4(1, "a", $f); /* this is a comment */
  656. ?>',
  657. ],
  658. [
  659. '<?php
  660. Service::method1();
  661. Service::method2()->method3();
  662. ?>',
  663. ],
  664. [
  665. '<?php
  666. Service::method1() ;
  667. Service::method2()->method3() ;
  668. ?>',
  669. ],
  670. [
  671. '<?php
  672. Service
  673. ::method2(function ($a) {
  674. $a->otherCall()
  675. ->a()
  676. ->b()
  677. ;
  678. })
  679. ;
  680. ?>',
  681. '<?php
  682. Service
  683. ::method2(function ($a) {
  684. $a->otherCall()
  685. ->a()
  686. ->b()
  687. ;
  688. });
  689. ?>',
  690. ],
  691. [
  692. '<?php
  693. $data = Service
  694. ::method2(function () {
  695. Foo::otherCall()
  696. ->a()
  697. ->b(array_merge([
  698. 1 => 1,
  699. 2 => 2,
  700. ], $this->getOtherArray()
  701. ))
  702. ;
  703. })
  704. ;
  705. ?>',
  706. '<?php
  707. $data = Service
  708. ::method2(function () {
  709. Foo::otherCall()
  710. ->a()
  711. ->b(array_merge([
  712. 1 => 1,
  713. 2 => 2,
  714. ], $this->getOtherArray()
  715. ));
  716. });
  717. ?>',
  718. ],
  719. [
  720. '<?php
  721. Service
  722. ::method1(null, null, [
  723. null => null,
  724. 1 => $data->getId() > 0,
  725. ])
  726. ->method2(4, Type::class)
  727. ;
  728. ',
  729. '<?php
  730. Service
  731. ::method1(null, null, [
  732. null => null,
  733. 1 => $data->getId() > 0,
  734. ])
  735. ->method2(4, Type::class);
  736. ',
  737. ],
  738. [
  739. '<?php
  740. Service
  741. ::method1()
  742. ->method2()
  743. ;
  744. ?>',
  745. '<?php
  746. Service
  747. ::method1()
  748. ->method2();
  749. ?>',
  750. ],
  751. [
  752. '<?php
  753. function foo($bar)
  754. {
  755. if ($bar === 1) {
  756. $baz
  757. ->bar()
  758. ;
  759. }
  760. return (new Foo($bar))
  761. ->baz()
  762. ;
  763. }
  764. ?>',
  765. '<?php
  766. function foo($bar)
  767. {
  768. if ($bar === 1) {
  769. $baz
  770. ->bar();
  771. }
  772. return (new Foo($bar))
  773. ->baz();
  774. }
  775. ?>',
  776. ],
  777. [
  778. '<?php
  779. $foo = (new Foo($bar))
  780. ->baz()
  781. ;
  782. function foo($bar)
  783. {
  784. $foo = (new Foo($bar))
  785. ->baz()
  786. ;
  787. }
  788. ?>',
  789. '<?php
  790. $foo = (new Foo($bar))
  791. ->baz();
  792. function foo($bar)
  793. {
  794. $foo = (new Foo($bar))
  795. ->baz();
  796. }
  797. ?>',
  798. ],
  799. [
  800. '<?php
  801. $object
  802. ->methodA()
  803. ->methodB()
  804. ;
  805. ',
  806. '<?php
  807. $object
  808. ->methodA()
  809. ->methodB();
  810. ',
  811. ],
  812. [
  813. '<?php $object
  814. ->methodA()
  815. ->methodB()
  816. ;
  817. ',
  818. '<?php $object
  819. ->methodA()
  820. ->methodB();
  821. ',
  822. ],
  823. [
  824. "<?php\n\$this\n ->one()\n ->two(2, )\n;",
  825. "<?php\n\$this\n ->one()\n ->two(2, );",
  826. ],
  827. [
  828. "<?php\n\$this\n ->one(1, )\n ->two()\n;",
  829. "<?php\n\$this\n ->one(1, )\n ->two();",
  830. ],
  831. [
  832. '<?php
  833. $foo->bar();
  834. Service::method1()
  835. ->method2()
  836. ->method3()->method4()
  837. ;
  838. ?>',
  839. '<?php
  840. $foo->bar()
  841. ;
  842. Service::method1()
  843. ->method2()
  844. ->method3()->method4();
  845. ?>',
  846. ],
  847. [
  848. '<?php
  849. $foo->bar();
  850. \Service::method1()
  851. ->method2()
  852. ->method3()->method4()
  853. ;
  854. ?>',
  855. '<?php
  856. $foo->bar()
  857. ;
  858. \Service::method1()
  859. ->method2()
  860. ->method3()->method4();
  861. ?>',
  862. ],
  863. [
  864. '<?php
  865. $foo->bar();
  866. Ns\Service::method1()
  867. ->method2()
  868. ->method3()->method4()
  869. ;
  870. ?>',
  871. '<?php
  872. $foo->bar()
  873. ;
  874. Ns\Service::method1()
  875. ->method2()
  876. ->method3()->method4();
  877. ?>',
  878. ],
  879. [
  880. '<?php
  881. $foo->bar();
  882. \Ns\Service::method1()
  883. ->method2()
  884. ->method3()->method4()
  885. ;
  886. ?>',
  887. '<?php
  888. $foo->bar()
  889. ;
  890. \Ns\Service::method1()
  891. ->method2()
  892. ->method3()->method4();
  893. ?>',
  894. ],
  895. [
  896. '<?php
  897. $this
  898. ->setName(\'readme2\')
  899. ->setDescription(\'Generates the README\')
  900. ;
  901. ',
  902. '<?php
  903. $this
  904. ->setName(\'readme2\')
  905. ->setDescription(\'Generates the README\')
  906. ;
  907. ',
  908. ],
  909. [
  910. '<?php
  911. $this
  912. ->foo()
  913. ->{$bar ? \'bar\' : \'baz\'}()
  914. ;
  915. ',
  916. ],
  917. ];
  918. }
  919. /**
  920. * @dataProvider provideMessyWhitespacesSemicolonForChainedCallsFixCases
  921. */
  922. public function testMessyWhitespacesSemicolonForChainedCalls(string $expected, ?string $input = null): void
  923. {
  924. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  925. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS]);
  926. $this->doTest($expected, $input);
  927. }
  928. public static function provideMessyWhitespacesSemicolonForChainedCallsFixCases(): array
  929. {
  930. return [
  931. [
  932. "<?php\r\n\r\n \$this\r\n\t->method1()\r\n\t\t->method2()\r\n ;",
  933. "<?php\r\n\r\n \$this\r\n\t->method1()\r\n\t\t->method2();",
  934. ],
  935. [
  936. "<?php\r\n\r\n\t\$this->method1()\r\n\t\t->method2()\r\n\t\t->method(3)\r\n\t;",
  937. "<?php\r\n\r\n\t\$this->method1()\r\n\t\t->method2()\r\n\t\t->method(3);",
  938. ],
  939. [
  940. "<?php\r\n\r\n\t\$data = \$service\r\n\t ->method2(function (\$a) {\r\n\t\t\t\$a->otherCall()\r\n\t\t\t\t->a()\r\n\t\t\t\t->b(array_merge([\r\n\t\t\t\t\t\t1 => 1,\r\n\t\t\t\t\t\t2 => 2,\r\n\t\t\t\t\t], \$this->getOtherArray()\r\n\t\t\t\t))\r\n\t\t\t;\r\n\t\t})\r\n\t;\r\n?>",
  941. "<?php\r\n\r\n\t\$data = \$service\r\n\t ->method2(function (\$a) {\r\n\t\t\t\$a->otherCall()\r\n\t\t\t\t->a()\r\n\t\t\t\t->b(array_merge([\r\n\t\t\t\t\t\t1 => 1,\r\n\t\t\t\t\t\t2 => 2,\r\n\t\t\t\t\t], \$this->getOtherArray()\r\n\t\t\t\t));\r\n\t\t});\r\n?>",
  942. ],
  943. ];
  944. }
  945. /**
  946. * @requires PHP 8.0
  947. */
  948. public function testFix80(): void
  949. {
  950. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS]);
  951. $this->doTest(
  952. '<?php
  953. $foo?->method1()
  954. ?->method2()
  955. ?->method3()
  956. ;
  957. ',
  958. '<?php
  959. $foo?->method1()
  960. ?->method2()
  961. ?->method3();
  962. '
  963. );
  964. }
  965. }