MultilineWhitespaceBeforeSemicolonsFixerTest.php 29 KB

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