MethodChainingIndentationFixerTest.php 10 KB

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