BracesFixerTest.php 109 KB

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