BracesFixerTest.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651
  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. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Basic\BracesFixer
  20. */
  21. final class BracesFixerTest extends AbstractFixerTestCase
  22. {
  23. private static $configurationOopPositionSameLine = array('position_after_functions_and_oop_constructs' => 'same');
  24. public function testInvalidConfigurationClassyConstructs()
  25. {
  26. $this->setExpectedExceptionRegExp(
  27. 'PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException',
  28. '#^\[braces\] Invalid configuration: The option "position_after_functions_and_oop_constructs" with value "neither" is invalid\. Accepted values are: "next", "same"\.$#'
  29. );
  30. $this->fixer->configure(array('position_after_functions_and_oop_constructs' => 'neither'));
  31. }
  32. /**
  33. * @param string $expected
  34. * @param null|string $input
  35. * @param null|array $configuration
  36. *
  37. * @dataProvider provideFixControlContinuationBracesCases
  38. */
  39. public function testFixControlContinuationBraces($expected, $input = null, array $configuration = null)
  40. {
  41. if (null !== $configuration) {
  42. $this->fixer->configure($configuration);
  43. }
  44. $this->doTest($expected, $input);
  45. }
  46. public function provideFixControlContinuationBracesCases()
  47. {
  48. return array(
  49. array(
  50. '<?php
  51. $a = function() {
  52. $a = 1;
  53. while (false);
  54. };',
  55. ),
  56. array(
  57. '<?php
  58. $a = function() {
  59. $a = 1;
  60. for ($i=0;$i<5;++$i);
  61. };',
  62. ),
  63. array(
  64. '<?php
  65. class Foo
  66. {
  67. public function A()
  68. {
  69. ?>
  70. Test<?php echo $foobar; ?>Test
  71. <?php
  72. $a = 1;
  73. }
  74. }',
  75. ),
  76. array(
  77. '<?php
  78. if (true) {
  79. $a = 1;
  80. } else {
  81. $b = 2;
  82. }',
  83. '<?php
  84. if (true) {
  85. $a = 1;
  86. }
  87. else {
  88. $b = 2;
  89. }',
  90. ),
  91. array(
  92. '<?php
  93. try {
  94. throw new \Exception();
  95. } catch (\LogicException $e) {
  96. // do nothing
  97. } catch (\Exception $e) {
  98. // do nothing
  99. }',
  100. '<?php
  101. try {
  102. throw new \Exception();
  103. }catch (\LogicException $e) {
  104. // do nothing
  105. }
  106. catch (\Exception $e) {
  107. // do nothing
  108. }',
  109. ),
  110. array(
  111. '<?php
  112. if (true) {
  113. echo 1;
  114. } elseif (true) {
  115. echo 2;
  116. }',
  117. '<?php
  118. if (true) {
  119. echo 1;
  120. } elseif (true)
  121. {
  122. echo 2;
  123. }',
  124. ),
  125. array(
  126. '<?php
  127. try {
  128. echo 1;
  129. } catch (Exception $e) {
  130. echo 2;
  131. }',
  132. '<?php
  133. try
  134. {
  135. echo 1;
  136. }
  137. catch (Exception $e)
  138. {
  139. echo 2;
  140. }',
  141. ),
  142. array(
  143. '<?php
  144. class Foo
  145. {
  146. public function bar(
  147. FooInterface $foo,
  148. BarInterface $bar,
  149. array $data = []
  150. ) {
  151. }
  152. }',
  153. '<?php
  154. class Foo
  155. {
  156. public function bar(
  157. FooInterface $foo,
  158. BarInterface $bar,
  159. array $data = []
  160. ){
  161. }
  162. }',
  163. ),
  164. array(
  165. '<?php
  166. if (1) {
  167. self::${$key} = $val;
  168. self::${$type}[$rule] = $pattern;
  169. self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
  170. self::${$type}[$rule] = $pattern + self::${$type}["rules"];
  171. }
  172. ',
  173. ),
  174. array(
  175. '<?php
  176. if (1) {
  177. do {
  178. $a = 1;
  179. } while (true);
  180. }',
  181. ),
  182. array(
  183. '<?php
  184. if /* 1 */ (2) {
  185. }',
  186. '<?php
  187. if /* 1 */ (2) {}',
  188. ),
  189. array(
  190. '<?php
  191. if (1) {
  192. echo $items{0}->foo;
  193. echo $collection->items{1}->property;
  194. }
  195. ',
  196. ),
  197. array(
  198. '<?php
  199. $a = function() {
  200. $a = 1;
  201. while (false);
  202. };',
  203. null,
  204. self::$configurationOopPositionSameLine,
  205. ),
  206. array(
  207. '<?php
  208. $a = function() {
  209. $a = 1;
  210. for ($i=0;$i<5;++$i);
  211. };',
  212. null,
  213. self::$configurationOopPositionSameLine,
  214. ),
  215. array(
  216. '<?php
  217. class Foo {
  218. public function A() {
  219. ?>
  220. Test<?php echo $foobar; ?>Test
  221. <?php
  222. $a = 1;
  223. }
  224. }',
  225. '<?php
  226. class Foo
  227. {
  228. public function A()
  229. {
  230. ?>
  231. Test<?php echo $foobar; ?>Test
  232. <?php
  233. $a = 1;
  234. }
  235. }',
  236. self::$configurationOopPositionSameLine,
  237. ),
  238. array(
  239. '<?php
  240. if (true) {
  241. $a = 1;
  242. } else {
  243. $b = 2;
  244. }',
  245. '<?php
  246. if (true) {
  247. $a = 1;
  248. }
  249. else {
  250. $b = 2;
  251. }',
  252. self::$configurationOopPositionSameLine,
  253. ),
  254. array(
  255. '<?php
  256. try {
  257. throw new \Exception();
  258. } catch (\LogicException $e) {
  259. // do nothing
  260. } catch (\Exception $e) {
  261. // do nothing
  262. }',
  263. '<?php
  264. try {
  265. throw new \Exception();
  266. }catch (\LogicException $e) {
  267. // do nothing
  268. }
  269. catch (\Exception $e) {
  270. // do nothing
  271. }',
  272. self::$configurationOopPositionSameLine,
  273. ),
  274. array(
  275. '<?php
  276. if (true) {
  277. echo 1;
  278. } elseif (true) {
  279. echo 2;
  280. }',
  281. '<?php
  282. if (true) {
  283. echo 1;
  284. } elseif (true)
  285. {
  286. echo 2;
  287. }',
  288. self::$configurationOopPositionSameLine,
  289. ),
  290. array(
  291. '<?php
  292. try {
  293. echo 1;
  294. } catch (Exception $e) {
  295. echo 2;
  296. }',
  297. '<?php
  298. try
  299. {
  300. echo 1;
  301. }
  302. catch (Exception $e)
  303. {
  304. echo 2;
  305. }',
  306. self::$configurationOopPositionSameLine,
  307. ),
  308. array(
  309. '<?php
  310. class Foo {
  311. public function bar(
  312. FooInterface $foo,
  313. BarInterface $bar,
  314. array $data = []
  315. ) {
  316. }
  317. }',
  318. '<?php
  319. class Foo
  320. {
  321. public function bar(
  322. FooInterface $foo,
  323. BarInterface $bar,
  324. array $data = []
  325. ){
  326. }
  327. }',
  328. self::$configurationOopPositionSameLine,
  329. ),
  330. array(
  331. '<?php
  332. if (1) {
  333. self::${$key} = $val;
  334. self::${$type}[$rule] = $pattern;
  335. self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
  336. self::${$type}[$rule] = $pattern + self::${$type}["rules"];
  337. }
  338. ',
  339. null,
  340. self::$configurationOopPositionSameLine,
  341. ),
  342. array(
  343. '<?php
  344. if (1) {
  345. do {
  346. $a = 1;
  347. } while (true);
  348. }',
  349. null,
  350. self::$configurationOopPositionSameLine,
  351. ),
  352. array(
  353. '<?php
  354. if /* 1 */ (2) {
  355. }',
  356. '<?php
  357. if /* 1 */ (2) {}',
  358. self::$configurationOopPositionSameLine,
  359. ),
  360. array(
  361. '<?php
  362. if (1) {
  363. echo $items{0}->foo;
  364. echo $collection->items{1}->property;
  365. }
  366. ',
  367. null,
  368. self::$configurationOopPositionSameLine,
  369. ),
  370. array(
  371. '<?php class A
  372. /** */
  373. {
  374. }',
  375. null,
  376. self::$configurationOopPositionSameLine,
  377. ),
  378. );
  379. }
  380. /**
  381. * @param string $expected
  382. * @param null|string $input
  383. * @param null|array $configuration
  384. *
  385. * @dataProvider provideFixMissingBracesAndIndentCases
  386. */
  387. public function testFixMissingBracesAndIndent($expected, $input = null, array $configuration = null)
  388. {
  389. if (null !== $configuration) {
  390. $this->fixer->configure($configuration);
  391. }
  392. $this->doTest($expected, $input);
  393. }
  394. public function provideFixMissingBracesAndIndentCases()
  395. {
  396. return array(
  397. array(
  398. '<?php
  399. if (true):
  400. $foo = 0;
  401. endif;',
  402. ),
  403. array(
  404. '<?php
  405. if (true) :
  406. $foo = 0;
  407. endif;',
  408. ),
  409. array(
  410. '<?php
  411. if (true) : $foo = 1; endif;',
  412. ),
  413. array(
  414. '<?php
  415. if (true) {
  416. $foo = 1;
  417. }',
  418. '<?php
  419. if (true)$foo = 1;',
  420. ),
  421. array(
  422. '<?php
  423. if (true) {
  424. $foo = 2;
  425. }',
  426. '<?php
  427. if (true) $foo = 2;',
  428. ),
  429. array(
  430. '<?php
  431. if (true) {
  432. $foo = 3;
  433. }',
  434. '<?php
  435. if (true){$foo = 3;}',
  436. ),
  437. array(
  438. '<?php
  439. if (true) {
  440. echo 1;
  441. } else {
  442. echo 2;
  443. }',
  444. '<?php
  445. if(true) { echo 1; } else echo 2;',
  446. ),
  447. array(
  448. '<?php
  449. if (true) {
  450. echo 3;
  451. } else {
  452. echo 4;
  453. }',
  454. '<?php
  455. if(true) echo 3; else { echo 4; }',
  456. ),
  457. array(
  458. '<?php
  459. if (true) {
  460. echo 5;
  461. } else {
  462. echo 6;
  463. }',
  464. '<?php
  465. if (true) echo 5; else echo 6;',
  466. ),
  467. array(
  468. '<?php
  469. if (true) {
  470. while (true) {
  471. $foo = 1;
  472. $bar = 2;
  473. }
  474. }',
  475. '<?php
  476. if (true) while (true) { $foo = 1; $bar = 2;}',
  477. ),
  478. array(
  479. '<?php
  480. if (true) {
  481. if (true) {
  482. echo 1;
  483. } else {
  484. echo 2;
  485. }
  486. } else {
  487. echo 3;
  488. }',
  489. '<?php
  490. if (true) if (true) echo 1; else echo 2; else echo 3;',
  491. ),
  492. array(
  493. '<?php
  494. if (true) {
  495. // sth here...
  496. if ($a && ($b || $c)) {
  497. $d = 1;
  498. }
  499. }',
  500. '<?php
  501. if (true) {
  502. // sth here...
  503. if ($a && ($b || $c)) $d = 1;
  504. }',
  505. ),
  506. array(
  507. '<?php
  508. for ($i = 1; $i < 10; ++$i) {
  509. echo $i;
  510. }
  511. for ($i = 1; $i < 10; ++$i) {
  512. echo $i;
  513. }',
  514. '<?php
  515. for ($i = 1; $i < 10; ++$i) echo $i;
  516. for ($i = 1; $i < 10; ++$i) { echo $i; }',
  517. ),
  518. array(
  519. '<?php
  520. for ($i = 1; $i < 5; ++$i) {
  521. for ($i = 1; $i < 10; ++$i) {
  522. echo $i;
  523. }
  524. }',
  525. '<?php
  526. for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
  527. ),
  528. array(
  529. '<?php
  530. do {
  531. echo 1;
  532. } while (false);',
  533. '<?php
  534. do { echo 1; } while (false);',
  535. ),
  536. array(
  537. '<?php
  538. while ($foo->next());',
  539. ),
  540. array(
  541. '<?php
  542. foreach ($foo as $bar) {
  543. echo $bar;
  544. }',
  545. '<?php
  546. foreach ($foo as $bar) echo $bar;',
  547. ),
  548. array(
  549. '<?php
  550. if (true) {
  551. $a = 1;
  552. }',
  553. '<?php
  554. if (true) {$a = 1;}',
  555. ),
  556. array(
  557. '<?php
  558. if (true) {
  559. $a = 1;
  560. }',
  561. '<?php
  562. if (true) {
  563. $a = 1;
  564. }',
  565. ),
  566. array(
  567. '<?php
  568. if (true) {
  569. $a = 1;
  570. $b = 2;
  571. while (true) {
  572. $c = 3;
  573. }
  574. $d = 4;
  575. }',
  576. '<?php
  577. if (true) {
  578. $a = 1;
  579. $b = 2;
  580. while (true) {
  581. $c = 3;
  582. }
  583. $d = 4;
  584. }',
  585. ),
  586. array(
  587. '<?php
  588. if (true) {
  589. $a = 1;
  590. $b = 2;
  591. }',
  592. ),
  593. array(
  594. '<?php
  595. if (1) {
  596. $a = 1;
  597. // comment at end
  598. }',
  599. ),
  600. array(
  601. '<?php
  602. if (1) {
  603. if (2) {
  604. $a = "a";
  605. } elseif (3) {
  606. $b = "b";
  607. // comment
  608. } else {
  609. $c = "c";
  610. }
  611. $d = "d";
  612. }',
  613. ),
  614. array(
  615. '<?php
  616. foreach ($numbers as $num) {
  617. for ($i = 0; $i < $num; ++$i) {
  618. $a = "a";
  619. }
  620. $b = "b";
  621. }',
  622. ),
  623. array(
  624. '<?php
  625. if (1) {
  626. if (2) {
  627. $foo = 2;
  628. if (3) {
  629. $foo = 3;
  630. }
  631. }
  632. }',
  633. ),
  634. array(
  635. '<?php
  636. declare(ticks = 1) {
  637. $ticks = 1;
  638. }',
  639. '<?php
  640. declare (
  641. ticks = 1 ) {
  642. $ticks = 1;
  643. }',
  644. ),
  645. array(
  646. '<?php
  647. if (true) {
  648. foo();
  649. } elseif (true) {
  650. bar();
  651. }',
  652. '<?php
  653. if (true)
  654. {
  655. foo();
  656. } elseif (true)
  657. {
  658. bar();
  659. }',
  660. ),
  661. array(
  662. '<?php
  663. while (true) {
  664. foo();
  665. }',
  666. '<?php
  667. while (true)
  668. {
  669. foo();
  670. }',
  671. ),
  672. array(
  673. '<?php
  674. do {
  675. echo $test;
  676. } while ($test = $this->getTest());',
  677. '<?php
  678. do
  679. {
  680. echo $test;
  681. }
  682. while ($test = $this->getTest());',
  683. ),
  684. array(
  685. '<?php
  686. do {
  687. echo $test;
  688. } while ($test = $this->getTest());',
  689. '<?php
  690. do
  691. {
  692. echo $test;
  693. }while ($test = $this->getTest());',
  694. ),
  695. array(
  696. '<?php
  697. class ClassName
  698. {
  699. /**
  700. * comment
  701. */
  702. public $foo = null;
  703. }',
  704. '<?php
  705. class ClassName
  706. {
  707. /**
  708. * comment
  709. */
  710. public $foo = null;
  711. }',
  712. ),
  713. array(
  714. '<?php
  715. while ($true) {
  716. try {
  717. throw new \Exception();
  718. } catch (\Exception $e) {
  719. // do nothing
  720. }
  721. }',
  722. ),
  723. array(
  724. '<?php
  725. interface Foo
  726. {
  727. public function setConfig(ConfigInterface $config);
  728. }',
  729. ),
  730. array(
  731. '<?php
  732. function bar()
  733. {
  734. $a = 1; //comment
  735. }',
  736. ),
  737. array(
  738. '<?php
  739. function & lambda()
  740. {
  741. return function () {
  742. };
  743. }',
  744. ),
  745. array(
  746. '<?php
  747. function nested()
  748. {
  749. $a = "a{$b->c()}d";
  750. }',
  751. ),
  752. array(
  753. '<?php
  754. function foo()
  755. {
  756. $a = $b->{$c->d}($e);
  757. $f->{$g} = $h;
  758. $i->{$j}[$k] = $l;
  759. $m = $n->{$o};
  760. $p = array($q->{$r}, $s->{$t});
  761. $u->{$v}->w = 1;
  762. }',
  763. ),
  764. array(
  765. '<?php
  766. function mixed()
  767. {
  768. $a = $b->{"a{$c}d"}();
  769. }',
  770. ),
  771. array(
  772. '<?php
  773. function mixedComplex()
  774. {
  775. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  776. }',
  777. ),
  778. array(
  779. '<?php
  780. function mixedComplex()
  781. {
  782. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  783. }',
  784. ),
  785. array(
  786. '<?php
  787. if (true):
  788. echo 1;
  789. else:
  790. echo 2;
  791. endif;
  792. ',
  793. ),
  794. array(
  795. '<?php
  796. if ($test) { //foo
  797. echo 1;
  798. }',
  799. ),
  800. array(
  801. '<?php
  802. if (true) {
  803. // foo
  804. // bar
  805. if (true) {
  806. print("foo");
  807. print("bar");
  808. }
  809. }',
  810. '<?php
  811. if (true)
  812. // foo
  813. // bar
  814. {
  815. if (true)
  816. {
  817. print("foo");
  818. print("bar");
  819. }
  820. }',
  821. ),
  822. array(
  823. '<?php
  824. if (true) {
  825. // foo
  826. /* bar */
  827. if (true) {
  828. print("foo");
  829. print("bar");
  830. }
  831. }',
  832. '<?php
  833. if (true)
  834. // foo
  835. /* bar */{
  836. if (true)
  837. {
  838. print("foo");
  839. print("bar");
  840. }
  841. }',
  842. ),
  843. array(
  844. '<?php if (true) {
  845. echo "s";
  846. } ?>x',
  847. '<?php if (true) echo "s" ?>x',
  848. ),
  849. array(
  850. '<?php
  851. class Foo
  852. {
  853. public function getFaxNumbers()
  854. {
  855. if (1) {
  856. return $this->phoneNumbers->filter(function ($phone) {
  857. $a = 1;
  858. $b = 1;
  859. $c = 1;
  860. return ($phone->getType() === 1) ? true : false;
  861. });
  862. }
  863. }
  864. }',
  865. '<?php
  866. class Foo
  867. {
  868. public function getFaxNumbers()
  869. {
  870. if (1)
  871. return $this->phoneNumbers->filter(function ($phone) {
  872. $a = 1;
  873. $b = 1;
  874. $c = 1;
  875. return ($phone->getType() === 1) ? true : false;
  876. });
  877. }
  878. }',
  879. ),
  880. array(
  881. '<?php
  882. if (true) {
  883. if (true) {
  884. echo 1;
  885. } elseif (true) {
  886. echo 2;
  887. } else {
  888. echo 3;
  889. }
  890. }
  891. ',
  892. '<?php
  893. if(true)
  894. if(true)
  895. echo 1;
  896. elseif(true)
  897. echo 2;
  898. else
  899. echo 3;
  900. ',
  901. ),
  902. array(
  903. '<?php
  904. if (true) {
  905. if (true) {
  906. echo 1;
  907. } elseif (true) {
  908. echo 2;
  909. } else {
  910. echo 3;
  911. }
  912. }
  913. echo 4;
  914. ',
  915. '<?php
  916. if(true)
  917. if(true)
  918. echo 1;
  919. elseif(true)
  920. echo 2;
  921. else
  922. echo 3;
  923. echo 4;
  924. ',
  925. ),
  926. array(
  927. '<?php
  928. if (true) {
  929. if (true) {
  930. echo 1;
  931. } elseif (true) {
  932. echo 2;
  933. } else {
  934. echo 3;
  935. }
  936. }',
  937. '<?php
  938. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  939. ),
  940. array(
  941. '<?php
  942. if (true) {
  943. if (true) {
  944. echo 1;
  945. } else {
  946. echo 2;
  947. }
  948. } else {
  949. echo 3;
  950. }',
  951. '<?php
  952. if(true) if(true) echo 1; else echo 2; else echo 3;',
  953. ),
  954. array(
  955. '<?php
  956. foreach ($data as $val) {
  957. // test val
  958. if ($val === "errors") {
  959. echo "!";
  960. }
  961. }',
  962. '<?php
  963. foreach ($data as $val)
  964. // test val
  965. if ($val === "errors") {
  966. echo "!";
  967. }',
  968. ),
  969. array(
  970. '<?php
  971. if (1) {
  972. foreach ($data as $val) {
  973. // test val
  974. if ($val === "errors") {
  975. echo "!";
  976. }
  977. }
  978. }',
  979. '<?php
  980. if (1)
  981. foreach ($data as $val)
  982. // test val
  983. if ($val === "errors") {
  984. echo "!";
  985. }',
  986. ),
  987. array(
  988. '<?php
  989. class Foo
  990. {
  991. public function main()
  992. {
  993. echo "Hello";
  994. }
  995. }',
  996. '<?php
  997. class Foo
  998. {
  999. public function main()
  1000. {
  1001. echo "Hello";
  1002. }
  1003. }',
  1004. ),
  1005. array(
  1006. '<?php
  1007. class Foo
  1008. {
  1009. public function main()
  1010. {
  1011. echo "Hello";
  1012. }
  1013. }',
  1014. '<?php
  1015. class Foo
  1016. {
  1017. public function main()
  1018. {
  1019. echo "Hello";
  1020. }
  1021. }',
  1022. ),
  1023. array(
  1024. '<?php
  1025. class Foo
  1026. {
  1027. public $bar;
  1028. public $baz;
  1029. }',
  1030. '<?php
  1031. class Foo
  1032. {
  1033. public $bar;
  1034. public $baz;
  1035. }',
  1036. ),
  1037. array(
  1038. '<?php
  1039. function myFunction($foo, $bar)
  1040. {
  1041. return \Foo::{$foo}($bar);
  1042. }',
  1043. ),
  1044. array(
  1045. '<?php
  1046. class C
  1047. {
  1048. public function __construct(
  1049. )
  1050. //comment
  1051. {
  1052. }
  1053. }',
  1054. '<?php
  1055. class C {
  1056. public function __construct(
  1057. )
  1058. //comment
  1059. {}
  1060. }',
  1061. ),
  1062. array(
  1063. '<?php
  1064. if (true):
  1065. $foo = 0;
  1066. endif;',
  1067. null,
  1068. self::$configurationOopPositionSameLine,
  1069. ),
  1070. array(
  1071. '<?php
  1072. if (true) :
  1073. $foo = 0;
  1074. endif;',
  1075. null,
  1076. self::$configurationOopPositionSameLine,
  1077. ),
  1078. array(
  1079. '<?php
  1080. if (true) : $foo = 1; endif;',
  1081. null,
  1082. self::$configurationOopPositionSameLine,
  1083. ),
  1084. array(
  1085. '<?php
  1086. if (true) {
  1087. $foo = 1;
  1088. }',
  1089. '<?php
  1090. if (true)$foo = 1;',
  1091. self::$configurationOopPositionSameLine,
  1092. ),
  1093. array(
  1094. '<?php
  1095. if (true) {
  1096. $foo = 2;
  1097. }',
  1098. '<?php
  1099. if (true) $foo = 2;',
  1100. self::$configurationOopPositionSameLine,
  1101. ),
  1102. array(
  1103. '<?php
  1104. if (true) {
  1105. $foo = 3;
  1106. }',
  1107. '<?php
  1108. if (true){$foo = 3;}',
  1109. self::$configurationOopPositionSameLine,
  1110. ),
  1111. array(
  1112. '<?php
  1113. if (true) {
  1114. echo 1;
  1115. } else {
  1116. echo 2;
  1117. }',
  1118. '<?php
  1119. if(true) { echo 1; } else echo 2;',
  1120. self::$configurationOopPositionSameLine,
  1121. ),
  1122. array(
  1123. '<?php
  1124. if (true) {
  1125. echo 3;
  1126. } else {
  1127. echo 4;
  1128. }',
  1129. '<?php
  1130. if(true) echo 3; else { echo 4; }',
  1131. self::$configurationOopPositionSameLine,
  1132. ),
  1133. array(
  1134. '<?php
  1135. if (true) {
  1136. echo 5;
  1137. } else {
  1138. echo 6;
  1139. }',
  1140. '<?php
  1141. if (true) echo 5; else echo 6;',
  1142. self::$configurationOopPositionSameLine,
  1143. ),
  1144. array(
  1145. '<?php
  1146. if (true) {
  1147. while (true) {
  1148. $foo = 1;
  1149. $bar = 2;
  1150. }
  1151. }',
  1152. '<?php
  1153. if (true) while (true) { $foo = 1; $bar = 2;}',
  1154. self::$configurationOopPositionSameLine,
  1155. ),
  1156. array(
  1157. '<?php
  1158. if (true) {
  1159. if (true) {
  1160. echo 1;
  1161. } else {
  1162. echo 2;
  1163. }
  1164. } else {
  1165. echo 3;
  1166. }',
  1167. '<?php
  1168. if (true) if (true) echo 1; else echo 2; else echo 3;',
  1169. self::$configurationOopPositionSameLine,
  1170. ),
  1171. array(
  1172. '<?php
  1173. if (true) {
  1174. // sth here...
  1175. if ($a && ($b || $c)) {
  1176. $d = 1;
  1177. }
  1178. }',
  1179. '<?php
  1180. if (true) {
  1181. // sth here...
  1182. if ($a && ($b || $c)) $d = 1;
  1183. }',
  1184. self::$configurationOopPositionSameLine,
  1185. ),
  1186. array(
  1187. '<?php
  1188. for ($i = 1; $i < 10; ++$i) {
  1189. echo $i;
  1190. }
  1191. for ($i = 1; $i < 10; ++$i) {
  1192. echo $i;
  1193. }',
  1194. '<?php
  1195. for ($i = 1; $i < 10; ++$i) echo $i;
  1196. for ($i = 1; $i < 10; ++$i) { echo $i; }',
  1197. self::$configurationOopPositionSameLine,
  1198. ),
  1199. array(
  1200. '<?php
  1201. for ($i = 1; $i < 5; ++$i) {
  1202. for ($i = 1; $i < 10; ++$i) {
  1203. echo $i;
  1204. }
  1205. }',
  1206. '<?php
  1207. for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
  1208. self::$configurationOopPositionSameLine,
  1209. ),
  1210. array(
  1211. '<?php
  1212. do {
  1213. echo 1;
  1214. } while (false);',
  1215. '<?php
  1216. do { echo 1; } while (false);',
  1217. self::$configurationOopPositionSameLine,
  1218. ),
  1219. array(
  1220. '<?php
  1221. while ($foo->next());',
  1222. null,
  1223. self::$configurationOopPositionSameLine,
  1224. ),
  1225. array(
  1226. '<?php
  1227. foreach ($foo as $bar) {
  1228. echo $bar;
  1229. }',
  1230. '<?php
  1231. foreach ($foo as $bar) echo $bar;',
  1232. self::$configurationOopPositionSameLine,
  1233. ),
  1234. array(
  1235. '<?php
  1236. if (true) {
  1237. $a = 1;
  1238. }',
  1239. '<?php
  1240. if (true) {$a = 1;}',
  1241. self::$configurationOopPositionSameLine,
  1242. ),
  1243. array(
  1244. '<?php
  1245. if (true) {
  1246. $a = 1;
  1247. }',
  1248. '<?php
  1249. if (true) {
  1250. $a = 1;
  1251. }',
  1252. self::$configurationOopPositionSameLine,
  1253. ),
  1254. array(
  1255. '<?php
  1256. if (true) {
  1257. $a = 1;
  1258. $b = 2;
  1259. while (true) {
  1260. $c = 3;
  1261. }
  1262. $d = 4;
  1263. }',
  1264. '<?php
  1265. if (true) {
  1266. $a = 1;
  1267. $b = 2;
  1268. while (true) {
  1269. $c = 3;
  1270. }
  1271. $d = 4;
  1272. }',
  1273. self::$configurationOopPositionSameLine,
  1274. ),
  1275. array(
  1276. '<?php
  1277. if (true) {
  1278. $a = 1;
  1279. $b = 2;
  1280. }',
  1281. null,
  1282. self::$configurationOopPositionSameLine,
  1283. ),
  1284. array(
  1285. '<?php
  1286. if (1) {
  1287. $a = 1;
  1288. // comment at end
  1289. }',
  1290. null,
  1291. self::$configurationOopPositionSameLine,
  1292. ),
  1293. array(
  1294. '<?php
  1295. if (1) {
  1296. if (2) {
  1297. $a = "a";
  1298. } elseif (3) {
  1299. $b = "b";
  1300. // comment
  1301. } else {
  1302. $c = "c";
  1303. }
  1304. $d = "d";
  1305. }',
  1306. null,
  1307. self::$configurationOopPositionSameLine,
  1308. ),
  1309. array(
  1310. '<?php
  1311. foreach ($numbers as $num) {
  1312. for ($i = 0; $i < $num; ++$i) {
  1313. $a = "a";
  1314. }
  1315. $b = "b";
  1316. }',
  1317. null,
  1318. self::$configurationOopPositionSameLine,
  1319. ),
  1320. array(
  1321. '<?php
  1322. if (1) {
  1323. if (2) {
  1324. $foo = 2;
  1325. if (3) {
  1326. $foo = 3;
  1327. }
  1328. }
  1329. }',
  1330. null,
  1331. self::$configurationOopPositionSameLine,
  1332. ),
  1333. array(
  1334. '<?php
  1335. declare(ticks = 1) {
  1336. $ticks = 1;
  1337. }',
  1338. '<?php
  1339. declare (
  1340. ticks = 1 ) {
  1341. $ticks = 1;
  1342. }',
  1343. self::$configurationOopPositionSameLine,
  1344. ),
  1345. array(
  1346. '<?php
  1347. if (true) {
  1348. foo();
  1349. } elseif (true) {
  1350. bar();
  1351. }',
  1352. '<?php
  1353. if (true)
  1354. {
  1355. foo();
  1356. } elseif (true)
  1357. {
  1358. bar();
  1359. }',
  1360. self::$configurationOopPositionSameLine,
  1361. ),
  1362. array(
  1363. '<?php
  1364. while (true) {
  1365. foo();
  1366. }',
  1367. '<?php
  1368. while (true)
  1369. {
  1370. foo();
  1371. }',
  1372. self::$configurationOopPositionSameLine,
  1373. ),
  1374. array(
  1375. '<?php
  1376. do {
  1377. echo $test;
  1378. } while ($test = $this->getTest());',
  1379. '<?php
  1380. do
  1381. {
  1382. echo $test;
  1383. }
  1384. while ($test = $this->getTest());',
  1385. self::$configurationOopPositionSameLine,
  1386. ),
  1387. array(
  1388. '<?php
  1389. do {
  1390. echo $test;
  1391. } while ($test = $this->getTest());',
  1392. '<?php
  1393. do
  1394. {
  1395. echo $test;
  1396. }while ($test = $this->getTest());',
  1397. self::$configurationOopPositionSameLine,
  1398. ),
  1399. array(
  1400. '<?php
  1401. class ClassName {
  1402. /**
  1403. * comment
  1404. */
  1405. public $foo = null;
  1406. }',
  1407. '<?php
  1408. class ClassName
  1409. {
  1410. /**
  1411. * comment
  1412. */
  1413. public $foo = null;
  1414. }',
  1415. self::$configurationOopPositionSameLine,
  1416. ),
  1417. array(
  1418. '<?php
  1419. while ($true) {
  1420. try {
  1421. throw new \Exception();
  1422. } catch (\Exception $e) {
  1423. // do nothing
  1424. }
  1425. }',
  1426. null,
  1427. self::$configurationOopPositionSameLine,
  1428. ),
  1429. array(
  1430. '<?php
  1431. interface Foo {
  1432. public function setConfig(ConfigInterface $config);
  1433. }',
  1434. '<?php
  1435. interface Foo
  1436. {
  1437. public function setConfig(ConfigInterface $config);
  1438. }',
  1439. self::$configurationOopPositionSameLine,
  1440. ),
  1441. array(
  1442. '<?php
  1443. function bar() {
  1444. $a = 1; //comment
  1445. }',
  1446. '<?php
  1447. function bar()
  1448. {
  1449. $a = 1; //comment
  1450. }',
  1451. self::$configurationOopPositionSameLine,
  1452. ),
  1453. array(
  1454. '<?php
  1455. function & lambda() {
  1456. return function () {
  1457. };
  1458. }',
  1459. '<?php
  1460. function & lambda()
  1461. {
  1462. return function () {
  1463. };
  1464. }',
  1465. self::$configurationOopPositionSameLine,
  1466. ),
  1467. array(
  1468. '<?php
  1469. function nested() {
  1470. $a = "a{$b->c()}d";
  1471. }',
  1472. '<?php
  1473. function nested()
  1474. {
  1475. $a = "a{$b->c()}d";
  1476. }',
  1477. self::$configurationOopPositionSameLine,
  1478. ),
  1479. array(
  1480. '<?php
  1481. function foo() {
  1482. $a = $b->{$c->d}($e);
  1483. $f->{$g} = $h;
  1484. $i->{$j}[$k] = $l;
  1485. $m = $n->{$o};
  1486. $p = array($q->{$r}, $s->{$t});
  1487. $u->{$v}->w = 1;
  1488. }',
  1489. '<?php
  1490. function foo()
  1491. {
  1492. $a = $b->{$c->d}($e);
  1493. $f->{$g} = $h;
  1494. $i->{$j}[$k] = $l;
  1495. $m = $n->{$o};
  1496. $p = array($q->{$r}, $s->{$t});
  1497. $u->{$v}->w = 1;
  1498. }',
  1499. self::$configurationOopPositionSameLine,
  1500. ),
  1501. array(
  1502. '<?php
  1503. function mixed() {
  1504. $a = $b->{"a{$c}d"}();
  1505. }',
  1506. '<?php
  1507. function mixed()
  1508. {
  1509. $a = $b->{"a{$c}d"}();
  1510. }',
  1511. self::$configurationOopPositionSameLine,
  1512. ),
  1513. array(
  1514. '<?php
  1515. function mixedComplex() {
  1516. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  1517. }',
  1518. '<?php
  1519. function mixedComplex()
  1520. {
  1521. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  1522. }',
  1523. self::$configurationOopPositionSameLine,
  1524. ),
  1525. array(
  1526. '<?php
  1527. function mixedComplex() {
  1528. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  1529. }',
  1530. '<?php
  1531. function mixedComplex()
  1532. {
  1533. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  1534. }',
  1535. self::$configurationOopPositionSameLine,
  1536. ),
  1537. array(
  1538. '<?php
  1539. if (true):
  1540. echo 1;
  1541. else:
  1542. echo 2;
  1543. endif;
  1544. ',
  1545. null,
  1546. self::$configurationOopPositionSameLine,
  1547. ),
  1548. array(
  1549. '<?php
  1550. if ($test) { //foo
  1551. echo 1;
  1552. }',
  1553. null,
  1554. self::$configurationOopPositionSameLine,
  1555. ),
  1556. array(
  1557. '<?php
  1558. if (true) {
  1559. // foo
  1560. // bar
  1561. if (true) {
  1562. print("foo");
  1563. print("bar");
  1564. }
  1565. }',
  1566. '<?php
  1567. if (true)
  1568. // foo
  1569. // bar
  1570. {
  1571. if (true)
  1572. {
  1573. print("foo");
  1574. print("bar");
  1575. }
  1576. }',
  1577. self::$configurationOopPositionSameLine,
  1578. ),
  1579. array(
  1580. '<?php
  1581. if (true) {
  1582. // foo
  1583. /* bar */
  1584. if (true) {
  1585. print("foo");
  1586. print("bar");
  1587. }
  1588. }',
  1589. '<?php
  1590. if (true)
  1591. // foo
  1592. /* bar */{
  1593. if (true)
  1594. {
  1595. print("foo");
  1596. print("bar");
  1597. }
  1598. }',
  1599. self::$configurationOopPositionSameLine,
  1600. ),
  1601. array(
  1602. '<?php if (true) {
  1603. echo "s";
  1604. } ?>x',
  1605. '<?php if (true) echo "s" ?>x',
  1606. self::$configurationOopPositionSameLine,
  1607. ),
  1608. array(
  1609. '<?php
  1610. class Foo {
  1611. public function getFaxNumbers() {
  1612. if (1) {
  1613. return $this->phoneNumbers->filter(function ($phone) {
  1614. $a = 1;
  1615. $b = 1;
  1616. $c = 1;
  1617. return ($phone->getType() === 1) ? true : false;
  1618. });
  1619. }
  1620. }
  1621. }',
  1622. '<?php
  1623. class Foo
  1624. {
  1625. public function getFaxNumbers()
  1626. {
  1627. if (1)
  1628. return $this->phoneNumbers->filter(function ($phone) {
  1629. $a = 1;
  1630. $b = 1;
  1631. $c = 1;
  1632. return ($phone->getType() === 1) ? true : false;
  1633. });
  1634. }
  1635. }',
  1636. self::$configurationOopPositionSameLine,
  1637. ),
  1638. array(
  1639. '<?php
  1640. if (true) {
  1641. if (true) {
  1642. echo 1;
  1643. } elseif (true) {
  1644. echo 2;
  1645. } else {
  1646. echo 3;
  1647. }
  1648. }
  1649. ',
  1650. '<?php
  1651. if(true)
  1652. if(true)
  1653. echo 1;
  1654. elseif(true)
  1655. echo 2;
  1656. else
  1657. echo 3;
  1658. ',
  1659. self::$configurationOopPositionSameLine,
  1660. ),
  1661. array(
  1662. '<?php
  1663. if (true) {
  1664. if (true) {
  1665. echo 1;
  1666. } elseif (true) {
  1667. echo 2;
  1668. } else {
  1669. echo 3;
  1670. }
  1671. }
  1672. echo 4;
  1673. ',
  1674. '<?php
  1675. if(true)
  1676. if(true)
  1677. echo 1;
  1678. elseif(true)
  1679. echo 2;
  1680. else
  1681. echo 3;
  1682. echo 4;
  1683. ',
  1684. self::$configurationOopPositionSameLine,
  1685. ),
  1686. array(
  1687. '<?php
  1688. if (true) {
  1689. if (true) {
  1690. echo 1;
  1691. } elseif (true) {
  1692. echo 2;
  1693. } else {
  1694. echo 3;
  1695. }
  1696. }',
  1697. '<?php
  1698. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  1699. self::$configurationOopPositionSameLine,
  1700. ),
  1701. array(
  1702. '<?php
  1703. if (true) {
  1704. if (true) {
  1705. echo 1;
  1706. } else {
  1707. echo 2;
  1708. }
  1709. } else {
  1710. echo 3;
  1711. }',
  1712. '<?php
  1713. if(true) if(true) echo 1; else echo 2; else echo 3;',
  1714. self::$configurationOopPositionSameLine,
  1715. ),
  1716. array(
  1717. '<?php
  1718. foreach ($data as $val) {
  1719. // test val
  1720. if ($val === "errors") {
  1721. echo "!";
  1722. }
  1723. }',
  1724. '<?php
  1725. foreach ($data as $val)
  1726. // test val
  1727. if ($val === "errors") {
  1728. echo "!";
  1729. }',
  1730. self::$configurationOopPositionSameLine,
  1731. ),
  1732. array(
  1733. '<?php
  1734. if (1) {
  1735. foreach ($data as $val) {
  1736. // test val
  1737. if ($val === "errors") {
  1738. echo "!";
  1739. }
  1740. }
  1741. }',
  1742. '<?php
  1743. if (1)
  1744. foreach ($data as $val)
  1745. // test val
  1746. if ($val === "errors") {
  1747. echo "!";
  1748. }',
  1749. self::$configurationOopPositionSameLine,
  1750. ),
  1751. array(
  1752. '<?php
  1753. class Foo {
  1754. public function main() {
  1755. echo "Hello";
  1756. }
  1757. }',
  1758. '<?php
  1759. class Foo
  1760. {
  1761. public function main()
  1762. {
  1763. echo "Hello";
  1764. }
  1765. }',
  1766. self::$configurationOopPositionSameLine,
  1767. ),
  1768. array(
  1769. '<?php
  1770. class Foo {
  1771. public function main() {
  1772. echo "Hello";
  1773. }
  1774. }',
  1775. '<?php
  1776. class Foo
  1777. {
  1778. public function main()
  1779. {
  1780. echo "Hello";
  1781. }
  1782. }',
  1783. self::$configurationOopPositionSameLine,
  1784. ),
  1785. array(
  1786. '<?php
  1787. class Foo {
  1788. public $bar;
  1789. public $baz;
  1790. }',
  1791. '<?php
  1792. class Foo
  1793. {
  1794. public $bar;
  1795. public $baz;
  1796. }',
  1797. self::$configurationOopPositionSameLine,
  1798. ),
  1799. array(
  1800. '<?php
  1801. function myFunction($foo, $bar) {
  1802. return \Foo::{$foo}($bar);
  1803. }',
  1804. '<?php
  1805. function myFunction($foo, $bar)
  1806. {
  1807. return \Foo::{$foo}($bar);
  1808. }',
  1809. self::$configurationOopPositionSameLine,
  1810. ),
  1811. array(
  1812. '<?php
  1813. class C {
  1814. public function __construct(
  1815. )
  1816. //comment
  1817. {
  1818. }
  1819. }',
  1820. '<?php
  1821. class C {
  1822. public function __construct(
  1823. )
  1824. //comment
  1825. {}
  1826. }',
  1827. self::$configurationOopPositionSameLine,
  1828. ),
  1829. array(
  1830. '<?php
  1831. class Something # a
  1832. {
  1833. public function sth() //
  1834. {
  1835. return function (int $foo) use ($bar) {
  1836. return $bar;
  1837. };
  1838. }
  1839. }
  1840. function C() /**/ // # /**/
  1841. {
  1842. }
  1843. function D() /**
  1844. *
  1845. */
  1846. {
  1847. }',
  1848. '<?php
  1849. class Something # a
  1850. {
  1851. public function sth() //
  1852. {
  1853. return function (int $foo) use ($bar) { return $bar; };
  1854. }
  1855. }
  1856. function C() /**/ // # /**/
  1857. {
  1858. }
  1859. function D() /**
  1860. *
  1861. */
  1862. {
  1863. }',
  1864. self::$configurationOopPositionSameLine,
  1865. ),
  1866. );
  1867. }
  1868. /**
  1869. * @param string $expected
  1870. * @param null|string $input
  1871. * @param null|array $configuration
  1872. *
  1873. * @dataProvider provideFixClassyBracesCases
  1874. */
  1875. public function testFixClassyBraces($expected, $input = null, array $configuration = null)
  1876. {
  1877. if (null !== $configuration) {
  1878. $this->fixer->configure($configuration);
  1879. }
  1880. $this->doTest($expected, $input);
  1881. }
  1882. public function provideFixClassyBracesCases()
  1883. {
  1884. return array(
  1885. array(
  1886. '<?php
  1887. class FooA
  1888. {
  1889. }',
  1890. '<?php
  1891. class FooA {}',
  1892. ),
  1893. array(
  1894. '<?php
  1895. class FooB
  1896. {
  1897. }',
  1898. '<?php
  1899. class FooB{}',
  1900. ),
  1901. array(
  1902. '<?php
  1903. class FooC
  1904. {
  1905. }',
  1906. '<?php
  1907. class FooC
  1908. {}',
  1909. ),
  1910. array(
  1911. '<?php
  1912. interface FooD
  1913. {
  1914. }',
  1915. '<?php
  1916. interface FooD {}',
  1917. ),
  1918. array(
  1919. '<?php
  1920. class TestClass extends BaseTestClass implements TestInterface
  1921. {
  1922. private $foo;
  1923. }',
  1924. '<?php
  1925. class TestClass extends BaseTestClass implements TestInterface { private $foo;}',
  1926. ),
  1927. array(
  1928. '<?php
  1929. abstract class Foo
  1930. {
  1931. public function getProcess($foo)
  1932. {
  1933. return true;
  1934. }
  1935. }',
  1936. ),
  1937. array('<?php
  1938. function foo()
  1939. {
  1940. return "$c ($d)";
  1941. }',
  1942. ),
  1943. array(
  1944. '<?php
  1945. class FooA {
  1946. }',
  1947. '<?php
  1948. class FooA {}',
  1949. self::$configurationOopPositionSameLine,
  1950. ),
  1951. array(
  1952. '<?php
  1953. class FooB {
  1954. }',
  1955. '<?php
  1956. class FooB{}',
  1957. self::$configurationOopPositionSameLine,
  1958. ),
  1959. array(
  1960. '<?php
  1961. class FooC {
  1962. }',
  1963. '<?php
  1964. class FooC
  1965. {}',
  1966. self::$configurationOopPositionSameLine,
  1967. ),
  1968. array(
  1969. '<?php
  1970. interface FooD {
  1971. }',
  1972. '<?php
  1973. interface FooD {}',
  1974. self::$configurationOopPositionSameLine,
  1975. ),
  1976. array(
  1977. '<?php
  1978. class TestClass extends BaseTestClass implements TestInterface {
  1979. private $foo;
  1980. }',
  1981. '<?php
  1982. class TestClass extends BaseTestClass implements TestInterface { private $foo;}',
  1983. self::$configurationOopPositionSameLine,
  1984. ),
  1985. array(
  1986. '<?php
  1987. abstract class Foo {
  1988. public function getProcess($foo) {
  1989. return true;
  1990. }
  1991. }',
  1992. '<?php
  1993. abstract class Foo
  1994. {
  1995. public function getProcess($foo)
  1996. {
  1997. return true;
  1998. }
  1999. }',
  2000. self::$configurationOopPositionSameLine,
  2001. ),
  2002. array(
  2003. '<?php
  2004. function foo() {
  2005. return "$c ($d)";
  2006. }',
  2007. '<?php
  2008. function foo()
  2009. {
  2010. return "$c ($d)";
  2011. }',
  2012. self::$configurationOopPositionSameLine,
  2013. ),
  2014. );
  2015. }
  2016. /**
  2017. * @param string $expected
  2018. * @param null|string $input
  2019. * @param null|array $configuration
  2020. *
  2021. * @dataProvider provideFixClassyBraces54Cases
  2022. * @requires PHP 5.4
  2023. */
  2024. public function testFixClassyBraces54($expected, $input = null, array $configuration = null)
  2025. {
  2026. if (null !== $configuration) {
  2027. $this->fixer->configure($configuration);
  2028. }
  2029. $this->doTest($expected, $input);
  2030. }
  2031. public function provideFixClassyBraces54Cases()
  2032. {
  2033. return array(
  2034. array(
  2035. '<?php
  2036. trait TFoo
  2037. {
  2038. public $a;
  2039. }',
  2040. '<?php
  2041. trait TFoo {public $a;}',
  2042. ),
  2043. array(
  2044. '<?php
  2045. trait TFoo {
  2046. public $a;
  2047. }',
  2048. '<?php
  2049. trait TFoo {public $a;}',
  2050. self::$configurationOopPositionSameLine,
  2051. ),
  2052. );
  2053. }
  2054. /**
  2055. * @param string $expected
  2056. * @param null|string $input
  2057. * @param null|array $configuration
  2058. *
  2059. * @dataProvider provideFixAnonFunctionInShortArraySyntax54Cases
  2060. * @requires PHP 5.4
  2061. */
  2062. public function testFixAnonFunctionInShortArraySyntax54($expected, $input = null, array $configuration = null)
  2063. {
  2064. if (null !== $configuration) {
  2065. $this->fixer->configure($configuration);
  2066. }
  2067. $this->doTest($expected, $input);
  2068. }
  2069. public function provideFixAnonFunctionInShortArraySyntax54Cases()
  2070. {
  2071. return array(
  2072. array(
  2073. '<?php
  2074. function myFunction()
  2075. {
  2076. return [
  2077. [
  2078. "callback" => function ($data) {
  2079. return true;
  2080. }
  2081. ],
  2082. [
  2083. "callback" => function ($data) {
  2084. return true;
  2085. },
  2086. ],
  2087. ];
  2088. }',
  2089. '<?php
  2090. function myFunction()
  2091. {
  2092. return [
  2093. [
  2094. "callback" => function ($data) {
  2095. return true;
  2096. }
  2097. ],
  2098. [
  2099. "callback" => function ($data) { return true; },
  2100. ],
  2101. ];
  2102. }',
  2103. ),
  2104. array(
  2105. '<?php
  2106. function myFunction() {
  2107. return [
  2108. [
  2109. "callback" => function ($data) {
  2110. return true;
  2111. }
  2112. ],
  2113. [
  2114. "callback" => function ($data) {
  2115. return true;
  2116. },
  2117. ],
  2118. ];
  2119. }',
  2120. '<?php
  2121. function myFunction()
  2122. {
  2123. return [
  2124. [
  2125. "callback" => function ($data) {
  2126. return true;
  2127. }
  2128. ],
  2129. [
  2130. "callback" => function ($data) { return true; },
  2131. ],
  2132. ];
  2133. }',
  2134. self::$configurationOopPositionSameLine,
  2135. ),
  2136. );
  2137. }
  2138. /**
  2139. * @param string $expected
  2140. * @param null|string $input
  2141. * @param null|array $configuration
  2142. *
  2143. * @dataProvider provideFixCommentBeforeBraceCases
  2144. */
  2145. public function testFixCommentBeforeBrace($expected, $input = null, array $configuration = null)
  2146. {
  2147. if (null !== $configuration) {
  2148. $this->fixer->configure($configuration);
  2149. }
  2150. $this->doTest($expected, $input);
  2151. }
  2152. public function provideFixCommentBeforeBraceCases()
  2153. {
  2154. return array(
  2155. array(
  2156. '<?php ',
  2157. ),
  2158. array(
  2159. '<?php
  2160. if ($test) { // foo
  2161. echo 1;
  2162. }',
  2163. '<?php
  2164. if ($test) // foo
  2165. {
  2166. echo 1;
  2167. }',
  2168. ),
  2169. array(
  2170. '<?php
  2171. $foo = function ($x) use ($y) { // foo
  2172. echo 1;
  2173. };',
  2174. '<?php
  2175. $foo = function ($x) use ($y) // foo
  2176. {
  2177. echo 1;
  2178. };',
  2179. ),
  2180. array(
  2181. '<?php ',
  2182. null,
  2183. self::$configurationOopPositionSameLine,
  2184. ),
  2185. array(
  2186. '<?php
  2187. if ($test) { // foo
  2188. echo 1;
  2189. }',
  2190. '<?php
  2191. if ($test) // foo
  2192. {
  2193. echo 1;
  2194. }',
  2195. self::$configurationOopPositionSameLine,
  2196. ),
  2197. array(
  2198. '<?php
  2199. $foo = function ($x) use ($y) { // foo
  2200. echo 1;
  2201. };',
  2202. '<?php
  2203. $foo = function ($x) use ($y) // foo
  2204. {
  2205. echo 1;
  2206. };',
  2207. self::$configurationOopPositionSameLine,
  2208. ),
  2209. );
  2210. }
  2211. /**
  2212. * @param string $expected
  2213. * @param null|string $input
  2214. * @param null|array $configuration
  2215. *
  2216. * @dataProvider provideFixCommentBeforeBrace70Cases
  2217. * @requires PHP 7.0
  2218. */
  2219. public function testFixCommentBeforeBrace70($expected, $input = null, array $configuration = null)
  2220. {
  2221. if (null !== $configuration) {
  2222. $this->fixer->configure($configuration);
  2223. }
  2224. $this->doTest($expected, $input);
  2225. }
  2226. public function provideFixCommentBeforeBrace70Cases()
  2227. {
  2228. return array(
  2229. array(
  2230. '<?php
  2231. $foo = new class ($a) extends Foo implements Bar { // foo
  2232. private $x;
  2233. };',
  2234. '<?php
  2235. $foo = new class ($a) extends Foo implements Bar // foo
  2236. {
  2237. private $x;
  2238. };',
  2239. ),
  2240. array(
  2241. '<?php
  2242. $foo = new class ($a) extends Foo implements Bar { // foo
  2243. private $x;
  2244. };',
  2245. '<?php
  2246. $foo = new class ($a) extends Foo implements Bar // foo
  2247. {
  2248. private $x;
  2249. };',
  2250. self::$configurationOopPositionSameLine,
  2251. ),
  2252. );
  2253. }
  2254. /**
  2255. * @param string $expected
  2256. * @param null|string $input
  2257. * @param null|array $configuration
  2258. *
  2259. * @dataProvider provideFixWhitespaceBeforeBraceCases
  2260. */
  2261. public function testFixWhitespaceBeforeBrace($expected, $input = null, array $configuration = null)
  2262. {
  2263. if (null !== $configuration) {
  2264. $this->fixer->configure($configuration);
  2265. }
  2266. $this->doTest($expected, $input);
  2267. }
  2268. public function provideFixWhitespaceBeforeBraceCases()
  2269. {
  2270. return array(
  2271. array(
  2272. '<?php
  2273. if (true) {
  2274. echo 1;
  2275. }',
  2276. '<?php
  2277. if (true)
  2278. {
  2279. echo 1;
  2280. }',
  2281. ),
  2282. array(
  2283. '<?php
  2284. if (true) {
  2285. echo 1;
  2286. }',
  2287. '<?php
  2288. if (true){
  2289. echo 1;
  2290. }',
  2291. ),
  2292. array(
  2293. '<?php
  2294. if (true) {
  2295. echo 1;
  2296. }',
  2297. '<?php
  2298. if (true) {
  2299. echo 1;
  2300. }',
  2301. ),
  2302. array(
  2303. '<?php
  2304. while ($file = $this->getFile()) {
  2305. }',
  2306. '<?php
  2307. while ($file = $this->getFile())
  2308. {
  2309. }',
  2310. ),
  2311. array(
  2312. '<?php
  2313. switch (n) {
  2314. case label1:
  2315. echo 1;
  2316. echo 2;
  2317. break;
  2318. default:
  2319. echo 3;
  2320. echo 4;
  2321. }',
  2322. '<?php
  2323. switch (n)
  2324. {
  2325. case label1:
  2326. echo 1;
  2327. echo 2;
  2328. break;
  2329. default:
  2330. echo 3;
  2331. echo 4;
  2332. }',
  2333. ),
  2334. array(
  2335. '<?php
  2336. if (true) {
  2337. echo 1;
  2338. }',
  2339. '<?php
  2340. if (true)
  2341. {
  2342. echo 1;
  2343. }',
  2344. self::$configurationOopPositionSameLine,
  2345. ),
  2346. array(
  2347. '<?php
  2348. if (true) {
  2349. echo 1;
  2350. }',
  2351. '<?php
  2352. if (true){
  2353. echo 1;
  2354. }',
  2355. self::$configurationOopPositionSameLine,
  2356. ),
  2357. array(
  2358. '<?php
  2359. if (true) {
  2360. echo 1;
  2361. }',
  2362. '<?php
  2363. if (true) {
  2364. echo 1;
  2365. }',
  2366. self::$configurationOopPositionSameLine,
  2367. ),
  2368. array(
  2369. '<?php
  2370. while ($file = $this->getFile()) {
  2371. }',
  2372. '<?php
  2373. while ($file = $this->getFile())
  2374. {
  2375. }',
  2376. self::$configurationOopPositionSameLine,
  2377. ),
  2378. array(
  2379. '<?php
  2380. switch (n) {
  2381. case label1:
  2382. echo 1;
  2383. echo 2;
  2384. break;
  2385. default:
  2386. echo 3;
  2387. echo 4;
  2388. }',
  2389. '<?php
  2390. switch (n)
  2391. {
  2392. case label1:
  2393. echo 1;
  2394. echo 2;
  2395. break;
  2396. default:
  2397. echo 3;
  2398. echo 4;
  2399. }',
  2400. self::$configurationOopPositionSameLine,
  2401. ),
  2402. );
  2403. }
  2404. /**
  2405. * @param string $expected
  2406. * @param null|string $input
  2407. * @param null|array $configuration
  2408. *
  2409. * @dataProvider provideFixFunctionsCases
  2410. */
  2411. public function testFixFunctions($expected, $input = null, array $configuration = null)
  2412. {
  2413. if (null !== $configuration) {
  2414. $this->fixer->configure($configuration);
  2415. }
  2416. $this->doTest($expected, $input);
  2417. }
  2418. public function provideFixFunctionsCases()
  2419. {
  2420. return array(
  2421. array(
  2422. '<?php
  2423. function download()
  2424. {
  2425. }',
  2426. '<?php
  2427. function download() {
  2428. }',
  2429. ),
  2430. array(
  2431. '<?php
  2432. class Foo
  2433. {
  2434. public function AAAA()
  2435. {
  2436. }
  2437. public function BBBB()
  2438. {
  2439. }
  2440. public function CCCC()
  2441. {
  2442. }
  2443. }',
  2444. '<?php
  2445. class Foo
  2446. {
  2447. public function AAAA(){
  2448. }
  2449. public function BBBB() {
  2450. }
  2451. public function CCCC()
  2452. {
  2453. }
  2454. }',
  2455. ),
  2456. array(
  2457. '<?php
  2458. filter(function () {
  2459. return true;
  2460. });
  2461. ',
  2462. ),
  2463. array(
  2464. '<?php
  2465. filter(function ($a) {
  2466. });',
  2467. '<?php
  2468. filter(function ($a)
  2469. {});',
  2470. ),
  2471. array(
  2472. '<?php
  2473. filter(function ($b) {
  2474. });',
  2475. '<?php
  2476. filter(function ($b){});',
  2477. ),
  2478. array(
  2479. '<?php
  2480. foo(array_map(function ($object) use ($x, $y) {
  2481. return array_filter($object->bar(), function ($o) {
  2482. return $o->isBaz();
  2483. });
  2484. }, $collection));',
  2485. '<?php
  2486. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  2487. ),
  2488. array(
  2489. '<?php
  2490. class Foo
  2491. {
  2492. public static function bar()
  2493. {
  2494. return 1;
  2495. }
  2496. }',
  2497. ),
  2498. array(
  2499. '<?php
  2500. usort($this->fixers, function &($a, $b) use ($selfName) {
  2501. return 1;
  2502. });',
  2503. ),
  2504. array(
  2505. '<?php
  2506. usort(
  2507. $this->fixers,
  2508. function &($a, $b) use ($selfName) {
  2509. return 1;
  2510. }
  2511. );',
  2512. ),
  2513. array(
  2514. '<?php
  2515. $fnc = function ($a, $b) { // random comment
  2516. return 0;
  2517. };',
  2518. '<?php
  2519. $fnc = function ($a, $b) // random comment
  2520. {
  2521. return 0;
  2522. };',
  2523. ),
  2524. array(
  2525. '<?php
  2526. $fnc = function ($a, $b) { # random comment
  2527. return 0;
  2528. };',
  2529. '<?php
  2530. $fnc = function ($a, $b) # random comment
  2531. {
  2532. return 0;
  2533. };',
  2534. ),
  2535. array(
  2536. '<?php
  2537. $fnc = function ($a, $b) /* random comment */ {
  2538. return 0;
  2539. };',
  2540. '<?php
  2541. $fnc = function ($a, $b) /* random comment */
  2542. {
  2543. return 0;
  2544. };',
  2545. ),
  2546. array(
  2547. '<?php
  2548. $fnc = function ($a, $b) /** random comment */ {
  2549. return 0;
  2550. };',
  2551. '<?php
  2552. $fnc = function ($a, $b) /** random comment */
  2553. {
  2554. return 0;
  2555. };',
  2556. ),
  2557. array(
  2558. '<?php
  2559. function download() {
  2560. }',
  2561. null,
  2562. self::$configurationOopPositionSameLine,
  2563. ),
  2564. array(
  2565. '<?php
  2566. class Foo {
  2567. public function AAAA() {
  2568. }
  2569. public function BBBB() {
  2570. }
  2571. public function CCCC() {
  2572. }
  2573. }',
  2574. '<?php
  2575. class Foo
  2576. {
  2577. public function AAAA(){
  2578. }
  2579. public function BBBB() {
  2580. }
  2581. public function CCCC()
  2582. {
  2583. }
  2584. }',
  2585. self::$configurationOopPositionSameLine,
  2586. ),
  2587. array(
  2588. '<?php
  2589. filter(function () {
  2590. return true;
  2591. });
  2592. ',
  2593. null,
  2594. self::$configurationOopPositionSameLine,
  2595. ),
  2596. array(
  2597. '<?php
  2598. filter(function ($a) {
  2599. });',
  2600. '<?php
  2601. filter(function ($a)
  2602. {});',
  2603. self::$configurationOopPositionSameLine,
  2604. ),
  2605. array(
  2606. '<?php
  2607. filter(function ($b) {
  2608. });',
  2609. '<?php
  2610. filter(function ($b){});',
  2611. self::$configurationOopPositionSameLine,
  2612. ),
  2613. array(
  2614. '<?php
  2615. foo(array_map(function ($object) use ($x, $y) {
  2616. return array_filter($object->bar(), function ($o) {
  2617. return $o->isBaz();
  2618. });
  2619. }, $collection));',
  2620. '<?php
  2621. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  2622. self::$configurationOopPositionSameLine,
  2623. ),
  2624. array(
  2625. '<?php
  2626. class Foo {
  2627. public static function bar() {
  2628. return 1;
  2629. }
  2630. }',
  2631. '<?php
  2632. class Foo
  2633. {
  2634. public static function bar()
  2635. {
  2636. return 1;
  2637. }
  2638. }',
  2639. self::$configurationOopPositionSameLine,
  2640. ),
  2641. array(
  2642. '<?php
  2643. usort($this->fixers, function &($a, $b) use ($selfName) {
  2644. return 1;
  2645. });',
  2646. null,
  2647. self::$configurationOopPositionSameLine,
  2648. ),
  2649. array(
  2650. '<?php
  2651. usort(
  2652. $this->fixers,
  2653. function &($a, $b) use ($selfName) {
  2654. return 1;
  2655. }
  2656. );',
  2657. null,
  2658. self::$configurationOopPositionSameLine,
  2659. ),
  2660. array(
  2661. '<?php
  2662. $fnc = function ($a, $b) { // random comment
  2663. return 0;
  2664. };',
  2665. '<?php
  2666. $fnc = function ($a, $b) // random comment
  2667. {
  2668. return 0;
  2669. };',
  2670. self::$configurationOopPositionSameLine,
  2671. ),
  2672. array(
  2673. '<?php
  2674. $fnc = function ($a, $b) { # random comment
  2675. return 0;
  2676. };',
  2677. '<?php
  2678. $fnc = function ($a, $b) # random comment
  2679. {
  2680. return 0;
  2681. };',
  2682. self::$configurationOopPositionSameLine,
  2683. ),
  2684. array(
  2685. '<?php
  2686. $fnc = function ($a, $b) /* random comment */ {
  2687. return 0;
  2688. };',
  2689. '<?php
  2690. $fnc = function ($a, $b) /* random comment */
  2691. {
  2692. return 0;
  2693. };',
  2694. self::$configurationOopPositionSameLine,
  2695. ),
  2696. array(
  2697. '<?php
  2698. $fnc = function ($a, $b) /** random comment */ {
  2699. return 0;
  2700. };',
  2701. '<?php
  2702. $fnc = function ($a, $b) /** random comment */
  2703. {
  2704. return 0;
  2705. };',
  2706. self::$configurationOopPositionSameLine,
  2707. ),
  2708. );
  2709. }
  2710. /**
  2711. * @param string $expected
  2712. * @param null|string $input
  2713. * @param null|array $configuration
  2714. *
  2715. * @dataProvider provideFixSpaceAroundTokenCases
  2716. */
  2717. public function testFixSpaceAroundToken($expected, $input = null, array $configuration = null)
  2718. {
  2719. if (null !== $configuration) {
  2720. $this->fixer->configure($configuration);
  2721. }
  2722. $this->doTest($expected, $input);
  2723. }
  2724. public function provideFixSpaceAroundTokenCases()
  2725. {
  2726. return array(
  2727. array(
  2728. '<?php
  2729. try {
  2730. throw new Exception();
  2731. } catch (Exception $e) {
  2732. log($e);
  2733. }',
  2734. '<?php
  2735. try{
  2736. throw new Exception();
  2737. }catch (Exception $e){
  2738. log($e);
  2739. }',
  2740. ),
  2741. array(
  2742. '<?php
  2743. do {
  2744. echo 1;
  2745. } while ($test);',
  2746. '<?php
  2747. do{
  2748. echo 1;
  2749. }while($test);',
  2750. ),
  2751. array(
  2752. '<?php
  2753. if (true === true
  2754. && true === true
  2755. ) {
  2756. }',
  2757. '<?php
  2758. if(true === true
  2759. && true === true
  2760. ) {
  2761. }',
  2762. ),
  2763. array(
  2764. '<?php
  2765. if (1) {
  2766. }
  2767. if ($this->tesT ($test)) {
  2768. }',
  2769. '<?php
  2770. if(1){
  2771. }
  2772. if ($this->tesT ($test)) {
  2773. }',
  2774. ),
  2775. array(
  2776. '<?php
  2777. if (true) {
  2778. } elseif (false) {
  2779. } else {
  2780. }',
  2781. '<?php
  2782. if(true){
  2783. }elseif(false){
  2784. }else{
  2785. }',
  2786. ),
  2787. array(
  2788. '<?php
  2789. $foo = function& () use ($bar) {
  2790. };',
  2791. '<?php
  2792. $foo = function& ()use($bar){};',
  2793. ),
  2794. array(
  2795. '<?php
  2796. // comment
  2797. declare(strict_types=1);
  2798. // comment
  2799. while (true) {
  2800. }',
  2801. ),
  2802. array(
  2803. '<?php
  2804. declare(ticks = 1) {
  2805. }',
  2806. '<?php
  2807. declare ( ticks = 1 ) {
  2808. }',
  2809. ),
  2810. array(
  2811. '<?php
  2812. try {
  2813. throw new Exception();
  2814. } catch (Exception $e) {
  2815. log($e);
  2816. }',
  2817. '<?php
  2818. try{
  2819. throw new Exception();
  2820. }catch (Exception $e){
  2821. log($e);
  2822. }',
  2823. self::$configurationOopPositionSameLine,
  2824. ),
  2825. array(
  2826. '<?php
  2827. do {
  2828. echo 1;
  2829. } while ($test);',
  2830. '<?php
  2831. do{
  2832. echo 1;
  2833. }while($test);',
  2834. self::$configurationOopPositionSameLine,
  2835. ),
  2836. array(
  2837. '<?php
  2838. if (true === true
  2839. && true === true
  2840. ) {
  2841. }',
  2842. '<?php
  2843. if(true === true
  2844. && true === true
  2845. ) {
  2846. }',
  2847. self::$configurationOopPositionSameLine,
  2848. ),
  2849. array(
  2850. '<?php
  2851. if (1) {
  2852. }
  2853. if ($this->tesT ($test)) {
  2854. }',
  2855. '<?php
  2856. if(1){
  2857. }
  2858. if ($this->tesT ($test)) {
  2859. }',
  2860. self::$configurationOopPositionSameLine,
  2861. ),
  2862. array(
  2863. '<?php
  2864. if (true) {
  2865. } elseif (false) {
  2866. } else {
  2867. }',
  2868. '<?php
  2869. if(true){
  2870. }elseif(false){
  2871. }else{
  2872. }',
  2873. self::$configurationOopPositionSameLine,
  2874. ),
  2875. array(
  2876. '<?php
  2877. $foo = function& () use ($bar) {
  2878. };',
  2879. '<?php
  2880. $foo = function& ()use($bar){};',
  2881. self::$configurationOopPositionSameLine,
  2882. ),
  2883. array(
  2884. '<?php
  2885. // comment
  2886. declare(strict_types=1);
  2887. // comment
  2888. while (true) {
  2889. }',
  2890. null,
  2891. self::$configurationOopPositionSameLine,
  2892. ),
  2893. array(
  2894. '<?php
  2895. declare(ticks = 1) {
  2896. }',
  2897. '<?php
  2898. declare ( ticks = 1 ) {
  2899. }',
  2900. self::$configurationOopPositionSameLine,
  2901. ),
  2902. );
  2903. }
  2904. /**
  2905. * @param string $expected
  2906. * @param null|string $input
  2907. * @param null|array $configuration
  2908. *
  2909. * @dataProvider provide55Cases
  2910. * @requires PHP 5.5
  2911. */
  2912. public function test55($expected, $input = null, array $configuration = null)
  2913. {
  2914. if (null !== $configuration) {
  2915. $this->fixer->configure($configuration);
  2916. }
  2917. $this->doTest($expected, $input);
  2918. }
  2919. public function provide55Cases()
  2920. {
  2921. return array(
  2922. array(
  2923. '<?php
  2924. try {
  2925. throw new \Exception();
  2926. } catch (\LogicException $e) {
  2927. // do nothing
  2928. } catch (\Exception $e) {
  2929. // do nothing
  2930. } finally {
  2931. echo "finish!";
  2932. }',
  2933. '<?php
  2934. try {
  2935. throw new \Exception();
  2936. }catch (\LogicException $e) {
  2937. // do nothing
  2938. }
  2939. catch (\Exception $e) {
  2940. // do nothing
  2941. }
  2942. finally {
  2943. echo "finish!";
  2944. }',
  2945. ),
  2946. array(
  2947. '<?php
  2948. try {
  2949. throw new \Exception();
  2950. } catch (\LogicException $e) {
  2951. // do nothing
  2952. } catch (\Exception $e) {
  2953. // do nothing
  2954. } finally {
  2955. echo "finish!";
  2956. }',
  2957. '<?php
  2958. try {
  2959. throw new \Exception();
  2960. }catch (\LogicException $e) {
  2961. // do nothing
  2962. }
  2963. catch (\Exception $e) {
  2964. // do nothing
  2965. }
  2966. finally {
  2967. echo "finish!";
  2968. }',
  2969. self::$configurationOopPositionSameLine,
  2970. ),
  2971. );
  2972. }
  2973. /**
  2974. * @param string $expected
  2975. * @param null|string $input
  2976. * @param null|array $configuration
  2977. *
  2978. * @dataProvider provide56Cases
  2979. * @requires PHP 5.6
  2980. */
  2981. public function test56($expected, $input = null, array $configuration = null)
  2982. {
  2983. if (null !== $configuration) {
  2984. $this->fixer->configure($configuration);
  2985. }
  2986. $this->doTest($expected, $input);
  2987. }
  2988. public function provide56Cases()
  2989. {
  2990. return array(
  2991. array(
  2992. '<?php
  2993. use function Foo\bar;
  2994. if (true) {
  2995. }',
  2996. ),
  2997. array(
  2998. '<?php
  2999. use function Foo\bar;
  3000. if (true) {
  3001. }',
  3002. null,
  3003. self::$configurationOopPositionSameLine,
  3004. ),
  3005. );
  3006. }
  3007. /**
  3008. * @param string $expected
  3009. * @param null|string $input
  3010. * @param null|array $configuration
  3011. *
  3012. * @dataProvider provide70Cases
  3013. * @requires PHP 7.0
  3014. */
  3015. public function test70($expected, $input = null, array $configuration = null)
  3016. {
  3017. if (null !== $configuration) {
  3018. $this->fixer->configure($configuration);
  3019. }
  3020. $this->doTest($expected, $input);
  3021. }
  3022. public function provide70Cases()
  3023. {
  3024. return array(
  3025. array(
  3026. '<?php
  3027. function foo($a)
  3028. {
  3029. // foo
  3030. $foo = new class($a) extends Foo {
  3031. public function bar()
  3032. {
  3033. }
  3034. };
  3035. }',
  3036. '<?php
  3037. function foo($a)
  3038. {
  3039. // foo
  3040. $foo = new class($a) extends Foo { public function bar() {} };
  3041. }',
  3042. ),
  3043. array(
  3044. '<?php
  3045. foo(1, new class implements Logger {
  3046. public function log($message)
  3047. {
  3048. log($message);
  3049. }
  3050. }, 3);',
  3051. '<?php
  3052. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  3053. ),
  3054. array(
  3055. '<?php
  3056. $message = (new class() implements FooInterface {
  3057. });',
  3058. '<?php
  3059. $message = (new class() implements FooInterface{});',
  3060. ),
  3061. array(
  3062. '<?php $message = (new class() {
  3063. });',
  3064. '<?php $message = (new class() {});',
  3065. ),
  3066. array(
  3067. '<?php
  3068. if (1) {
  3069. $message = (new class() extends Foo {
  3070. public function bar()
  3071. {
  3072. echo 1;
  3073. }
  3074. });
  3075. }',
  3076. '<?php
  3077. if (1) {
  3078. $message = (new class() extends Foo
  3079. {
  3080. public function bar() { echo 1; }
  3081. });
  3082. }',
  3083. ),
  3084. array(
  3085. '<?php
  3086. class Foo
  3087. {
  3088. public function use()
  3089. {
  3090. }
  3091. public function use1(): string
  3092. {
  3093. }
  3094. }
  3095. ',
  3096. '<?php
  3097. class Foo
  3098. {
  3099. public function use() {
  3100. }
  3101. public function use1(): string {
  3102. }
  3103. }
  3104. ',
  3105. ),
  3106. array(
  3107. '<?php
  3108. $a = function (int $foo): string {
  3109. echo $foo;
  3110. };
  3111. $b = function (int $foo) use ($bar): string {
  3112. echo $foo . $bar;
  3113. };
  3114. function a()
  3115. {
  3116. }
  3117. ',
  3118. '<?php
  3119. $a = function (int $foo): string
  3120. {
  3121. echo $foo;
  3122. };
  3123. $b = function (int $foo) use($bar): string
  3124. {
  3125. echo $foo . $bar;
  3126. };
  3127. function a() {
  3128. }
  3129. ',
  3130. ),
  3131. array(
  3132. '<?php
  3133. class Something
  3134. {
  3135. public function sth(): string
  3136. {
  3137. return function (int $foo) use ($bar): string {
  3138. return $bar;
  3139. };
  3140. }
  3141. }',
  3142. '<?php
  3143. class Something
  3144. {
  3145. public function sth(): string
  3146. {
  3147. return function (int $foo) use ($bar): string { return $bar; };
  3148. }
  3149. }',
  3150. ),
  3151. array(
  3152. '<?php
  3153. use function some\a\{
  3154. test1,
  3155. test2
  3156. };
  3157. test();',
  3158. ),
  3159. array(
  3160. '<?php
  3161. use some\a\{ClassA, ClassB, ClassC as C};
  3162. use function some\a\{fn_a, fn_b, fn_c};
  3163. use const some\a\{ConstA, ConstB, ConstC};
  3164. ',
  3165. ),
  3166. array(
  3167. '<?php
  3168. function foo($a) {
  3169. // foo
  3170. $foo = new class($a) extends Foo {
  3171. public function bar() {
  3172. }
  3173. };
  3174. }',
  3175. '<?php
  3176. function foo($a)
  3177. {
  3178. // foo
  3179. $foo = new class($a) extends Foo { public function bar() {} };
  3180. }',
  3181. self::$configurationOopPositionSameLine,
  3182. ),
  3183. array(
  3184. '<?php
  3185. foo(1, new class implements Logger {
  3186. public function log($message) {
  3187. log($message);
  3188. }
  3189. }, 3);',
  3190. '<?php
  3191. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  3192. self::$configurationOopPositionSameLine,
  3193. ),
  3194. array(
  3195. '<?php
  3196. $message = (new class() implements FooInterface {
  3197. });',
  3198. '<?php
  3199. $message = (new class() implements FooInterface{});',
  3200. self::$configurationOopPositionSameLine,
  3201. ),
  3202. array(
  3203. '<?php $message = (new class() {
  3204. });',
  3205. '<?php $message = (new class() {});',
  3206. self::$configurationOopPositionSameLine,
  3207. ),
  3208. array(
  3209. '<?php
  3210. if (1) {
  3211. $message = (new class() extends Foo {
  3212. public function bar() {
  3213. echo 1;
  3214. }
  3215. });
  3216. }',
  3217. '<?php
  3218. if (1) {
  3219. $message = (new class() extends Foo
  3220. {
  3221. public function bar() { echo 1; }
  3222. });
  3223. }',
  3224. self::$configurationOopPositionSameLine,
  3225. ),
  3226. array(
  3227. '<?php
  3228. class Foo {
  3229. public function use() {
  3230. }
  3231. public function use1(): string {
  3232. }
  3233. }
  3234. ',
  3235. '<?php
  3236. class Foo
  3237. {
  3238. public function use() {
  3239. }
  3240. public function use1(): string {
  3241. }
  3242. }
  3243. ',
  3244. self::$configurationOopPositionSameLine,
  3245. ),
  3246. array(
  3247. '<?php
  3248. $a = function (int $foo): string {
  3249. echo $foo;
  3250. };
  3251. $b = function (int $foo) use ($bar): string {
  3252. echo $foo . $bar;
  3253. };
  3254. function a() {
  3255. }
  3256. ',
  3257. '<?php
  3258. $a = function (int $foo): string
  3259. {
  3260. echo $foo;
  3261. };
  3262. $b = function (int $foo) use($bar): string
  3263. {
  3264. echo $foo . $bar;
  3265. };
  3266. function a() {
  3267. }
  3268. ',
  3269. self::$configurationOopPositionSameLine,
  3270. ),
  3271. array(
  3272. '<?php
  3273. class Something {
  3274. public function sth(): string {
  3275. return function (int $foo) use ($bar): string {
  3276. return $bar;
  3277. };
  3278. }
  3279. }',
  3280. '<?php
  3281. class Something
  3282. {
  3283. public function sth(): string
  3284. {
  3285. return function (int $foo) use ($bar): string { return $bar; };
  3286. }
  3287. }',
  3288. self::$configurationOopPositionSameLine,
  3289. ),
  3290. array(
  3291. '<?php
  3292. use function some\a\{
  3293. test1,
  3294. test2
  3295. };
  3296. test();',
  3297. null,
  3298. self::$configurationOopPositionSameLine,
  3299. ),
  3300. array(
  3301. '<?php
  3302. use some\a\{ClassA, ClassB, ClassC as C};
  3303. use function some\a\{fn_a, fn_b, fn_c};
  3304. use const some\a\{ConstA, ConstB, ConstC};
  3305. ',
  3306. null,
  3307. self::$configurationOopPositionSameLine,
  3308. ),
  3309. );
  3310. }
  3311. /**
  3312. * @param string $expected
  3313. * @param null|string $input
  3314. * @param null|array $configuration
  3315. *
  3316. * @dataProvider providePreserveLineAfterControlBrace
  3317. */
  3318. public function testPreserveLineAfterControlBrace($expected, $input = null, array $configuration = null)
  3319. {
  3320. if (null !== $configuration) {
  3321. $this->fixer->configure($configuration);
  3322. }
  3323. $this->doTest($expected, $input);
  3324. }
  3325. public function providePreserveLineAfterControlBrace()
  3326. {
  3327. return array(
  3328. array(
  3329. '<?php
  3330. if (1==1) { // test
  3331. $a = 1;
  3332. }
  3333. echo $a;',
  3334. '<?php
  3335. if (1==1) // test
  3336. { $a = 1; }
  3337. echo $a;',
  3338. ),
  3339. array(
  3340. '<?php
  3341. if ($test) { // foo
  3342. echo 1;
  3343. }
  3344. if (1 === 1) {//a
  3345. $a = "b"; /*d*/
  3346. }//c
  3347. echo $a;
  3348. if ($a === 3) /**/
  3349. {echo 1;}
  3350. ',
  3351. '<?php
  3352. if ($test) // foo
  3353. {
  3354. echo 1;
  3355. }
  3356. if (1 === 1)//a
  3357. {$a = "b"; /*d*/}//c
  3358. echo $a;
  3359. if ($a === 3) /**/
  3360. {echo 1;}
  3361. ',
  3362. ),
  3363. array(
  3364. '<?php
  3365. if (true) {
  3366. // The blank line helps with legibility in nested control structures
  3367. if (true) {
  3368. // if body
  3369. }
  3370. // if body
  3371. }',
  3372. ),
  3373. array(
  3374. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  3375. ),
  3376. array(
  3377. '<?php
  3378. if (true) {
  3379. // The blank line helps with legibility in nested control structures
  3380. if (true) {
  3381. // if body
  3382. }
  3383. // if body
  3384. }',
  3385. null,
  3386. self::$configurationOopPositionSameLine,
  3387. ),
  3388. array(
  3389. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  3390. null,
  3391. self::$configurationOopPositionSameLine,
  3392. ),
  3393. );
  3394. }
  3395. /**
  3396. * @param string $expected
  3397. * @param null|string $input
  3398. * @param null|array $configuration
  3399. *
  3400. * @dataProvider provideFixWithAllowOnelineLambdaCases
  3401. */
  3402. public function testFixWithAllowSingleLineClosure($expected, $input = null, array $configuration = null)
  3403. {
  3404. if (null !== $configuration) {
  3405. $this->fixer->configure($configuration);
  3406. }
  3407. $this->fixer->configure(array(
  3408. 'allow_single_line_closure' => true,
  3409. ));
  3410. $this->doTest($expected, $input);
  3411. }
  3412. public function provideFixWithAllowOnelineLambdaCases()
  3413. {
  3414. return array(
  3415. array(
  3416. '<?php
  3417. $callback = function () { return true; };',
  3418. ),
  3419. array(
  3420. '<?php
  3421. $callback = function () { if ($a) { return true; } return false; };',
  3422. '<?php
  3423. $callback = function () { if($a){ return true; } return false; };',
  3424. ),
  3425. array(
  3426. '<?php
  3427. $callback = function () { if ($a) { return true; } return false; };',
  3428. '<?php
  3429. $callback = function () { if($a) return true; return false; };',
  3430. ),
  3431. array(
  3432. '<?php
  3433. $callback = function () {
  3434. if ($a) {
  3435. return true;
  3436. }
  3437. return false;
  3438. };',
  3439. '<?php
  3440. $callback = function () { if($a) return true;
  3441. return false; };',
  3442. ),
  3443. );
  3444. }
  3445. /**
  3446. * @param string $expected
  3447. * @param null|string $input
  3448. * @param null|array $configuration
  3449. *
  3450. * @dataProvider provideMessyWhitespacesCases
  3451. */
  3452. public function testMessyWhitespaces($expected, $input = null, array $configuration = null)
  3453. {
  3454. if (null !== $configuration) {
  3455. $this->fixer->configure($configuration);
  3456. }
  3457. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  3458. $this->doTest($expected, $input);
  3459. }
  3460. public function provideMessyWhitespacesCases()
  3461. {
  3462. return array(
  3463. array(
  3464. '<?php
  3465. if (true) {'."\r\n"
  3466. ."\t".'if (true) {'."\r\n"
  3467. ."\t\t".'echo 1;'."\r\n"
  3468. ."\t".'} elseif (true) {'."\r\n"
  3469. ."\t\t".'echo 2;'."\r\n"
  3470. ."\t".'} else {'."\r\n"
  3471. ."\t\t".'echo 3;'."\r\n"
  3472. ."\t".'}'."\r\n"
  3473. .'}',
  3474. '<?php
  3475. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  3476. ),
  3477. array(
  3478. '<?php
  3479. if (true) {'."\r\n"
  3480. ."\t".'if (true) {'."\r\n"
  3481. ."\t\t".'echo 1;'."\r\n"
  3482. ."\t".'} elseif (true) {'."\r\n"
  3483. ."\t\t".'echo 2;'."\r\n"
  3484. ."\t".'} else {'."\r\n"
  3485. ."\t\t".'echo 3;'."\r\n"
  3486. ."\t".'}'."\r\n"
  3487. .'}',
  3488. '<?php
  3489. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  3490. self::$configurationOopPositionSameLine,
  3491. ),
  3492. );
  3493. }
  3494. public function provideDoWhileLoopInsideAnIfWithoutBrackets()
  3495. {
  3496. return array(
  3497. array(
  3498. '<?php
  3499. if (true) {
  3500. do {
  3501. echo 1;
  3502. } while (false);
  3503. }',
  3504. '<?php
  3505. if (true)
  3506. do {
  3507. echo 1;
  3508. } while (false);',
  3509. ),
  3510. );
  3511. }
  3512. /**
  3513. * @param string $expected
  3514. * @param null|string $input
  3515. *
  3516. * @dataProvider provideDoWhileLoopInsideAnIfWithoutBrackets
  3517. */
  3518. public function testDoWhileLoopInsideAnIfWithoutBrackets($expected, $input = null)
  3519. {
  3520. $this->doTest($expected, $input);
  3521. }
  3522. }