BracesFixerTest.php 102 KB

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