MethodChainingIndentationFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Vladimir Boliev <voff.web@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer>
  23. */
  24. final class MethodChainingIndentationFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield [
  39. '<?php
  40. $user->setEmail(\'voff.web@gmail.com\')
  41. ->setPassword(\'233434\')
  42. ->setEmailConfirmed(false)
  43. ->setEmailConfirmationCode(\'123456\')
  44. ->setHashsalt(\'1234\')
  45. ->setTncAccepted(true);
  46. ',
  47. '<?php
  48. $user->setEmail(\'voff.web@gmail.com\')
  49. ->setPassword(\'233434\')
  50. ->setEmailConfirmed(false)
  51. ->setEmailConfirmationCode(\'123456\')
  52. ->setHashsalt(\'1234\')
  53. ->setTncAccepted(true);
  54. ',
  55. ];
  56. yield [
  57. '<?php
  58. $foo
  59. ->bar1() // comment
  60. ->bar2() /*
  61. comment
  62. */
  63. ->bar3()
  64. // comment
  65. ->bar4()
  66. ->bar5()
  67. /** buahaha */
  68. ->bar6()
  69. /** buahaha */
  70. ->bar7();',
  71. '<?php
  72. $foo
  73. ->bar1() // comment
  74. ->bar2() /*
  75. comment
  76. */
  77. ->bar3()
  78. // comment
  79. ->bar4()
  80. ->bar5()
  81. /** buahaha */ ->bar6()
  82. /** buahaha */->bar7();',
  83. ];
  84. yield [
  85. '<?php
  86. $foo
  87. ->bar1()
  88. ->bar2();',
  89. '<?php
  90. $foo
  91. ->bar1()
  92. ->bar2();',
  93. ];
  94. yield [
  95. '<?php $foo
  96. ->bar();',
  97. '<?php $foo
  98. ->bar();',
  99. ];
  100. yield [
  101. '<?php $foo->bar()->baz()
  102. ->qux();',
  103. '<?php $foo->bar()->baz()
  104. ->qux();',
  105. ];
  106. yield [
  107. '<?php
  108. someCodeHereAndMultipleBreaks();
  109. $foo
  110. ->bar1()
  111. ->bar2();',
  112. ];
  113. yield [
  114. '<?php
  115. if (null !== $files) {
  116. return $files;
  117. }
  118. $finder = Finder::create()
  119. ->files()
  120. ;',
  121. ];
  122. yield [
  123. '<?php
  124. $finder = Finder::create()
  125. ->files()
  126. ;',
  127. ];
  128. yield [
  129. '<?php
  130. $replacements = $replacements
  131. ->setAllowedTypes([\'array\'])
  132. ->setNormalizer(function (Options $options, $value) use ($toTypes, $default) {
  133. return $normalizedValue;
  134. })
  135. ->setDefault($default)
  136. ->setWhitespacesConfig(
  137. new WhitespacesFixerConfig($config[\'indent\'], $config[\'lineEnding\'])
  138. )
  139. ;',
  140. ];
  141. yield [
  142. '<?php
  143. return foo()
  144. ->bar (
  145. new foo()
  146. )
  147. ->bar();
  148. ',
  149. ];
  150. yield [
  151. '<?php
  152. return new Foo("param", [
  153. (new Bar("param1", "param2"))
  154. ->Foo([
  155. (new Bar())->foo(),
  156. ])
  157. ]);
  158. ',
  159. ];
  160. yield [
  161. '<?php
  162. (new Foo(
  163. \'argument on line 1\',
  164. \'argument on line 2\'
  165. ))
  166. ->foo()
  167. ->bar()
  168. ;',
  169. '<?php
  170. (new Foo(
  171. \'argument on line 1\',
  172. \'argument on line 2\'
  173. ))
  174. ->foo()
  175. ->bar()
  176. ;',
  177. ];
  178. yield [
  179. '<div>
  180. <?php $object
  181. ->method()
  182. ->method();
  183. ?>
  184. </div>
  185. <?= $object
  186. ->method()
  187. ->method();
  188. ?>',
  189. '<div>
  190. <?php $object
  191. ->method()
  192. ->method();
  193. ?>
  194. </div>
  195. <?= $object
  196. ->method()
  197. ->method();
  198. ?>',
  199. ];
  200. yield [
  201. '<?php
  202. $user->setFoo(1)
  203. ->setBar([
  204. 1 => 1,
  205. ])
  206. ->setBaz(true)
  207. ->setX(array(
  208. 2 => 2,
  209. ))
  210. ->setY();
  211. ',
  212. '<?php
  213. $user->setFoo(1)
  214. ->setBar([
  215. 1 => 1,
  216. ])
  217. ->setBaz(true)
  218. ->setX(array(
  219. 2 => 2,
  220. ))
  221. ->setY();
  222. ',
  223. ];
  224. yield [
  225. '<?php
  226. $user->setEmail("voff.web@gmail.com", )
  227. ->setPassword("233434" ,)
  228. ->setEmailConfirmed(false , )
  229. ->setEmailConfirmationCode("123456", );
  230. ',
  231. '<?php
  232. $user->setEmail("voff.web@gmail.com", )
  233. ->setPassword("233434" ,)
  234. ->setEmailConfirmed(false , )
  235. ->setEmailConfirmationCode("123456", );
  236. ',
  237. ];
  238. yield [
  239. '<?php
  240. $obj = (new Foo)
  241. ->setBar((new Bar)
  242. ->baz());
  243. ',
  244. '<?php
  245. $obj = (new Foo)
  246. ->setBar((new Bar)
  247. ->baz());
  248. ',
  249. ];
  250. yield [
  251. '<?php
  252. $obj
  253. ->foo("bar", function ($baz) {
  254. return $baz
  255. ->on("table1", "table2");
  256. })
  257. ->where("a", "b");
  258. ',
  259. '<?php
  260. $obj
  261. ->foo("bar", function ($baz) {
  262. return $baz
  263. ->on("table1", "table2");
  264. })
  265. ->where("a", "b");
  266. ',
  267. ];
  268. yield [
  269. '<?php
  270. $obj
  271. ->foo("baz", fn ($bar) => $bar
  272. ->baz("foobar"))
  273. ->baz();
  274. ',
  275. '<?php
  276. $obj
  277. ->foo("baz", fn ($bar) => $bar
  278. ->baz("foobar"))
  279. ->baz();
  280. ',
  281. ];
  282. yield [
  283. '<?php
  284. $obj
  285. ->foo("baz", fn (string $bar) => otherFunc($bar)
  286. ->baz("foobar"))
  287. ->baz();
  288. ',
  289. '<?php
  290. $obj
  291. ->foo("baz", fn (string $bar) => otherFunc($bar)
  292. ->baz("foobar"))
  293. ->baz();
  294. ',
  295. ];
  296. yield [
  297. '<?php
  298. $obj
  299. ->foo("baz", fn (SomeClass $bar) => $bar
  300. ->baz("foobar"))
  301. ->baz();
  302. ',
  303. '<?php
  304. $obj
  305. ->foo("baz", fn (SomeClass $bar) => $bar
  306. ->baz("foobar"))
  307. ->baz();
  308. ',
  309. ];
  310. yield [
  311. '<?php
  312. $obj
  313. ->foo("baz", fn (?AnotherClass $bar) => $bar
  314. ->baz("foobar"))
  315. ->baz();
  316. ',
  317. '<?php
  318. $obj
  319. ->foo("baz", fn (?AnotherClass $bar) => $bar
  320. ->baz("foobar"))
  321. ->baz();
  322. ',
  323. ];
  324. yield [
  325. '<?php
  326. $obj
  327. /*buahaha*/
  328. ->foo("baz", fn ($bar) => $bar
  329. ->baz/*buahaha*/("foobar"))
  330. ->/**buahaha*/baz();
  331. ',
  332. '<?php
  333. $obj
  334. /*buahaha*/ ->foo("baz", fn ($bar) => $bar
  335. ->baz/*buahaha*/("foobar"))
  336. ->/**buahaha*/baz();
  337. ',
  338. ];
  339. yield [
  340. '<?php
  341. $obj
  342. -> foo("baz", fn ($bar) => $bar
  343. ->baz ("foobar"))
  344. -> baz ();
  345. ',
  346. '<?php
  347. $obj
  348. -> foo("baz", fn ($bar) => $bar
  349. ->baz ("foobar"))
  350. -> baz ();
  351. ',
  352. ];
  353. yield [
  354. '<?php return $foo
  355. ->bar;',
  356. ];
  357. yield [
  358. '<?php return $foo
  359. ->bar;
  360. if (foo()) {
  361. echo 123;
  362. }
  363. ',
  364. ];
  365. yield [
  366. '<?php return $foo
  367. ->bar?>
  368. <?php
  369. if (foo()) {
  370. echo 123;
  371. }
  372. ',
  373. ];
  374. yield [
  375. '<?php return [$foo
  376. ->bar,
  377. 1,
  378. 2,
  379. abc(),
  380. ];
  381. ',
  382. ];
  383. yield [
  384. '<?php
  385. $obj
  386. ->foo()
  387. ->bar;
  388. ',
  389. '<?php
  390. $obj
  391. ->foo()
  392. ->bar;
  393. ',
  394. ];
  395. yield [
  396. '<?php
  397. return $obj
  398. ->foo()
  399. ->bar
  400. ->baz();
  401. ',
  402. '<?php
  403. return $obj
  404. ->foo()
  405. ->bar
  406. ->baz();
  407. ',
  408. ];
  409. yield [
  410. '<?php
  411. foo()
  412. ->bar()
  413. ->baz;
  414. $obj
  415. ->foo(\'123\', 456)
  416. ->bar(\'789\')
  417. ->baz;
  418. ',
  419. '<?php
  420. foo()
  421. ->bar()
  422. ->baz;
  423. $obj
  424. ->foo(\'123\', 456)
  425. ->bar(\'789\')
  426. ->baz;
  427. ',
  428. ];
  429. }
  430. /**
  431. * @dataProvider provideWithWhitespacesConfigCases
  432. */
  433. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  434. {
  435. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  436. $this->doTest($expected, $input);
  437. }
  438. /**
  439. * @return iterable<array{string, string}>
  440. */
  441. public static function provideWithWhitespacesConfigCases(): iterable
  442. {
  443. yield [
  444. "<?php\r\n\$user->setEmail('voff.web@gmail.com')\r\n\t->setPassword('233434')\r\n\t->setEmailConfirmed(false)\r\n\t->setEmailConfirmationCode('123456')\r\n\t->setHashsalt('1234')\r\n\t->setTncAccepted(true);",
  445. "<?php\r\n\$user->setEmail('voff.web@gmail.com')\r\n\r\n ->setPassword('233434')\r\n\t\t\t->setEmailConfirmed(false)\r\n\t\t ->setEmailConfirmationCode('123456')\r\n->setHashsalt('1234')\r\n\t\t->setTncAccepted(true);",
  446. ];
  447. }
  448. /**
  449. * @requires PHP 8.0
  450. */
  451. public function testFix80(): void
  452. {
  453. $this->doTest(
  454. '<?php
  455. $user?->setEmail("voff.web@gmail.com")
  456. ?->setPassword("233434")
  457. ?->setEmailConfirmed(false)
  458. ?->setEmailConfirmationCode("123456");
  459. ',
  460. '<?php
  461. $user?->setEmail("voff.web@gmail.com")
  462. ?->setPassword("233434")
  463. ?->setEmailConfirmed(false)
  464. ?->setEmailConfirmationCode("123456");
  465. '
  466. );
  467. }
  468. }