BracesFixerTest.php 109 KB

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