BracesFixerTest.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Basic;
  12. use PhpCsFixer\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class BracesFixerTest extends AbstractFixerTestCase
  19. {
  20. /**
  21. * @dataProvider provideFixControlContinuationBracesCases
  22. */
  23. public function testFixControlContinuationBraces($expected, $input = null)
  24. {
  25. $this->doTest($expected, $input);
  26. }
  27. public function provideFixControlContinuationBracesCases()
  28. {
  29. return array(
  30. array(
  31. '<?php
  32. $a = function() {
  33. $a = 1;
  34. while (false);
  35. };',
  36. ),
  37. array(
  38. '<?php
  39. $a = function() {
  40. $a = 1;
  41. for ($i=0;$i<5;++$i);
  42. };',
  43. ),
  44. array(
  45. '<?php
  46. class Foo
  47. {
  48. public function A()
  49. {
  50. ?>
  51. Test<?php echo $foobar; ?>Test
  52. <?php
  53. $a = 1;
  54. }
  55. }',
  56. ),
  57. array(
  58. '<?php
  59. if (true) {
  60. $a = 1;
  61. } else {
  62. $b = 2;
  63. }',
  64. '<?php
  65. if (true) {
  66. $a = 1;
  67. }
  68. else {
  69. $b = 2;
  70. }',
  71. ),
  72. array(
  73. '<?php
  74. try {
  75. throw new \Exception();
  76. } catch (\LogicException $e) {
  77. // do nothing
  78. } catch (\Exception $e) {
  79. // do nothing
  80. }',
  81. '<?php
  82. try {
  83. throw new \Exception();
  84. }catch (\LogicException $e) {
  85. // do nothing
  86. }
  87. catch (\Exception $e) {
  88. // do nothing
  89. }',
  90. ),
  91. array(
  92. '<?php
  93. if (true) {
  94. echo 1;
  95. } elseif (true) {
  96. echo 2;
  97. }',
  98. '<?php
  99. if (true) {
  100. echo 1;
  101. } elseif (true)
  102. {
  103. echo 2;
  104. }',
  105. ),
  106. array(
  107. '<?php
  108. try {
  109. echo 1;
  110. } catch (Exception $e) {
  111. echo 2;
  112. }',
  113. '<?php
  114. try
  115. {
  116. echo 1;
  117. }
  118. catch (Exception $e)
  119. {
  120. echo 2;
  121. }',
  122. ),
  123. array(
  124. '<?php
  125. class Foo
  126. {
  127. public function bar(
  128. FooInterface $foo,
  129. BarInterface $bar,
  130. array $data = []
  131. ) {
  132. }
  133. }',
  134. '<?php
  135. class Foo
  136. {
  137. public function bar(
  138. FooInterface $foo,
  139. BarInterface $bar,
  140. array $data = []
  141. ){
  142. }
  143. }',
  144. ),
  145. array(
  146. '<?php
  147. if (1) {
  148. self::${$key} = $val;
  149. self::${$type}[$rule] = $pattern;
  150. self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
  151. self::${$type}[$rule] = $pattern + self::${$type}["rules"];
  152. }
  153. ',
  154. ),
  155. array(
  156. '<?php
  157. if (1) {
  158. do {
  159. $a = 1;
  160. } while (true);
  161. }',
  162. ),
  163. array(
  164. '<?php
  165. if /* 1 */ (2) {
  166. }',
  167. '<?php
  168. if /* 1 */ (2) {}',
  169. ),
  170. array(
  171. '<?php
  172. if (1) {
  173. echo $items{0}->foo;
  174. echo $collection->items{1}->property;
  175. }
  176. ',
  177. ),
  178. );
  179. }
  180. /**
  181. * @dataProvider provideFixMissingBracesAndIndentCases
  182. */
  183. public function testFixMissingBracesAndIndent($expected, $input = null)
  184. {
  185. $this->doTest($expected, $input);
  186. }
  187. public function provideFixMissingBracesAndIndentCases()
  188. {
  189. return array(
  190. array(
  191. '<?php
  192. if (true):
  193. $foo = 0;
  194. endif;',
  195. ),
  196. array(
  197. '<?php
  198. if (true) :
  199. $foo = 0;
  200. endif;',
  201. ),
  202. array(
  203. '<?php
  204. if (true) : $foo = 1; endif;',
  205. ),
  206. array(
  207. '<?php
  208. if (true) {
  209. $foo = 1;
  210. }',
  211. '<?php
  212. if (true)$foo = 1;',
  213. ),
  214. array(
  215. '<?php
  216. if (true) {
  217. $foo = 2;
  218. }',
  219. '<?php
  220. if (true) $foo = 2;',
  221. ),
  222. array(
  223. '<?php
  224. if (true) {
  225. $foo = 3;
  226. }',
  227. '<?php
  228. if (true){$foo = 3;}',
  229. ),
  230. array(
  231. '<?php
  232. if (true) {
  233. echo 1;
  234. } else {
  235. echo 2;
  236. }',
  237. '<?php
  238. if(true) { echo 1; } else echo 2;',
  239. ),
  240. array(
  241. '<?php
  242. if (true) {
  243. echo 3;
  244. } else {
  245. echo 4;
  246. }',
  247. '<?php
  248. if(true) echo 3; else { echo 4; }',
  249. ),
  250. array(
  251. '<?php
  252. if (true) {
  253. echo 5;
  254. } else {
  255. echo 6;
  256. }',
  257. '<?php
  258. if (true) echo 5; else echo 6;',
  259. ),
  260. array(
  261. '<?php
  262. if (true) {
  263. while (true) {
  264. $foo = 1;
  265. $bar = 2;
  266. }
  267. }',
  268. '<?php
  269. if (true) while (true) { $foo = 1; $bar = 2;}',
  270. ),
  271. array(
  272. '<?php
  273. if (true) {
  274. if (true) {
  275. echo 1;
  276. } else {
  277. echo 2;
  278. }
  279. } else {
  280. echo 3;
  281. }',
  282. '<?php
  283. if (true) if (true) echo 1; else echo 2; else echo 3;',
  284. ),
  285. array(
  286. '<?php
  287. if (true) {
  288. // sth here...
  289. if ($a && ($b || $c)) {
  290. $d = 1;
  291. }
  292. }',
  293. '<?php
  294. if (true) {
  295. // sth here...
  296. if ($a && ($b || $c)) $d = 1;
  297. }',
  298. ),
  299. array(
  300. '<?php
  301. for ($i = 1; $i < 10; ++$i) {
  302. echo $i;
  303. }
  304. for ($i = 1; $i < 10; ++$i) {
  305. echo $i;
  306. }',
  307. '<?php
  308. for ($i = 1; $i < 10; ++$i) echo $i;
  309. for ($i = 1; $i < 10; ++$i) { echo $i; }',
  310. ),
  311. array(
  312. '<?php
  313. for ($i = 1; $i < 5; ++$i) {
  314. for ($i = 1; $i < 10; ++$i) {
  315. echo $i;
  316. }
  317. }',
  318. '<?php
  319. for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
  320. ),
  321. array(
  322. '<?php
  323. do {
  324. echo 1;
  325. } while (false);',
  326. '<?php
  327. do { echo 1; } while (false);',
  328. ),
  329. array(
  330. '<?php
  331. while ($foo->next());',
  332. ),
  333. array(
  334. '<?php
  335. foreach ($foo as $bar) {
  336. echo $bar;
  337. }',
  338. '<?php
  339. foreach ($foo as $bar) echo $bar;',
  340. ),
  341. array(
  342. '<?php
  343. if (true) {
  344. $a = 1;
  345. }',
  346. '<?php
  347. if (true) {$a = 1;}',
  348. ),
  349. array(
  350. '<?php
  351. if (true) {
  352. $a = 1;
  353. }',
  354. '<?php
  355. if (true) {
  356. $a = 1;
  357. }',
  358. ),
  359. array(
  360. '<?php
  361. if (true) {
  362. $a = 1;
  363. $b = 2;
  364. while (true) {
  365. $c = 3;
  366. }
  367. $d = 4;
  368. }',
  369. '<?php
  370. if (true) {
  371. $a = 1;
  372. $b = 2;
  373. while (true) {
  374. $c = 3;
  375. }
  376. $d = 4;
  377. }',
  378. ),
  379. array(
  380. '<?php
  381. if (true) {
  382. $a = 1;
  383. $b = 2;
  384. }',
  385. ),
  386. array(
  387. '<?php
  388. if (1) {
  389. $a = 1;
  390. // comment at end
  391. }',
  392. ),
  393. array(
  394. '<?php
  395. if (1) {
  396. if (2) {
  397. $a = "a";
  398. } elseif (3) {
  399. $b = "b";
  400. // comment
  401. } else {
  402. $c = "c";
  403. }
  404. $d = "d";
  405. }',
  406. ),
  407. array(
  408. '<?php
  409. foreach ($numbers as $num) {
  410. for ($i = 0; $i < $num; ++$i) {
  411. $a = "a";
  412. }
  413. $b = "b";
  414. }',
  415. ),
  416. array(
  417. '<?php
  418. if (1) {
  419. if (2) {
  420. $foo = 2;
  421. if (3) {
  422. $foo = 3;
  423. }
  424. }
  425. }',
  426. ),
  427. array(
  428. '<?php
  429. declare(ticks=1) {
  430. $ticks = 1;
  431. }',
  432. '<?php
  433. declare(ticks=1) {
  434. $ticks = 1;
  435. }',
  436. ),
  437. array(
  438. '<?php
  439. if (true) {
  440. foo();
  441. } elseif (true) {
  442. bar();
  443. }',
  444. '<?php
  445. if (true)
  446. {
  447. foo();
  448. } elseif (true)
  449. {
  450. bar();
  451. }',
  452. ),
  453. array(
  454. '<?php
  455. while (true) {
  456. foo();
  457. }',
  458. '<?php
  459. while (true)
  460. {
  461. foo();
  462. }',
  463. ),
  464. array(
  465. '<?php
  466. do {
  467. echo $test;
  468. } while ($test = $this->getTest());',
  469. '<?php
  470. do
  471. {
  472. echo $test;
  473. }
  474. while ($test = $this->getTest());',
  475. ),
  476. array(
  477. '<?php
  478. do {
  479. echo $test;
  480. } while ($test = $this->getTest());',
  481. '<?php
  482. do
  483. {
  484. echo $test;
  485. }while ($test = $this->getTest());',
  486. ),
  487. array(
  488. '<?php
  489. class ClassName
  490. {
  491. /**
  492. * comment
  493. */
  494. public $foo = null;
  495. }',
  496. '<?php
  497. class ClassName
  498. {
  499. /**
  500. * comment
  501. */
  502. public $foo = null;
  503. }',
  504. ),
  505. array(
  506. '<?php
  507. while ($true) {
  508. try {
  509. throw new \Exception();
  510. } catch (\Exception $e) {
  511. // do nothing
  512. }
  513. }',
  514. ),
  515. array(
  516. '<?php
  517. interface Foo
  518. {
  519. public function setConfig(ConfigInterface $config);
  520. }',
  521. ),
  522. array(
  523. '<?php
  524. function bar()
  525. {
  526. $a = 1; //comment
  527. }',
  528. ),
  529. array(
  530. '<?php
  531. function & lambda()
  532. {
  533. return function () {
  534. };
  535. }',
  536. ),
  537. array(
  538. '<?php
  539. function nested()
  540. {
  541. $a = "a{$b->c()}d";
  542. }',
  543. ),
  544. array(
  545. '<?php
  546. function foo()
  547. {
  548. $a = $b->{$c->d}($e);
  549. $f->{$g} = $h;
  550. $i->{$j}[$k] = $l;
  551. $m = $n->{$o};
  552. $p = array($q->{$r}, $s->{$t});
  553. $u->{$v}->w = 1;
  554. }',
  555. ),
  556. array(
  557. '<?php
  558. function mixed()
  559. {
  560. $a = $b->{"a{$c}d"}();
  561. }',
  562. ),
  563. array(
  564. '<?php
  565. function mixedComplex()
  566. {
  567. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  568. }',
  569. ),
  570. array(
  571. '<?php
  572. function mixedComplex()
  573. {
  574. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  575. }',
  576. ),
  577. array(
  578. '<?php
  579. if (true):
  580. echo 1;
  581. else:
  582. echo 2;
  583. endif;
  584. ',
  585. ),
  586. array(
  587. '<?php
  588. if ($test) { //foo
  589. echo 1;
  590. }',
  591. ),
  592. array(
  593. '<?php
  594. if (true) {
  595. // foo
  596. // bar
  597. if (true) {
  598. print("foo");
  599. print("bar");
  600. }
  601. }',
  602. '<?php
  603. if (true)
  604. // foo
  605. // bar
  606. {
  607. if (true)
  608. {
  609. print("foo");
  610. print("bar");
  611. }
  612. }',
  613. ),
  614. array(
  615. '<?php
  616. if (true) {
  617. // foo
  618. /* bar */
  619. if (true) {
  620. print("foo");
  621. print("bar");
  622. }
  623. }',
  624. '<?php
  625. if (true)
  626. // foo
  627. /* bar */{
  628. if (true)
  629. {
  630. print("foo");
  631. print("bar");
  632. }
  633. }',
  634. ),
  635. array(
  636. '<?php if (true) {
  637. echo "s";
  638. } ?>x',
  639. '<?php if (true) echo "s" ?>x',
  640. ),
  641. array(
  642. '<?php
  643. class Foo
  644. {
  645. public function getFaxNumbers()
  646. {
  647. if (1) {
  648. return $this->phoneNumbers->filter(function ($phone) {
  649. $a = 1;
  650. $b = 1;
  651. $c = 1;
  652. return ($phone->getType() === 1) ? true : false;
  653. });
  654. }
  655. }
  656. }',
  657. '<?php
  658. class Foo
  659. {
  660. public function getFaxNumbers()
  661. {
  662. if (1)
  663. return $this->phoneNumbers->filter(function ($phone) {
  664. $a = 1;
  665. $b = 1;
  666. $c = 1;
  667. return ($phone->getType() === 1) ? true : false;
  668. });
  669. }
  670. }',
  671. ),
  672. array(
  673. '<?php
  674. if (true) {
  675. if (true) {
  676. echo 1;
  677. } elseif (true) {
  678. echo 2;
  679. } else {
  680. echo 3;
  681. }
  682. }
  683. ',
  684. '<?php
  685. if(true)
  686. if(true)
  687. echo 1;
  688. elseif(true)
  689. echo 2;
  690. else
  691. echo 3;
  692. ',
  693. ),
  694. array(
  695. '<?php
  696. if (true) {
  697. if (true) {
  698. echo 1;
  699. } elseif (true) {
  700. echo 2;
  701. } else {
  702. echo 3;
  703. }
  704. }
  705. echo 4;
  706. ',
  707. '<?php
  708. if(true)
  709. if(true)
  710. echo 1;
  711. elseif(true)
  712. echo 2;
  713. else
  714. echo 3;
  715. echo 4;
  716. ',
  717. ),
  718. array(
  719. '<?php
  720. if (true) {
  721. if (true) {
  722. echo 1;
  723. } elseif (true) {
  724. echo 2;
  725. } else {
  726. echo 3;
  727. }
  728. }',
  729. '<?php
  730. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  731. ),
  732. array(
  733. '<?php
  734. if (true) {
  735. if (true) {
  736. echo 1;
  737. } else {
  738. echo 2;
  739. }
  740. } else {
  741. echo 3;
  742. }',
  743. '<?php
  744. if(true) if(true) echo 1; else echo 2; else echo 3;',
  745. ),
  746. array(
  747. '<?php
  748. foreach ($data as $val) {
  749. // test val
  750. if ($val === "errors") {
  751. echo "!";
  752. }
  753. }',
  754. '<?php
  755. foreach ($data as $val)
  756. // test val
  757. if ($val === "errors") {
  758. echo "!";
  759. }',
  760. ),
  761. array(
  762. '<?php
  763. if (1) {
  764. foreach ($data as $val) {
  765. // test val
  766. if ($val === "errors") {
  767. echo "!";
  768. }
  769. }
  770. }',
  771. '<?php
  772. if (1)
  773. foreach ($data as $val)
  774. // test val
  775. if ($val === "errors") {
  776. echo "!";
  777. }',
  778. ),
  779. array(
  780. '<?php
  781. class Foo
  782. {
  783. public function main()
  784. {
  785. echo "Hello";
  786. }
  787. }',
  788. '<?php
  789. class Foo
  790. {
  791. public function main()
  792. {
  793. echo "Hello";
  794. }
  795. }',
  796. ),
  797. array(
  798. '<?php
  799. class Foo
  800. {
  801. public function main()
  802. {
  803. echo "Hello";
  804. }
  805. }',
  806. '<?php
  807. class Foo
  808. {
  809. public function main()
  810. {
  811. echo "Hello";
  812. }
  813. }',
  814. ),
  815. array(
  816. '<?php
  817. class Foo
  818. {
  819. public $bar;
  820. public $baz;
  821. }',
  822. '<?php
  823. class Foo
  824. {
  825. public $bar;
  826. public $baz;
  827. }',
  828. ),
  829. array(
  830. '<?php
  831. function myFunction($foo, $bar)
  832. {
  833. return \Foo::{$foo}($bar);
  834. }',
  835. ),
  836. );
  837. }
  838. /**
  839. * @dataProvider provideFixClassyBracesCases
  840. */
  841. public function testFixClassyBraces($expected, $input = null)
  842. {
  843. $this->doTest($expected, $input);
  844. }
  845. public function provideFixClassyBracesCases()
  846. {
  847. return array(
  848. array(
  849. '<?php
  850. class FooA
  851. {
  852. }',
  853. '<?php
  854. class FooA {}',
  855. ),
  856. array(
  857. '<?php
  858. class FooB
  859. {
  860. }',
  861. '<?php
  862. class FooB{}',
  863. ),
  864. array(
  865. '<?php
  866. class FooC
  867. {
  868. }',
  869. '<?php
  870. class FooC
  871. {}',
  872. ),
  873. array(
  874. '<?php
  875. interface FooD
  876. {
  877. }',
  878. '<?php
  879. interface FooD {}',
  880. ),
  881. array(
  882. '<?php
  883. class TestClass extends BaseTestClass implements TestInterface
  884. {
  885. private $foo;
  886. }',
  887. '<?php
  888. class TestClass extends BaseTestClass implements TestInterface { private $foo;}',
  889. ),
  890. array(
  891. '<?php
  892. abstract class Foo
  893. {
  894. public function getProcess($foo)
  895. {
  896. return true;
  897. }
  898. }',
  899. ),
  900. array('<?php
  901. function foo()
  902. {
  903. return "$c ($d)";
  904. }',
  905. ),
  906. );
  907. }
  908. /**
  909. * @dataProvider provideFixClassyBraces54Cases
  910. * @requires PHP 5.4
  911. */
  912. public function testFixClassyBraces54($expected, $input = null)
  913. {
  914. $this->doTest($expected, $input);
  915. }
  916. public function provideFixClassyBraces54Cases()
  917. {
  918. return array(
  919. array(
  920. '<?php
  921. trait TFoo
  922. {
  923. public $a;
  924. }',
  925. '<?php
  926. trait TFoo {public $a;}',
  927. ),
  928. );
  929. }
  930. /**
  931. * @dataProvider provideFixAnonFunctionInShortArraySyntax54Cases
  932. * @requires PHP 5.4
  933. */
  934. public function testFixAnonFunctionInShortArraySyntax54($expected, $input = null)
  935. {
  936. $this->doTest($expected, $input);
  937. }
  938. public function provideFixAnonFunctionInShortArraySyntax54Cases()
  939. {
  940. return array(
  941. array(
  942. '<?php
  943. function myFunction()
  944. {
  945. return [
  946. [
  947. "callback" => function ($data) {
  948. return true;
  949. }
  950. ],
  951. [
  952. "callback" => function ($data) {
  953. return true;
  954. },
  955. ],
  956. ];
  957. }',
  958. '<?php
  959. function myFunction()
  960. {
  961. return [
  962. [
  963. "callback" => function ($data) {
  964. return true;
  965. }
  966. ],
  967. [
  968. "callback" => function ($data) { return true; },
  969. ],
  970. ];
  971. }',
  972. ),
  973. );
  974. }
  975. /**
  976. * @dataProvider provideFixCommentBeforeBraceCases
  977. */
  978. public function testFixCommentBeforeBrace($expected, $input = null)
  979. {
  980. $this->doTest($expected, $input);
  981. }
  982. public function provideFixCommentBeforeBraceCases()
  983. {
  984. return array(
  985. array('<?php '),
  986. array(
  987. '<?php
  988. if ($test) { // foo
  989. echo 1;
  990. }',
  991. '<?php
  992. if ($test) // foo
  993. {
  994. echo 1;
  995. }',
  996. ),
  997. array(
  998. '<?php
  999. $foo = function ($x) use ($y) { // foo
  1000. echo 1;
  1001. };',
  1002. '<?php
  1003. $foo = function ($x) use ($y) // foo
  1004. {
  1005. echo 1;
  1006. };',
  1007. ),
  1008. array(
  1009. '<?php
  1010. $foo = new class ($a) extends Foo implements Bar { // foo
  1011. private $x;
  1012. };',
  1013. '<?php
  1014. $foo = new class ($a) extends Foo implements Bar // foo
  1015. {
  1016. private $x;
  1017. };',
  1018. ),
  1019. );
  1020. }
  1021. /**
  1022. * @dataProvider provideFixWhitespaceBeforeBraceCases
  1023. */
  1024. public function testFixWhitespaceBeforeBrace($expected, $input = null)
  1025. {
  1026. $this->doTest($expected, $input);
  1027. }
  1028. public function provideFixWhitespaceBeforeBraceCases()
  1029. {
  1030. return array(
  1031. array(
  1032. '<?php
  1033. if (true) {
  1034. echo 1;
  1035. }',
  1036. '<?php
  1037. if (true)
  1038. {
  1039. echo 1;
  1040. }',
  1041. ),
  1042. array(
  1043. '<?php
  1044. if (true) {
  1045. echo 1;
  1046. }',
  1047. '<?php
  1048. if (true){
  1049. echo 1;
  1050. }',
  1051. ),
  1052. array(
  1053. '<?php
  1054. if (true) {
  1055. echo 1;
  1056. }',
  1057. '<?php
  1058. if (true) {
  1059. echo 1;
  1060. }',
  1061. ),
  1062. array(
  1063. '<?php
  1064. while ($file = $this->getFile()) {
  1065. }',
  1066. '<?php
  1067. while ($file = $this->getFile())
  1068. {
  1069. }',
  1070. ),
  1071. array(
  1072. '<?php
  1073. switch (n) {
  1074. case label1:
  1075. echo 1;
  1076. echo 2;
  1077. break;
  1078. default:
  1079. echo 3;
  1080. echo 4;
  1081. }',
  1082. '<?php
  1083. switch (n)
  1084. {
  1085. case label1:
  1086. echo 1;
  1087. echo 2;
  1088. break;
  1089. default:
  1090. echo 3;
  1091. echo 4;
  1092. }',
  1093. ),
  1094. );
  1095. }
  1096. /**
  1097. * @dataProvider provideFixFunctionsCases
  1098. */
  1099. public function testFixFunctions($expected, $input = null)
  1100. {
  1101. $this->doTest($expected, $input);
  1102. }
  1103. public function provideFixFunctionsCases()
  1104. {
  1105. return array(
  1106. array(
  1107. '<?php
  1108. function download()
  1109. {
  1110. }',
  1111. '<?php
  1112. function download() {
  1113. }',
  1114. ),
  1115. array(
  1116. '<?php
  1117. class Foo
  1118. {
  1119. public function AAAA()
  1120. {
  1121. }
  1122. public function BBBB()
  1123. {
  1124. }
  1125. public function CCCC()
  1126. {
  1127. }
  1128. }',
  1129. '<?php
  1130. class Foo
  1131. {
  1132. public function AAAA(){
  1133. }
  1134. public function BBBB() {
  1135. }
  1136. public function CCCC()
  1137. {
  1138. }
  1139. }',
  1140. ),
  1141. array(
  1142. '<?php
  1143. filter(function () {
  1144. return true;
  1145. });
  1146. ',
  1147. ),
  1148. array(
  1149. '<?php
  1150. filter(function ($a) {
  1151. });',
  1152. '<?php
  1153. filter(function ($a)
  1154. {});',
  1155. ),
  1156. array(
  1157. '<?php
  1158. filter(function ($b) {
  1159. });',
  1160. '<?php
  1161. filter(function ($b){});',
  1162. ),
  1163. array(
  1164. '<?php
  1165. foo(array_map(function ($object) use ($x, $y) {
  1166. return array_filter($object->bar(), function ($o) {
  1167. return $o->isBaz();
  1168. });
  1169. }, $collection));',
  1170. '<?php
  1171. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  1172. ),
  1173. array(
  1174. '<?php
  1175. class Foo
  1176. {
  1177. public static function bar()
  1178. {
  1179. return 1;
  1180. }
  1181. }',
  1182. ),
  1183. array(
  1184. '<?php
  1185. usort($this->fixers, function &($a, $b) use ($selfName) {
  1186. return 1;
  1187. });',
  1188. ),
  1189. array(
  1190. '<?php
  1191. usort(
  1192. $this->fixers,
  1193. function &($a, $b) use ($selfName) {
  1194. return 1;
  1195. }
  1196. );',
  1197. ),
  1198. array(
  1199. '<?php
  1200. use function Foo\bar;
  1201. if (true) {
  1202. }',
  1203. ),
  1204. array(
  1205. '<?php
  1206. $fnc = function ($a, $b) { // random comment
  1207. return 0;
  1208. };',
  1209. '<?php
  1210. $fnc = function ($a, $b) // random comment
  1211. {
  1212. return 0;
  1213. };',
  1214. ),
  1215. array(
  1216. '<?php
  1217. $fnc = function ($a, $b) { # random comment
  1218. return 0;
  1219. };',
  1220. '<?php
  1221. $fnc = function ($a, $b) # random comment
  1222. {
  1223. return 0;
  1224. };',
  1225. ),
  1226. array(
  1227. '<?php
  1228. $fnc = function ($a, $b) /* random comment */ {
  1229. return 0;
  1230. };',
  1231. '<?php
  1232. $fnc = function ($a, $b) /* random comment */
  1233. {
  1234. return 0;
  1235. };',
  1236. ),
  1237. array(
  1238. '<?php
  1239. $fnc = function ($a, $b) /** random comment */ {
  1240. return 0;
  1241. };',
  1242. '<?php
  1243. $fnc = function ($a, $b) /** random comment */
  1244. {
  1245. return 0;
  1246. };',
  1247. ),
  1248. );
  1249. }
  1250. /**
  1251. * @dataProvider provideFixSpaceAroundTokenCases
  1252. */
  1253. public function testFixSpaceAroundToken($expected, $input = null)
  1254. {
  1255. $this->doTest($expected, $input);
  1256. }
  1257. public function provideFixSpaceAroundTokenCases()
  1258. {
  1259. return array(
  1260. array(
  1261. '<?php
  1262. try {
  1263. throw new Exception();
  1264. } catch (Exception $e) {
  1265. log($e);
  1266. }',
  1267. '<?php
  1268. try{
  1269. throw new Exception();
  1270. }catch (Exception $e){
  1271. log($e);
  1272. }',
  1273. ),
  1274. array(
  1275. '<?php
  1276. do {
  1277. echo 1;
  1278. } while ($test);',
  1279. '<?php
  1280. do{
  1281. echo 1;
  1282. }while($test);',
  1283. ),
  1284. array(
  1285. '<?php
  1286. if (true === true
  1287. && true === true
  1288. ) {
  1289. }',
  1290. '<?php
  1291. if(true === true
  1292. && true === true
  1293. ) {
  1294. }',
  1295. ),
  1296. array(
  1297. '<?php
  1298. if (1) {
  1299. }
  1300. if ($this->tesT ($test)) {
  1301. }',
  1302. '<?php
  1303. if(1){
  1304. }
  1305. if ($this->tesT ($test)) {
  1306. }',
  1307. ),
  1308. array(
  1309. '<?php
  1310. if (true) {
  1311. } elseif (false) {
  1312. } else {
  1313. }',
  1314. '<?php
  1315. if(true){
  1316. }elseif(false){
  1317. }else{
  1318. }',
  1319. ),
  1320. array(
  1321. '<?php
  1322. $foo = function& () use ($bar) {
  1323. };',
  1324. '<?php
  1325. $foo = function& ()use($bar){};',
  1326. ),
  1327. array(
  1328. '<?php
  1329. // comment
  1330. declare(strict_types=1);
  1331. // comment
  1332. while (true) {
  1333. }',
  1334. ),
  1335. array(
  1336. '<?php
  1337. declare(ticks=1) {
  1338. }',
  1339. '<?php
  1340. declare ( ticks = 1 ) {
  1341. }',
  1342. ),
  1343. );
  1344. }
  1345. /**
  1346. * @dataProvider provide55Cases
  1347. * @requires PHP 5.5
  1348. */
  1349. public function test55($expected, $input = null)
  1350. {
  1351. $this->doTest($expected, $input);
  1352. }
  1353. public function provide55Cases()
  1354. {
  1355. return array(
  1356. array(
  1357. '<?php
  1358. try {
  1359. throw new \Exception();
  1360. } catch (\LogicException $e) {
  1361. // do nothing
  1362. } catch (\Exception $e) {
  1363. // do nothing
  1364. } finally {
  1365. echo "finish!";
  1366. }',
  1367. '<?php
  1368. try {
  1369. throw new \Exception();
  1370. }catch (\LogicException $e) {
  1371. // do nothing
  1372. }
  1373. catch (\Exception $e) {
  1374. // do nothing
  1375. }
  1376. finally {
  1377. echo "finish!";
  1378. }',
  1379. ),
  1380. );
  1381. }
  1382. /**
  1383. * @dataProvider provide70Cases
  1384. * @requires PHP 7.0
  1385. */
  1386. public function test70($expected, $input = null)
  1387. {
  1388. $this->doTest($expected, $input);
  1389. }
  1390. public function provide70Cases()
  1391. {
  1392. return array(
  1393. array(
  1394. '<?php
  1395. function foo($a)
  1396. {
  1397. // foo
  1398. $foo = new class($a) extends Foo {
  1399. public function bar()
  1400. {
  1401. }
  1402. };
  1403. }',
  1404. '<?php
  1405. function foo($a)
  1406. {
  1407. // foo
  1408. $foo = new class($a) extends Foo { public function bar() {} };
  1409. }',
  1410. ),
  1411. array(
  1412. '<?php
  1413. foo(1, new class implements Logger {
  1414. public function log($message)
  1415. {
  1416. log($message);
  1417. }
  1418. }, 3);',
  1419. '<?php
  1420. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  1421. ),
  1422. array(
  1423. '<?php
  1424. $message = (new class() implements FooInterface {
  1425. });',
  1426. '<?php
  1427. $message = (new class() implements FooInterface{});',
  1428. ),
  1429. array(
  1430. '<?php $message = (new class() {
  1431. });',
  1432. '<?php $message = (new class() {});',
  1433. ),
  1434. array(
  1435. '<?php
  1436. if (1) {
  1437. $message = (new class() extends Foo {
  1438. public function bar()
  1439. {
  1440. echo 1;
  1441. }
  1442. });
  1443. }',
  1444. '<?php
  1445. if (1) {
  1446. $message = (new class() extends Foo
  1447. {
  1448. public function bar() { echo 1; }
  1449. });
  1450. }',
  1451. ),
  1452. array(
  1453. '<?php
  1454. class Foo
  1455. {
  1456. public function use()
  1457. {
  1458. }
  1459. public function use1(): string
  1460. {
  1461. }
  1462. }
  1463. ',
  1464. '<?php
  1465. class Foo
  1466. {
  1467. public function use() {
  1468. }
  1469. public function use1(): string {
  1470. }
  1471. }
  1472. ',
  1473. ),
  1474. array(
  1475. '<?php
  1476. $a = function (int $foo): string {
  1477. echo $foo;
  1478. };
  1479. $b = function (int $foo) use ($bar): string {
  1480. echo $foo . $bar;
  1481. };
  1482. function a()
  1483. {
  1484. }
  1485. ',
  1486. '<?php
  1487. $a = function (int $foo): string
  1488. {
  1489. echo $foo;
  1490. };
  1491. $b = function (int $foo) use($bar): string
  1492. {
  1493. echo $foo . $bar;
  1494. };
  1495. function a() {
  1496. }
  1497. ',
  1498. ),
  1499. array(
  1500. '<?php
  1501. class Something
  1502. {
  1503. public function sth(): string
  1504. {
  1505. return function (int $foo) use ($bar): string {
  1506. return $bar;
  1507. };
  1508. }
  1509. }',
  1510. '<?php
  1511. class Something
  1512. {
  1513. public function sth(): string
  1514. {
  1515. return function (int $foo) use ($bar): string { return $bar; };
  1516. }
  1517. }',
  1518. ),
  1519. array(
  1520. '<?php
  1521. use function some\a\{
  1522. test1,
  1523. test2
  1524. };
  1525. test();',
  1526. ),
  1527. array(
  1528. '<?php
  1529. use some\a\{ClassA, ClassB, ClassC as C};
  1530. use function some\a\{fn_a, fn_b, fn_c};
  1531. use const some\a\{ConstA, ConstB, ConstC};
  1532. ',
  1533. ),
  1534. );
  1535. }
  1536. /**
  1537. * @dataProvider providePreserveLineAfterControlBrace
  1538. */
  1539. public function testPreserveLineAfterControlBrace($expected, $input = null)
  1540. {
  1541. $this->doTest($expected, $input);
  1542. }
  1543. public function providePreserveLineAfterControlBrace()
  1544. {
  1545. return array(
  1546. array(
  1547. '<?php
  1548. if (true) {
  1549. // The blank line helps with legibility in nested control structures
  1550. if (true) {
  1551. // if body
  1552. }
  1553. // if body
  1554. }',
  1555. ),
  1556. array(
  1557. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  1558. ),
  1559. );
  1560. }
  1561. }