BracesFixerTest.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639
  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 = ['position_after_functions_and_oop_constructs' => 'same'];
  24. public function testInvalidConfigurationClassyConstructs()
  25. {
  26. $this->setExpectedExceptionRegExp(
  27. \PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class,
  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(['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 [
  49. [
  50. '<?php
  51. $a = function() {
  52. $a = 1;
  53. while (false);
  54. };',
  55. ],
  56. [
  57. '<?php
  58. $a = function() {
  59. $a = 1;
  60. for ($i=0;$i<5;++$i);
  61. };',
  62. ],
  63. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  175. '<?php
  176. if (1) {
  177. do {
  178. $a = 1;
  179. } while (true);
  180. }',
  181. ],
  182. [
  183. '<?php
  184. if /* 1 */ (2) {
  185. }',
  186. '<?php
  187. if /* 1 */ (2) {}',
  188. ],
  189. [
  190. '<?php
  191. if (1) {
  192. echo $items{0}->foo;
  193. echo $collection->items{1}->property;
  194. }
  195. ',
  196. ],
  197. [
  198. '<?php
  199. $a = function() {
  200. $a = 1;
  201. while (false);
  202. };',
  203. null,
  204. self::$configurationOopPositionSameLine,
  205. ],
  206. [
  207. '<?php
  208. $a = function() {
  209. $a = 1;
  210. for ($i=0;$i<5;++$i);
  211. };',
  212. null,
  213. self::$configurationOopPositionSameLine,
  214. ],
  215. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  343. '<?php
  344. if (1) {
  345. do {
  346. $a = 1;
  347. } while (true);
  348. }',
  349. null,
  350. self::$configurationOopPositionSameLine,
  351. ],
  352. [
  353. '<?php
  354. if /* 1 */ (2) {
  355. }',
  356. '<?php
  357. if /* 1 */ (2) {}',
  358. self::$configurationOopPositionSameLine,
  359. ],
  360. [
  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. [
  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 [
  397. [
  398. '<?php
  399. if (true):
  400. $foo = 0;
  401. endif;',
  402. ],
  403. [
  404. '<?php
  405. if (true) :
  406. $foo = 0;
  407. endif;',
  408. ],
  409. [
  410. '<?php
  411. if (true) : $foo = 1; endif;',
  412. ],
  413. [
  414. '<?php
  415. if (true) {
  416. $foo = 1;
  417. }',
  418. '<?php
  419. if (true)$foo = 1;',
  420. ],
  421. [
  422. '<?php
  423. if (true) {
  424. $foo = 2;
  425. }',
  426. '<?php
  427. if (true) $foo = 2;',
  428. ],
  429. [
  430. '<?php
  431. if (true) {
  432. $foo = 3;
  433. }',
  434. '<?php
  435. if (true){$foo = 3;}',
  436. ],
  437. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  529. '<?php
  530. do {
  531. echo 1;
  532. } while (false);',
  533. '<?php
  534. do { echo 1; } while (false);',
  535. ],
  536. [
  537. '<?php
  538. while ($foo->next());',
  539. ],
  540. [
  541. '<?php
  542. foreach ($foo as $bar) {
  543. echo $bar;
  544. }',
  545. '<?php
  546. foreach ($foo as $bar) echo $bar;',
  547. ],
  548. [
  549. '<?php
  550. if (true) {
  551. $a = 1;
  552. }',
  553. '<?php
  554. if (true) {$a = 1;}',
  555. ],
  556. [
  557. '<?php
  558. if (true) {
  559. $a = 1;
  560. }',
  561. '<?php
  562. if (true) {
  563. $a = 1;
  564. }',
  565. ],
  566. [
  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. [
  587. '<?php
  588. if (true) {
  589. $a = 1;
  590. $b = 2;
  591. }',
  592. ],
  593. [
  594. '<?php
  595. if (1) {
  596. $a = 1;
  597. // comment at end
  598. }',
  599. ],
  600. [
  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. [
  615. '<?php
  616. foreach ($numbers as $num) {
  617. for ($i = 0; $i < $num; ++$i) {
  618. $a = "a";
  619. }
  620. $b = "b";
  621. }',
  622. ],
  623. [
  624. '<?php
  625. if (1) {
  626. if (2) {
  627. $foo = 2;
  628. if (3) {
  629. $foo = 3;
  630. }
  631. }
  632. }',
  633. ],
  634. [
  635. '<?php
  636. declare(ticks = 1) {
  637. $ticks = 1;
  638. }',
  639. '<?php
  640. declare (
  641. ticks = 1 ) {
  642. $ticks = 1;
  643. }',
  644. ],
  645. [
  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. [
  662. '<?php
  663. while (true) {
  664. foo();
  665. }',
  666. '<?php
  667. while (true)
  668. {
  669. foo();
  670. }',
  671. ],
  672. [
  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. [
  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. [
  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. [
  714. '<?php
  715. while ($true) {
  716. try {
  717. throw new \Exception();
  718. } catch (\Exception $e) {
  719. // do nothing
  720. }
  721. }',
  722. ],
  723. [
  724. '<?php
  725. interface Foo
  726. {
  727. public function setConfig(ConfigInterface $config);
  728. }',
  729. ],
  730. [
  731. '<?php
  732. function bar()
  733. {
  734. $a = 1; //comment
  735. }',
  736. ],
  737. [
  738. '<?php
  739. function & lambda()
  740. {
  741. return function () {
  742. };
  743. }',
  744. ],
  745. [
  746. '<?php
  747. function nested()
  748. {
  749. $a = "a{$b->c()}d";
  750. }',
  751. ],
  752. [
  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. [
  765. '<?php
  766. function mixed()
  767. {
  768. $a = $b->{"a{$c}d"}();
  769. }',
  770. ],
  771. [
  772. '<?php
  773. function mixedComplex()
  774. {
  775. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  776. }',
  777. ],
  778. [
  779. '<?php
  780. function mixedComplex()
  781. {
  782. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  783. }',
  784. ],
  785. [
  786. '<?php
  787. if (true):
  788. echo 1;
  789. else:
  790. echo 2;
  791. endif;
  792. ',
  793. ],
  794. [
  795. '<?php
  796. if ($test) { //foo
  797. echo 1;
  798. }',
  799. ],
  800. [
  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. [
  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. [
  844. '<?php if (true) {
  845. echo "s";
  846. } ?>x',
  847. '<?php if (true) echo "s" ?>x',
  848. ],
  849. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  1038. '<?php
  1039. function myFunction($foo, $bar)
  1040. {
  1041. return \Foo::{$foo}($bar);
  1042. }',
  1043. ],
  1044. [
  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. [
  1063. '<?php
  1064. if (true):
  1065. $foo = 0;
  1066. endif;',
  1067. null,
  1068. self::$configurationOopPositionSameLine,
  1069. ],
  1070. [
  1071. '<?php
  1072. if (true) :
  1073. $foo = 0;
  1074. endif;',
  1075. null,
  1076. self::$configurationOopPositionSameLine,
  1077. ],
  1078. [
  1079. '<?php
  1080. if (true) : $foo = 1; endif;',
  1081. null,
  1082. self::$configurationOopPositionSameLine,
  1083. ],
  1084. [
  1085. '<?php
  1086. if (true) {
  1087. $foo = 1;
  1088. }',
  1089. '<?php
  1090. if (true)$foo = 1;',
  1091. self::$configurationOopPositionSameLine,
  1092. ],
  1093. [
  1094. '<?php
  1095. if (true) {
  1096. $foo = 2;
  1097. }',
  1098. '<?php
  1099. if (true) $foo = 2;',
  1100. self::$configurationOopPositionSameLine,
  1101. ],
  1102. [
  1103. '<?php
  1104. if (true) {
  1105. $foo = 3;
  1106. }',
  1107. '<?php
  1108. if (true){$foo = 3;}',
  1109. self::$configurationOopPositionSameLine,
  1110. ],
  1111. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  1211. '<?php
  1212. do {
  1213. echo 1;
  1214. } while (false);',
  1215. '<?php
  1216. do { echo 1; } while (false);',
  1217. self::$configurationOopPositionSameLine,
  1218. ],
  1219. [
  1220. '<?php
  1221. while ($foo->next());',
  1222. null,
  1223. self::$configurationOopPositionSameLine,
  1224. ],
  1225. [
  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. [
  1235. '<?php
  1236. if (true) {
  1237. $a = 1;
  1238. }',
  1239. '<?php
  1240. if (true) {$a = 1;}',
  1241. self::$configurationOopPositionSameLine,
  1242. ],
  1243. [
  1244. '<?php
  1245. if (true) {
  1246. $a = 1;
  1247. }',
  1248. '<?php
  1249. if (true) {
  1250. $a = 1;
  1251. }',
  1252. self::$configurationOopPositionSameLine,
  1253. ],
  1254. [
  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. [
  1276. '<?php
  1277. if (true) {
  1278. $a = 1;
  1279. $b = 2;
  1280. }',
  1281. null,
  1282. self::$configurationOopPositionSameLine,
  1283. ],
  1284. [
  1285. '<?php
  1286. if (1) {
  1287. $a = 1;
  1288. // comment at end
  1289. }',
  1290. null,
  1291. self::$configurationOopPositionSameLine,
  1292. ],
  1293. [
  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. [
  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. [
  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. [
  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. [
  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. [
  1363. '<?php
  1364. while (true) {
  1365. foo();
  1366. }',
  1367. '<?php
  1368. while (true)
  1369. {
  1370. foo();
  1371. }',
  1372. self::$configurationOopPositionSameLine,
  1373. ],
  1374. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  1538. '<?php
  1539. if (true):
  1540. echo 1;
  1541. else:
  1542. echo 2;
  1543. endif;
  1544. ',
  1545. null,
  1546. self::$configurationOopPositionSameLine,
  1547. ],
  1548. [
  1549. '<?php
  1550. if ($test) { //foo
  1551. echo 1;
  1552. }',
  1553. null,
  1554. self::$configurationOopPositionSameLine,
  1555. ],
  1556. [
  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. [
  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. [
  1602. '<?php if (true) {
  1603. echo "s";
  1604. } ?>x',
  1605. '<?php if (true) echo "s" ?>x',
  1606. self::$configurationOopPositionSameLine,
  1607. ],
  1608. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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 [
  1885. [
  1886. '<?php
  1887. class FooA
  1888. {
  1889. }',
  1890. '<?php
  1891. class FooA {}',
  1892. ],
  1893. [
  1894. '<?php
  1895. class FooB
  1896. {
  1897. }',
  1898. '<?php
  1899. class FooB{}',
  1900. ],
  1901. [
  1902. '<?php
  1903. class FooC
  1904. {
  1905. }',
  1906. '<?php
  1907. class FooC
  1908. {}',
  1909. ],
  1910. [
  1911. '<?php
  1912. interface FooD
  1913. {
  1914. }',
  1915. '<?php
  1916. interface FooD {}',
  1917. ],
  1918. [
  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. [
  1928. '<?php
  1929. abstract class Foo
  1930. {
  1931. public function getProcess($foo)
  1932. {
  1933. return true;
  1934. }
  1935. }',
  1936. ],
  1937. ['<?php
  1938. function foo()
  1939. {
  1940. return "$c ($d)";
  1941. }',
  1942. ],
  1943. [
  1944. '<?php
  1945. class FooA {
  1946. }',
  1947. '<?php
  1948. class FooA {}',
  1949. self::$configurationOopPositionSameLine,
  1950. ],
  1951. [
  1952. '<?php
  1953. class FooB {
  1954. }',
  1955. '<?php
  1956. class FooB{}',
  1957. self::$configurationOopPositionSameLine,
  1958. ],
  1959. [
  1960. '<?php
  1961. class FooC {
  1962. }',
  1963. '<?php
  1964. class FooC
  1965. {}',
  1966. self::$configurationOopPositionSameLine,
  1967. ],
  1968. [
  1969. '<?php
  1970. interface FooD {
  1971. }',
  1972. '<?php
  1973. interface FooD {}',
  1974. self::$configurationOopPositionSameLine,
  1975. ],
  1976. [
  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. [
  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. [
  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. '<?php
  2016. trait TFoo
  2017. {
  2018. public $a;
  2019. }',
  2020. '<?php
  2021. trait TFoo {public $a;}',
  2022. ],
  2023. [
  2024. '<?php
  2025. trait TFoo {
  2026. public $a;
  2027. }',
  2028. '<?php
  2029. trait TFoo {public $a;}',
  2030. self::$configurationOopPositionSameLine,
  2031. ],
  2032. [
  2033. '<?php
  2034. trait TFoo
  2035. {
  2036. public $a;
  2037. }',
  2038. '<?php
  2039. trait TFoo {public $a;}',
  2040. ],
  2041. [
  2042. '<?php
  2043. trait TFoo {
  2044. public $a;
  2045. }',
  2046. '<?php
  2047. trait TFoo {public $a;}',
  2048. self::$configurationOopPositionSameLine,
  2049. ],
  2050. ];
  2051. }
  2052. /**
  2053. * @param string $expected
  2054. * @param null|string $input
  2055. * @param null|array $configuration
  2056. *
  2057. * @dataProvider provideFixAnonFunctionInShortArraySyntaxCases
  2058. */
  2059. public function testFixAnonFunctionInShortArraySyntax($expected, $input = null, array $configuration = null)
  2060. {
  2061. if (null !== $configuration) {
  2062. $this->fixer->configure($configuration);
  2063. }
  2064. $this->doTest($expected, $input);
  2065. }
  2066. public function provideFixAnonFunctionInShortArraySyntaxCases()
  2067. {
  2068. return [
  2069. [
  2070. '<?php
  2071. function myFunction()
  2072. {
  2073. return [
  2074. [
  2075. "callback" => function ($data) {
  2076. return true;
  2077. }
  2078. ],
  2079. [
  2080. "callback" => function ($data) {
  2081. return true;
  2082. },
  2083. ],
  2084. ];
  2085. }',
  2086. '<?php
  2087. function myFunction()
  2088. {
  2089. return [
  2090. [
  2091. "callback" => function ($data) {
  2092. return true;
  2093. }
  2094. ],
  2095. [
  2096. "callback" => function ($data) { return true; },
  2097. ],
  2098. ];
  2099. }',
  2100. ],
  2101. [
  2102. '<?php
  2103. function myFunction() {
  2104. return [
  2105. [
  2106. "callback" => function ($data) {
  2107. return true;
  2108. }
  2109. ],
  2110. [
  2111. "callback" => function ($data) {
  2112. return true;
  2113. },
  2114. ],
  2115. ];
  2116. }',
  2117. '<?php
  2118. function myFunction()
  2119. {
  2120. return [
  2121. [
  2122. "callback" => function ($data) {
  2123. return true;
  2124. }
  2125. ],
  2126. [
  2127. "callback" => function ($data) { return true; },
  2128. ],
  2129. ];
  2130. }',
  2131. self::$configurationOopPositionSameLine,
  2132. ],
  2133. ];
  2134. }
  2135. /**
  2136. * @param string $expected
  2137. * @param null|string $input
  2138. * @param null|array $configuration
  2139. *
  2140. * @dataProvider provideFixCommentBeforeBraceCases
  2141. */
  2142. public function testFixCommentBeforeBrace($expected, $input = null, array $configuration = null)
  2143. {
  2144. if (null !== $configuration) {
  2145. $this->fixer->configure($configuration);
  2146. }
  2147. $this->doTest($expected, $input);
  2148. }
  2149. public function provideFixCommentBeforeBraceCases()
  2150. {
  2151. return [
  2152. ['<?php '],
  2153. [
  2154. '<?php
  2155. if ($test) { // foo
  2156. echo 1;
  2157. }',
  2158. '<?php
  2159. if ($test) // foo
  2160. {
  2161. echo 1;
  2162. }',
  2163. ],
  2164. [
  2165. '<?php
  2166. $foo = function ($x) use ($y) { // foo
  2167. echo 1;
  2168. };',
  2169. '<?php
  2170. $foo = function ($x) use ($y) // foo
  2171. {
  2172. echo 1;
  2173. };',
  2174. ],
  2175. ['<?php ', null, self::$configurationOopPositionSameLine],
  2176. [
  2177. '<?php
  2178. if ($test) { // foo
  2179. echo 1;
  2180. }',
  2181. '<?php
  2182. if ($test) // foo
  2183. {
  2184. echo 1;
  2185. }',
  2186. null,
  2187. self::$configurationOopPositionSameLine,
  2188. ],
  2189. [
  2190. '<?php
  2191. $foo = function ($x) use ($y) { // foo
  2192. echo 1;
  2193. };',
  2194. '<?php
  2195. $foo = function ($x) use ($y) // foo
  2196. {
  2197. echo 1;
  2198. };',
  2199. null,
  2200. self::$configurationOopPositionSameLine,
  2201. ],
  2202. ];
  2203. }
  2204. /**
  2205. * @param string $expected
  2206. * @param null|string $input
  2207. * @param null|array $configuration
  2208. *
  2209. * @dataProvider provideFixCommentBeforeBrace70Cases
  2210. * @requires PHP 7.0
  2211. */
  2212. public function testFixCommentBeforeBrace70($expected, $input = null, array $configuration = null)
  2213. {
  2214. if (null !== $configuration) {
  2215. $this->fixer->configure($configuration);
  2216. }
  2217. $this->doTest($expected, $input);
  2218. }
  2219. public function provideFixCommentBeforeBrace70Cases()
  2220. {
  2221. return [
  2222. [
  2223. '<?php
  2224. $foo = new class ($a) extends Foo implements Bar { // foo
  2225. private $x;
  2226. };',
  2227. '<?php
  2228. $foo = new class ($a) extends Foo implements Bar // foo
  2229. {
  2230. private $x;
  2231. };',
  2232. ],
  2233. [
  2234. '<?php
  2235. $foo = new class ($a) extends Foo implements Bar { // foo
  2236. private $x;
  2237. };',
  2238. '<?php
  2239. $foo = new class ($a) extends Foo implements Bar // foo
  2240. {
  2241. private $x;
  2242. };',
  2243. self::$configurationOopPositionSameLine,
  2244. ],
  2245. ];
  2246. }
  2247. /**
  2248. * @param string $expected
  2249. * @param null|string $input
  2250. * @param null|array $configuration
  2251. *
  2252. * @dataProvider provideFixWhitespaceBeforeBraceCases
  2253. */
  2254. public function testFixWhitespaceBeforeBrace($expected, $input = null, array $configuration = null)
  2255. {
  2256. if (null !== $configuration) {
  2257. $this->fixer->configure($configuration);
  2258. }
  2259. $this->doTest($expected, $input);
  2260. }
  2261. public function provideFixWhitespaceBeforeBraceCases()
  2262. {
  2263. return [
  2264. [
  2265. '<?php
  2266. if (true) {
  2267. echo 1;
  2268. }',
  2269. '<?php
  2270. if (true)
  2271. {
  2272. echo 1;
  2273. }',
  2274. ],
  2275. [
  2276. '<?php
  2277. if (true) {
  2278. echo 1;
  2279. }',
  2280. '<?php
  2281. if (true){
  2282. echo 1;
  2283. }',
  2284. ],
  2285. [
  2286. '<?php
  2287. if (true) {
  2288. echo 1;
  2289. }',
  2290. '<?php
  2291. if (true) {
  2292. echo 1;
  2293. }',
  2294. ],
  2295. [
  2296. '<?php
  2297. while ($file = $this->getFile()) {
  2298. }',
  2299. '<?php
  2300. while ($file = $this->getFile())
  2301. {
  2302. }',
  2303. ],
  2304. [
  2305. '<?php
  2306. switch (n) {
  2307. case label1:
  2308. echo 1;
  2309. echo 2;
  2310. break;
  2311. default:
  2312. echo 3;
  2313. echo 4;
  2314. }',
  2315. '<?php
  2316. switch (n)
  2317. {
  2318. case label1:
  2319. echo 1;
  2320. echo 2;
  2321. break;
  2322. default:
  2323. echo 3;
  2324. echo 4;
  2325. }',
  2326. ],
  2327. [
  2328. '<?php
  2329. if (true) {
  2330. echo 1;
  2331. }',
  2332. '<?php
  2333. if (true)
  2334. {
  2335. echo 1;
  2336. }',
  2337. self::$configurationOopPositionSameLine,
  2338. ],
  2339. [
  2340. '<?php
  2341. if (true) {
  2342. echo 1;
  2343. }',
  2344. '<?php
  2345. if (true){
  2346. echo 1;
  2347. }',
  2348. self::$configurationOopPositionSameLine,
  2349. ],
  2350. [
  2351. '<?php
  2352. if (true) {
  2353. echo 1;
  2354. }',
  2355. '<?php
  2356. if (true) {
  2357. echo 1;
  2358. }',
  2359. self::$configurationOopPositionSameLine,
  2360. ],
  2361. [
  2362. '<?php
  2363. while ($file = $this->getFile()) {
  2364. }',
  2365. '<?php
  2366. while ($file = $this->getFile())
  2367. {
  2368. }',
  2369. self::$configurationOopPositionSameLine,
  2370. ],
  2371. [
  2372. '<?php
  2373. switch (n) {
  2374. case label1:
  2375. echo 1;
  2376. echo 2;
  2377. break;
  2378. default:
  2379. echo 3;
  2380. echo 4;
  2381. }',
  2382. '<?php
  2383. switch (n)
  2384. {
  2385. case label1:
  2386. echo 1;
  2387. echo 2;
  2388. break;
  2389. default:
  2390. echo 3;
  2391. echo 4;
  2392. }',
  2393. self::$configurationOopPositionSameLine,
  2394. ],
  2395. ];
  2396. }
  2397. /**
  2398. * @param string $expected
  2399. * @param null|string $input
  2400. * @param null|array $configuration
  2401. *
  2402. * @dataProvider provideFixFunctionsCases
  2403. */
  2404. public function testFixFunctions($expected, $input = null, array $configuration = null)
  2405. {
  2406. if (null !== $configuration) {
  2407. $this->fixer->configure($configuration);
  2408. }
  2409. $this->doTest($expected, $input);
  2410. }
  2411. public function provideFixFunctionsCases()
  2412. {
  2413. return [
  2414. [
  2415. '<?php
  2416. function download()
  2417. {
  2418. }',
  2419. '<?php
  2420. function download() {
  2421. }',
  2422. ],
  2423. [
  2424. '<?php
  2425. class Foo
  2426. {
  2427. public function AAAA()
  2428. {
  2429. }
  2430. public function BBBB()
  2431. {
  2432. }
  2433. public function CCCC()
  2434. {
  2435. }
  2436. }',
  2437. '<?php
  2438. class Foo
  2439. {
  2440. public function AAAA(){
  2441. }
  2442. public function BBBB() {
  2443. }
  2444. public function CCCC()
  2445. {
  2446. }
  2447. }',
  2448. ],
  2449. [
  2450. '<?php
  2451. filter(function () {
  2452. return true;
  2453. });
  2454. ',
  2455. ],
  2456. [
  2457. '<?php
  2458. filter(function ($a) {
  2459. });',
  2460. '<?php
  2461. filter(function ($a)
  2462. {});',
  2463. ],
  2464. [
  2465. '<?php
  2466. filter(function ($b) {
  2467. });',
  2468. '<?php
  2469. filter(function ($b){});',
  2470. ],
  2471. [
  2472. '<?php
  2473. foo(array_map(function ($object) use ($x, $y) {
  2474. return array_filter($object->bar(), function ($o) {
  2475. return $o->isBaz();
  2476. });
  2477. }, $collection));',
  2478. '<?php
  2479. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  2480. ],
  2481. [
  2482. '<?php
  2483. class Foo
  2484. {
  2485. public static function bar()
  2486. {
  2487. return 1;
  2488. }
  2489. }',
  2490. ],
  2491. [
  2492. '<?php
  2493. usort($this->fixers, function &($a, $b) use ($selfName) {
  2494. return 1;
  2495. });',
  2496. ],
  2497. [
  2498. '<?php
  2499. usort(
  2500. $this->fixers,
  2501. function &($a, $b) use ($selfName) {
  2502. return 1;
  2503. }
  2504. );',
  2505. ],
  2506. [
  2507. '<?php
  2508. $fnc = function ($a, $b) { // random comment
  2509. return 0;
  2510. };',
  2511. '<?php
  2512. $fnc = function ($a, $b) // random comment
  2513. {
  2514. return 0;
  2515. };',
  2516. ],
  2517. [
  2518. '<?php
  2519. $fnc = function ($a, $b) { # random comment
  2520. return 0;
  2521. };',
  2522. '<?php
  2523. $fnc = function ($a, $b) # random comment
  2524. {
  2525. return 0;
  2526. };',
  2527. ],
  2528. [
  2529. '<?php
  2530. $fnc = function ($a, $b) /* random comment */ {
  2531. return 0;
  2532. };',
  2533. '<?php
  2534. $fnc = function ($a, $b) /* random comment */
  2535. {
  2536. return 0;
  2537. };',
  2538. ],
  2539. [
  2540. '<?php
  2541. $fnc = function ($a, $b) /** random comment */ {
  2542. return 0;
  2543. };',
  2544. '<?php
  2545. $fnc = function ($a, $b) /** random comment */
  2546. {
  2547. return 0;
  2548. };',
  2549. ],
  2550. [
  2551. '<?php
  2552. function download() {
  2553. }',
  2554. null,
  2555. self::$configurationOopPositionSameLine,
  2556. ],
  2557. [
  2558. '<?php
  2559. class Foo {
  2560. public function AAAA() {
  2561. }
  2562. public function BBBB() {
  2563. }
  2564. public function CCCC() {
  2565. }
  2566. }',
  2567. '<?php
  2568. class Foo
  2569. {
  2570. public function AAAA(){
  2571. }
  2572. public function BBBB() {
  2573. }
  2574. public function CCCC()
  2575. {
  2576. }
  2577. }',
  2578. self::$configurationOopPositionSameLine,
  2579. ],
  2580. [
  2581. '<?php
  2582. filter(function () {
  2583. return true;
  2584. });
  2585. ',
  2586. null,
  2587. self::$configurationOopPositionSameLine,
  2588. ],
  2589. [
  2590. '<?php
  2591. filter(function ($a) {
  2592. });',
  2593. '<?php
  2594. filter(function ($a)
  2595. {});',
  2596. self::$configurationOopPositionSameLine,
  2597. ],
  2598. [
  2599. '<?php
  2600. filter(function ($b) {
  2601. });',
  2602. '<?php
  2603. filter(function ($b){});',
  2604. self::$configurationOopPositionSameLine,
  2605. ],
  2606. [
  2607. '<?php
  2608. foo(array_map(function ($object) use ($x, $y) {
  2609. return array_filter($object->bar(), function ($o) {
  2610. return $o->isBaz();
  2611. });
  2612. }, $collection));',
  2613. '<?php
  2614. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  2615. self::$configurationOopPositionSameLine,
  2616. ],
  2617. [
  2618. '<?php
  2619. class Foo {
  2620. public static function bar() {
  2621. return 1;
  2622. }
  2623. }',
  2624. '<?php
  2625. class Foo
  2626. {
  2627. public static function bar()
  2628. {
  2629. return 1;
  2630. }
  2631. }',
  2632. self::$configurationOopPositionSameLine,
  2633. ],
  2634. [
  2635. '<?php
  2636. usort($this->fixers, function &($a, $b) use ($selfName) {
  2637. return 1;
  2638. });',
  2639. null,
  2640. self::$configurationOopPositionSameLine,
  2641. ],
  2642. [
  2643. '<?php
  2644. usort(
  2645. $this->fixers,
  2646. function &($a, $b) use ($selfName) {
  2647. return 1;
  2648. }
  2649. );',
  2650. null,
  2651. self::$configurationOopPositionSameLine,
  2652. ],
  2653. [
  2654. '<?php
  2655. $fnc = function ($a, $b) { // random comment
  2656. return 0;
  2657. };',
  2658. '<?php
  2659. $fnc = function ($a, $b) // random comment
  2660. {
  2661. return 0;
  2662. };',
  2663. self::$configurationOopPositionSameLine,
  2664. ],
  2665. [
  2666. '<?php
  2667. $fnc = function ($a, $b) { # random comment
  2668. return 0;
  2669. };',
  2670. '<?php
  2671. $fnc = function ($a, $b) # random comment
  2672. {
  2673. return 0;
  2674. };',
  2675. self::$configurationOopPositionSameLine,
  2676. ],
  2677. [
  2678. '<?php
  2679. $fnc = function ($a, $b) /* random comment */ {
  2680. return 0;
  2681. };',
  2682. '<?php
  2683. $fnc = function ($a, $b) /* random comment */
  2684. {
  2685. return 0;
  2686. };',
  2687. self::$configurationOopPositionSameLine,
  2688. ],
  2689. [
  2690. '<?php
  2691. $fnc = function ($a, $b) /** random comment */ {
  2692. return 0;
  2693. };',
  2694. '<?php
  2695. $fnc = function ($a, $b) /** random comment */
  2696. {
  2697. return 0;
  2698. };',
  2699. self::$configurationOopPositionSameLine,
  2700. ],
  2701. ];
  2702. }
  2703. /**
  2704. * @param string $expected
  2705. * @param null|string $input
  2706. * @param null|array $configuration
  2707. *
  2708. * @dataProvider provideFixSpaceAroundTokenCases
  2709. */
  2710. public function testFixSpaceAroundToken($expected, $input = null, array $configuration = null)
  2711. {
  2712. if (null !== $configuration) {
  2713. $this->fixer->configure($configuration);
  2714. }
  2715. $this->doTest($expected, $input);
  2716. }
  2717. public function provideFixSpaceAroundTokenCases()
  2718. {
  2719. return [
  2720. [
  2721. '<?php
  2722. try {
  2723. throw new Exception();
  2724. } catch (Exception $e) {
  2725. log($e);
  2726. }',
  2727. '<?php
  2728. try{
  2729. throw new Exception();
  2730. }catch (Exception $e){
  2731. log($e);
  2732. }',
  2733. ],
  2734. [
  2735. '<?php
  2736. do {
  2737. echo 1;
  2738. } while ($test);',
  2739. '<?php
  2740. do{
  2741. echo 1;
  2742. }while($test);',
  2743. ],
  2744. [
  2745. '<?php
  2746. if (true === true
  2747. && true === true
  2748. ) {
  2749. }',
  2750. '<?php
  2751. if(true === true
  2752. && true === true
  2753. ) {
  2754. }',
  2755. ],
  2756. [
  2757. '<?php
  2758. if (1) {
  2759. }
  2760. if ($this->tesT ($test)) {
  2761. }',
  2762. '<?php
  2763. if(1){
  2764. }
  2765. if ($this->tesT ($test)) {
  2766. }',
  2767. ],
  2768. [
  2769. '<?php
  2770. if (true) {
  2771. } elseif (false) {
  2772. } else {
  2773. }',
  2774. '<?php
  2775. if(true){
  2776. }elseif(false){
  2777. }else{
  2778. }',
  2779. ],
  2780. [
  2781. '<?php
  2782. $foo = function& () use ($bar) {
  2783. };',
  2784. '<?php
  2785. $foo = function& ()use($bar){};',
  2786. ],
  2787. [
  2788. '<?php
  2789. // comment
  2790. declare(strict_types=1);
  2791. // comment
  2792. while (true) {
  2793. }',
  2794. ],
  2795. [
  2796. '<?php
  2797. declare(ticks = 1) {
  2798. }',
  2799. '<?php
  2800. declare ( ticks = 1 ) {
  2801. }',
  2802. ],
  2803. [
  2804. '<?php
  2805. try {
  2806. throw new Exception();
  2807. } catch (Exception $e) {
  2808. log($e);
  2809. }',
  2810. '<?php
  2811. try{
  2812. throw new Exception();
  2813. }catch (Exception $e){
  2814. log($e);
  2815. }',
  2816. self::$configurationOopPositionSameLine,
  2817. ],
  2818. [
  2819. '<?php
  2820. do {
  2821. echo 1;
  2822. } while ($test);',
  2823. '<?php
  2824. do{
  2825. echo 1;
  2826. }while($test);',
  2827. self::$configurationOopPositionSameLine,
  2828. ],
  2829. [
  2830. '<?php
  2831. if (true === true
  2832. && true === true
  2833. ) {
  2834. }',
  2835. '<?php
  2836. if(true === true
  2837. && true === true
  2838. ) {
  2839. }',
  2840. self::$configurationOopPositionSameLine,
  2841. ],
  2842. [
  2843. '<?php
  2844. if (1) {
  2845. }
  2846. if ($this->tesT ($test)) {
  2847. }',
  2848. '<?php
  2849. if(1){
  2850. }
  2851. if ($this->tesT ($test)) {
  2852. }',
  2853. self::$configurationOopPositionSameLine,
  2854. ],
  2855. [
  2856. '<?php
  2857. if (true) {
  2858. } elseif (false) {
  2859. } else {
  2860. }',
  2861. '<?php
  2862. if(true){
  2863. }elseif(false){
  2864. }else{
  2865. }',
  2866. self::$configurationOopPositionSameLine,
  2867. ],
  2868. [
  2869. '<?php
  2870. $foo = function& () use ($bar) {
  2871. };',
  2872. '<?php
  2873. $foo = function& ()use($bar){};',
  2874. self::$configurationOopPositionSameLine,
  2875. ],
  2876. [
  2877. '<?php
  2878. // comment
  2879. declare(strict_types=1);
  2880. // comment
  2881. while (true) {
  2882. }',
  2883. null,
  2884. self::$configurationOopPositionSameLine,
  2885. ],
  2886. [
  2887. '<?php
  2888. declare(ticks = 1) {
  2889. }',
  2890. '<?php
  2891. declare ( ticks = 1 ) {
  2892. }',
  2893. self::$configurationOopPositionSameLine,
  2894. ],
  2895. ];
  2896. }
  2897. /**
  2898. * @param string $expected
  2899. * @param null|string $input
  2900. * @param null|array $configuration
  2901. *
  2902. * @dataProvider provideFinallyCases
  2903. */
  2904. public function testFinally($expected, $input = null, array $configuration = null)
  2905. {
  2906. if (null !== $configuration) {
  2907. $this->fixer->configure($configuration);
  2908. }
  2909. $this->doTest($expected, $input);
  2910. }
  2911. public function provideFinallyCases()
  2912. {
  2913. return [
  2914. [
  2915. '<?php
  2916. try {
  2917. throw new \Exception();
  2918. } catch (\LogicException $e) {
  2919. // do nothing
  2920. } catch (\Exception $e) {
  2921. // do nothing
  2922. } finally {
  2923. echo "finish!";
  2924. }',
  2925. '<?php
  2926. try {
  2927. throw new \Exception();
  2928. }catch (\LogicException $e) {
  2929. // do nothing
  2930. }
  2931. catch (\Exception $e) {
  2932. // do nothing
  2933. }
  2934. finally {
  2935. echo "finish!";
  2936. }',
  2937. ],
  2938. [
  2939. '<?php
  2940. try {
  2941. throw new \Exception();
  2942. } catch (\LogicException $e) {
  2943. // do nothing
  2944. } catch (\Exception $e) {
  2945. // do nothing
  2946. } finally {
  2947. echo "finish!";
  2948. }',
  2949. '<?php
  2950. try {
  2951. throw new \Exception();
  2952. }catch (\LogicException $e) {
  2953. // do nothing
  2954. }
  2955. catch (\Exception $e) {
  2956. // do nothing
  2957. }
  2958. finally {
  2959. echo "finish!";
  2960. }',
  2961. self::$configurationOopPositionSameLine,
  2962. ],
  2963. ];
  2964. }
  2965. /**
  2966. * @param string $expected
  2967. * @param null|string $input
  2968. * @param null|array $configuration
  2969. *
  2970. * @dataProvider provideFunctionImportCases
  2971. */
  2972. public function testFunctionImport($expected, $input = null, array $configuration = null)
  2973. {
  2974. if (null !== $configuration) {
  2975. $this->fixer->configure($configuration);
  2976. }
  2977. $this->doTest($expected, $input);
  2978. }
  2979. public function provideFunctionImportCases()
  2980. {
  2981. return [
  2982. [
  2983. '<?php
  2984. use function Foo\bar;
  2985. if (true) {
  2986. }',
  2987. ],
  2988. [
  2989. '<?php
  2990. use function Foo\bar;
  2991. if (true) {
  2992. }',
  2993. null,
  2994. self::$configurationOopPositionSameLine,
  2995. ],
  2996. ];
  2997. }
  2998. /**
  2999. * @param string $expected
  3000. * @param null|string $input
  3001. * @param null|array $configuration
  3002. *
  3003. * @dataProvider provide70Cases
  3004. * @requires PHP 7.0
  3005. */
  3006. public function test70($expected, $input = null, array $configuration = null)
  3007. {
  3008. if (null !== $configuration) {
  3009. $this->fixer->configure($configuration);
  3010. }
  3011. $this->doTest($expected, $input);
  3012. }
  3013. public function provide70Cases()
  3014. {
  3015. return [
  3016. [
  3017. '<?php
  3018. function foo($a)
  3019. {
  3020. // foo
  3021. $foo = new class($a) extends Foo {
  3022. public function bar()
  3023. {
  3024. }
  3025. };
  3026. }',
  3027. '<?php
  3028. function foo($a)
  3029. {
  3030. // foo
  3031. $foo = new class($a) extends Foo { public function bar() {} };
  3032. }',
  3033. ],
  3034. [
  3035. '<?php
  3036. foo(1, new class implements Logger {
  3037. public function log($message)
  3038. {
  3039. log($message);
  3040. }
  3041. }, 3);',
  3042. '<?php
  3043. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  3044. ],
  3045. [
  3046. '<?php
  3047. $message = (new class() implements FooInterface {
  3048. });',
  3049. '<?php
  3050. $message = (new class() implements FooInterface{});',
  3051. ],
  3052. [
  3053. '<?php $message = (new class() {
  3054. });',
  3055. '<?php $message = (new class() {});',
  3056. ],
  3057. [
  3058. '<?php
  3059. if (1) {
  3060. $message = (new class() extends Foo {
  3061. public function bar()
  3062. {
  3063. echo 1;
  3064. }
  3065. });
  3066. }',
  3067. '<?php
  3068. if (1) {
  3069. $message = (new class() extends Foo
  3070. {
  3071. public function bar() { echo 1; }
  3072. });
  3073. }',
  3074. ],
  3075. [
  3076. '<?php
  3077. class Foo
  3078. {
  3079. public function use()
  3080. {
  3081. }
  3082. public function use1(): string
  3083. {
  3084. }
  3085. }
  3086. ',
  3087. '<?php
  3088. class Foo
  3089. {
  3090. public function use() {
  3091. }
  3092. public function use1(): string {
  3093. }
  3094. }
  3095. ',
  3096. ],
  3097. [
  3098. '<?php
  3099. $a = function (int $foo): string {
  3100. echo $foo;
  3101. };
  3102. $b = function (int $foo) use ($bar): string {
  3103. echo $foo . $bar;
  3104. };
  3105. function a()
  3106. {
  3107. }
  3108. ',
  3109. '<?php
  3110. $a = function (int $foo): string
  3111. {
  3112. echo $foo;
  3113. };
  3114. $b = function (int $foo) use($bar): string
  3115. {
  3116. echo $foo . $bar;
  3117. };
  3118. function a() {
  3119. }
  3120. ',
  3121. ],
  3122. [
  3123. '<?php
  3124. class Something
  3125. {
  3126. public function sth(): string
  3127. {
  3128. return function (int $foo) use ($bar): string {
  3129. return $bar;
  3130. };
  3131. }
  3132. }',
  3133. '<?php
  3134. class Something
  3135. {
  3136. public function sth(): string
  3137. {
  3138. return function (int $foo) use ($bar): string { return $bar; };
  3139. }
  3140. }',
  3141. ],
  3142. [
  3143. '<?php
  3144. use function some\a\{
  3145. test1,
  3146. test2
  3147. };
  3148. test();',
  3149. ],
  3150. [
  3151. '<?php
  3152. use some\a\{ClassA, ClassB, ClassC as C};
  3153. use function some\a\{fn_a, fn_b, fn_c};
  3154. use const some\a\{ConstA, ConstB, ConstC};
  3155. ',
  3156. ],
  3157. [
  3158. '<?php
  3159. function foo($a) {
  3160. // foo
  3161. $foo = new class($a) extends Foo {
  3162. public function bar() {
  3163. }
  3164. };
  3165. }',
  3166. '<?php
  3167. function foo($a)
  3168. {
  3169. // foo
  3170. $foo = new class($a) extends Foo { public function bar() {} };
  3171. }',
  3172. self::$configurationOopPositionSameLine,
  3173. ],
  3174. [
  3175. '<?php
  3176. foo(1, new class implements Logger {
  3177. public function log($message) {
  3178. log($message);
  3179. }
  3180. }, 3);',
  3181. '<?php
  3182. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  3183. self::$configurationOopPositionSameLine,
  3184. ],
  3185. [
  3186. '<?php
  3187. $message = (new class() implements FooInterface {
  3188. });',
  3189. '<?php
  3190. $message = (new class() implements FooInterface{});',
  3191. self::$configurationOopPositionSameLine,
  3192. ],
  3193. [
  3194. '<?php $message = (new class() {
  3195. });',
  3196. '<?php $message = (new class() {});',
  3197. self::$configurationOopPositionSameLine,
  3198. ],
  3199. [
  3200. '<?php
  3201. if (1) {
  3202. $message = (new class() extends Foo {
  3203. public function bar() {
  3204. echo 1;
  3205. }
  3206. });
  3207. }',
  3208. '<?php
  3209. if (1) {
  3210. $message = (new class() extends Foo
  3211. {
  3212. public function bar() { echo 1; }
  3213. });
  3214. }',
  3215. self::$configurationOopPositionSameLine,
  3216. ],
  3217. [
  3218. '<?php
  3219. class Foo {
  3220. public function use() {
  3221. }
  3222. public function use1(): string {
  3223. }
  3224. }
  3225. ',
  3226. '<?php
  3227. class Foo
  3228. {
  3229. public function use() {
  3230. }
  3231. public function use1(): string {
  3232. }
  3233. }
  3234. ',
  3235. self::$configurationOopPositionSameLine,
  3236. ],
  3237. [
  3238. '<?php
  3239. $a = function (int $foo): string {
  3240. echo $foo;
  3241. };
  3242. $b = function (int $foo) use ($bar): string {
  3243. echo $foo . $bar;
  3244. };
  3245. function a() {
  3246. }
  3247. ',
  3248. '<?php
  3249. $a = function (int $foo): string
  3250. {
  3251. echo $foo;
  3252. };
  3253. $b = function (int $foo) use($bar): string
  3254. {
  3255. echo $foo . $bar;
  3256. };
  3257. function a() {
  3258. }
  3259. ',
  3260. self::$configurationOopPositionSameLine,
  3261. ],
  3262. [
  3263. '<?php
  3264. class Something {
  3265. public function sth(): string {
  3266. return function (int $foo) use ($bar): string {
  3267. return $bar;
  3268. };
  3269. }
  3270. }',
  3271. '<?php
  3272. class Something
  3273. {
  3274. public function sth(): string
  3275. {
  3276. return function (int $foo) use ($bar): string { return $bar; };
  3277. }
  3278. }',
  3279. self::$configurationOopPositionSameLine,
  3280. ],
  3281. [
  3282. '<?php
  3283. use function some\a\{
  3284. test1,
  3285. test2
  3286. };
  3287. test();',
  3288. null,
  3289. self::$configurationOopPositionSameLine,
  3290. ],
  3291. [
  3292. '<?php
  3293. use some\a\{ClassA, ClassB, ClassC as C};
  3294. use function some\a\{fn_a, fn_b, fn_c};
  3295. use const some\a\{ConstA, ConstB, ConstC};
  3296. ',
  3297. null,
  3298. self::$configurationOopPositionSameLine,
  3299. ],
  3300. ];
  3301. }
  3302. /**
  3303. * @param string $expected
  3304. * @param null|string $input
  3305. * @param null|array $configuration
  3306. *
  3307. * @dataProvider providePreserveLineAfterControlBrace
  3308. */
  3309. public function testPreserveLineAfterControlBrace($expected, $input = null, array $configuration = null)
  3310. {
  3311. if (null !== $configuration) {
  3312. $this->fixer->configure($configuration);
  3313. }
  3314. $this->doTest($expected, $input);
  3315. }
  3316. public function providePreserveLineAfterControlBrace()
  3317. {
  3318. return [
  3319. [
  3320. '<?php
  3321. if (1==1) { // test
  3322. $a = 1;
  3323. }
  3324. echo $a;',
  3325. '<?php
  3326. if (1==1) // test
  3327. { $a = 1; }
  3328. echo $a;',
  3329. ],
  3330. [
  3331. '<?php
  3332. if ($test) { // foo
  3333. echo 1;
  3334. }
  3335. if (1 === 1) {//a
  3336. $a = "b"; /*d*/
  3337. }//c
  3338. echo $a;
  3339. if ($a === 3) /**/
  3340. {echo 1;}
  3341. ',
  3342. '<?php
  3343. if ($test) // foo
  3344. {
  3345. echo 1;
  3346. }
  3347. if (1 === 1)//a
  3348. {$a = "b"; /*d*/}//c
  3349. echo $a;
  3350. if ($a === 3) /**/
  3351. {echo 1;}
  3352. ',
  3353. ],
  3354. [
  3355. '<?php
  3356. if (true) {
  3357. // The blank line helps with legibility in nested control structures
  3358. if (true) {
  3359. // if body
  3360. }
  3361. // if body
  3362. }',
  3363. ],
  3364. [
  3365. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  3366. ],
  3367. [
  3368. '<?php
  3369. if (true) {
  3370. // The blank line helps with legibility in nested control structures
  3371. if (true) {
  3372. // if body
  3373. }
  3374. // if body
  3375. }',
  3376. null,
  3377. self::$configurationOopPositionSameLine,
  3378. ],
  3379. [
  3380. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  3381. null,
  3382. self::$configurationOopPositionSameLine,
  3383. ],
  3384. ];
  3385. }
  3386. /**
  3387. * @param string $expected
  3388. * @param null|string $input
  3389. * @param null|array $configuration
  3390. *
  3391. * @dataProvider provideFixWithAllowOnelineLambdaCases
  3392. */
  3393. public function testFixWithAllowSingleLineClosure($expected, $input = null, array $configuration = null)
  3394. {
  3395. if (null !== $configuration) {
  3396. $this->fixer->configure($configuration);
  3397. }
  3398. $this->fixer->configure([
  3399. 'allow_single_line_closure' => true,
  3400. ]);
  3401. $this->doTest($expected, $input);
  3402. }
  3403. public function provideFixWithAllowOnelineLambdaCases()
  3404. {
  3405. return [
  3406. [
  3407. '<?php
  3408. $callback = function () { return true; };',
  3409. ],
  3410. [
  3411. '<?php
  3412. $callback = function () { if ($a) { return true; } return false; };',
  3413. '<?php
  3414. $callback = function () { if($a){ return true; } return false; };',
  3415. ],
  3416. [
  3417. '<?php
  3418. $callback = function () { if ($a) { return true; } return false; };',
  3419. '<?php
  3420. $callback = function () { if($a) return true; return false; };',
  3421. ],
  3422. [
  3423. '<?php
  3424. $callback = function () {
  3425. if ($a) {
  3426. return true;
  3427. }
  3428. return false;
  3429. };',
  3430. '<?php
  3431. $callback = function () { if($a) return true;
  3432. return false; };',
  3433. ],
  3434. ];
  3435. }
  3436. /**
  3437. * @param string $expected
  3438. * @param null|string $input
  3439. * @param null|array $configuration
  3440. *
  3441. * @dataProvider provideMessyWhitespacesCases
  3442. */
  3443. public function testMessyWhitespaces($expected, $input = null, array $configuration = null)
  3444. {
  3445. if (null !== $configuration) {
  3446. $this->fixer->configure($configuration);
  3447. }
  3448. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  3449. $this->doTest($expected, $input);
  3450. }
  3451. public function provideMessyWhitespacesCases()
  3452. {
  3453. return [
  3454. [
  3455. '<?php
  3456. if (true) {'."\r\n"
  3457. ."\t".'if (true) {'."\r\n"
  3458. ."\t\t".'echo 1;'."\r\n"
  3459. ."\t".'} elseif (true) {'."\r\n"
  3460. ."\t\t".'echo 2;'."\r\n"
  3461. ."\t".'} else {'."\r\n"
  3462. ."\t\t".'echo 3;'."\r\n"
  3463. ."\t".'}'."\r\n"
  3464. .'}',
  3465. '<?php
  3466. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  3467. ],
  3468. [
  3469. '<?php
  3470. if (true) {'."\r\n"
  3471. ."\t".'if (true) {'."\r\n"
  3472. ."\t\t".'echo 1;'."\r\n"
  3473. ."\t".'} elseif (true) {'."\r\n"
  3474. ."\t\t".'echo 2;'."\r\n"
  3475. ."\t".'} else {'."\r\n"
  3476. ."\t\t".'echo 3;'."\r\n"
  3477. ."\t".'}'."\r\n"
  3478. .'}',
  3479. '<?php
  3480. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  3481. self::$configurationOopPositionSameLine,
  3482. ],
  3483. ];
  3484. }
  3485. public function provideDoWhileLoopInsideAnIfWithoutBrackets()
  3486. {
  3487. return [
  3488. [
  3489. '<?php
  3490. if (true) {
  3491. do {
  3492. echo 1;
  3493. } while (false);
  3494. }',
  3495. '<?php
  3496. if (true)
  3497. do {
  3498. echo 1;
  3499. } while (false);',
  3500. ],
  3501. ];
  3502. }
  3503. /**
  3504. * @param string $expected
  3505. * @param null|string $input
  3506. *
  3507. * @dataProvider provideDoWhileLoopInsideAnIfWithoutBrackets
  3508. */
  3509. public function testDoWhileLoopInsideAnIfWithoutBrackets($expected, $input = null)
  3510. {
  3511. $this->doTest($expected, $input);
  3512. }
  3513. }