MultilineWhitespaceBeforeSemicolonsFixerTest.php 29 KB

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