BracesFixerTest.php 112 KB

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