BracesFixerTest.php 109 KB

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