MultilineWhitespaceBeforeSemicolonsFixerTest.php 29 KB

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