MethodChainingIndentationFixerTest.php 10 KB

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