BracesFixerTest.php 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Basic;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Basic\BracesFixer
  21. */
  22. final class BracesFixerTest extends AbstractFixerTestCase
  23. {
  24. private static $configurationOopPositionSameLine = ['position_after_functions_and_oop_constructs' => 'same'];
  25. private static $configurationCtrlStructPositionNextLine = ['position_after_control_structures' => 'next'];
  26. private static $configurationAnonymousPositionNextLine = ['position_after_anonymous_constructs' => 'next'];
  27. public function testInvalidConfigurationClassyConstructs(): void
  28. {
  29. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  30. $this->expectExceptionMessageMatches('#^\[braces\] Invalid configuration: The option "position_after_functions_and_oop_constructs" with value "neither" is invalid\. Accepted values are: "next", "same"\.$#');
  31. $this->fixer->configure(['position_after_functions_and_oop_constructs' => 'neither']);
  32. }
  33. /**
  34. * @dataProvider provideFixControlContinuationBracesCases
  35. */
  36. public function testFixControlContinuationBraces(string $expected, ?string $input = null, array $configuration = []): void
  37. {
  38. $this->fixer->configure($configuration);
  39. $this->doTest($expected, $input);
  40. }
  41. public function provideFixControlContinuationBracesCases()
  42. {
  43. return [
  44. [
  45. '<?php
  46. $a = function() {
  47. $a = 1;
  48. while (false);
  49. };',
  50. ],
  51. [
  52. '<?php
  53. $a = function() {
  54. $a = 1;
  55. for ($i=0;$i<5;++$i);
  56. };',
  57. ],
  58. [
  59. '<?php
  60. class Foo
  61. {
  62. public function A()
  63. {
  64. ?>
  65. Test<?php echo $foobar; ?>Test
  66. <?php
  67. $a = 1;
  68. }
  69. }',
  70. ],
  71. [
  72. '<?php
  73. if (true) {
  74. $a = 1;
  75. } else {
  76. $b = 2;
  77. }',
  78. '<?php
  79. if (true) {
  80. $a = 1;
  81. }
  82. else {
  83. $b = 2;
  84. }',
  85. ],
  86. [
  87. '<?php
  88. try {
  89. throw new \Exception();
  90. } catch (\LogicException $e) {
  91. // do nothing
  92. } catch (\Exception $e) {
  93. // do nothing
  94. }',
  95. '<?php
  96. try {
  97. throw new \Exception();
  98. }catch (\LogicException $e) {
  99. // do nothing
  100. }
  101. catch (\Exception $e) {
  102. // do nothing
  103. }',
  104. ],
  105. [
  106. '<?php
  107. if (true) {
  108. echo 1;
  109. } elseif (true) {
  110. echo 2;
  111. }',
  112. '<?php
  113. if (true) {
  114. echo 1;
  115. } elseif (true)
  116. {
  117. echo 2;
  118. }',
  119. ],
  120. [
  121. '<?php
  122. try {
  123. echo 1;
  124. } catch (Exception $e) {
  125. echo 2;
  126. }',
  127. '<?php
  128. try
  129. {
  130. echo 1;
  131. }
  132. catch (Exception $e)
  133. {
  134. echo 2;
  135. }',
  136. ],
  137. [
  138. '<?php
  139. class Foo
  140. {
  141. public function bar(
  142. FooInterface $foo,
  143. BarInterface $bar,
  144. array $data = []
  145. ) {
  146. }
  147. }',
  148. '<?php
  149. class Foo
  150. {
  151. public function bar(
  152. FooInterface $foo,
  153. BarInterface $bar,
  154. array $data = []
  155. ){
  156. }
  157. }',
  158. ],
  159. [
  160. '<?php
  161. if (1) {
  162. self::${$key} = $val;
  163. self::${$type}[$rule] = $pattern;
  164. self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
  165. self::${$type}[$rule] = $pattern + self::${$type}["rules"];
  166. }
  167. ',
  168. ],
  169. [
  170. '<?php
  171. if (1) {
  172. do {
  173. $a = 1;
  174. } while (true);
  175. }',
  176. ],
  177. [
  178. '<?php
  179. if /* 1 */ (2) {
  180. }',
  181. '<?php
  182. if /* 1 */ (2) {}',
  183. ],
  184. [
  185. '<?php
  186. if (1) {
  187. echo $items{0}->foo;
  188. echo $collection->items{1}->property;
  189. }
  190. ',
  191. ],
  192. [
  193. '<?php
  194. $a = function() {
  195. $a = 1;
  196. while (false);
  197. };',
  198. null,
  199. self::$configurationOopPositionSameLine,
  200. ],
  201. [
  202. '<?php
  203. $a = function()
  204. {
  205. $a = 1;
  206. while (false);
  207. };',
  208. '<?php
  209. $a = function() {
  210. $a = 1;
  211. while (false);
  212. };',
  213. self::$configurationAnonymousPositionNextLine,
  214. ],
  215. [
  216. '<?php
  217. $a = function() {
  218. $a = 1;
  219. for ($i=0;$i<5;++$i);
  220. };',
  221. null,
  222. self::$configurationOopPositionSameLine,
  223. ],
  224. [
  225. '<?php
  226. $a = function()
  227. {
  228. $a = 1;
  229. for ($i=0;$i<5;++$i);
  230. };',
  231. '<?php
  232. $a = function() {
  233. $a = 1;
  234. for ($i=0;$i<5;++$i);
  235. };',
  236. self::$configurationAnonymousPositionNextLine,
  237. ],
  238. [
  239. '<?php
  240. class Foo {
  241. public function A() {
  242. ?>
  243. Test<?php echo $foobar; ?>Test
  244. <?php
  245. $a = 1;
  246. }
  247. }',
  248. '<?php
  249. class Foo
  250. {
  251. public function A()
  252. {
  253. ?>
  254. Test<?php echo $foobar; ?>Test
  255. <?php
  256. $a = 1;
  257. }
  258. }',
  259. self::$configurationOopPositionSameLine,
  260. ],
  261. [
  262. '<?php
  263. if (true) {
  264. $a = 1;
  265. } else {
  266. $b = 2;
  267. }',
  268. '<?php
  269. if (true) {
  270. $a = 1;
  271. }
  272. else {
  273. $b = 2;
  274. }',
  275. self::$configurationOopPositionSameLine,
  276. ],
  277. [
  278. '<?php
  279. if (true)
  280. {
  281. $a = 1;
  282. }
  283. else
  284. {
  285. $b = 2;
  286. }',
  287. '<?php
  288. if (true) {
  289. $a = 1;
  290. }
  291. else {
  292. $b = 2;
  293. }',
  294. self::$configurationCtrlStructPositionNextLine,
  295. ],
  296. [
  297. '<?php
  298. try {
  299. throw new \Exception();
  300. } catch (\LogicException $e) {
  301. // do nothing
  302. } catch (\Exception $e) {
  303. // do nothing
  304. }',
  305. '<?php
  306. try {
  307. throw new \Exception();
  308. }catch (\LogicException $e) {
  309. // do nothing
  310. }
  311. catch (\Exception $e) {
  312. // do nothing
  313. }',
  314. self::$configurationOopPositionSameLine,
  315. ],
  316. [
  317. '<?php
  318. try
  319. {
  320. throw new \Exception();
  321. }
  322. catch (\LogicException $e)
  323. {
  324. // do nothing
  325. }
  326. catch (\Exception $e)
  327. {
  328. // do nothing
  329. }',
  330. '<?php
  331. try {
  332. throw new \Exception();
  333. }catch (\LogicException $e) {
  334. // do nothing
  335. }
  336. catch (\Exception $e) {
  337. // do nothing
  338. }',
  339. self::$configurationCtrlStructPositionNextLine,
  340. ],
  341. [
  342. '<?php
  343. if (true) {
  344. echo 1;
  345. } elseif (true) {
  346. echo 2;
  347. }',
  348. '<?php
  349. if (true) {
  350. echo 1;
  351. } elseif (true)
  352. {
  353. echo 2;
  354. }',
  355. self::$configurationOopPositionSameLine,
  356. ],
  357. [
  358. '<?php
  359. try {
  360. echo 1;
  361. } catch (Exception $e) {
  362. echo 2;
  363. }',
  364. '<?php
  365. try
  366. {
  367. echo 1;
  368. }
  369. catch (Exception $e)
  370. {
  371. echo 2;
  372. }',
  373. self::$configurationOopPositionSameLine,
  374. ],
  375. [
  376. '<?php
  377. class Foo {
  378. public function bar(
  379. FooInterface $foo,
  380. BarInterface $bar,
  381. array $data = []
  382. ) {
  383. }
  384. }',
  385. '<?php
  386. class Foo
  387. {
  388. public function bar(
  389. FooInterface $foo,
  390. BarInterface $bar,
  391. array $data = []
  392. ){
  393. }
  394. }',
  395. self::$configurationOopPositionSameLine,
  396. ],
  397. [
  398. '<?php
  399. if (1) {
  400. self::${$key} = $val;
  401. self::${$type}[$rule] = $pattern;
  402. self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
  403. self::${$type}[$rule] = $pattern + self::${$type}["rules"];
  404. }
  405. ',
  406. null,
  407. self::$configurationOopPositionSameLine,
  408. ],
  409. [
  410. '<?php
  411. if (1) {
  412. do {
  413. $a = 1;
  414. } while (true);
  415. }',
  416. null,
  417. self::$configurationOopPositionSameLine,
  418. ],
  419. [
  420. '<?php
  421. if /* 1 */ (2) {
  422. }',
  423. '<?php
  424. if /* 1 */ (2) {}',
  425. self::$configurationOopPositionSameLine,
  426. ],
  427. [
  428. '<?php
  429. if (1) {
  430. echo $items{0}->foo;
  431. echo $collection->items{1}->property;
  432. }
  433. ',
  434. null,
  435. self::$configurationOopPositionSameLine,
  436. ],
  437. [
  438. '<?php class A
  439. /** */
  440. {
  441. }',
  442. null,
  443. self::$configurationOopPositionSameLine,
  444. ],
  445. [
  446. '<?php
  447. class Foo
  448. {
  449. public function foo()
  450. {
  451. foo();
  452. // baz
  453. bar();
  454. }
  455. }',
  456. '<?php
  457. class Foo
  458. {
  459. public function foo(){
  460. foo();
  461. // baz
  462. bar();
  463. }
  464. }',
  465. ],
  466. [
  467. '<?php
  468. class Foo
  469. {
  470. public function foo($foo)
  471. {
  472. return $foo // foo
  473. ? \'foo\'
  474. : \'bar\'
  475. ;
  476. }
  477. }',
  478. ],
  479. [
  480. '<?php
  481. class Foo
  482. {
  483. /**
  484. * Foo.
  485. */
  486. public $foo;
  487. /**
  488. * Bar.
  489. */
  490. public $bar;
  491. }',
  492. '<?php
  493. class Foo {
  494. /**
  495. * Foo.
  496. */
  497. public $foo;
  498. /**
  499. * Bar.
  500. */
  501. public $bar;
  502. }',
  503. ],
  504. [
  505. '<?php
  506. class Foo
  507. {
  508. /*
  509. * Foo.
  510. */
  511. public $foo;
  512. /*
  513. * Bar.
  514. */
  515. public $bar;
  516. }',
  517. '<?php
  518. class Foo {
  519. /*
  520. * Foo.
  521. */
  522. public $foo;
  523. /*
  524. * Bar.
  525. */
  526. public $bar;
  527. }',
  528. ],
  529. [
  530. '<?php
  531. if (1==1) {
  532. $a = 1;
  533. // test
  534. $b = 2;
  535. }',
  536. '<?php
  537. if (1==1) {
  538. $a = 1;
  539. // test
  540. $b = 2;
  541. }',
  542. ],
  543. [
  544. '<?php
  545. if (1==1) {
  546. $a = 1;
  547. # test
  548. $b = 2;
  549. }',
  550. '<?php
  551. if (1==1) {
  552. $a = 1;
  553. # test
  554. $b = 2;
  555. }',
  556. ],
  557. [
  558. '<?php
  559. if (1==1) {
  560. $a = 1;
  561. /** @var int $b */
  562. $b = a();
  563. }',
  564. '<?php
  565. if (1==1) {
  566. $a = 1;
  567. /** @var int $b */
  568. $b = a();
  569. }',
  570. ],
  571. [
  572. '<?php
  573. if ($b) {
  574. if (1==1) {
  575. $a = 1;
  576. // test
  577. $b = 2;
  578. }
  579. }
  580. ',
  581. '<?php
  582. if ($b) {
  583. if (1==1) {
  584. $a = 1;
  585. // test
  586. $b = 2;
  587. }
  588. }
  589. ',
  590. ],
  591. [
  592. '<?php
  593. if ($b) {
  594. if (1==1) {
  595. $a = 1;
  596. /* test */
  597. $b = 2;
  598. echo 123;//
  599. }
  600. }
  601. ',
  602. '<?php
  603. if ($b) {
  604. if (1==1) {
  605. $a = 1;
  606. /* test */
  607. $b = 2;
  608. echo 123;//
  609. }
  610. }
  611. ',
  612. ],
  613. [
  614. '<?php
  615. class A
  616. {
  617. public function B()
  618. {/*
  619. */
  620. $a = 1;
  621. }
  622. }',
  623. '<?php
  624. class A {
  625. public function B()
  626. {/*
  627. */
  628. $a = 1;
  629. }
  630. }',
  631. ],
  632. [
  633. '<?php
  634. class B
  635. {
  636. public function B()
  637. {
  638. /*
  639. *//**/
  640. $a = 1;
  641. }
  642. }',
  643. '<?php
  644. class B {
  645. public function B()
  646. {
  647. /*
  648. *//**/
  649. $a = 1;
  650. }
  651. }',
  652. ],
  653. [
  654. '<?php
  655. class C
  656. {
  657. public function C()
  658. {
  659. /* */#
  660. $a = 1;
  661. }
  662. }',
  663. '<?php
  664. class C {
  665. public function C()
  666. {
  667. /* */#
  668. $a = 1;
  669. }
  670. }',
  671. ],
  672. [
  673. '<?php
  674. if ($a) { /*
  675. */
  676. echo 1;
  677. }',
  678. '<?php
  679. if ($a){ /*
  680. */
  681. echo 1;
  682. }',
  683. ],
  684. [
  685. '<?php
  686. if ($a) { /**/ /*
  687. */
  688. echo 1;
  689. echo 2;
  690. }',
  691. '<?php
  692. if ($a){ /**/ /*
  693. */
  694. echo 1;
  695. echo 2;
  696. }',
  697. ],
  698. [
  699. '<?php
  700. foreach ($foo as $bar) {
  701. if (true) {
  702. }
  703. // comment
  704. elseif (false) {
  705. }
  706. }',
  707. ],
  708. [
  709. '<?php
  710. function foo()
  711. {
  712. $bar = 1; // multiline ...
  713. // ... comment
  714. $baz = 2; // next comment
  715. }',
  716. ],
  717. [
  718. '<?php
  719. function foo()
  720. {
  721. $foo = 1;
  722. // multiline...
  723. // ... comment
  724. return $foo;
  725. }',
  726. '<?php
  727. function foo()
  728. {
  729. $foo = 1;
  730. // multiline...
  731. // ... comment
  732. return $foo;
  733. }',
  734. ],
  735. [
  736. '<?php
  737. function foo()
  738. {
  739. $bar = 1; /* bar */ // multiline ...
  740. // ... comment
  741. $baz = 2; /* baz */ // next comment
  742. }',
  743. ],
  744. [
  745. '<?php
  746. class Foo
  747. {
  748. public function bar()
  749. {
  750. foreach (new Bar() as $file) {
  751. foo();
  752. }
  753. }
  754. }',
  755. '<?php
  756. class Foo {
  757. public function bar() {
  758. foreach (new Bar() as $file)
  759. {
  760. foo();
  761. }
  762. }
  763. }',
  764. ],
  765. [
  766. '<?php if ($condition) { ?>
  767. echo 1;
  768. <?php } else { ?>
  769. echo 2;
  770. <?php } ?>
  771. ',
  772. ],
  773. ];
  774. }
  775. /**
  776. * @dataProvider provideFixMissingBracesAndIndentCases
  777. */
  778. public function testFixMissingBracesAndIndent(string $expected, ?string $input = null, array $configuration = []): void
  779. {
  780. $this->fixer->configure($configuration);
  781. $this->doTest($expected, $input);
  782. }
  783. public function provideFixMissingBracesAndIndentCases()
  784. {
  785. return [
  786. [
  787. '<?php
  788. if (true):
  789. $foo = 0;
  790. endif;',
  791. ],
  792. [
  793. '<?php
  794. if (true) :
  795. $foo = 0;
  796. endif;',
  797. ],
  798. [
  799. '<?php
  800. if (true) : $foo = 1; endif;',
  801. ],
  802. [
  803. '<?php
  804. if (true) {
  805. $foo = 1;
  806. }',
  807. '<?php
  808. if (true)$foo = 1;',
  809. ],
  810. [
  811. '<?php
  812. if (true) {
  813. $foo = 2;
  814. }',
  815. '<?php
  816. if (true) $foo = 2;',
  817. ],
  818. [
  819. '<?php
  820. if (true) {
  821. $foo = 3;
  822. }',
  823. '<?php
  824. if (true){$foo = 3;}',
  825. ],
  826. [
  827. '<?php
  828. if (true) {
  829. echo 1;
  830. } else {
  831. echo 2;
  832. }',
  833. '<?php
  834. if(true) { echo 1; } else echo 2;',
  835. ],
  836. [
  837. '<?php
  838. if (true) {
  839. echo 3;
  840. } else {
  841. echo 4;
  842. }',
  843. '<?php
  844. if(true) echo 3; else { echo 4; }',
  845. ],
  846. [
  847. '<?php
  848. if (true) {
  849. echo 5;
  850. } else {
  851. echo 6;
  852. }',
  853. '<?php
  854. if (true) echo 5; else echo 6;',
  855. ],
  856. [
  857. '<?php
  858. if (true) {
  859. while (true) {
  860. $foo = 1;
  861. $bar = 2;
  862. }
  863. }',
  864. '<?php
  865. if (true) while (true) { $foo = 1; $bar = 2;}',
  866. ],
  867. [
  868. '<?php
  869. if (true) {
  870. if (true) {
  871. echo 1;
  872. } else {
  873. echo 2;
  874. }
  875. } else {
  876. echo 3;
  877. }',
  878. '<?php
  879. if (true) if (true) echo 1; else echo 2; else echo 3;',
  880. ],
  881. [
  882. '<?php
  883. if (true) {
  884. // sth here...
  885. if ($a && ($b || $c)) {
  886. $d = 1;
  887. }
  888. }',
  889. '<?php
  890. if (true) {
  891. // sth here...
  892. if ($a && ($b || $c)) $d = 1;
  893. }',
  894. ],
  895. [
  896. '<?php
  897. for ($i = 1; $i < 10; ++$i) {
  898. echo $i;
  899. }
  900. for ($i = 1; $i < 10; ++$i) {
  901. echo $i;
  902. }',
  903. '<?php
  904. for ($i = 1; $i < 10; ++$i) echo $i;
  905. for ($i = 1; $i < 10; ++$i) { echo $i; }',
  906. ],
  907. [
  908. '<?php
  909. for ($i = 1; $i < 5; ++$i) {
  910. for ($i = 1; $i < 10; ++$i) {
  911. echo $i;
  912. }
  913. }',
  914. '<?php
  915. for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
  916. ],
  917. [
  918. '<?php
  919. do {
  920. echo 1;
  921. } while (false);',
  922. '<?php
  923. do { echo 1; } while (false);',
  924. ],
  925. [
  926. '<?php
  927. while ($foo->next());',
  928. ],
  929. [
  930. '<?php
  931. foreach ($foo as $bar) {
  932. echo $bar;
  933. }',
  934. '<?php
  935. foreach ($foo as $bar) echo $bar;',
  936. ],
  937. [
  938. '<?php
  939. if (true) {
  940. $a = 1;
  941. }',
  942. '<?php
  943. if (true) {$a = 1;}',
  944. ],
  945. [
  946. '<?php
  947. if (true) {
  948. $a = 1;
  949. }',
  950. '<?php
  951. if (true) {
  952. $a = 1;
  953. }',
  954. ],
  955. [
  956. '<?php
  957. if (true) {
  958. $a = 1;
  959. $b = 2;
  960. while (true) {
  961. $c = 3;
  962. }
  963. $d = 4;
  964. }',
  965. '<?php
  966. if (true) {
  967. $a = 1;
  968. $b = 2;
  969. while (true) {
  970. $c = 3;
  971. }
  972. $d = 4;
  973. }',
  974. ],
  975. [
  976. '<?php
  977. if (true) {
  978. $a = 1;
  979. $b = 2;
  980. }',
  981. ],
  982. [
  983. '<?php
  984. if (1) {
  985. $a = 1;
  986. // comment at end
  987. }',
  988. ],
  989. [
  990. '<?php
  991. if (1) {
  992. if (2) {
  993. $a = "a";
  994. } elseif (3) {
  995. $b = "b";
  996. // comment
  997. } else {
  998. $c = "c";
  999. }
  1000. $d = "d";
  1001. }',
  1002. ],
  1003. [
  1004. '<?php
  1005. foreach ($numbers as $num) {
  1006. for ($i = 0; $i < $num; ++$i) {
  1007. $a = "a";
  1008. }
  1009. $b = "b";
  1010. }',
  1011. ],
  1012. [
  1013. '<?php
  1014. if (1) {
  1015. if (2) {
  1016. $foo = 2;
  1017. if (3) {
  1018. $foo = 3;
  1019. }
  1020. }
  1021. }',
  1022. ],
  1023. [
  1024. '<?php
  1025. declare(ticks = 1) {
  1026. $ticks = 1;
  1027. }',
  1028. '<?php
  1029. declare (
  1030. ticks = 1 ) {
  1031. $ticks = 1;
  1032. }',
  1033. ],
  1034. [
  1035. '<?php
  1036. if (true) {
  1037. foo();
  1038. } elseif (true) {
  1039. bar();
  1040. }',
  1041. '<?php
  1042. if (true)
  1043. {
  1044. foo();
  1045. } elseif (true)
  1046. {
  1047. bar();
  1048. }',
  1049. ],
  1050. [
  1051. '<?php
  1052. while (true) {
  1053. foo();
  1054. }',
  1055. '<?php
  1056. while (true)
  1057. {
  1058. foo();
  1059. }',
  1060. ],
  1061. [
  1062. '<?php
  1063. do {
  1064. echo $test;
  1065. } while ($test = $this->getTest());',
  1066. '<?php
  1067. do
  1068. {
  1069. echo $test;
  1070. }
  1071. while ($test = $this->getTest());',
  1072. ],
  1073. [
  1074. '<?php
  1075. do {
  1076. echo $test;
  1077. } while ($test = $this->getTest());',
  1078. '<?php
  1079. do
  1080. {
  1081. echo $test;
  1082. }while ($test = $this->getTest());',
  1083. ],
  1084. [
  1085. '<?php
  1086. class ClassName
  1087. {
  1088. /**
  1089. * comment
  1090. */
  1091. public $foo = null;
  1092. }',
  1093. '<?php
  1094. class ClassName
  1095. {
  1096. /**
  1097. * comment
  1098. */
  1099. public $foo = null;
  1100. }',
  1101. ],
  1102. [
  1103. '<?php
  1104. while ($true) {
  1105. try {
  1106. throw new \Exception();
  1107. } catch (\Exception $e) {
  1108. // do nothing
  1109. }
  1110. }',
  1111. ],
  1112. [
  1113. '<?php
  1114. interface Foo
  1115. {
  1116. public function setConfig(ConfigInterface $config);
  1117. }',
  1118. ],
  1119. [
  1120. '<?php
  1121. function bar()
  1122. {
  1123. $a = 1; //comment
  1124. }',
  1125. ],
  1126. [
  1127. '<?php
  1128. function & lambda()
  1129. {
  1130. return function () {
  1131. };
  1132. }',
  1133. ],
  1134. [
  1135. '<?php
  1136. function nested()
  1137. {
  1138. $a = "a{$b->c()}d";
  1139. }',
  1140. ],
  1141. [
  1142. '<?php
  1143. function foo()
  1144. {
  1145. $a = $b->{$c->d}($e);
  1146. $f->{$g} = $h;
  1147. $i->{$j}[$k] = $l;
  1148. $m = $n->{$o};
  1149. $p = array($q->{$r}, $s->{$t});
  1150. $u->{$v}->w = 1;
  1151. }',
  1152. ],
  1153. [
  1154. '<?php
  1155. function mixed()
  1156. {
  1157. $a = $b->{"a{$c}d"}();
  1158. }',
  1159. ],
  1160. [
  1161. '<?php
  1162. function mixedComplex()
  1163. {
  1164. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  1165. }',
  1166. ],
  1167. [
  1168. '<?php
  1169. function mixedComplex()
  1170. {
  1171. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  1172. }',
  1173. ],
  1174. [
  1175. '<?php
  1176. if (true):
  1177. echo 1;
  1178. else:
  1179. echo 2;
  1180. endif;
  1181. ',
  1182. ],
  1183. [
  1184. '<?php
  1185. if ($test) { //foo
  1186. echo 1;
  1187. }',
  1188. ],
  1189. [
  1190. '<?php
  1191. if (true) {
  1192. // foo
  1193. // bar
  1194. if (true) {
  1195. print("foo");
  1196. print("bar");
  1197. }
  1198. }',
  1199. '<?php
  1200. if (true)
  1201. // foo
  1202. // bar
  1203. {
  1204. if (true)
  1205. {
  1206. print("foo");
  1207. print("bar");
  1208. }
  1209. }',
  1210. ],
  1211. [
  1212. '<?php
  1213. if (true) {
  1214. // foo
  1215. /* bar */
  1216. if (true) {
  1217. print("foo");
  1218. print("bar");
  1219. }
  1220. }',
  1221. '<?php
  1222. if (true)
  1223. // foo
  1224. /* bar */{
  1225. if (true)
  1226. {
  1227. print("foo");
  1228. print("bar");
  1229. }
  1230. }',
  1231. ],
  1232. [
  1233. '<?php if (true) {
  1234. echo "s";
  1235. } ?>x',
  1236. '<?php if (true) echo "s" ?>x',
  1237. ],
  1238. [
  1239. '<?php
  1240. class Foo
  1241. {
  1242. public function getFaxNumbers()
  1243. {
  1244. if (1) {
  1245. return $this->phoneNumbers->filter(function ($phone) {
  1246. $a = 1;
  1247. $b = 1;
  1248. $c = 1;
  1249. return ($phone->getType() === 1) ? true : false;
  1250. });
  1251. }
  1252. }
  1253. }',
  1254. '<?php
  1255. class Foo
  1256. {
  1257. public function getFaxNumbers()
  1258. {
  1259. if (1)
  1260. return $this->phoneNumbers->filter(function ($phone) {
  1261. $a = 1;
  1262. $b = 1;
  1263. $c = 1;
  1264. return ($phone->getType() === 1) ? true : false;
  1265. });
  1266. }
  1267. }',
  1268. ],
  1269. [
  1270. '<?php
  1271. if (true) {
  1272. if (true) {
  1273. echo 1;
  1274. } elseif (true) {
  1275. echo 2;
  1276. } else {
  1277. echo 3;
  1278. }
  1279. }
  1280. ',
  1281. '<?php
  1282. if(true)
  1283. if(true)
  1284. echo 1;
  1285. elseif(true)
  1286. echo 2;
  1287. else
  1288. echo 3;
  1289. ',
  1290. ],
  1291. [
  1292. '<?php
  1293. if (true) {
  1294. if (true) {
  1295. echo 1;
  1296. } elseif (true) {
  1297. echo 2;
  1298. } else {
  1299. echo 3;
  1300. }
  1301. }
  1302. echo 4;
  1303. ',
  1304. '<?php
  1305. if(true)
  1306. if(true)
  1307. echo 1;
  1308. elseif(true)
  1309. echo 2;
  1310. else
  1311. echo 3;
  1312. echo 4;
  1313. ',
  1314. ],
  1315. [
  1316. '<?php
  1317. if (true) {
  1318. if (true) {
  1319. echo 1;
  1320. } elseif (true) {
  1321. echo 2;
  1322. } else {
  1323. echo 3;
  1324. }
  1325. }',
  1326. '<?php
  1327. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  1328. ],
  1329. [
  1330. '<?php
  1331. if (true) {
  1332. if (true) {
  1333. echo 1;
  1334. } else {
  1335. echo 2;
  1336. }
  1337. } else {
  1338. echo 3;
  1339. }',
  1340. '<?php
  1341. if(true) if(true) echo 1; else echo 2; else echo 3;',
  1342. ],
  1343. [
  1344. '<?php
  1345. foreach ($data as $val) {
  1346. // test val
  1347. if ($val === "errors") {
  1348. echo "!";
  1349. }
  1350. }',
  1351. '<?php
  1352. foreach ($data as $val)
  1353. // test val
  1354. if ($val === "errors") {
  1355. echo "!";
  1356. }',
  1357. ],
  1358. [
  1359. '<?php
  1360. if (1) {
  1361. foreach ($data as $val) {
  1362. // test val
  1363. if ($val === "errors") {
  1364. echo "!";
  1365. }
  1366. }
  1367. }',
  1368. '<?php
  1369. if (1)
  1370. foreach ($data as $val)
  1371. // test val
  1372. if ($val === "errors") {
  1373. echo "!";
  1374. }',
  1375. ],
  1376. [
  1377. '<?php
  1378. class Foo
  1379. {
  1380. public function main()
  1381. {
  1382. echo "Hello";
  1383. }
  1384. }',
  1385. '<?php
  1386. class Foo
  1387. {
  1388. public function main()
  1389. {
  1390. echo "Hello";
  1391. }
  1392. }',
  1393. ],
  1394. [
  1395. '<?php
  1396. class Foo
  1397. {
  1398. public function main()
  1399. {
  1400. echo "Hello";
  1401. }
  1402. }',
  1403. '<?php
  1404. class Foo
  1405. {
  1406. public function main()
  1407. {
  1408. echo "Hello";
  1409. }
  1410. }',
  1411. ],
  1412. [
  1413. '<?php
  1414. class Foo
  1415. {
  1416. public $bar;
  1417. public $baz;
  1418. }',
  1419. '<?php
  1420. class Foo
  1421. {
  1422. public $bar;
  1423. public $baz;
  1424. }',
  1425. ],
  1426. [
  1427. '<?php
  1428. function myFunction($foo, $bar)
  1429. {
  1430. return \Foo::{$foo}($bar);
  1431. }',
  1432. ],
  1433. [
  1434. '<?php
  1435. class C
  1436. {
  1437. public function __construct(
  1438. )
  1439. //comment
  1440. {
  1441. }
  1442. }',
  1443. '<?php
  1444. class C {
  1445. public function __construct(
  1446. )
  1447. //comment
  1448. {}
  1449. }',
  1450. ],
  1451. [
  1452. '<?php
  1453. if (true):
  1454. $foo = 0;
  1455. endif;',
  1456. null,
  1457. self::$configurationOopPositionSameLine,
  1458. ],
  1459. [
  1460. '<?php
  1461. if (true) :
  1462. $foo = 0;
  1463. endif;',
  1464. null,
  1465. self::$configurationOopPositionSameLine,
  1466. ],
  1467. [
  1468. '<?php
  1469. if (true) : $foo = 1; endif;',
  1470. null,
  1471. self::$configurationOopPositionSameLine,
  1472. ],
  1473. [
  1474. '<?php
  1475. if (true) {
  1476. $foo = 1;
  1477. }',
  1478. '<?php
  1479. if (true)$foo = 1;',
  1480. self::$configurationOopPositionSameLine,
  1481. ],
  1482. [
  1483. '<?php
  1484. if (true) {
  1485. $foo = 2;
  1486. }',
  1487. '<?php
  1488. if (true) $foo = 2;',
  1489. self::$configurationOopPositionSameLine,
  1490. ],
  1491. [
  1492. '<?php
  1493. if (true) {
  1494. $foo = 3;
  1495. }',
  1496. '<?php
  1497. if (true){$foo = 3;}',
  1498. self::$configurationOopPositionSameLine,
  1499. ],
  1500. [
  1501. '<?php
  1502. if (true) {
  1503. echo 1;
  1504. } else {
  1505. echo 2;
  1506. }',
  1507. '<?php
  1508. if(true) { echo 1; } else echo 2;',
  1509. self::$configurationOopPositionSameLine,
  1510. ],
  1511. [
  1512. '<?php
  1513. if (true) {
  1514. echo 3;
  1515. } else {
  1516. echo 4;
  1517. }',
  1518. '<?php
  1519. if(true) echo 3; else { echo 4; }',
  1520. self::$configurationOopPositionSameLine,
  1521. ],
  1522. [
  1523. '<?php
  1524. if (true) {
  1525. echo 5;
  1526. } else {
  1527. echo 6;
  1528. }',
  1529. '<?php
  1530. if (true) echo 5; else echo 6;',
  1531. self::$configurationOopPositionSameLine,
  1532. ],
  1533. [
  1534. '<?php
  1535. if (true) {
  1536. while (true) {
  1537. $foo = 1;
  1538. $bar = 2;
  1539. }
  1540. }',
  1541. '<?php
  1542. if (true) while (true) { $foo = 1; $bar = 2;}',
  1543. self::$configurationOopPositionSameLine,
  1544. ],
  1545. [
  1546. '<?php
  1547. if (true) {
  1548. if (true) {
  1549. echo 1;
  1550. } else {
  1551. echo 2;
  1552. }
  1553. } else {
  1554. echo 3;
  1555. }',
  1556. '<?php
  1557. if (true) if (true) echo 1; else echo 2; else echo 3;',
  1558. self::$configurationOopPositionSameLine,
  1559. ],
  1560. [
  1561. '<?php
  1562. if (true) {
  1563. // sth here...
  1564. if ($a && ($b || $c)) {
  1565. $d = 1;
  1566. }
  1567. }',
  1568. '<?php
  1569. if (true) {
  1570. // sth here...
  1571. if ($a && ($b || $c)) $d = 1;
  1572. }',
  1573. self::$configurationOopPositionSameLine,
  1574. ],
  1575. [
  1576. '<?php
  1577. for ($i = 1; $i < 10; ++$i) {
  1578. echo $i;
  1579. }
  1580. for ($i = 1; $i < 10; ++$i) {
  1581. echo $i;
  1582. }',
  1583. '<?php
  1584. for ($i = 1; $i < 10; ++$i) echo $i;
  1585. for ($i = 1; $i < 10; ++$i) { echo $i; }',
  1586. self::$configurationOopPositionSameLine,
  1587. ],
  1588. [
  1589. '<?php
  1590. for ($i = 1; $i < 5; ++$i) {
  1591. for ($i = 1; $i < 10; ++$i) {
  1592. echo $i;
  1593. }
  1594. }',
  1595. '<?php
  1596. for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
  1597. self::$configurationOopPositionSameLine,
  1598. ],
  1599. [
  1600. '<?php
  1601. do {
  1602. echo 1;
  1603. } while (false);',
  1604. '<?php
  1605. do { echo 1; } while (false);',
  1606. self::$configurationOopPositionSameLine,
  1607. ],
  1608. [
  1609. '<?php
  1610. while ($foo->next());',
  1611. null,
  1612. self::$configurationOopPositionSameLine,
  1613. ],
  1614. [
  1615. '<?php
  1616. foreach ($foo as $bar) {
  1617. echo $bar;
  1618. }',
  1619. '<?php
  1620. foreach ($foo as $bar) echo $bar;',
  1621. self::$configurationOopPositionSameLine,
  1622. ],
  1623. [
  1624. '<?php
  1625. if (true) {
  1626. $a = 1;
  1627. }',
  1628. '<?php
  1629. if (true) {$a = 1;}',
  1630. self::$configurationOopPositionSameLine,
  1631. ],
  1632. [
  1633. '<?php
  1634. if (true) {
  1635. $a = 1;
  1636. }',
  1637. '<?php
  1638. if (true) {
  1639. $a = 1;
  1640. }',
  1641. self::$configurationOopPositionSameLine,
  1642. ],
  1643. [
  1644. '<?php
  1645. if (true) {
  1646. $a = 1;
  1647. $b = 2;
  1648. while (true) {
  1649. $c = 3;
  1650. }
  1651. $d = 4;
  1652. }',
  1653. '<?php
  1654. if (true) {
  1655. $a = 1;
  1656. $b = 2;
  1657. while (true) {
  1658. $c = 3;
  1659. }
  1660. $d = 4;
  1661. }',
  1662. self::$configurationOopPositionSameLine,
  1663. ],
  1664. [
  1665. '<?php
  1666. if (true) {
  1667. $a = 1;
  1668. $b = 2;
  1669. }',
  1670. null,
  1671. self::$configurationOopPositionSameLine,
  1672. ],
  1673. [
  1674. '<?php
  1675. if (1) {
  1676. $a = 1;
  1677. // comment at end
  1678. }',
  1679. null,
  1680. self::$configurationOopPositionSameLine,
  1681. ],
  1682. [
  1683. '<?php
  1684. if (1) {
  1685. if (2) {
  1686. $a = "a";
  1687. } elseif (3) {
  1688. $b = "b";
  1689. // comment
  1690. } else {
  1691. $c = "c";
  1692. }
  1693. $d = "d";
  1694. }',
  1695. null,
  1696. self::$configurationOopPositionSameLine,
  1697. ],
  1698. [
  1699. '<?php
  1700. foreach ($numbers as $num) {
  1701. for ($i = 0; $i < $num; ++$i) {
  1702. $a = "a";
  1703. }
  1704. $b = "b";
  1705. }',
  1706. null,
  1707. self::$configurationOopPositionSameLine,
  1708. ],
  1709. [
  1710. '<?php
  1711. if (1) {
  1712. if (2) {
  1713. $foo = 2;
  1714. if (3) {
  1715. $foo = 3;
  1716. }
  1717. }
  1718. }',
  1719. null,
  1720. self::$configurationOopPositionSameLine,
  1721. ],
  1722. [
  1723. '<?php
  1724. declare(ticks = 1) {
  1725. $ticks = 1;
  1726. }',
  1727. '<?php
  1728. declare (
  1729. ticks = 1 ) {
  1730. $ticks = 1;
  1731. }',
  1732. self::$configurationOopPositionSameLine,
  1733. ],
  1734. [
  1735. '<?php
  1736. if (true) {
  1737. foo();
  1738. } elseif (true) {
  1739. bar();
  1740. }',
  1741. '<?php
  1742. if (true)
  1743. {
  1744. foo();
  1745. } elseif (true)
  1746. {
  1747. bar();
  1748. }',
  1749. self::$configurationOopPositionSameLine,
  1750. ],
  1751. [
  1752. '<?php
  1753. while (true) {
  1754. foo();
  1755. }',
  1756. '<?php
  1757. while (true)
  1758. {
  1759. foo();
  1760. }',
  1761. self::$configurationOopPositionSameLine,
  1762. ],
  1763. [
  1764. '<?php
  1765. do {
  1766. echo $test;
  1767. } while ($test = $this->getTest());',
  1768. '<?php
  1769. do
  1770. {
  1771. echo $test;
  1772. }
  1773. while ($test = $this->getTest());',
  1774. self::$configurationOopPositionSameLine,
  1775. ],
  1776. [
  1777. '<?php
  1778. do {
  1779. echo $test;
  1780. } while ($test = $this->getTest());',
  1781. '<?php
  1782. do
  1783. {
  1784. echo $test;
  1785. }while ($test = $this->getTest());',
  1786. self::$configurationOopPositionSameLine,
  1787. ],
  1788. [
  1789. '<?php
  1790. class ClassName {
  1791. /**
  1792. * comment
  1793. */
  1794. public $foo = null;
  1795. }',
  1796. '<?php
  1797. class ClassName
  1798. {
  1799. /**
  1800. * comment
  1801. */
  1802. public $foo = null;
  1803. }',
  1804. self::$configurationOopPositionSameLine,
  1805. ],
  1806. [
  1807. '<?php
  1808. class ClassName {
  1809. /**
  1810. * comment
  1811. */
  1812. public $foo = null;
  1813. }',
  1814. '<?php
  1815. class ClassName
  1816. {
  1817. /**
  1818. * comment
  1819. */
  1820. public $foo = null;
  1821. }',
  1822. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  1823. ],
  1824. [
  1825. '<?php
  1826. while ($true) {
  1827. try {
  1828. throw new \Exception();
  1829. } catch (\Exception $e) {
  1830. // do nothing
  1831. }
  1832. }',
  1833. null,
  1834. self::$configurationOopPositionSameLine,
  1835. ],
  1836. [
  1837. '<?php
  1838. while ($true)
  1839. {
  1840. try
  1841. {
  1842. throw new \Exception();
  1843. }
  1844. catch (\Exception $e)
  1845. {
  1846. // do nothing
  1847. }
  1848. }',
  1849. '<?php
  1850. while ($true) {
  1851. try {
  1852. throw new \Exception();
  1853. } catch (\Exception $e) {
  1854. // do nothing
  1855. }
  1856. }',
  1857. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  1858. ],
  1859. [
  1860. '<?php
  1861. interface Foo {
  1862. public function setConfig(ConfigInterface $config);
  1863. }',
  1864. '<?php
  1865. interface Foo
  1866. {
  1867. public function setConfig(ConfigInterface $config);
  1868. }',
  1869. self::$configurationOopPositionSameLine,
  1870. ],
  1871. [
  1872. '<?php
  1873. interface Foo {
  1874. public function setConfig(ConfigInterface $config);
  1875. }',
  1876. '<?php
  1877. interface Foo
  1878. {
  1879. public function setConfig(ConfigInterface $config);
  1880. }',
  1881. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  1882. ],
  1883. [
  1884. '<?php
  1885. function bar() {
  1886. $a = 1; //comment
  1887. }',
  1888. '<?php
  1889. function bar()
  1890. {
  1891. $a = 1; //comment
  1892. }',
  1893. self::$configurationOopPositionSameLine,
  1894. ],
  1895. [
  1896. '<?php
  1897. function & lambda() {
  1898. return function () {
  1899. };
  1900. }',
  1901. '<?php
  1902. function & lambda()
  1903. {
  1904. return function () {
  1905. };
  1906. }',
  1907. self::$configurationOopPositionSameLine,
  1908. ],
  1909. [
  1910. '<?php
  1911. function & lambda() {
  1912. return function ()
  1913. {
  1914. };
  1915. }',
  1916. '<?php
  1917. function & lambda()
  1918. {
  1919. return function () {
  1920. };
  1921. }',
  1922. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  1923. ],
  1924. [
  1925. '<?php
  1926. function & lambda() {
  1927. return function () {
  1928. };
  1929. }',
  1930. '<?php
  1931. function & lambda()
  1932. {
  1933. return function () {
  1934. };
  1935. }',
  1936. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  1937. ],
  1938. [
  1939. '<?php
  1940. function nested() {
  1941. $a = "a{$b->c()}d";
  1942. }',
  1943. '<?php
  1944. function nested()
  1945. {
  1946. $a = "a{$b->c()}d";
  1947. }',
  1948. self::$configurationOopPositionSameLine,
  1949. ],
  1950. [
  1951. '<?php
  1952. function nested() {
  1953. $a = "a{$b->c()}d";
  1954. }',
  1955. '<?php
  1956. function nested()
  1957. {
  1958. $a = "a{$b->c()}d";
  1959. }',
  1960. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  1961. ],
  1962. [
  1963. '<?php
  1964. function foo() {
  1965. $a = $b->{$c->d}($e);
  1966. $f->{$g} = $h;
  1967. $i->{$j}[$k] = $l;
  1968. $m = $n->{$o};
  1969. $p = array($q->{$r}, $s->{$t});
  1970. $u->{$v}->w = 1;
  1971. }',
  1972. '<?php
  1973. function foo()
  1974. {
  1975. $a = $b->{$c->d}($e);
  1976. $f->{$g} = $h;
  1977. $i->{$j}[$k] = $l;
  1978. $m = $n->{$o};
  1979. $p = array($q->{$r}, $s->{$t});
  1980. $u->{$v}->w = 1;
  1981. }',
  1982. self::$configurationOopPositionSameLine,
  1983. ],
  1984. [
  1985. '<?php
  1986. function foo() {
  1987. $a = $b->{$c->d}($e);
  1988. $f->{$g} = $h;
  1989. $i->{$j}[$k] = $l;
  1990. $m = $n->{$o};
  1991. $p = array($q->{$r}, $s->{$t});
  1992. $u->{$v}->w = 1;
  1993. }',
  1994. '<?php
  1995. function foo()
  1996. {
  1997. $a = $b->{$c->d}($e);
  1998. $f->{$g} = $h;
  1999. $i->{$j}[$k] = $l;
  2000. $m = $n->{$o};
  2001. $p = array($q->{$r}, $s->{$t});
  2002. $u->{$v}->w = 1;
  2003. }',
  2004. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  2005. ],
  2006. [
  2007. '<?php
  2008. function mixed() {
  2009. $a = $b->{"a{$c}d"}();
  2010. }',
  2011. '<?php
  2012. function mixed()
  2013. {
  2014. $a = $b->{"a{$c}d"}();
  2015. }',
  2016. self::$configurationOopPositionSameLine,
  2017. ],
  2018. [
  2019. '<?php
  2020. function mixedComplex() {
  2021. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  2022. }',
  2023. '<?php
  2024. function mixedComplex()
  2025. {
  2026. $a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
  2027. }',
  2028. self::$configurationOopPositionSameLine,
  2029. ],
  2030. [
  2031. '<?php
  2032. function mixedComplex() {
  2033. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  2034. }',
  2035. '<?php
  2036. function mixedComplex()
  2037. {
  2038. $a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
  2039. }',
  2040. self::$configurationOopPositionSameLine,
  2041. ],
  2042. [
  2043. '<?php
  2044. if (true):
  2045. echo 1;
  2046. else:
  2047. echo 2;
  2048. endif;
  2049. ',
  2050. null,
  2051. self::$configurationOopPositionSameLine,
  2052. ],
  2053. [
  2054. '<?php
  2055. if (true):
  2056. echo 1;
  2057. else:
  2058. echo 2;
  2059. endif;
  2060. ',
  2061. null,
  2062. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  2063. ],
  2064. [
  2065. '<?php
  2066. if ($test) { //foo
  2067. echo 1;
  2068. }',
  2069. null,
  2070. self::$configurationOopPositionSameLine,
  2071. ],
  2072. [
  2073. '<?php
  2074. if (true) {
  2075. // foo
  2076. // bar
  2077. if (true) {
  2078. print("foo");
  2079. print("bar");
  2080. }
  2081. }',
  2082. '<?php
  2083. if (true)
  2084. // foo
  2085. // bar
  2086. {
  2087. if (true)
  2088. {
  2089. print("foo");
  2090. print("bar");
  2091. }
  2092. }',
  2093. self::$configurationOopPositionSameLine,
  2094. ],
  2095. [
  2096. '<?php
  2097. if (true)
  2098. {
  2099. // foo
  2100. // bar
  2101. if (true)
  2102. {
  2103. print("foo");
  2104. print("bar");
  2105. }
  2106. }',
  2107. '<?php
  2108. if (true)
  2109. // foo
  2110. // bar
  2111. {
  2112. if (true)
  2113. {
  2114. print("foo");
  2115. print("bar");
  2116. }
  2117. }',
  2118. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  2119. ],
  2120. [
  2121. '<?php
  2122. if (true) {
  2123. // foo
  2124. /* bar */
  2125. if (true) {
  2126. print("foo");
  2127. print("bar");
  2128. }
  2129. }',
  2130. '<?php
  2131. if (true)
  2132. // foo
  2133. /* bar */{
  2134. if (true)
  2135. {
  2136. print("foo");
  2137. print("bar");
  2138. }
  2139. }',
  2140. self::$configurationOopPositionSameLine,
  2141. ],
  2142. [
  2143. '<?php if (true) {
  2144. echo "s";
  2145. } ?>x',
  2146. '<?php if (true) echo "s" ?>x',
  2147. self::$configurationOopPositionSameLine,
  2148. ],
  2149. [
  2150. '<?php
  2151. class Foo {
  2152. public function getFaxNumbers() {
  2153. if (1) {
  2154. return $this->phoneNumbers->filter(function ($phone) {
  2155. $a = 1;
  2156. $b = 1;
  2157. $c = 1;
  2158. return ($phone->getType() === 1) ? true : false;
  2159. });
  2160. }
  2161. }
  2162. }',
  2163. '<?php
  2164. class Foo
  2165. {
  2166. public function getFaxNumbers()
  2167. {
  2168. if (1)
  2169. return $this->phoneNumbers->filter(function ($phone) {
  2170. $a = 1;
  2171. $b = 1;
  2172. $c = 1;
  2173. return ($phone->getType() === 1) ? true : false;
  2174. });
  2175. }
  2176. }',
  2177. self::$configurationOopPositionSameLine,
  2178. ],
  2179. [
  2180. '<?php
  2181. class Foo {
  2182. public function getFaxNumbers() {
  2183. if (1)
  2184. {
  2185. return $this->phoneNumbers->filter(function ($phone) {
  2186. $a = 1;
  2187. $b = 1;
  2188. $c = 1;
  2189. return ($phone->getType() === 1) ? true : false;
  2190. });
  2191. }
  2192. }
  2193. }',
  2194. '<?php
  2195. class Foo
  2196. {
  2197. public function getFaxNumbers()
  2198. {
  2199. if (1)
  2200. return $this->phoneNumbers->filter(function ($phone) {
  2201. $a = 1;
  2202. $b = 1;
  2203. $c = 1;
  2204. return ($phone->getType() === 1) ? true : false;
  2205. });
  2206. }
  2207. }',
  2208. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  2209. ],
  2210. [
  2211. '<?php
  2212. class Foo {
  2213. public function getFaxNumbers() {
  2214. if (1)
  2215. {
  2216. return $this->phoneNumbers->filter(function ($phone)
  2217. {
  2218. $a = 1;
  2219. $b = 1;
  2220. $c = 1;
  2221. return ($phone->getType() === 1) ? true : false;
  2222. });
  2223. }
  2224. }
  2225. }',
  2226. '<?php
  2227. class Foo
  2228. {
  2229. public function getFaxNumbers()
  2230. {
  2231. if (1)
  2232. return $this->phoneNumbers->filter(function ($phone) {
  2233. $a = 1;
  2234. $b = 1;
  2235. $c = 1;
  2236. return ($phone->getType() === 1) ? true : false;
  2237. });
  2238. }
  2239. }',
  2240. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine + self::$configurationAnonymousPositionNextLine,
  2241. ],
  2242. [
  2243. '<?php
  2244. if (true) {
  2245. if (true) {
  2246. echo 1;
  2247. } elseif (true) {
  2248. echo 2;
  2249. } else {
  2250. echo 3;
  2251. }
  2252. }
  2253. ',
  2254. '<?php
  2255. if(true)
  2256. if(true)
  2257. echo 1;
  2258. elseif(true)
  2259. echo 2;
  2260. else
  2261. echo 3;
  2262. ',
  2263. self::$configurationOopPositionSameLine,
  2264. ],
  2265. [
  2266. '<?php
  2267. if (true) {
  2268. if (true) {
  2269. echo 1;
  2270. } elseif (true) {
  2271. echo 2;
  2272. } else {
  2273. echo 3;
  2274. }
  2275. }
  2276. echo 4;
  2277. ',
  2278. '<?php
  2279. if(true)
  2280. if(true)
  2281. echo 1;
  2282. elseif(true)
  2283. echo 2;
  2284. else
  2285. echo 3;
  2286. echo 4;
  2287. ',
  2288. self::$configurationOopPositionSameLine,
  2289. ],
  2290. [
  2291. '<?php
  2292. if (true) {
  2293. if (true) {
  2294. echo 1;
  2295. } elseif (true) {
  2296. echo 2;
  2297. } else {
  2298. echo 3;
  2299. }
  2300. }',
  2301. '<?php
  2302. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  2303. self::$configurationOopPositionSameLine,
  2304. ],
  2305. [
  2306. '<?php
  2307. if (true) {
  2308. if (true) {
  2309. echo 1;
  2310. } else {
  2311. echo 2;
  2312. }
  2313. } else {
  2314. echo 3;
  2315. }',
  2316. '<?php
  2317. if(true) if(true) echo 1; else echo 2; else echo 3;',
  2318. self::$configurationOopPositionSameLine,
  2319. ],
  2320. [
  2321. '<?php
  2322. foreach ($data as $val) {
  2323. // test val
  2324. if ($val === "errors") {
  2325. echo "!";
  2326. }
  2327. }',
  2328. '<?php
  2329. foreach ($data as $val)
  2330. // test val
  2331. if ($val === "errors") {
  2332. echo "!";
  2333. }',
  2334. self::$configurationOopPositionSameLine,
  2335. ],
  2336. [
  2337. '<?php
  2338. if (1) {
  2339. foreach ($data as $val) {
  2340. // test val
  2341. if ($val === "errors") {
  2342. echo "!";
  2343. }
  2344. }
  2345. }',
  2346. '<?php
  2347. if (1)
  2348. foreach ($data as $val)
  2349. // test val
  2350. if ($val === "errors") {
  2351. echo "!";
  2352. }',
  2353. self::$configurationOopPositionSameLine,
  2354. ],
  2355. [
  2356. '<?php
  2357. class Foo {
  2358. public function main() {
  2359. echo "Hello";
  2360. }
  2361. }',
  2362. '<?php
  2363. class Foo
  2364. {
  2365. public function main()
  2366. {
  2367. echo "Hello";
  2368. }
  2369. }',
  2370. self::$configurationOopPositionSameLine,
  2371. ],
  2372. [
  2373. '<?php
  2374. class Foo {
  2375. public function main() {
  2376. echo "Hello";
  2377. }
  2378. }',
  2379. '<?php
  2380. class Foo
  2381. {
  2382. public function main()
  2383. {
  2384. echo "Hello";
  2385. }
  2386. }',
  2387. self::$configurationOopPositionSameLine,
  2388. ],
  2389. [
  2390. '<?php
  2391. class Foo {
  2392. public function main() {
  2393. echo "Hello";
  2394. }
  2395. }',
  2396. '<?php
  2397. class Foo
  2398. {
  2399. public function main()
  2400. {
  2401. echo "Hello";
  2402. }
  2403. }',
  2404. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  2405. ],
  2406. [
  2407. '<?php
  2408. class Foo {
  2409. public $bar;
  2410. public $baz;
  2411. }',
  2412. '<?php
  2413. class Foo
  2414. {
  2415. public $bar;
  2416. public $baz;
  2417. }',
  2418. self::$configurationOopPositionSameLine,
  2419. ],
  2420. [
  2421. '<?php
  2422. function myFunction($foo, $bar) {
  2423. return \Foo::{$foo}($bar);
  2424. }',
  2425. '<?php
  2426. function myFunction($foo, $bar)
  2427. {
  2428. return \Foo::{$foo}($bar);
  2429. }',
  2430. self::$configurationOopPositionSameLine,
  2431. ],
  2432. [
  2433. '<?php
  2434. class C {
  2435. public function __construct(
  2436. )
  2437. //comment
  2438. {
  2439. }
  2440. }',
  2441. '<?php
  2442. class C {
  2443. public function __construct(
  2444. )
  2445. //comment
  2446. {}
  2447. }',
  2448. self::$configurationOopPositionSameLine,
  2449. ],
  2450. [
  2451. '<?php
  2452. class Something # a
  2453. {
  2454. public function sth() //
  2455. {
  2456. return function (int $foo) use ($bar) {
  2457. return $bar;
  2458. };
  2459. }
  2460. }
  2461. function C() /**/ // # /**/
  2462. {
  2463. }
  2464. function D() /**
  2465. *
  2466. */
  2467. {
  2468. }',
  2469. '<?php
  2470. class Something # a
  2471. {
  2472. public function sth() //
  2473. {
  2474. return function (int $foo) use ($bar) { return $bar; };
  2475. }
  2476. }
  2477. function C() /**/ // # /**/
  2478. {
  2479. }
  2480. function D() /**
  2481. *
  2482. */
  2483. {
  2484. }',
  2485. self::$configurationOopPositionSameLine,
  2486. ],
  2487. ];
  2488. }
  2489. /**
  2490. * @dataProvider provideFixClassyBracesCases
  2491. */
  2492. public function testFixClassyBraces(string $expected, ?string $input = null, array $configuration = []): void
  2493. {
  2494. $this->fixer->configure($configuration);
  2495. $this->doTest($expected, $input);
  2496. }
  2497. public function provideFixClassyBracesCases()
  2498. {
  2499. return [
  2500. [
  2501. '<?php
  2502. class FooA
  2503. {
  2504. }',
  2505. '<?php
  2506. class FooA {}',
  2507. ],
  2508. [
  2509. '<?php
  2510. class FooB
  2511. {
  2512. }',
  2513. '<?php
  2514. class FooB{}',
  2515. ],
  2516. [
  2517. '<?php
  2518. class FooC
  2519. {
  2520. }',
  2521. '<?php
  2522. class FooC
  2523. {}',
  2524. ],
  2525. [
  2526. '<?php
  2527. interface FooD
  2528. {
  2529. }',
  2530. '<?php
  2531. interface FooD {}',
  2532. ],
  2533. [
  2534. '<?php
  2535. class TestClass extends BaseTestClass implements TestInterface
  2536. {
  2537. private $foo;
  2538. }',
  2539. '<?php
  2540. class TestClass extends BaseTestClass implements TestInterface { private $foo;}',
  2541. ],
  2542. [
  2543. '<?php
  2544. abstract class Foo
  2545. {
  2546. public function getProcess($foo)
  2547. {
  2548. return true;
  2549. }
  2550. }',
  2551. ],
  2552. ['<?php
  2553. function foo()
  2554. {
  2555. return "$c ($d)";
  2556. }',
  2557. ],
  2558. [
  2559. '<?php
  2560. class FooA {
  2561. }',
  2562. '<?php
  2563. class FooA {}',
  2564. self::$configurationOopPositionSameLine,
  2565. ],
  2566. [
  2567. '<?php
  2568. class FooB {
  2569. }',
  2570. '<?php
  2571. class FooB{}',
  2572. self::$configurationOopPositionSameLine,
  2573. ],
  2574. [
  2575. '<?php
  2576. class FooC {
  2577. }',
  2578. '<?php
  2579. class FooC
  2580. {}',
  2581. self::$configurationOopPositionSameLine,
  2582. ],
  2583. [
  2584. '<?php
  2585. interface FooD {
  2586. }',
  2587. '<?php
  2588. interface FooD {}',
  2589. self::$configurationOopPositionSameLine,
  2590. ],
  2591. [
  2592. '<?php
  2593. class TestClass extends BaseTestClass implements TestInterface {
  2594. private $foo;
  2595. }',
  2596. '<?php
  2597. class TestClass extends BaseTestClass implements TestInterface { private $foo;}',
  2598. self::$configurationOopPositionSameLine,
  2599. ],
  2600. [
  2601. '<?php
  2602. abstract class Foo {
  2603. public function getProcess($foo) {
  2604. return true;
  2605. }
  2606. }',
  2607. '<?php
  2608. abstract class Foo
  2609. {
  2610. public function getProcess($foo)
  2611. {
  2612. return true;
  2613. }
  2614. }',
  2615. self::$configurationOopPositionSameLine,
  2616. ],
  2617. [
  2618. '<?php
  2619. function foo() {
  2620. return "$c ($d)";
  2621. }',
  2622. '<?php
  2623. function foo()
  2624. {
  2625. return "$c ($d)";
  2626. }',
  2627. self::$configurationOopPositionSameLine,
  2628. ],
  2629. [
  2630. '<?php
  2631. trait TFoo
  2632. {
  2633. public $a;
  2634. }',
  2635. '<?php
  2636. trait TFoo {public $a;}',
  2637. ],
  2638. [
  2639. '<?php
  2640. trait TFoo {
  2641. public $a;
  2642. }',
  2643. '<?php
  2644. trait TFoo {public $a;}',
  2645. self::$configurationOopPositionSameLine,
  2646. ],
  2647. [
  2648. '<?php
  2649. trait TFoo
  2650. {
  2651. public $a;
  2652. }',
  2653. '<?php
  2654. trait TFoo {public $a;}',
  2655. ],
  2656. [
  2657. '<?php
  2658. trait TFoo {
  2659. public $a;
  2660. }',
  2661. '<?php
  2662. trait TFoo {public $a;}',
  2663. self::$configurationOopPositionSameLine,
  2664. ],
  2665. ];
  2666. }
  2667. /**
  2668. * @dataProvider provideFixAnonFunctionInShortArraySyntaxCases
  2669. */
  2670. public function testFixAnonFunctionInShortArraySyntax(string $expected, ?string $input = null, array $configuration = []): void
  2671. {
  2672. $this->fixer->configure($configuration);
  2673. $this->doTest($expected, $input);
  2674. }
  2675. public function provideFixAnonFunctionInShortArraySyntaxCases()
  2676. {
  2677. return [
  2678. [
  2679. '<?php
  2680. function myFunction()
  2681. {
  2682. return [
  2683. [
  2684. "callback" => function ($data) {
  2685. return true;
  2686. }
  2687. ],
  2688. [
  2689. "callback" => function ($data) {
  2690. return true;
  2691. },
  2692. ],
  2693. ];
  2694. }',
  2695. '<?php
  2696. function myFunction()
  2697. {
  2698. return [
  2699. [
  2700. "callback" => function ($data) {
  2701. return true;
  2702. }
  2703. ],
  2704. [
  2705. "callback" => function ($data) { return true; },
  2706. ],
  2707. ];
  2708. }',
  2709. ],
  2710. [
  2711. '<?php
  2712. function myFunction() {
  2713. return [
  2714. [
  2715. "callback" => function ($data) {
  2716. return true;
  2717. }
  2718. ],
  2719. [
  2720. "callback" => function ($data) {
  2721. return true;
  2722. },
  2723. ],
  2724. ];
  2725. }',
  2726. '<?php
  2727. function myFunction()
  2728. {
  2729. return [
  2730. [
  2731. "callback" => function ($data) {
  2732. return true;
  2733. }
  2734. ],
  2735. [
  2736. "callback" => function ($data) { return true; },
  2737. ],
  2738. ];
  2739. }',
  2740. self::$configurationOopPositionSameLine,
  2741. ],
  2742. [
  2743. '<?php
  2744. function myFunction() {
  2745. return [
  2746. [
  2747. "callback" => function ($data)
  2748. {
  2749. return true;
  2750. }
  2751. ],
  2752. [
  2753. "callback" => function ($data)
  2754. {
  2755. return true;
  2756. },
  2757. ],
  2758. ];
  2759. }',
  2760. '<?php
  2761. function myFunction()
  2762. {
  2763. return [
  2764. [
  2765. "callback" => function ($data) {
  2766. return true;
  2767. }
  2768. ],
  2769. [
  2770. "callback" => function ($data) { return true; },
  2771. ],
  2772. ];
  2773. }',
  2774. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  2775. ],
  2776. ];
  2777. }
  2778. /**
  2779. * @dataProvider provideFixCommentBeforeBraceCases
  2780. */
  2781. public function testFixCommentBeforeBrace(string $expected, ?string $input = null, array $configuration = []): void
  2782. {
  2783. $this->fixer->configure($configuration);
  2784. $this->doTest($expected, $input);
  2785. }
  2786. public function provideFixCommentBeforeBraceCases()
  2787. {
  2788. return [
  2789. [
  2790. '<?php ',
  2791. ],
  2792. [
  2793. '<?php
  2794. if ($test) { // foo
  2795. echo 1;
  2796. }',
  2797. '<?php
  2798. if ($test) // foo
  2799. {
  2800. echo 1;
  2801. }',
  2802. ],
  2803. [
  2804. '<?php
  2805. $foo = function ($x) use ($y) { // foo
  2806. echo 1;
  2807. };',
  2808. '<?php
  2809. $foo = function ($x) use ($y) // foo
  2810. {
  2811. echo 1;
  2812. };',
  2813. ],
  2814. [
  2815. '<?php ',
  2816. null,
  2817. self::$configurationOopPositionSameLine,
  2818. ],
  2819. [
  2820. '<?php
  2821. if ($test) { // foo
  2822. echo 1;
  2823. }',
  2824. '<?php
  2825. if ($test) // foo
  2826. {
  2827. echo 1;
  2828. }',
  2829. self::$configurationOopPositionSameLine,
  2830. ],
  2831. [
  2832. '<?php
  2833. $foo = function ($x) use ($y) { // foo
  2834. echo 1;
  2835. };',
  2836. '<?php
  2837. $foo = function ($x) use ($y) // foo
  2838. {
  2839. echo 1;
  2840. };',
  2841. self::$configurationOopPositionSameLine,
  2842. ],
  2843. [
  2844. '<?php
  2845. // 2.5+ API
  2846. if (isNewApi()) {
  2847. echo "new API";
  2848. // 2.4- API
  2849. } elseif (isOldApi()) {
  2850. echo "old API";
  2851. // 2.4- API
  2852. } else {
  2853. echo "unknown API";
  2854. // sth
  2855. }
  2856. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  2857. return $guesser->guessRequiredForConstraint($constraint);
  2858. // Fallback to false...
  2859. // ... due to sth...
  2860. }, false);
  2861. ',
  2862. ],
  2863. [
  2864. '<?php
  2865. if ($a) { //
  2866. ?><?php ++$a;
  2867. } ?>',
  2868. ],
  2869. [
  2870. '<?php
  2871. if ($a) { /* */ /* */ /* */ /* */ /* */
  2872. ?><?php ++$a;
  2873. } ?>',
  2874. ],
  2875. ];
  2876. }
  2877. /**
  2878. * @dataProvider provideFixCommentBeforeBrace70Cases
  2879. * @requires PHP 7.0
  2880. */
  2881. public function testFixCommentBeforeBrace70(string $expected, ?string $input = null, array $configuration = []): void
  2882. {
  2883. $this->fixer->configure($configuration);
  2884. $this->doTest($expected, $input);
  2885. }
  2886. public function provideFixCommentBeforeBrace70Cases()
  2887. {
  2888. return [
  2889. [
  2890. '<?php
  2891. $foo = new class ($a) extends Foo implements Bar { // foo
  2892. private $x;
  2893. };',
  2894. '<?php
  2895. $foo = new class ($a) extends Foo implements Bar // foo
  2896. {
  2897. private $x;
  2898. };',
  2899. ],
  2900. [
  2901. '<?php
  2902. $foo = new class ($a) extends Foo implements Bar { // foo
  2903. private $x;
  2904. };',
  2905. '<?php
  2906. $foo = new class ($a) extends Foo implements Bar // foo
  2907. {
  2908. private $x;
  2909. };',
  2910. self::$configurationOopPositionSameLine,
  2911. ],
  2912. ];
  2913. }
  2914. /**
  2915. * @dataProvider provideFixWhitespaceBeforeBraceCases
  2916. */
  2917. public function testFixWhitespaceBeforeBrace(string $expected, ?string $input = null, array $configuration = []): void
  2918. {
  2919. $this->fixer->configure($configuration);
  2920. $this->doTest($expected, $input);
  2921. }
  2922. public function provideFixWhitespaceBeforeBraceCases()
  2923. {
  2924. return [
  2925. [
  2926. '<?php
  2927. if (true) {
  2928. echo 1;
  2929. }',
  2930. '<?php
  2931. if (true)
  2932. {
  2933. echo 1;
  2934. }',
  2935. ],
  2936. [
  2937. '<?php
  2938. if (true) {
  2939. echo 1;
  2940. }',
  2941. '<?php
  2942. if (true){
  2943. echo 1;
  2944. }',
  2945. ],
  2946. [
  2947. '<?php
  2948. if (true) {
  2949. echo 1;
  2950. }',
  2951. '<?php
  2952. if (true) {
  2953. echo 1;
  2954. }',
  2955. ],
  2956. [
  2957. '<?php
  2958. while ($file = $this->getFile()) {
  2959. }',
  2960. '<?php
  2961. while ($file = $this->getFile())
  2962. {
  2963. }',
  2964. ],
  2965. [
  2966. '<?php
  2967. switch (n) {
  2968. case label1:
  2969. echo 1;
  2970. echo 2;
  2971. break;
  2972. default:
  2973. echo 3;
  2974. echo 4;
  2975. }',
  2976. '<?php
  2977. switch (n)
  2978. {
  2979. case label1:
  2980. echo 1;
  2981. echo 2;
  2982. break;
  2983. default:
  2984. echo 3;
  2985. echo 4;
  2986. }',
  2987. ],
  2988. [
  2989. '<?php
  2990. if (true) {
  2991. echo 1;
  2992. }',
  2993. '<?php
  2994. if (true)
  2995. {
  2996. echo 1;
  2997. }',
  2998. self::$configurationOopPositionSameLine,
  2999. ],
  3000. [
  3001. '<?php
  3002. if (true) {
  3003. echo 1;
  3004. }',
  3005. '<?php
  3006. if (true){
  3007. echo 1;
  3008. }',
  3009. self::$configurationOopPositionSameLine,
  3010. ],
  3011. [
  3012. '<?php
  3013. if (true) {
  3014. echo 1;
  3015. }',
  3016. '<?php
  3017. if (true) {
  3018. echo 1;
  3019. }',
  3020. self::$configurationOopPositionSameLine,
  3021. ],
  3022. [
  3023. '<?php
  3024. while ($file = $this->getFile()) {
  3025. }',
  3026. '<?php
  3027. while ($file = $this->getFile())
  3028. {
  3029. }',
  3030. self::$configurationOopPositionSameLine,
  3031. ],
  3032. [
  3033. '<?php
  3034. switch (n) {
  3035. case label1:
  3036. echo 1;
  3037. echo 2;
  3038. break;
  3039. default:
  3040. echo 3;
  3041. echo 4;
  3042. }',
  3043. '<?php
  3044. switch (n)
  3045. {
  3046. case label1:
  3047. echo 1;
  3048. echo 2;
  3049. break;
  3050. default:
  3051. echo 3;
  3052. echo 4;
  3053. }',
  3054. self::$configurationOopPositionSameLine,
  3055. ],
  3056. [
  3057. '<?php
  3058. if (true) {
  3059. echo 1;
  3060. }',
  3061. '<?php
  3062. if (true)
  3063. {
  3064. echo 1;
  3065. }',
  3066. self::$configurationAnonymousPositionNextLine,
  3067. ],
  3068. [
  3069. '<?php
  3070. if (true) {
  3071. echo 1;
  3072. }',
  3073. '<?php
  3074. if (true){
  3075. echo 1;
  3076. }',
  3077. self::$configurationAnonymousPositionNextLine,
  3078. ],
  3079. [
  3080. '<?php
  3081. if (true) {
  3082. echo 1;
  3083. }',
  3084. '<?php
  3085. if (true) {
  3086. echo 1;
  3087. }',
  3088. self::$configurationAnonymousPositionNextLine,
  3089. ],
  3090. [
  3091. '<?php
  3092. while ($file = $this->getFile()) {
  3093. }',
  3094. '<?php
  3095. while ($file = $this->getFile())
  3096. {
  3097. }',
  3098. self::$configurationAnonymousPositionNextLine,
  3099. ],
  3100. [
  3101. '<?php
  3102. switch (n) {
  3103. case label1:
  3104. echo 1;
  3105. echo 2;
  3106. break;
  3107. default:
  3108. echo 3;
  3109. echo 4;
  3110. }',
  3111. '<?php
  3112. switch (n)
  3113. {
  3114. case label1:
  3115. echo 1;
  3116. echo 2;
  3117. break;
  3118. default:
  3119. echo 3;
  3120. echo 4;
  3121. }',
  3122. self::$configurationAnonymousPositionNextLine,
  3123. ],
  3124. ];
  3125. }
  3126. /**
  3127. * @dataProvider provideFixFunctionsCases
  3128. */
  3129. public function testFixFunctions(string $expected, ?string $input = null, array $configuration = []): void
  3130. {
  3131. $this->fixer->configure($configuration);
  3132. $this->doTest($expected, $input);
  3133. }
  3134. public function provideFixFunctionsCases()
  3135. {
  3136. return [
  3137. [
  3138. '<?php
  3139. function download()
  3140. {
  3141. }',
  3142. '<?php
  3143. function download() {
  3144. }',
  3145. ],
  3146. [
  3147. '<?php
  3148. class Foo
  3149. {
  3150. public function AAAA()
  3151. {
  3152. }
  3153. public function BBBB()
  3154. {
  3155. }
  3156. public function CCCC()
  3157. {
  3158. }
  3159. }',
  3160. '<?php
  3161. class Foo
  3162. {
  3163. public function AAAA(){
  3164. }
  3165. public function BBBB() {
  3166. }
  3167. public function CCCC()
  3168. {
  3169. }
  3170. }',
  3171. ],
  3172. [
  3173. '<?php
  3174. filter(function () {
  3175. return true;
  3176. });
  3177. ',
  3178. ],
  3179. [
  3180. '<?php
  3181. filter(function ($a) {
  3182. });',
  3183. '<?php
  3184. filter(function ($a)
  3185. {});',
  3186. ],
  3187. [
  3188. '<?php
  3189. filter(function ($b) {
  3190. });',
  3191. '<?php
  3192. filter(function ($b){});',
  3193. ],
  3194. [
  3195. '<?php
  3196. foo(array_map(function ($object) use ($x, $y) {
  3197. return array_filter($object->bar(), function ($o) {
  3198. return $o->isBaz();
  3199. });
  3200. }, $collection));',
  3201. '<?php
  3202. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  3203. ],
  3204. [
  3205. '<?php
  3206. class Foo
  3207. {
  3208. public static function bar()
  3209. {
  3210. return 1;
  3211. }
  3212. }',
  3213. ],
  3214. [
  3215. '<?php
  3216. usort($this->fixers, function &($a, $b) use ($selfName) {
  3217. return 1;
  3218. });',
  3219. ],
  3220. [
  3221. '<?php
  3222. usort(
  3223. $this->fixers,
  3224. function &($a, $b) use ($selfName) {
  3225. return 1;
  3226. }
  3227. );',
  3228. ],
  3229. [
  3230. '<?php
  3231. $fnc = function ($a, $b) { // random comment
  3232. return 0;
  3233. };',
  3234. '<?php
  3235. $fnc = function ($a, $b) // random comment
  3236. {
  3237. return 0;
  3238. };',
  3239. ],
  3240. [
  3241. '<?php
  3242. $fnc = function ($a, $b) { # random comment
  3243. return 0;
  3244. };',
  3245. '<?php
  3246. $fnc = function ($a, $b) # random comment
  3247. {
  3248. return 0;
  3249. };',
  3250. ],
  3251. [
  3252. '<?php
  3253. $fnc = function ($a, $b) /* random comment */ {
  3254. return 0;
  3255. };',
  3256. '<?php
  3257. $fnc = function ($a, $b) /* random comment */
  3258. {
  3259. return 0;
  3260. };',
  3261. ],
  3262. [
  3263. '<?php
  3264. $fnc = function ($a, $b) /** random comment */ {
  3265. return 0;
  3266. };',
  3267. '<?php
  3268. $fnc = function ($a, $b) /** random comment */
  3269. {
  3270. return 0;
  3271. };',
  3272. ],
  3273. [
  3274. '<?php
  3275. function download() {
  3276. }',
  3277. null,
  3278. self::$configurationOopPositionSameLine,
  3279. ],
  3280. [
  3281. '<?php
  3282. class Foo {
  3283. public function AAAA() {
  3284. }
  3285. public function BBBB() {
  3286. }
  3287. public function CCCC() {
  3288. }
  3289. }',
  3290. '<?php
  3291. class Foo
  3292. {
  3293. public function AAAA(){
  3294. }
  3295. public function BBBB() {
  3296. }
  3297. public function CCCC()
  3298. {
  3299. }
  3300. }',
  3301. self::$configurationOopPositionSameLine,
  3302. ],
  3303. [
  3304. '<?php
  3305. filter(function () {
  3306. return true;
  3307. });
  3308. ',
  3309. null,
  3310. self::$configurationOopPositionSameLine,
  3311. ],
  3312. [
  3313. '<?php
  3314. filter(function ($a) {
  3315. });',
  3316. '<?php
  3317. filter(function ($a)
  3318. {});',
  3319. self::$configurationOopPositionSameLine,
  3320. ],
  3321. [
  3322. '<?php
  3323. filter(function ($b) {
  3324. });',
  3325. '<?php
  3326. filter(function ($b){});',
  3327. self::$configurationOopPositionSameLine,
  3328. ],
  3329. [
  3330. '<?php
  3331. foo(array_map(function ($object) use ($x, $y) {
  3332. return array_filter($object->bar(), function ($o) {
  3333. return $o->isBaz();
  3334. });
  3335. }, $collection));',
  3336. '<?php
  3337. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  3338. self::$configurationOopPositionSameLine,
  3339. ],
  3340. [
  3341. '<?php
  3342. foo(array_map(function ($object) use ($x, $y)
  3343. {
  3344. return array_filter($object->bar(), function ($o)
  3345. {
  3346. return $o->isBaz();
  3347. });
  3348. }, $collection));',
  3349. '<?php
  3350. foo(array_map(function ($object) use ($x, $y) { return array_filter($object->bar(), function ($o) { return $o->isBaz(); }); }, $collection));',
  3351. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  3352. ],
  3353. [
  3354. '<?php
  3355. class Foo {
  3356. public static function bar() {
  3357. return 1;
  3358. }
  3359. }',
  3360. '<?php
  3361. class Foo
  3362. {
  3363. public static function bar()
  3364. {
  3365. return 1;
  3366. }
  3367. }',
  3368. self::$configurationOopPositionSameLine,
  3369. ],
  3370. [
  3371. '<?php
  3372. class Foo {
  3373. public static function bar() {
  3374. return 1;
  3375. }
  3376. }',
  3377. '<?php
  3378. class Foo
  3379. {
  3380. public static function bar()
  3381. {
  3382. return 1;
  3383. }
  3384. }',
  3385. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  3386. ],
  3387. [
  3388. '<?php
  3389. class Foo {
  3390. public static function bar() {
  3391. return 1;
  3392. }
  3393. }',
  3394. '<?php
  3395. class Foo
  3396. {
  3397. public static function bar()
  3398. {
  3399. return 1;
  3400. }
  3401. }',
  3402. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  3403. ],
  3404. [
  3405. '<?php
  3406. usort($this->fixers, function &($a, $b) use ($selfName) {
  3407. return 1;
  3408. });',
  3409. null,
  3410. self::$configurationOopPositionSameLine,
  3411. ],
  3412. [
  3413. '<?php
  3414. usort(
  3415. $this->fixers,
  3416. function &($a, $b) use ($selfName) {
  3417. return 1;
  3418. }
  3419. );',
  3420. null,
  3421. self::$configurationOopPositionSameLine,
  3422. ],
  3423. [
  3424. '<?php
  3425. $fnc = function ($a, $b) { // random comment
  3426. return 0;
  3427. };',
  3428. '<?php
  3429. $fnc = function ($a, $b) // random comment
  3430. {
  3431. return 0;
  3432. };',
  3433. self::$configurationOopPositionSameLine,
  3434. ],
  3435. [
  3436. '<?php
  3437. $fnc = function ($a, $b) { # random comment
  3438. return 0;
  3439. };',
  3440. '<?php
  3441. $fnc = function ($a, $b) # random comment
  3442. {
  3443. return 0;
  3444. };',
  3445. self::$configurationOopPositionSameLine,
  3446. ],
  3447. [
  3448. '<?php
  3449. $fnc = function ($a, $b) /* random comment */ {
  3450. return 0;
  3451. };',
  3452. '<?php
  3453. $fnc = function ($a, $b) /* random comment */
  3454. {
  3455. return 0;
  3456. };',
  3457. self::$configurationOopPositionSameLine,
  3458. ],
  3459. [
  3460. '<?php
  3461. $fnc = function ($a, $b) /** random comment */ {
  3462. return 0;
  3463. };',
  3464. '<?php
  3465. $fnc = function ($a, $b) /** random comment */
  3466. {
  3467. return 0;
  3468. };',
  3469. self::$configurationOopPositionSameLine,
  3470. ],
  3471. ];
  3472. }
  3473. /**
  3474. * @dataProvider provideFixMultiLineStructuresCases
  3475. */
  3476. public function testFixMultiLineStructures(string $expected, ?string $input = null, array $configuration = []): void
  3477. {
  3478. $this->fixer->configure($configuration);
  3479. $this->doTest($expected, $input);
  3480. }
  3481. public function provideFixMultiLineStructuresCases()
  3482. {
  3483. return [
  3484. [
  3485. '<?php
  3486. if (true === true
  3487. && true === true
  3488. ) {
  3489. }',
  3490. '<?php
  3491. if(true === true
  3492. && true === true
  3493. )
  3494. {
  3495. }',
  3496. self::$configurationCtrlStructPositionNextLine,
  3497. ],
  3498. [
  3499. '<?php
  3500. foreach (
  3501. $boo as $bar => $fooBarBazBuzz
  3502. ) {
  3503. }',
  3504. '<?php
  3505. foreach (
  3506. $boo as $bar => $fooBarBazBuzz
  3507. )
  3508. {
  3509. }',
  3510. self::$configurationCtrlStructPositionNextLine,
  3511. ],
  3512. [
  3513. '<?php
  3514. $foo = function (
  3515. $baz,
  3516. $boo
  3517. ) {
  3518. };',
  3519. '<?php
  3520. $foo = function (
  3521. $baz,
  3522. $boo
  3523. )
  3524. {
  3525. };',
  3526. self::$configurationAnonymousPositionNextLine,
  3527. ],
  3528. [
  3529. '<?php
  3530. class Foo
  3531. {
  3532. public static function bar(
  3533. $baz,
  3534. $boo
  3535. ) {
  3536. }
  3537. }',
  3538. '<?php
  3539. class Foo
  3540. {
  3541. public static function bar(
  3542. $baz,
  3543. $boo
  3544. )
  3545. {
  3546. }
  3547. }',
  3548. self::$configurationCtrlStructPositionNextLine,
  3549. ],
  3550. [
  3551. '<?php
  3552. if (true === true
  3553. && true === true
  3554. ) {
  3555. }',
  3556. '<?php
  3557. if(true === true
  3558. && true === true
  3559. )
  3560. {
  3561. }',
  3562. self::$configurationCtrlStructPositionNextLine,
  3563. ],
  3564. [
  3565. '<?php
  3566. if ($foo)
  3567. {
  3568. }
  3569. elseif (
  3570. true === true
  3571. && true === true
  3572. ) {
  3573. }',
  3574. '<?php
  3575. if ($foo)
  3576. {
  3577. }
  3578. elseif (
  3579. true === true
  3580. && true === true
  3581. )
  3582. {
  3583. }',
  3584. self::$configurationCtrlStructPositionNextLine,
  3585. ],
  3586. ];
  3587. }
  3588. /**
  3589. * @dataProvider provideFixSpaceAroundTokenCases
  3590. */
  3591. public function testFixSpaceAroundToken(string $expected, ?string $input = null, array $configuration = []): void
  3592. {
  3593. $this->fixer->configure($configuration);
  3594. $this->doTest($expected, $input);
  3595. }
  3596. public function provideFixSpaceAroundTokenCases()
  3597. {
  3598. return [
  3599. [
  3600. '<?php
  3601. try {
  3602. throw new Exception();
  3603. } catch (Exception $e) {
  3604. log($e);
  3605. }',
  3606. '<?php
  3607. try{
  3608. throw new Exception();
  3609. }catch (Exception $e){
  3610. log($e);
  3611. }',
  3612. ],
  3613. [
  3614. '<?php
  3615. do {
  3616. echo 1;
  3617. } while ($test);',
  3618. '<?php
  3619. do{
  3620. echo 1;
  3621. }while($test);',
  3622. ],
  3623. [
  3624. '<?php
  3625. if (true === true
  3626. && true === true
  3627. ) {
  3628. }',
  3629. '<?php
  3630. if(true === true
  3631. && true === true
  3632. ) {
  3633. }',
  3634. ],
  3635. [
  3636. '<?php
  3637. if (1) {
  3638. }
  3639. if ($this->tesT ($test)) {
  3640. }',
  3641. '<?php
  3642. if(1){
  3643. }
  3644. if ($this->tesT ($test)) {
  3645. }',
  3646. ],
  3647. [
  3648. '<?php
  3649. if (true) {
  3650. } elseif (false) {
  3651. } else {
  3652. }',
  3653. '<?php
  3654. if(true){
  3655. }elseif(false){
  3656. }else{
  3657. }',
  3658. ],
  3659. [
  3660. '<?php
  3661. $foo = function& () use ($bar) {
  3662. };',
  3663. '<?php
  3664. $foo = function& ()use($bar){};',
  3665. ],
  3666. [
  3667. '<?php
  3668. // comment
  3669. declare(strict_types=1);
  3670. // comment
  3671. while (true) {
  3672. }',
  3673. ],
  3674. [
  3675. '<?php
  3676. declare(ticks = 1) {
  3677. }',
  3678. '<?php
  3679. declare ( ticks = 1 ) {
  3680. }',
  3681. ],
  3682. [
  3683. '<?php
  3684. try {
  3685. throw new Exception();
  3686. } catch (Exception $e) {
  3687. log($e);
  3688. }',
  3689. '<?php
  3690. try{
  3691. throw new Exception();
  3692. }catch (Exception $e){
  3693. log($e);
  3694. }',
  3695. self::$configurationOopPositionSameLine,
  3696. ],
  3697. [
  3698. '<?php
  3699. do {
  3700. echo 1;
  3701. } while ($test);',
  3702. '<?php
  3703. do{
  3704. echo 1;
  3705. }while($test);',
  3706. self::$configurationOopPositionSameLine,
  3707. ],
  3708. [
  3709. '<?php
  3710. if (true === true
  3711. && true === true
  3712. ) {
  3713. }',
  3714. '<?php
  3715. if(true === true
  3716. && true === true
  3717. ) {
  3718. }',
  3719. self::$configurationOopPositionSameLine,
  3720. ],
  3721. [
  3722. '<?php
  3723. if (1) {
  3724. }
  3725. if ($this->tesT ($test)) {
  3726. }',
  3727. '<?php
  3728. if(1){
  3729. }
  3730. if ($this->tesT ($test)) {
  3731. }',
  3732. self::$configurationOopPositionSameLine,
  3733. ],
  3734. [
  3735. '<?php
  3736. if (true) {
  3737. } elseif (false) {
  3738. } else {
  3739. }',
  3740. '<?php
  3741. if(true){
  3742. }elseif(false){
  3743. }else{
  3744. }',
  3745. self::$configurationOopPositionSameLine,
  3746. ],
  3747. [
  3748. '<?php
  3749. $foo = function& () use ($bar) {
  3750. };',
  3751. '<?php
  3752. $foo = function& ()use($bar){};',
  3753. self::$configurationOopPositionSameLine,
  3754. ],
  3755. [
  3756. '<?php
  3757. // comment
  3758. declare(strict_types=1);
  3759. // comment
  3760. while (true) {
  3761. }',
  3762. null,
  3763. self::$configurationOopPositionSameLine,
  3764. ],
  3765. [
  3766. '<?php
  3767. declare(ticks = 1) {
  3768. }',
  3769. '<?php
  3770. declare ( ticks = 1 ) {
  3771. }',
  3772. self::$configurationOopPositionSameLine,
  3773. ],
  3774. ];
  3775. }
  3776. /**
  3777. * @dataProvider provideFinallyCases
  3778. */
  3779. public function testFinally(string $expected, ?string $input = null, array $configuration = []): void
  3780. {
  3781. $this->fixer->configure($configuration);
  3782. $this->doTest($expected, $input);
  3783. }
  3784. public function provideFinallyCases()
  3785. {
  3786. return [
  3787. [
  3788. '<?php
  3789. try {
  3790. throw new \Exception();
  3791. } catch (\LogicException $e) {
  3792. // do nothing
  3793. } catch (\Exception $e) {
  3794. // do nothing
  3795. } finally {
  3796. echo "finish!";
  3797. }',
  3798. '<?php
  3799. try {
  3800. throw new \Exception();
  3801. }catch (\LogicException $e) {
  3802. // do nothing
  3803. }
  3804. catch (\Exception $e) {
  3805. // do nothing
  3806. }
  3807. finally {
  3808. echo "finish!";
  3809. }',
  3810. ],
  3811. [
  3812. '<?php
  3813. try {
  3814. throw new \Exception();
  3815. } catch (\LogicException $e) {
  3816. // do nothing
  3817. } catch (\Exception $e) {
  3818. // do nothing
  3819. } finally {
  3820. echo "finish!";
  3821. }',
  3822. '<?php
  3823. try {
  3824. throw new \Exception();
  3825. }catch (\LogicException $e) {
  3826. // do nothing
  3827. }
  3828. catch (\Exception $e) {
  3829. // do nothing
  3830. }
  3831. finally {
  3832. echo "finish!";
  3833. }',
  3834. self::$configurationOopPositionSameLine,
  3835. ],
  3836. [
  3837. '<?php
  3838. try
  3839. {
  3840. throw new \Exception();
  3841. }
  3842. catch (\LogicException $e)
  3843. {
  3844. // do nothing
  3845. }
  3846. catch (\Exception $e)
  3847. {
  3848. // do nothing
  3849. }
  3850. finally
  3851. {
  3852. echo "finish!";
  3853. }',
  3854. '<?php
  3855. try {
  3856. throw new \Exception();
  3857. }catch (\LogicException $e) {
  3858. // do nothing
  3859. }
  3860. catch (\Exception $e) {
  3861. // do nothing
  3862. }
  3863. finally {
  3864. echo "finish!";
  3865. }',
  3866. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  3867. ],
  3868. ];
  3869. }
  3870. /**
  3871. * @dataProvider provideFunctionImportCases
  3872. */
  3873. public function testFunctionImport(string $expected, ?string $input = null, array $configuration = []): void
  3874. {
  3875. $this->fixer->configure($configuration);
  3876. $this->doTest($expected, $input);
  3877. }
  3878. public function provideFunctionImportCases()
  3879. {
  3880. return [
  3881. [
  3882. '<?php
  3883. use function Foo\bar;
  3884. if (true) {
  3885. }',
  3886. ],
  3887. [
  3888. '<?php
  3889. use function Foo\bar;
  3890. if (true) {
  3891. }',
  3892. null,
  3893. self::$configurationOopPositionSameLine,
  3894. ],
  3895. [
  3896. '<?php
  3897. use function Foo\bar;
  3898. if (true)
  3899. {
  3900. }',
  3901. '<?php
  3902. use function Foo\bar;
  3903. if (true) {
  3904. }',
  3905. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  3906. ],
  3907. [
  3908. '<?php
  3909. use function Foo\bar;
  3910. if (true) {
  3911. }',
  3912. ],
  3913. ];
  3914. }
  3915. /**
  3916. * @dataProvider provideFix70Cases
  3917. * @requires PHP 7.0
  3918. */
  3919. public function testFix70(string $expected, ?string $input = null, array $configuration = []): void
  3920. {
  3921. $this->fixer->configure($configuration);
  3922. $this->doTest($expected, $input);
  3923. }
  3924. public function provideFix70Cases()
  3925. {
  3926. return [
  3927. [
  3928. '<?php
  3929. function foo($a)
  3930. {
  3931. // foo
  3932. $foo = new class($a) extends Foo {
  3933. public function bar()
  3934. {
  3935. }
  3936. };
  3937. }',
  3938. '<?php
  3939. function foo($a)
  3940. {
  3941. // foo
  3942. $foo = new class($a) extends Foo { public function bar() {} };
  3943. }',
  3944. ],
  3945. [
  3946. '<?php
  3947. foo(1, new class implements Logger {
  3948. public function log($message)
  3949. {
  3950. log($message);
  3951. }
  3952. }, 3);',
  3953. '<?php
  3954. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  3955. ],
  3956. [
  3957. '<?php
  3958. $message = (new class() implements FooInterface {
  3959. });',
  3960. '<?php
  3961. $message = (new class() implements FooInterface{});',
  3962. ],
  3963. [
  3964. '<?php $message = (new class() {
  3965. });',
  3966. '<?php $message = (new class() {});',
  3967. ],
  3968. [
  3969. '<?php
  3970. if (1) {
  3971. $message = (new class() extends Foo {
  3972. public function bar()
  3973. {
  3974. echo 1;
  3975. }
  3976. });
  3977. }',
  3978. '<?php
  3979. if (1) {
  3980. $message = (new class() extends Foo
  3981. {
  3982. public function bar() { echo 1; }
  3983. });
  3984. }',
  3985. ],
  3986. [
  3987. '<?php
  3988. class Foo
  3989. {
  3990. public function use()
  3991. {
  3992. }
  3993. public function use1(): string
  3994. {
  3995. }
  3996. }
  3997. ',
  3998. '<?php
  3999. class Foo
  4000. {
  4001. public function use() {
  4002. }
  4003. public function use1(): string {
  4004. }
  4005. }
  4006. ',
  4007. ],
  4008. [
  4009. '<?php
  4010. $a = function (int $foo): string {
  4011. echo $foo;
  4012. };
  4013. $b = function (int $foo) use ($bar): string {
  4014. echo $foo . $bar;
  4015. };
  4016. function a()
  4017. {
  4018. }
  4019. ',
  4020. '<?php
  4021. $a = function (int $foo): string
  4022. {
  4023. echo $foo;
  4024. };
  4025. $b = function (int $foo) use($bar): string
  4026. {
  4027. echo $foo . $bar;
  4028. };
  4029. function a() {
  4030. }
  4031. ',
  4032. ],
  4033. [
  4034. '<?php
  4035. class Something
  4036. {
  4037. public function sth(): string
  4038. {
  4039. return function (int $foo) use ($bar): string {
  4040. return $bar;
  4041. };
  4042. }
  4043. }',
  4044. '<?php
  4045. class Something
  4046. {
  4047. public function sth(): string
  4048. {
  4049. return function (int $foo) use ($bar): string { return $bar; };
  4050. }
  4051. }',
  4052. ],
  4053. [
  4054. '<?php
  4055. use function some\a\{
  4056. test1,
  4057. test2
  4058. };
  4059. test();',
  4060. ],
  4061. [
  4062. '<?php
  4063. use some\a\{ClassA, ClassB, ClassC as C};
  4064. use function some\a\{fn_a, fn_b, fn_c};
  4065. use const some\a\{ConstA, ConstB, ConstC};
  4066. ',
  4067. ],
  4068. [
  4069. '<?php
  4070. function foo($a) {
  4071. // foo
  4072. $foo = new class($a) extends Foo {
  4073. public function bar() {
  4074. }
  4075. };
  4076. }',
  4077. '<?php
  4078. function foo($a)
  4079. {
  4080. // foo
  4081. $foo = new class($a) extends Foo { public function bar() {} };
  4082. }',
  4083. self::$configurationOopPositionSameLine,
  4084. ],
  4085. [
  4086. '<?php
  4087. function foo($a)
  4088. {
  4089. // foo
  4090. $foo = new class($a) extends Foo {
  4091. public function bar()
  4092. {
  4093. }
  4094. };
  4095. }',
  4096. '<?php
  4097. function foo($a)
  4098. {
  4099. // foo
  4100. $foo = new class($a) extends Foo { public function bar() {} };
  4101. }',
  4102. self::$configurationCtrlStructPositionNextLine,
  4103. ],
  4104. [
  4105. '<?php
  4106. function foo($a) {
  4107. // foo
  4108. $foo = new class($a) extends Foo {
  4109. public function bar() {
  4110. }
  4111. };
  4112. }',
  4113. '<?php
  4114. function foo($a)
  4115. {
  4116. // foo
  4117. $foo = new class($a) extends Foo { public function bar() {} };
  4118. }',
  4119. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4120. ],
  4121. [
  4122. '<?php
  4123. function foo($a)
  4124. {
  4125. // foo
  4126. $foo = new class($a) extends Foo
  4127. {
  4128. public function bar()
  4129. {
  4130. }
  4131. };
  4132. }',
  4133. '<?php
  4134. function foo($a)
  4135. {
  4136. // foo
  4137. $foo = new class($a) extends Foo { public function bar() {} };
  4138. }',
  4139. self::$configurationAnonymousPositionNextLine,
  4140. ],
  4141. [
  4142. '<?php
  4143. function foo($a) {
  4144. // foo
  4145. $foo = new class($a) extends Foo
  4146. {
  4147. public function bar() {
  4148. }
  4149. };
  4150. }',
  4151. '<?php
  4152. function foo($a)
  4153. {
  4154. // foo
  4155. $foo = new class($a) extends Foo { public function bar() {} };
  4156. }',
  4157. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4158. ],
  4159. [
  4160. '<?php
  4161. foo(1, new class implements Logger {
  4162. public function log($message) {
  4163. log($message);
  4164. }
  4165. }, 3);',
  4166. '<?php
  4167. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  4168. self::$configurationOopPositionSameLine,
  4169. ],
  4170. [
  4171. '<?php
  4172. foo(1, new class implements Logger {
  4173. public function log($message)
  4174. {
  4175. log($message);
  4176. }
  4177. }, 3);',
  4178. '<?php
  4179. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  4180. self::$configurationCtrlStructPositionNextLine,
  4181. ],
  4182. [
  4183. '<?php
  4184. foo(1, new class implements Logger
  4185. {
  4186. public function log($message) {
  4187. log($message);
  4188. }
  4189. }, 3);',
  4190. '<?php
  4191. foo(1, new class implements Logger { public function log($message) { log($message); } }, 3);',
  4192. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4193. ],
  4194. [
  4195. '<?php
  4196. $message = (new class() implements FooInterface {
  4197. });',
  4198. '<?php
  4199. $message = (new class() implements FooInterface{});',
  4200. self::$configurationOopPositionSameLine,
  4201. ],
  4202. [
  4203. '<?php
  4204. $message = (new class() implements FooInterface {
  4205. });',
  4206. '<?php
  4207. $message = (new class() implements FooInterface{});',
  4208. self::$configurationCtrlStructPositionNextLine,
  4209. ],
  4210. [
  4211. '<?php
  4212. $message = (new class() implements FooInterface
  4213. {
  4214. });',
  4215. '<?php
  4216. $message = (new class() implements FooInterface{});',
  4217. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4218. ],
  4219. [
  4220. '<?php $message = (new class() {
  4221. });',
  4222. '<?php $message = (new class() {});',
  4223. self::$configurationOopPositionSameLine,
  4224. ],
  4225. [
  4226. '<?php $message = (new class() {
  4227. });',
  4228. '<?php $message = (new class() {});',
  4229. self::$configurationCtrlStructPositionNextLine,
  4230. ],
  4231. [
  4232. '<?php $message = (new class()
  4233. {
  4234. });',
  4235. '<?php $message = (new class() {});',
  4236. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4237. ],
  4238. [
  4239. '<?php
  4240. if (1) {
  4241. $message = (new class() extends Foo {
  4242. public function bar() {
  4243. echo 1;
  4244. }
  4245. });
  4246. }',
  4247. '<?php
  4248. if (1) {
  4249. $message = (new class() extends Foo
  4250. {
  4251. public function bar() { echo 1; }
  4252. });
  4253. }',
  4254. self::$configurationOopPositionSameLine,
  4255. ],
  4256. [
  4257. '<?php
  4258. if (1)
  4259. {
  4260. $message = (new class() extends Foo {
  4261. public function bar()
  4262. {
  4263. echo 1;
  4264. }
  4265. });
  4266. }',
  4267. '<?php
  4268. if (1) {
  4269. $message = (new class() extends Foo
  4270. {
  4271. public function bar() { echo 1; }
  4272. });
  4273. }',
  4274. self::$configurationCtrlStructPositionNextLine,
  4275. ],
  4276. [
  4277. '<?php
  4278. if (1) {
  4279. $message = (new class() extends Foo
  4280. {
  4281. public function bar() {
  4282. echo 1;
  4283. }
  4284. });
  4285. }',
  4286. '<?php
  4287. if (1) {
  4288. $message = (new class() extends Foo
  4289. {
  4290. public function bar() { echo 1; }
  4291. });
  4292. }',
  4293. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4294. ],
  4295. [
  4296. '<?php
  4297. if (1) {
  4298. $message = (new class() extends Foo
  4299. {
  4300. public function bar() {
  4301. echo 1;
  4302. }
  4303. });
  4304. }',
  4305. '<?php
  4306. if (1) {
  4307. $message = (new class() extends Foo
  4308. {
  4309. public function bar() { echo 1; }
  4310. });
  4311. }',
  4312. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4313. ],
  4314. [
  4315. '<?php
  4316. if (1)
  4317. {
  4318. $message = (new class() extends Foo
  4319. {
  4320. public function bar()
  4321. {
  4322. echo 1;
  4323. }
  4324. });
  4325. }',
  4326. '<?php
  4327. if (1) {
  4328. $message = (new class() extends Foo
  4329. {
  4330. public function bar() { echo 1; }
  4331. });
  4332. }',
  4333. self::$configurationCtrlStructPositionNextLine + self::$configurationAnonymousPositionNextLine,
  4334. ],
  4335. [
  4336. '<?php
  4337. if (1)
  4338. {
  4339. $message = (new class() extends Foo
  4340. {
  4341. public function bar() {
  4342. echo 1;
  4343. }
  4344. });
  4345. }',
  4346. '<?php
  4347. if (1) {
  4348. $message = (new class() extends Foo
  4349. {
  4350. public function bar() { echo 1; }
  4351. });
  4352. }',
  4353. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine + self::$configurationAnonymousPositionNextLine,
  4354. ],
  4355. [
  4356. '<?php
  4357. class Foo {
  4358. public function use() {
  4359. }
  4360. public function use1(): string {
  4361. }
  4362. }
  4363. ',
  4364. '<?php
  4365. class Foo
  4366. {
  4367. public function use() {
  4368. }
  4369. public function use1(): string {
  4370. }
  4371. }
  4372. ',
  4373. self::$configurationOopPositionSameLine,
  4374. ],
  4375. [
  4376. '<?php
  4377. class Foo {
  4378. public function use() {
  4379. }
  4380. public function use1(): string {
  4381. }
  4382. }
  4383. ',
  4384. '<?php
  4385. class Foo
  4386. {
  4387. public function use() {
  4388. }
  4389. public function use1(): string {
  4390. }
  4391. }
  4392. ',
  4393. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4394. ],
  4395. [
  4396. '<?php
  4397. $a = function (int $foo): string {
  4398. echo $foo;
  4399. };
  4400. $b = function (int $foo) use ($bar): string {
  4401. echo $foo . $bar;
  4402. };
  4403. function a() {
  4404. }
  4405. ',
  4406. '<?php
  4407. $a = function (int $foo): string
  4408. {
  4409. echo $foo;
  4410. };
  4411. $b = function (int $foo) use($bar): string
  4412. {
  4413. echo $foo . $bar;
  4414. };
  4415. function a() {
  4416. }
  4417. ',
  4418. self::$configurationOopPositionSameLine,
  4419. ],
  4420. [
  4421. '<?php
  4422. $a = function (int $foo): string
  4423. {
  4424. echo $foo;
  4425. };
  4426. $b = function (int $foo) use ($bar): string
  4427. {
  4428. echo $foo . $bar;
  4429. };
  4430. function a() {
  4431. }
  4432. ',
  4433. '<?php
  4434. $a = function (int $foo): string
  4435. {
  4436. echo $foo;
  4437. };
  4438. $b = function (int $foo) use($bar): string
  4439. {
  4440. echo $foo . $bar;
  4441. };
  4442. function a() {
  4443. }
  4444. ',
  4445. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4446. ],
  4447. [
  4448. '<?php
  4449. class Something {
  4450. public function sth(): string {
  4451. return function (int $foo) use ($bar): string {
  4452. return $bar;
  4453. };
  4454. }
  4455. }',
  4456. '<?php
  4457. class Something
  4458. {
  4459. public function sth(): string
  4460. {
  4461. return function (int $foo) use ($bar): string { return $bar; };
  4462. }
  4463. }',
  4464. self::$configurationOopPositionSameLine,
  4465. ],
  4466. [
  4467. '<?php
  4468. class Something {
  4469. public function sth(): string {
  4470. return function (int $foo) use ($bar): string
  4471. {
  4472. return $bar;
  4473. };
  4474. }
  4475. }',
  4476. '<?php
  4477. class Something
  4478. {
  4479. public function sth(): string
  4480. {
  4481. return function (int $foo) use ($bar): string { return $bar; };
  4482. }
  4483. }',
  4484. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4485. ],
  4486. [
  4487. '<?php
  4488. use function some\a\{
  4489. test1,
  4490. test2
  4491. };
  4492. test();',
  4493. null,
  4494. self::$configurationOopPositionSameLine,
  4495. ],
  4496. [
  4497. '<?php
  4498. use function some\a\{
  4499. test1,
  4500. test2
  4501. };
  4502. test();',
  4503. null,
  4504. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4505. ],
  4506. [
  4507. '<?php
  4508. use function some\a\{
  4509. test1,
  4510. test2
  4511. };
  4512. test();',
  4513. null,
  4514. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4515. ],
  4516. [
  4517. '<?php
  4518. use some\a\{ClassA, ClassB, ClassC as C};
  4519. use function some\a\{fn_a, fn_b, fn_c};
  4520. use const some\a\{ConstA, ConstB, ConstC};
  4521. ',
  4522. null,
  4523. self::$configurationOopPositionSameLine,
  4524. ],
  4525. [
  4526. '<?php
  4527. use some\a\{ClassA, ClassB, ClassC as C};
  4528. use function some\a\{fn_a, fn_b, fn_c};
  4529. use const some\a\{ConstA, ConstB, ConstC};
  4530. ',
  4531. null,
  4532. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4533. ],
  4534. [
  4535. '<?php
  4536. use some\a\{ClassA, ClassB, ClassC as C};
  4537. use function some\a\{fn_a, fn_b, fn_c};
  4538. use const some\a\{ConstA, ConstB, ConstC};
  4539. ',
  4540. null,
  4541. self::$configurationOopPositionSameLine + self::$configurationAnonymousPositionNextLine,
  4542. ],
  4543. [
  4544. '<?php
  4545. $foo = new class () extends \Exception {
  4546. };
  4547. ',
  4548. '<?php
  4549. $foo = new class () extends \Exception {};
  4550. ',
  4551. ],
  4552. [
  4553. '<?php
  4554. $foo = new class () extends \Exception {};
  4555. ',
  4556. null,
  4557. ['allow_single_line_anonymous_class_with_empty_body' => true],
  4558. ],
  4559. [
  4560. '<?php
  4561. $foo = new class() {}; // comment
  4562. ',
  4563. null,
  4564. ['allow_single_line_anonymous_class_with_empty_body' => true],
  4565. ],
  4566. [
  4567. '<?php
  4568. $foo = new class() { /* comment */ }; // another comment
  4569. ',
  4570. null,
  4571. ['allow_single_line_anonymous_class_with_empty_body' => true],
  4572. ],
  4573. [
  4574. '<?php
  4575. $foo = new class () extends \Exception {
  4576. protected $message = "Surprise";
  4577. };
  4578. ',
  4579. '<?php
  4580. $foo = new class () extends \Exception { protected $message = "Surprise"; };
  4581. ',
  4582. ['allow_single_line_anonymous_class_with_empty_body' => true],
  4583. ],
  4584. ];
  4585. }
  4586. /**
  4587. * @dataProvider providePreserveLineAfterControlBraceCases
  4588. */
  4589. public function testPreserveLineAfterControlBrace(string $expected, ?string $input = null, array $configuration = []): void
  4590. {
  4591. $this->fixer->configure($configuration);
  4592. $this->doTest($expected, $input);
  4593. }
  4594. public function providePreserveLineAfterControlBraceCases()
  4595. {
  4596. return [
  4597. [
  4598. '<?php
  4599. if (1==1) { // test
  4600. $a = 1;
  4601. }
  4602. echo $a;',
  4603. '<?php
  4604. if (1==1) // test
  4605. { $a = 1; }
  4606. echo $a;',
  4607. ],
  4608. [
  4609. '<?php
  4610. if ($test) { // foo
  4611. echo 1;
  4612. }
  4613. if (1 === 1) {//a
  4614. $a = "b"; /*d*/
  4615. }//c
  4616. echo $a;
  4617. if ($a === 3) /**/
  4618. {echo 1;}
  4619. ',
  4620. '<?php
  4621. if ($test) // foo
  4622. {
  4623. echo 1;
  4624. }
  4625. if (1 === 1)//a
  4626. {$a = "b"; /*d*/}//c
  4627. echo $a;
  4628. if ($a === 3) /**/
  4629. {echo 1;}
  4630. ',
  4631. ],
  4632. [
  4633. '<?php
  4634. if (true) {
  4635. // The blank line helps with legibility in nested control structures
  4636. if (true) {
  4637. // if body
  4638. }
  4639. // if body
  4640. }',
  4641. ],
  4642. [
  4643. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  4644. ],
  4645. [
  4646. '<?php
  4647. if (true) {
  4648. // The blank line helps with legibility in nested control structures
  4649. if (true) {
  4650. // if body
  4651. }
  4652. // if body
  4653. }',
  4654. null,
  4655. self::$configurationOopPositionSameLine,
  4656. ],
  4657. [
  4658. '<?php
  4659. if (true)
  4660. {
  4661. // The blank line helps with legibility in nested control structures
  4662. if (true)
  4663. {
  4664. // if body
  4665. }
  4666. // if body
  4667. }',
  4668. '<?php
  4669. if (true) {
  4670. // The blank line helps with legibility in nested control structures
  4671. if (true) {
  4672. // if body
  4673. }
  4674. // if body
  4675. }',
  4676. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4677. ],
  4678. [
  4679. "<?php if (true) {\r\n\r\n// CRLF newline\n}",
  4680. null,
  4681. self::$configurationOopPositionSameLine,
  4682. ],
  4683. [
  4684. "<?php if (true)
  4685. {\r\n\r\n// CRLF newline\n}",
  4686. "<?php if (true){\r\n\r\n// CRLF newline\n}",
  4687. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4688. ],
  4689. ];
  4690. }
  4691. /**
  4692. * @dataProvider provideFixWithAllowOnelineLambdaCases
  4693. */
  4694. public function testFixWithAllowSingleLineClosure(string $expected, ?string $input = null): void
  4695. {
  4696. $this->fixer->configure([
  4697. 'allow_single_line_closure' => true,
  4698. ]);
  4699. $this->doTest($expected, $input);
  4700. }
  4701. public function provideFixWithAllowOnelineLambdaCases()
  4702. {
  4703. return [
  4704. [
  4705. '<?php
  4706. $callback = function () { return true; };',
  4707. ],
  4708. [
  4709. '<?php
  4710. $callback = function () { if ($a) { return true; } return false; };',
  4711. '<?php
  4712. $callback = function () { if($a){ return true; } return false; };',
  4713. ],
  4714. [
  4715. '<?php
  4716. $callback = function () { if ($a) { return true; } return false; };',
  4717. '<?php
  4718. $callback = function () { if($a) return true; return false; };',
  4719. ],
  4720. [
  4721. '<?php
  4722. $callback = function () {
  4723. if ($a) {
  4724. return true;
  4725. }
  4726. return false;
  4727. };',
  4728. '<?php
  4729. $callback = function () { if($a) return true;
  4730. return false; };',
  4731. ],
  4732. ];
  4733. }
  4734. /**
  4735. * @dataProvider provideDoWhileLoopInsideAnIfWithoutBracketsCases
  4736. */
  4737. public function testDoWhileLoopInsideAnIfWithoutBrackets(string $expected, ?string $input = null): void
  4738. {
  4739. $this->doTest($expected, $input);
  4740. }
  4741. public function provideDoWhileLoopInsideAnIfWithoutBracketsCases()
  4742. {
  4743. return [
  4744. [
  4745. '<?php
  4746. if (true) {
  4747. do {
  4748. echo 1;
  4749. } while (false);
  4750. }',
  4751. '<?php
  4752. if (true)
  4753. do {
  4754. echo 1;
  4755. } while (false);',
  4756. ],
  4757. ];
  4758. }
  4759. /**
  4760. * @dataProvider provideMessyWhitespacesCases
  4761. */
  4762. public function testMessyWhitespaces(string $expected, ?string $input = null, array $configuration = []): void
  4763. {
  4764. $this->fixer->configure($configuration);
  4765. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  4766. $this->doTest($expected, $input);
  4767. }
  4768. public function provideMessyWhitespacesCases()
  4769. {
  4770. return [
  4771. [
  4772. '<?php
  4773. if (true) {'."\r\n"
  4774. ."\t".'if (true) {'."\r\n"
  4775. ."\t\t".'echo 1;'."\r\n"
  4776. ."\t".'} elseif (true) {'."\r\n"
  4777. ."\t\t".'echo 2;'."\r\n"
  4778. ."\t".'} else {'."\r\n"
  4779. ."\t\t".'echo 3;'."\r\n"
  4780. ."\t".'}'."\r\n"
  4781. .'}',
  4782. '<?php
  4783. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  4784. ],
  4785. [
  4786. '<?php
  4787. if (true) {'."\r\n"
  4788. ."\t".'if (true) {'."\r\n"
  4789. ."\t\t".'echo 1;'."\r\n"
  4790. ."\t".'} elseif (true) {'."\r\n"
  4791. ."\t\t".'echo 2;'."\r\n"
  4792. ."\t".'} else {'."\r\n"
  4793. ."\t\t".'echo 3;'."\r\n"
  4794. ."\t".'}'."\r\n"
  4795. .'}',
  4796. '<?php
  4797. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  4798. self::$configurationOopPositionSameLine,
  4799. ],
  4800. [
  4801. '<?php
  4802. if (true)'
  4803. ."\r\n".'{'."\r\n"
  4804. ."\t".'if (true)'."\r\n\t".'{'."\r\n"
  4805. ."\t\t".'echo 1;'."\r\n"
  4806. ."\t".'}'
  4807. ."\r\n\t".'elseif (true)'
  4808. ."\r\n\t".'{'."\r\n"
  4809. ."\t\t".'echo 2;'."\r\n"
  4810. ."\t".'}'
  4811. ."\r\n\t".'else'
  4812. ."\r\n\t".'{'."\r\n"
  4813. ."\t\t".'echo 3;'."\r\n"
  4814. ."\t".'}'."\r\n"
  4815. .'}',
  4816. '<?php
  4817. if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
  4818. self::$configurationOopPositionSameLine + self::$configurationCtrlStructPositionNextLine,
  4819. ],
  4820. ];
  4821. }
  4822. /**
  4823. * @dataProvider provideNowdocInTemplatesCases
  4824. */
  4825. public function testNowdocInTemplates(string $expected, ?string $input = null): void
  4826. {
  4827. $this->doTest($expected, $input);
  4828. }
  4829. public function provideNowdocInTemplatesCases()
  4830. {
  4831. return [
  4832. [
  4833. <<<'EOT'
  4834. <?php
  4835. if (true) {
  4836. $var = <<<'NOWDOC'
  4837. NOWDOC;
  4838. ?>
  4839. <?php
  4840. }
  4841. EOT
  4842. ,
  4843. <<<'EOT'
  4844. <?php
  4845. if (true) {
  4846. $var = <<<'NOWDOC'
  4847. NOWDOC;
  4848. ?>
  4849. <?php
  4850. }
  4851. EOT
  4852. ,
  4853. ],
  4854. [
  4855. <<<'EOT'
  4856. <?php
  4857. if (true) {
  4858. $var = <<<HEREDOC
  4859. HEREDOC;
  4860. ?>
  4861. <?php
  4862. }
  4863. EOT
  4864. ,
  4865. <<<'EOT'
  4866. <?php
  4867. if (true) {
  4868. $var = <<<HEREDOC
  4869. HEREDOC;
  4870. ?>
  4871. <?php
  4872. }
  4873. EOT
  4874. ,
  4875. ],
  4876. ];
  4877. }
  4878. /**
  4879. * @dataProvider provideFixCommentsCases
  4880. */
  4881. public function testFixComments(string $expected, ?string $input = null): void
  4882. {
  4883. $this->doTest($expected, $input);
  4884. $this->doTest(str_replace('//', '#', $expected), null === $input ? null : str_replace('//', '#', $input));
  4885. }
  4886. public function provideFixCommentsCases()
  4887. {
  4888. return [
  4889. [
  4890. '<?php
  4891. function test()
  4892. {
  4893. // $closure = function ($callback) use ($query) {
  4894. // doSomething();
  4895. //
  4896. // return true;
  4897. // };
  4898. $a = 3;
  4899. }',
  4900. ],
  4901. [
  4902. '<?php
  4903. function test()
  4904. {
  4905. // $closure = function ($callback) use ($query) {
  4906. // doSomething();
  4907. // '.'
  4908. // return true;
  4909. // };
  4910. $a = 3;
  4911. }',
  4912. ],
  4913. [
  4914. '<?php
  4915. if ($foo) {
  4916. foo();
  4917. // if ($bar === \'bar\') {
  4918. // return [];
  4919. // }
  4920. } else {
  4921. bar();
  4922. }
  4923. ',
  4924. ],
  4925. [
  4926. '<?php
  4927. if ($foo) {
  4928. foo();
  4929. // if ($bar === \'bar\') {
  4930. // return [];
  4931. // }
  4932. } else {
  4933. bar();
  4934. }
  4935. ',
  4936. ],
  4937. [
  4938. '<?php
  4939. if ($foo) {
  4940. foo();
  4941. // if ($bar === \'bar\') {
  4942. // return [];
  4943. // }
  4944. '.'
  4945. $bar = \'bar\';
  4946. } else {
  4947. bar();
  4948. }
  4949. ',
  4950. ],
  4951. [
  4952. '<?php
  4953. if ($foo) {
  4954. foo();
  4955. // bar();
  4956. '.'
  4957. $bar = \'bar\';
  4958. } else {
  4959. bar();
  4960. }
  4961. ',
  4962. ],
  4963. [
  4964. '<?php
  4965. if ($foo) {
  4966. foo();
  4967. // bar();
  4968. '.'
  4969. $bar = \'bar\';
  4970. } else {
  4971. bar();
  4972. }
  4973. ',
  4974. ],
  4975. [
  4976. '<?php
  4977. if ($foo) {
  4978. foo();
  4979. '.'
  4980. // bar();
  4981. $bar = \'bar\';
  4982. } else {
  4983. bar();
  4984. }
  4985. ',
  4986. ],
  4987. [
  4988. '<?php
  4989. if ($foo) {
  4990. foo();
  4991. '.'
  4992. // bar();
  4993. } else {
  4994. bar();
  4995. }
  4996. ',
  4997. ],
  4998. [
  4999. '<?php
  5000. function foo()
  5001. {
  5002. $a = 1;
  5003. // we will return sth
  5004. return $a;
  5005. }
  5006. ',
  5007. '<?php
  5008. function foo()
  5009. {
  5010. $a = 1;
  5011. // we will return sth
  5012. return $a;
  5013. }
  5014. ',
  5015. ],
  5016. [
  5017. '<?php
  5018. function foo()
  5019. {
  5020. $a = 1;
  5021. '.'
  5022. // bar();
  5023. // we will return sth
  5024. return $a;
  5025. }
  5026. ',
  5027. '<?php
  5028. function foo()
  5029. {
  5030. $a = 1;
  5031. '.'
  5032. // bar();
  5033. // we will return sth
  5034. return $a;
  5035. }
  5036. ',
  5037. ],
  5038. [
  5039. '<?php
  5040. function foo()
  5041. {
  5042. $a = 1;
  5043. // if ($a === \'bar\') {
  5044. // return [];
  5045. // }
  5046. // we will return sth
  5047. return $a;
  5048. }
  5049. ',
  5050. '<?php
  5051. function foo()
  5052. {
  5053. $a = 1;
  5054. // if ($a === \'bar\') {
  5055. // return [];
  5056. // }
  5057. // we will return sth
  5058. return $a;
  5059. }
  5060. ',
  5061. ],
  5062. ];
  5063. }
  5064. public function testDynamicStaticMethodCallNotTouched(): void
  5065. {
  5066. $this->doTest(
  5067. '<?php
  5068. SomeClass::{$method}(new \stdClass());
  5069. SomeClass::{\'test\'}(new \stdClass());
  5070. function example()
  5071. {
  5072. SomeClass::{$method}(new \stdClass());
  5073. SomeClass::{\'test\'}(new \stdClass());
  5074. }'
  5075. );
  5076. }
  5077. /**
  5078. * @dataProvider provideIndentCommentCases
  5079. */
  5080. public function testIndentComment(string $expected, ?string $input, WhitespacesFixerConfig $config = null): void
  5081. {
  5082. if (null !== $config) {
  5083. $this->fixer->setWhitespacesConfig($config);
  5084. }
  5085. $this->doTest($expected, $input);
  5086. }
  5087. public function provideIndentCommentCases()
  5088. {
  5089. yield [
  5090. "<?php
  5091. if (true) {
  5092. \t\$i += 2;
  5093. \treturn foo(\$i);
  5094. \t/*
  5095. \t \$i += 3;
  5096. \t // 1
  5097. "."
  5098. \t return foo(\$i);
  5099. \t */
  5100. }",
  5101. '<?php
  5102. if (true) {
  5103. $i += 2;
  5104. return foo($i);
  5105. /*
  5106. $i += 3;
  5107. // 1
  5108. '.'
  5109. return foo($i);
  5110. */
  5111. }',
  5112. new WhitespacesFixerConfig("\t", "\n"),
  5113. ];
  5114. yield [
  5115. '<?php
  5116. class MyClass extends SomeClass
  5117. {
  5118. /* public function myFunction() {
  5119. $MyItems = [];
  5120. return $MyItems;
  5121. }
  5122. */
  5123. }',
  5124. '<?php
  5125. class MyClass extends SomeClass {
  5126. /* public function myFunction() {
  5127. $MyItems = [];
  5128. return $MyItems;
  5129. }
  5130. */
  5131. }',
  5132. ];
  5133. yield [
  5134. '<?php
  5135. if (true) {
  5136. $i += 2;
  5137. return foo($i);
  5138. /*
  5139. $i += 3;
  5140. return foo($i);
  5141. */
  5142. }',
  5143. '<?php
  5144. if (true) {
  5145. $i += 2;
  5146. return foo($i);
  5147. /*
  5148. $i += 3;
  5149. return foo($i);
  5150. */
  5151. }',
  5152. ];
  5153. }
  5154. /**
  5155. * @dataProvider provideFixAlternativeSyntaxCases
  5156. */
  5157. public function testFixAlternativeSyntax(string $expected, ?string $input = null): void
  5158. {
  5159. $this->doTest($expected, $input);
  5160. }
  5161. public function provideFixAlternativeSyntaxCases()
  5162. {
  5163. yield [
  5164. '<?php if (foo()) {
  5165. while (bar()) {
  5166. }
  5167. }',
  5168. '<?php if (foo()) while (bar()) {}',
  5169. ];
  5170. yield [
  5171. '<?php if ($a) {
  5172. foreach ($b as $c) {
  5173. }
  5174. }',
  5175. '<?php if ($a) foreach ($b as $c) {}',
  5176. ];
  5177. yield [
  5178. '<?php if ($a) foreach ($b as $c): ?> X <?php endforeach; ?>',
  5179. ];
  5180. yield [
  5181. '<?php if ($a) while ($b): ?> X <?php endwhile; ?>',
  5182. ];
  5183. yield [
  5184. '<?php if ($a) for (;;): ?> X <?php endfor; ?>',
  5185. ];
  5186. yield [
  5187. '<?php if ($a) switch ($a): case 1: ?> X <?php endswitch; ?>',
  5188. ];
  5189. yield [
  5190. '<?php if ($a): elseif ($b): for (;;): ?> X <?php endfor; endif; ?>',
  5191. ];
  5192. yield [
  5193. '<?php switch ($a): case 1: for (;;): ?> X <?php endfor; endswitch; ?>,',
  5194. ];
  5195. yield [
  5196. '<?php
  5197. if ($a) foreach ($b as $c): ?>
  5198. <?php if ($a) for (;;): ?>
  5199. <?php if ($a) foreach ($b as $c): ?>
  5200. <?php if ($a) for (;;): ?>
  5201. <?php if ($a) while ($b): ?>
  5202. <?php if ($a) while ($b): ?>
  5203. <?php if ($a) foreach ($b as $c): ?>
  5204. <?php if ($a) for (;;): ?>
  5205. <?php if ($a) while ($b): ?>
  5206. <?php if ($a) while ($b): ?>
  5207. <?php endwhile; ?>
  5208. <?php endwhile; ?>
  5209. <?php endfor; ?>
  5210. <?php endforeach; ?>
  5211. <?php endwhile; ?>
  5212. <?php endwhile; ?>
  5213. <?php endfor; ?>
  5214. <?php endforeach; ?>
  5215. <?php endfor; ?>
  5216. <?php endforeach; ?>',
  5217. ];
  5218. }
  5219. /**
  5220. * @requires PHP 8.0
  5221. *
  5222. * @param string $input
  5223. * @param string $expected
  5224. *
  5225. * @dataProvider provideFix80Cases
  5226. */
  5227. public function testFix80($expected, $input): void
  5228. {
  5229. $this->doTest($expected, $input);
  5230. }
  5231. public function provideFix80Cases()
  5232. {
  5233. yield 'match' => [
  5234. '<?php echo match ($x) {
  5235. 1, 2 => "Same for 1 and 2",
  5236. };',
  5237. '<?php echo match($x)
  5238. {
  5239. 1, 2 => "Same for 1 and 2",
  5240. };',
  5241. ];
  5242. }
  5243. }