BracesFixerTest.php 106 KB

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