SingleSpaceAroundConstructFixerTest.php 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\LanguageConstruct;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Andreas Möller <am@localheinz.com>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAroundConstructFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAroundConstructFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAroundConstructFixer
  26. */
  27. final class SingleSpaceAroundConstructFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @dataProvider provideConfigureRejectsInvalidControlStatementCases
  31. *
  32. * @param mixed $construct
  33. */
  34. public function testConfigureRejectsInvalidControlStatement($construct): void
  35. {
  36. $this->expectException(InvalidFixerConfigurationException::class);
  37. $this->fixer->configure([
  38. 'constructs_followed_by_a_single_space' => [
  39. $construct,
  40. ],
  41. ]);
  42. }
  43. public static function provideConfigureRejectsInvalidControlStatementCases(): iterable
  44. {
  45. yield 'null' => [null];
  46. yield 'false' => [false];
  47. yield 'true' => [true];
  48. yield 'int' => [0];
  49. yield 'float' => [3.14];
  50. yield 'array' => [[]];
  51. yield 'object' => [new \stdClass()];
  52. yield 'unknown' => ['foo'];
  53. }
  54. /**
  55. * @dataProvider provideFixWithAbstractCases
  56. */
  57. public function testFixWithAbstract(string $expected, ?string $input = null): void
  58. {
  59. $this->fixer->configure([
  60. 'constructs_followed_by_a_single_space' => [
  61. 'abstract',
  62. ],
  63. ]);
  64. $this->doTest($expected, $input);
  65. }
  66. /**
  67. * @return iterable<array{string, string}>
  68. */
  69. public static function provideFixWithAbstractCases(): iterable
  70. {
  71. yield [
  72. '<?php abstract class Foo {}; if($a){}',
  73. '<?php abstract class Foo {}; if($a){}',
  74. ];
  75. yield [
  76. '<?php abstract class Foo {};',
  77. '<?php abstract
  78. class Foo {};',
  79. ];
  80. yield [
  81. '<?php abstract /* foo */class Foo {};',
  82. '<?php abstract/* foo */class Foo {};',
  83. ];
  84. yield [
  85. '<?php abstract /* foo */class Foo {};',
  86. '<?php abstract /* foo */class Foo {};',
  87. ];
  88. yield [
  89. '<?php
  90. abstract class Foo
  91. {
  92. abstract function bar();
  93. }',
  94. '<?php
  95. abstract class Foo
  96. {
  97. abstract function bar();
  98. }',
  99. ];
  100. yield [
  101. '<?php
  102. abstract class Foo
  103. {
  104. abstract function bar();
  105. }',
  106. '<?php
  107. abstract class Foo
  108. {
  109. abstract
  110. function bar();
  111. }',
  112. ];
  113. yield [
  114. '<?php
  115. abstract class Foo
  116. {
  117. abstract /* foo */function bar();
  118. }',
  119. '<?php
  120. abstract class Foo
  121. {
  122. abstract /* foo */function bar();
  123. }',
  124. ];
  125. yield [
  126. '<?php
  127. abstract class Foo
  128. {
  129. abstract /* foo */function bar();
  130. }',
  131. '<?php
  132. abstract class Foo
  133. {
  134. abstract/* foo */function bar();
  135. }',
  136. ];
  137. }
  138. /**
  139. * @dataProvider provideFixWithBreakCases
  140. */
  141. public function testFixWithBreak(string $expected, ?string $input = null): void
  142. {
  143. $this->fixer->configure([
  144. 'constructs_followed_by_a_single_space' => [
  145. 'break',
  146. ],
  147. ]);
  148. $this->doTest($expected, $input);
  149. }
  150. /**
  151. * @return iterable<array{0: string, 1?: string}>
  152. */
  153. public static function provideFixWithBreakCases(): iterable
  154. {
  155. yield [
  156. '<?php while (true) { break; }',
  157. ];
  158. yield [
  159. '<?php while (true) { break /* foo */; }',
  160. '<?php while (true) { break/* foo */; }',
  161. ];
  162. yield [
  163. '<?php while (true) { break /* foo */; }',
  164. '<?php while (true) { break /* foo */; }',
  165. ];
  166. yield [
  167. '<?php while (true) { break 1; }',
  168. '<?php while (true) { break 1; }',
  169. ];
  170. yield [
  171. '<?php while (true) { break 1; }',
  172. '<?php while (true) { break
  173. 1; }',
  174. ];
  175. yield [
  176. '<?php while (true) { break /* foo */1; }',
  177. '<?php while (true) { break/* foo */1; }',
  178. ];
  179. yield [
  180. '<?php while (true) { break /* foo */1; }',
  181. '<?php while (true) { break /* foo */1; }',
  182. ];
  183. }
  184. /**
  185. * @dataProvider provideFixWithAsCases
  186. *
  187. * @param _AutogeneratedInputConfiguration $config
  188. */
  189. public function testFixWithAs(string $expected, ?string $input = null, array $config = []): void
  190. {
  191. $this->fixer->configure($config);
  192. $this->doTest($expected, $input);
  193. }
  194. public static function provideFixWithAsCases(): iterable
  195. {
  196. yield [
  197. '<?php foreach ($foo as $bar) {}',
  198. '<?php foreach ($foo as$bar) {}',
  199. [
  200. 'constructs_preceded_by_a_single_space' => [],
  201. 'constructs_followed_by_a_single_space' => ['as'],
  202. ],
  203. ];
  204. yield [
  205. '<?php foreach ($foo as $bar) {}',
  206. '<?php foreach ($foo as $bar) {}',
  207. [
  208. 'constructs_preceded_by_a_single_space' => [],
  209. 'constructs_followed_by_a_single_space' => ['as'],
  210. ],
  211. ];
  212. yield [
  213. '<?php foreach ($foo as $bar) {}',
  214. '<?php foreach ($foo as $bar) {}',
  215. [
  216. 'constructs_preceded_by_a_single_space' => ['as'],
  217. 'constructs_followed_by_a_single_space' => [],
  218. ],
  219. ];
  220. yield [
  221. '<?php foreach ($foo as $bar) {}',
  222. '<?php foreach ($foo as
  223. $bar) {}',
  224. [
  225. 'constructs_preceded_by_a_single_space' => [],
  226. 'constructs_followed_by_a_single_space' => ['as'],
  227. ],
  228. ];
  229. yield [
  230. '<?php foreach ($foo as $bar) {}',
  231. '<?php foreach ($foo
  232. as $bar) {}',
  233. [
  234. 'constructs_preceded_by_a_single_space' => ['as'],
  235. 'constructs_followed_by_a_single_space' => [],
  236. ],
  237. ];
  238. yield [
  239. '<?php foreach ($foo as /* foo */$bar) {}',
  240. '<?php foreach ($foo as/* foo */$bar) {}',
  241. [
  242. 'constructs_preceded_by_a_single_space' => [],
  243. 'constructs_followed_by_a_single_space' => ['as'],
  244. ],
  245. ];
  246. yield [
  247. '<?php foreach ($foo/* foo */ as $bar) {}',
  248. '<?php foreach ($foo/* foo */as $bar) {}',
  249. [
  250. 'constructs_preceded_by_a_single_space' => ['as'],
  251. 'constructs_followed_by_a_single_space' => [],
  252. ],
  253. ];
  254. yield [
  255. '<?php foreach ($foo as /* foo */$bar) {}',
  256. '<?php foreach ($foo as /* foo */$bar) {}',
  257. [
  258. 'constructs_preceded_by_a_single_space' => [],
  259. 'constructs_followed_by_a_single_space' => ['as'],
  260. ],
  261. ];
  262. yield [
  263. '<?php foreach ($foo /* foo */ as $bar) {}',
  264. '<?php foreach ($foo /* foo */ as $bar) {}',
  265. [
  266. 'constructs_preceded_by_a_single_space' => ['as'],
  267. 'constructs_followed_by_a_single_space' => [],
  268. ],
  269. ];
  270. yield [
  271. '<?php foreach (range(1, 12) as $num) {}',
  272. '<?php foreach (range(1, 12)as $num) {}',
  273. [
  274. 'constructs_preceded_by_a_single_space' => ['as'],
  275. 'constructs_followed_by_a_single_space' => [],
  276. ],
  277. ];
  278. yield [
  279. '<?php foreach (range(1, 12) as $num) {}',
  280. '<?php foreach (range(1, 12) as $num) {}',
  281. [
  282. 'constructs_preceded_by_a_single_space' => ['as'],
  283. 'constructs_followed_by_a_single_space' => [],
  284. ],
  285. ];
  286. yield [
  287. '<?php foreach ([1, 2, 3, 4] as $int) {}',
  288. '<?php foreach ([1, 2, 3, 4]as $int) {}',
  289. [
  290. 'constructs_preceded_by_a_single_space' => ['as'],
  291. 'constructs_followed_by_a_single_space' => [],
  292. ],
  293. ];
  294. yield [
  295. '<?php foreach ([1, 2, 3, 4] as $int) {}',
  296. '<?php foreach ([1, 2, 3, 4]
  297. as $int) {}',
  298. [
  299. 'constructs_preceded_by_a_single_space' => ['as'],
  300. 'constructs_followed_by_a_single_space' => [],
  301. ],
  302. ];
  303. yield [
  304. '<?php
  305. class Foo
  306. {
  307. use Bar {
  308. Bar::baz as bar;
  309. }
  310. }',
  311. '<?php
  312. class Foo
  313. {
  314. use Bar {
  315. Bar::baz as bar;
  316. }
  317. }',
  318. [
  319. 'constructs_preceded_by_a_single_space' => [],
  320. 'constructs_followed_by_a_single_space' => ['as'],
  321. ],
  322. ];
  323. yield [
  324. '<?php
  325. class Foo
  326. {
  327. use Bar {
  328. Bar::baz as bar;
  329. }
  330. }',
  331. '<?php
  332. class Foo
  333. {
  334. use Bar {
  335. Bar::baz as
  336. bar;
  337. }
  338. }',
  339. [
  340. 'constructs_preceded_by_a_single_space' => [],
  341. 'constructs_followed_by_a_single_space' => ['as'],
  342. ],
  343. ];
  344. yield [
  345. '<?php
  346. class Foo
  347. {
  348. use Bar {
  349. Bar::baz as /* foo */bar;
  350. }
  351. }',
  352. '<?php
  353. class Foo
  354. {
  355. use Bar {
  356. Bar::baz as/* foo */bar;
  357. }
  358. }',
  359. [
  360. 'constructs_preceded_by_a_single_space' => [],
  361. 'constructs_followed_by_a_single_space' => ['as'],
  362. ],
  363. ];
  364. yield [
  365. '<?php
  366. class Foo
  367. {
  368. use Bar {
  369. Bar::baz as /* foo */bar;
  370. }
  371. }',
  372. '<?php
  373. class Foo
  374. {
  375. use Bar {
  376. Bar::baz as /* foo */bar;
  377. }
  378. }',
  379. [
  380. 'constructs_preceded_by_a_single_space' => [],
  381. 'constructs_followed_by_a_single_space' => ['as'],
  382. ],
  383. ];
  384. yield [
  385. '<?php
  386. namespace Foo;
  387. use Bar as Baz;
  388. final class Qux extends Baz {}
  389. ',
  390. '<?php
  391. namespace Foo;
  392. use Bar as Baz;
  393. final class Qux extends Baz {}
  394. ',
  395. [
  396. 'constructs_preceded_by_a_single_space' => ['as'],
  397. 'constructs_followed_by_a_single_space' => [],
  398. ],
  399. ];
  400. yield [
  401. '<?php
  402. namespace Foo;
  403. use Bar as Baz;
  404. final class Qux extends Baz {}
  405. ',
  406. '<?php
  407. namespace Foo;
  408. use Bar
  409. as Baz;
  410. final class Qux extends Baz {}
  411. ',
  412. [
  413. 'constructs_preceded_by_a_single_space' => ['as'],
  414. 'constructs_followed_by_a_single_space' => [],
  415. ],
  416. ];
  417. yield [
  418. '<?php
  419. namespace Foo;
  420. use Bar /** foo */ as Baz;
  421. final class Qux extends Baz {}
  422. ',
  423. '<?php
  424. namespace Foo;
  425. use Bar /** foo */as Baz;
  426. final class Qux extends Baz {}
  427. ',
  428. [
  429. 'constructs_preceded_by_a_single_space' => ['as'],
  430. 'constructs_followed_by_a_single_space' => [],
  431. ],
  432. ];
  433. yield [
  434. '<?php
  435. class Foo
  436. {
  437. use Bar {
  438. Bar::baz as bar;
  439. }
  440. }
  441. ',
  442. '<?php
  443. class Foo
  444. {
  445. use Bar {
  446. Bar::baz as bar;
  447. }
  448. }
  449. ',
  450. [
  451. 'constructs_preceded_by_a_single_space' => ['as'],
  452. 'constructs_followed_by_a_single_space' => [],
  453. ],
  454. ];
  455. yield [
  456. '<?php
  457. class Foo
  458. {
  459. use Bar {
  460. Bar::baz as bar;
  461. }
  462. }
  463. ',
  464. '<?php
  465. class Foo
  466. {
  467. use Bar {
  468. Bar::baz
  469. as bar;
  470. }
  471. }
  472. ',
  473. [
  474. 'constructs_preceded_by_a_single_space' => ['as'],
  475. 'constructs_followed_by_a_single_space' => [],
  476. ],
  477. ];
  478. yield [
  479. '<?php
  480. class Foo
  481. {
  482. use Bar {
  483. Bar::baz/** foo */ as bar;
  484. }
  485. }
  486. ',
  487. '<?php
  488. class Foo
  489. {
  490. use Bar {
  491. Bar::baz/** foo */as bar;
  492. }
  493. }
  494. ',
  495. [
  496. 'constructs_preceded_by_a_single_space' => ['as'],
  497. 'constructs_followed_by_a_single_space' => [],
  498. ],
  499. ];
  500. }
  501. /**
  502. * @dataProvider provideFixWithCaseCases
  503. */
  504. public function testFixWithCase(string $expected, ?string $input = null): void
  505. {
  506. $this->fixer->configure([
  507. 'constructs_followed_by_a_single_space' => [
  508. 'case',
  509. ],
  510. ]);
  511. $this->doTest($expected, $input);
  512. }
  513. /**
  514. * @return iterable<array{string, string}>
  515. */
  516. public static function provideFixWithCaseCases(): iterable
  517. {
  518. yield [
  519. '<?php
  520. switch ($i) {
  521. case $j:
  522. break;
  523. }',
  524. '<?php
  525. switch ($i) {
  526. case$j:
  527. break;
  528. }',
  529. ];
  530. yield [
  531. '<?php
  532. switch ($i) {
  533. case 0:
  534. break;
  535. }',
  536. '<?php
  537. switch ($i) {
  538. case 0:
  539. break;
  540. }',
  541. ];
  542. yield [
  543. '<?php
  544. switch ($i) {
  545. case 0:
  546. break;
  547. }',
  548. '<?php
  549. switch ($i) {
  550. case
  551. 0:
  552. break;
  553. }',
  554. ];
  555. yield [
  556. '<?php
  557. switch ($i) {
  558. case /* foo */0:
  559. break;
  560. }',
  561. '<?php
  562. switch ($i) {
  563. case/* foo */0:
  564. break;
  565. }',
  566. ];
  567. }
  568. /**
  569. * @dataProvider provideFixWithCatchCases
  570. */
  571. public function testFixWithCatch(string $expected, ?string $input = null): void
  572. {
  573. $this->fixer->configure([
  574. 'constructs_followed_by_a_single_space' => [
  575. 'catch',
  576. ],
  577. ]);
  578. $this->doTest($expected, $input);
  579. }
  580. /**
  581. * @return iterable<array{string, string}>
  582. */
  583. public static function provideFixWithCatchCases(): iterable
  584. {
  585. yield [
  586. '<?php try {} catch (\Exception $exception) {}',
  587. '<?php try {} catch(\Exception $exception) {}',
  588. ];
  589. yield [
  590. '<?php try {} catch (\Exception $exception) {}',
  591. '<?php try {} catch (\Exception $exception) {}',
  592. ];
  593. yield [
  594. '<?php try {} catch (\Exception $exception) {}',
  595. '<?php try {} catch
  596. (\Exception $exception) {}',
  597. ];
  598. yield [
  599. '<?php try {} catch /* foo */(Exception $exception) {}',
  600. '<?php try {} catch/* foo */(Exception $exception) {}',
  601. ];
  602. yield [
  603. '<?php try {} catch /* foo */(Exception $exception) {}',
  604. '<?php try {} catch /* foo */(Exception $exception) {}',
  605. ];
  606. }
  607. /**
  608. * @dataProvider provideFixWithClassCases
  609. */
  610. public function testFixWithClass(string $expected, ?string $input = null): void
  611. {
  612. $this->fixer->configure([
  613. 'constructs_followed_by_a_single_space' => [
  614. 'class',
  615. ],
  616. ]);
  617. $this->doTest($expected, $input);
  618. }
  619. public static function provideFixWithClassCases(): iterable
  620. {
  621. yield [
  622. '<?php class Foo {}',
  623. '<?php class Foo {}',
  624. ];
  625. yield [
  626. '<?php class Foo {}',
  627. '<?php class
  628. Foo {}',
  629. ];
  630. yield [
  631. '<?php class /* foo */Foo {}',
  632. '<?php class /* foo */Foo {}',
  633. ];
  634. yield [
  635. '<?php class /* foo */Foo {}',
  636. '<?php class/* foo */Foo {}',
  637. ];
  638. yield [
  639. '<?php $foo = stdClass::class;',
  640. ];
  641. yield [
  642. '<?php $foo = new class {};',
  643. '<?php $foo = new class {};',
  644. ['constructs_followed_by_a_single_space' => ['class']],
  645. ];
  646. yield [
  647. '<?php $foo = new class {};',
  648. '<?php $foo = new class{};',
  649. ['constructs_followed_by_a_single_space' => ['class']],
  650. ];
  651. yield [
  652. '<?php $foo = new class /* foo */{};',
  653. '<?php $foo = new class/* foo */{};',
  654. ['constructs_followed_by_a_single_space' => ['class']],
  655. ];
  656. yield [
  657. '<?php $foo = new class /* foo */{};',
  658. '<?php $foo = new class /* foo */{};',
  659. ['constructs_followed_by_a_single_space' => ['class']],
  660. ];
  661. yield [
  662. '<?php $foo = new class(){};',
  663. null,
  664. ['constructs_followed_by_a_single_space' => ['class']],
  665. ];
  666. yield [
  667. '<?php return
  668. $a ? new class(){ public function foo() { echo 1; }}
  669. : 1
  670. ;',
  671. null,
  672. ['constructs_followed_by_a_single_space' => ['return']],
  673. ];
  674. }
  675. /**
  676. * @dataProvider provideFixWithContinueCases
  677. */
  678. public function testFixWithContinue(string $expected, ?string $input = null): void
  679. {
  680. $this->fixer->configure([
  681. 'constructs_followed_by_a_single_space' => [
  682. 'continue',
  683. ],
  684. ]);
  685. $this->doTest($expected, $input);
  686. }
  687. /**
  688. * @return iterable<array{0: string, 1?: string}>
  689. */
  690. public static function provideFixWithContinueCases(): iterable
  691. {
  692. yield [
  693. '<?php while (true) { continue; }',
  694. ];
  695. yield [
  696. '<?php while (true) { continue /* foo */; }',
  697. '<?php while (true) { continue/* foo */; }',
  698. ];
  699. yield [
  700. '<?php while (true) { continue /* foo */; }',
  701. '<?php while (true) { continue /* foo */; }',
  702. ];
  703. yield [
  704. '<?php while (true) { continue 1; }',
  705. '<?php while (true) { continue 1; }',
  706. ];
  707. yield [
  708. '<?php while (true) { continue 1; }',
  709. '<?php while (true) { continue
  710. 1; }',
  711. ];
  712. yield [
  713. '<?php while (true) { continue /* foo*/ 1; }',
  714. '<?php while (true) { continue /* foo*/ 1; }',
  715. ];
  716. }
  717. /**
  718. * @dataProvider provideFixWithConstCases
  719. */
  720. public function testFixWithConst(string $expected, ?string $input = null): void
  721. {
  722. $this->fixer->configure([
  723. 'constructs_followed_by_a_single_space' => [
  724. 'const',
  725. ],
  726. ]);
  727. $this->doTest($expected, $input);
  728. }
  729. /**
  730. * @return iterable<array{0: string, 1?: string}>
  731. */
  732. public static function provideFixWithConstCases(): iterable
  733. {
  734. yield [
  735. '<?php class Foo { const FOO = 9000; }',
  736. '<?php class Foo { const FOO = 9000; }',
  737. ];
  738. yield [
  739. '<?php class Foo { const FOO = 9000; }',
  740. '<?php class Foo { const
  741. FOO = 9000; }',
  742. ];
  743. yield [
  744. '<?php class Foo { const /* foo */FOO = 9000; }',
  745. '<?php class Foo { const/* foo */FOO = 9000; }',
  746. ];
  747. yield [
  748. '<?php class Foo { const /* foo */FOO = 9000; }',
  749. '<?php class Foo { const /* foo */FOO = 9000; }',
  750. ];
  751. yield [
  752. '<?php class Foo {
  753. const FOO = [
  754. 1
  755. ];
  756. const BAR = [
  757. 2,
  758. ];
  759. }',
  760. '<?php class Foo {
  761. const FOO = [
  762. 1
  763. ];
  764. const BAR = [
  765. 2,
  766. ];
  767. }',
  768. ];
  769. yield ['<?php class Foo {
  770. const
  771. FOO = 9000,
  772. BAR = 10000;
  773. }',
  774. ];
  775. yield [
  776. '<?php
  777. const
  778. A = 3,
  779. B = 3
  780. ?>',
  781. ];
  782. yield [
  783. '<?php
  784. const A = 3 ?>
  785. <?php
  786. [ ,
  787. ,
  788. ,$z
  789. ] = foo() ;',
  790. '<?php
  791. const A = 3 ?>
  792. <?php
  793. [ ,
  794. ,
  795. ,$z
  796. ] = foo() ;',
  797. ];
  798. yield [
  799. '<?php
  800. const A
  801. =
  802. 1;
  803. ',
  804. '<?php
  805. const
  806. A
  807. =
  808. 1;
  809. ',
  810. ];
  811. }
  812. /**
  813. * @dataProvider provideFixWithConstImportCases
  814. */
  815. public function testFixWithConstImport(string $expected, ?string $input = null): void
  816. {
  817. $this->fixer->configure([
  818. 'constructs_followed_by_a_single_space' => [
  819. 'const_import',
  820. ],
  821. ]);
  822. $this->doTest($expected, $input);
  823. }
  824. /**
  825. * @return iterable<array{string, string}>
  826. */
  827. public static function provideFixWithConstImportCases(): iterable
  828. {
  829. yield [
  830. '<?php use const FOO\BAR;',
  831. '<?php use const FOO\BAR;',
  832. ];
  833. yield [
  834. '<?php use const FOO\BAR;',
  835. '<?php use const
  836. FOO\BAR;',
  837. ];
  838. yield [
  839. '<?php use const /* foo */FOO\BAR;',
  840. '<?php use const/* foo */FOO\BAR;',
  841. ];
  842. yield [
  843. '<?php use const /* foo */FOO\BAR;',
  844. '<?php use const /* foo */FOO\BAR;',
  845. ];
  846. }
  847. /**
  848. * @dataProvider provideFixWithCloneCases
  849. */
  850. public function testFixWithClone(string $expected, ?string $input = null): void
  851. {
  852. $this->fixer->configure([
  853. 'constructs_followed_by_a_single_space' => [
  854. 'clone',
  855. ],
  856. ]);
  857. $this->doTest($expected, $input);
  858. }
  859. /**
  860. * @return iterable<array{string, string}>
  861. */
  862. public static function provideFixWithCloneCases(): iterable
  863. {
  864. yield [
  865. '<?php clone $foo;',
  866. '<?php clone$foo;',
  867. ];
  868. yield [
  869. '<?php clone $foo;',
  870. '<?php clone $foo;',
  871. ];
  872. yield [
  873. '<?php clone $foo;',
  874. '<?php clone
  875. $foo;',
  876. ];
  877. yield [
  878. '<?php clone /* foo */$foo;',
  879. '<?php clone/* foo */$foo;',
  880. ];
  881. }
  882. /**
  883. * @dataProvider provideFixWithDoCases
  884. */
  885. public function testFixWithDo(string $expected, ?string $input = null): void
  886. {
  887. $this->fixer->configure([
  888. 'constructs_followed_by_a_single_space' => [
  889. 'do',
  890. ],
  891. ]);
  892. $this->doTest($expected, $input);
  893. }
  894. /**
  895. * @return iterable<array{string, string}>
  896. */
  897. public static function provideFixWithDoCases(): iterable
  898. {
  899. yield [
  900. '<?php do {} while (true);',
  901. '<?php do{} while (true);',
  902. ];
  903. yield [
  904. '<?php DO {} while (true);',
  905. '<?php DO{} while (true);',
  906. ];
  907. yield [
  908. '<?php do {} while (true);',
  909. '<?php do {} while (true);',
  910. ];
  911. yield [
  912. '<?php do {} while (true);',
  913. '<?php do
  914. {} while (true);',
  915. ];
  916. yield [
  917. '<?php do /* foo*/{} while (true);',
  918. '<?php do/* foo*/{} while (true);',
  919. ];
  920. yield [
  921. '<?php do /* foo*/{} while (true);',
  922. '<?php do /* foo*/{} while (true);',
  923. ];
  924. }
  925. /**
  926. * @dataProvider provideFixWithEchoCases
  927. */
  928. public function testFixWithEcho(string $expected, ?string $input = null): void
  929. {
  930. $this->fixer->configure([
  931. 'constructs_followed_by_a_single_space' => [
  932. 'echo',
  933. ],
  934. ]);
  935. $this->doTest($expected, $input);
  936. }
  937. /**
  938. * @return iterable<array{string, string}>
  939. */
  940. public static function provideFixWithEchoCases(): iterable
  941. {
  942. yield [
  943. '<?php echo $foo;',
  944. '<?php echo$foo;',
  945. ];
  946. yield [
  947. '<?php echo 9000;',
  948. '<?php echo 9000;',
  949. ];
  950. yield [
  951. '<?php echo 9000;',
  952. '<?php echo
  953. 9000;',
  954. ];
  955. yield [
  956. '<?php ECHO /* foo */9000;',
  957. '<?php ECHO/* foo */9000;',
  958. ];
  959. }
  960. /**
  961. * @dataProvider provideFixWithElseCases
  962. */
  963. public function testFixWithElse(string $expected, ?string $input = null): void
  964. {
  965. $this->fixer->configure([
  966. 'constructs_followed_by_a_single_space' => [
  967. 'else',
  968. ],
  969. ]);
  970. $this->doTest($expected, $input);
  971. }
  972. /**
  973. * @return iterable<array{string, string}>
  974. */
  975. public static function provideFixWithElseCases(): iterable
  976. {
  977. yield [
  978. '<?php if (true) {} else {}',
  979. '<?php if (true) {} else{}',
  980. ];
  981. yield [
  982. '<?php if (true) {} else {}',
  983. '<?php if (true) {} else {}',
  984. ];
  985. yield [
  986. '<?php if (true){} else {}',
  987. '<?php if (true){} else
  988. {}',
  989. ];
  990. yield [
  991. '<?php if (true) {} else /* foo */{}',
  992. '<?php if (true) {} else/* foo */{}',
  993. ];
  994. }
  995. /**
  996. * @dataProvider provideFixWithElsePrecededCases
  997. */
  998. public function testFixWithElsePreceded(string $expected, ?string $input = null): void
  999. {
  1000. $this->fixer->configure([
  1001. 'constructs_preceded_by_a_single_space' => [
  1002. 'else',
  1003. ],
  1004. ]);
  1005. $this->doTest($expected, $input);
  1006. }
  1007. /**
  1008. * @return iterable<array{0: string, 1?: string}>
  1009. */
  1010. public static function provideFixWithElsePrecededCases(): iterable
  1011. {
  1012. yield 'A' => [
  1013. '<?php if ($a222) {} else {}',
  1014. '<?php if ($a222) {}else{}',
  1015. ];
  1016. yield 'B' => [
  1017. "<?php if (\$b333) {}//foo X\nelse {}",
  1018. ];
  1019. }
  1020. /**
  1021. * @dataProvider provideFixWithElseIfCases
  1022. */
  1023. public function testFixWithElseIf(string $expected, ?string $input = null): void
  1024. {
  1025. $this->fixer->configure([
  1026. 'constructs_followed_by_a_single_space' => [
  1027. 'elseif',
  1028. ],
  1029. ]);
  1030. $this->doTest($expected, $input);
  1031. }
  1032. /**
  1033. * @return iterable<array{string, string}>
  1034. */
  1035. public static function provideFixWithElseIfCases(): iterable
  1036. {
  1037. yield [
  1038. '<?php if (true) {}elseif (false) {}',
  1039. '<?php if (true) {}elseif(false) {}',
  1040. ];
  1041. yield [
  1042. '<?php if (true) {} elseif (false) {}',
  1043. '<?php if (true) {} elseif (false) {}',
  1044. ];
  1045. yield [
  1046. '<?php if (true) {} elseif (false) {}',
  1047. '<?php if (true) {} elseif
  1048. (false) {}',
  1049. ];
  1050. yield [
  1051. '<?php if (true) {} elseif /* foo */(false) {}',
  1052. '<?php if (true) {} elseif/* foo */(false) {}',
  1053. ];
  1054. }
  1055. /**
  1056. * @dataProvider provideFixWithElseIfPrecededCases
  1057. */
  1058. public function testFixWithElseIfPreceded(string $expected, ?string $input = null): void
  1059. {
  1060. $this->fixer->configure([
  1061. 'constructs_preceded_by_a_single_space' => [
  1062. 'elseif',
  1063. ],
  1064. ]);
  1065. $this->doTest($expected, $input);
  1066. }
  1067. /**
  1068. * @return iterable<array{0: string, 1?: string}>
  1069. */
  1070. public static function provideFixWithElseIfPrecededCases(): iterable
  1071. {
  1072. yield [
  1073. '<?php if ($y1x) {} elseif (false) {}',
  1074. '<?php if ($y1x) {}elseif(false) {}',
  1075. ];
  1076. yield [
  1077. '<?php if ($y2y) {} elseif (false) {}',
  1078. '<?php if ($y2y) {} elseif (false) {}',
  1079. ];
  1080. yield [
  1081. "<?php if (\$y3t) {}//foo Y\nelseif (false) {}",
  1082. ];
  1083. }
  1084. /**
  1085. * @dataProvider provideFixWithExtendsCases
  1086. */
  1087. public function testFixWithExtends(string $expected, ?string $input = null): void
  1088. {
  1089. $this->fixer->configure([
  1090. 'constructs_followed_by_a_single_space' => [
  1091. 'extends',
  1092. ],
  1093. ]);
  1094. $this->doTest($expected, $input);
  1095. }
  1096. /**
  1097. * @return iterable<array{0: string, 1?: string}>
  1098. */
  1099. public static function provideFixWithExtendsCases(): iterable
  1100. {
  1101. yield [
  1102. '<?php class Foo extends \InvalidArgumentException {}',
  1103. '<?php class Foo extends \InvalidArgumentException {}',
  1104. ];
  1105. yield [
  1106. '<?php class Foo extends \InvalidArgumentException {}',
  1107. '<?php class Foo extends
  1108. \InvalidArgumentException {}',
  1109. ];
  1110. yield [
  1111. '<?php class Foo extends /* foo */\InvalidArgumentException {}',
  1112. '<?php class Foo extends/* foo */\InvalidArgumentException {}',
  1113. ];
  1114. yield [
  1115. '<?php class Foo extends /* foo */\InvalidArgumentException {}',
  1116. '<?php class Foo extends /* foo */\InvalidArgumentException {}',
  1117. ];
  1118. yield [
  1119. '<?php interface Foo extends Bar1 {}',
  1120. '<?php interface Foo extends Bar1 {}',
  1121. ];
  1122. yield [
  1123. '<?php interface Foo extends Bar2 {}',
  1124. '<?php interface Foo extends
  1125. Bar2 {}',
  1126. ];
  1127. yield [
  1128. '<?php interface Foo extends /* foo */Bar3 {}',
  1129. '<?php interface Foo extends/* foo */Bar3 {}',
  1130. ];
  1131. yield [
  1132. '<?php interface Foo extends /* foo */Bar4 {}',
  1133. '<?php interface Foo extends /* foo */Bar4 {}',
  1134. ];
  1135. yield [
  1136. '<?php interface Foo extends Bar5, Baz, Qux {}',
  1137. '<?php interface Foo extends Bar5, Baz, Qux {}',
  1138. ];
  1139. yield [
  1140. '<?php interface Foo extends Bar6, Baz, Qux {}',
  1141. '<?php interface Foo extends
  1142. Bar6, Baz, Qux {}',
  1143. ];
  1144. yield [
  1145. '<?php interface Foo extends /* foo */Bar7, Baz, Qux {}',
  1146. '<?php interface Foo extends/* foo */Bar7, Baz, Qux {}',
  1147. ];
  1148. yield [
  1149. '<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
  1150. '<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
  1151. ];
  1152. yield [
  1153. '<?php interface Foo extends
  1154. Bar9,
  1155. Baz,
  1156. Qux
  1157. {}',
  1158. ];
  1159. yield [
  1160. '<?php $foo = new class extends \InvalidArgumentException {};',
  1161. '<?php $foo = new class extends \InvalidArgumentException {};',
  1162. ];
  1163. yield [
  1164. '<?php $foo = new class extends \InvalidArgumentException {};',
  1165. '<?php $foo = new class extends
  1166. \InvalidArgumentException {};',
  1167. ];
  1168. yield [
  1169. '<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
  1170. '<?php $foo = new class extends/* foo */\InvalidArgumentException {};',
  1171. ];
  1172. yield [
  1173. '<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
  1174. '<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
  1175. ];
  1176. }
  1177. /**
  1178. * @dataProvider provideFixWithFinalCases
  1179. */
  1180. public function testFixWithFinal(string $expected, ?string $input = null): void
  1181. {
  1182. $this->fixer->configure([
  1183. 'constructs_followed_by_a_single_space' => [
  1184. 'final',
  1185. ],
  1186. ]);
  1187. $this->doTest($expected, $input);
  1188. }
  1189. /**
  1190. * @return iterable<array{string, string}>
  1191. */
  1192. public static function provideFixWithFinalCases(): iterable
  1193. {
  1194. yield [
  1195. '<?php final class Foo {}',
  1196. '<?php final class Foo {}',
  1197. ];
  1198. yield [
  1199. '<?php final class Foo {}',
  1200. '<?php final
  1201. class Foo {}',
  1202. ];
  1203. yield [
  1204. '<?php final /* foo */class Foo {}',
  1205. '<?php final/* foo */class Foo {}',
  1206. ];
  1207. yield [
  1208. '<?php final /* foo */class Foo {}',
  1209. '<?php final /* foo */class Foo {}',
  1210. ];
  1211. yield [
  1212. '<?php
  1213. class Foo
  1214. {
  1215. final function bar() {}
  1216. }',
  1217. '<?php
  1218. class Foo
  1219. {
  1220. final function bar() {}
  1221. }',
  1222. ];
  1223. yield [
  1224. '<?php
  1225. class Foo
  1226. {
  1227. final function bar() {}
  1228. }',
  1229. '<?php
  1230. class Foo
  1231. {
  1232. final
  1233. function bar() {}
  1234. }',
  1235. ];
  1236. yield [
  1237. '<?php
  1238. class Foo
  1239. {
  1240. final /* foo */function bar() {}
  1241. }',
  1242. '<?php
  1243. class Foo
  1244. {
  1245. final/* foo */function bar() {}
  1246. }',
  1247. ];
  1248. yield [
  1249. '<?php
  1250. class Foo
  1251. {
  1252. final /* foo */function bar() {}
  1253. }',
  1254. '<?php
  1255. class Foo
  1256. {
  1257. final /* foo */function bar() {}
  1258. }',
  1259. ];
  1260. }
  1261. /**
  1262. * @dataProvider provideFixWithFinallyCases
  1263. */
  1264. public function testFixWithFinally(string $expected, ?string $input = null): void
  1265. {
  1266. $this->fixer->configure([
  1267. 'constructs_followed_by_a_single_space' => [
  1268. 'finally',
  1269. ],
  1270. ]);
  1271. $this->doTest($expected, $input);
  1272. }
  1273. /**
  1274. * @return iterable<array{string, string}>
  1275. */
  1276. public static function provideFixWithFinallyCases(): iterable
  1277. {
  1278. yield [
  1279. '<?php try {} finally {}',
  1280. '<?php try {} finally{}',
  1281. ];
  1282. yield [
  1283. '<?php try {} finally {}',
  1284. '<?php try {} finally {}',
  1285. ];
  1286. yield [
  1287. '<?php try {} finally {}',
  1288. '<?php try {} finally
  1289. {}',
  1290. ];
  1291. yield [
  1292. '<?php try {} finally /* foo */{}',
  1293. '<?php try {} finally/* foo */{}',
  1294. ];
  1295. yield [
  1296. '<?php try {} finally /* foo */{}',
  1297. '<?php try {} finally /* foo */{}',
  1298. ];
  1299. }
  1300. /**
  1301. * @dataProvider provideFixWithForCases
  1302. */
  1303. public function testFixWithFor(string $expected, ?string $input = null): void
  1304. {
  1305. $this->fixer->configure([
  1306. 'constructs_followed_by_a_single_space' => [
  1307. 'for',
  1308. ],
  1309. ]);
  1310. $this->doTest($expected, $input);
  1311. }
  1312. /**
  1313. * @return iterable<array{string, string}>
  1314. */
  1315. public static function provideFixWithForCases(): iterable
  1316. {
  1317. yield [
  1318. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1319. '<?php for($i = 0; $i < 3; ++$i) {}',
  1320. ];
  1321. yield [
  1322. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1323. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1324. ];
  1325. yield [
  1326. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1327. '<?php for
  1328. ($i = 0; $i < 3; ++$i) {}',
  1329. ];
  1330. yield [
  1331. '<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
  1332. '<?php for/* foo */($i = 0; $i < 3; ++$i) {}',
  1333. ];
  1334. yield [
  1335. '<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
  1336. '<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
  1337. ];
  1338. }
  1339. /**
  1340. * @dataProvider provideFixWithForeachCases
  1341. */
  1342. public function testFixWithForeach(string $expected, ?string $input = null): void
  1343. {
  1344. $this->fixer->configure([
  1345. 'constructs_followed_by_a_single_space' => [
  1346. 'foreach',
  1347. ],
  1348. ]);
  1349. $this->doTest($expected, $input);
  1350. }
  1351. /**
  1352. * @return iterable<array{string, string}>
  1353. */
  1354. public static function provideFixWithForeachCases(): iterable
  1355. {
  1356. yield [
  1357. '<?php foreach ($foo as $bar) {}',
  1358. '<?php foreach($foo as $bar) {}',
  1359. ];
  1360. yield [
  1361. '<?php foreach ($foo as $bar) {}',
  1362. '<?php foreach ($foo as $bar) {}',
  1363. ];
  1364. yield [
  1365. '<?php foreach ($foo as $bar) {}',
  1366. '<?php foreach
  1367. ($foo as $bar) {}',
  1368. ];
  1369. yield [
  1370. '<?php foreach /* foo */($foo as $bar) {}',
  1371. '<?php foreach/* foo */($foo as $bar) {}',
  1372. ];
  1373. yield [
  1374. '<?php foreach /* foo */($foo as $bar) {}',
  1375. '<?php foreach /* foo */($foo as $bar) {}',
  1376. ];
  1377. }
  1378. /**
  1379. * @dataProvider provideFixWithFunctionCases
  1380. */
  1381. public function testFixWithFunction(string $expected, ?string $input = null): void
  1382. {
  1383. $this->fixer->configure([
  1384. 'constructs_followed_by_a_single_space' => [
  1385. 'function',
  1386. ],
  1387. ]);
  1388. $this->doTest($expected, $input);
  1389. }
  1390. /**
  1391. * @return iterable<array{string, string}>
  1392. */
  1393. public static function provideFixWithFunctionCases(): iterable
  1394. {
  1395. yield [
  1396. '<?php function foo() {}',
  1397. '<?php function foo() {}',
  1398. ];
  1399. yield [
  1400. '<?php function foo() {}',
  1401. '<?php function
  1402. foo() {}',
  1403. ];
  1404. yield [
  1405. '<?php function /* foo */foo() {}',
  1406. '<?php function/* foo */foo() {}',
  1407. ];
  1408. yield [
  1409. '<?php function /* foo */foo() {}',
  1410. '<?php function /* foo */foo() {}',
  1411. ];
  1412. yield [
  1413. '<?php
  1414. class Foo
  1415. {
  1416. function bar() {}
  1417. }
  1418. ',
  1419. '<?php
  1420. class Foo
  1421. {
  1422. function bar() {}
  1423. }
  1424. ',
  1425. ];
  1426. yield [
  1427. '<?php
  1428. class Foo
  1429. {
  1430. function bar() {}
  1431. }
  1432. ',
  1433. '<?php
  1434. class Foo
  1435. {
  1436. function
  1437. bar() {}
  1438. }
  1439. ',
  1440. ];
  1441. yield [
  1442. '<?php
  1443. class Foo
  1444. {
  1445. function /* foo */bar() {}
  1446. }
  1447. ',
  1448. '<?php
  1449. class Foo
  1450. {
  1451. function/* foo */bar() {}
  1452. }
  1453. ',
  1454. ];
  1455. yield [
  1456. '<?php
  1457. class Foo
  1458. {
  1459. function /* foo */bar() {}
  1460. }
  1461. ',
  1462. '<?php
  1463. class Foo
  1464. {
  1465. function /* foo */bar() {}
  1466. }
  1467. ',
  1468. ];
  1469. }
  1470. /**
  1471. * @dataProvider provideFixWithFunctionImportCases
  1472. */
  1473. public function testFixWithFunctionImport(string $expected, ?string $input = null): void
  1474. {
  1475. $this->fixer->configure([
  1476. 'constructs_followed_by_a_single_space' => [
  1477. 'function_import',
  1478. ],
  1479. ]);
  1480. $this->doTest($expected, $input);
  1481. }
  1482. /**
  1483. * @return iterable<array{string, string}>
  1484. */
  1485. public static function provideFixWithFunctionImportCases(): iterable
  1486. {
  1487. yield [
  1488. '<?php use function Foo\bar;',
  1489. '<?php use function Foo\bar;',
  1490. ];
  1491. yield [
  1492. '<?php use function Foo\bar;',
  1493. '<?php use function
  1494. Foo\bar;',
  1495. ];
  1496. yield [
  1497. '<?php use function /* foo */Foo\bar;',
  1498. '<?php use function/* foo */Foo\bar;',
  1499. ];
  1500. yield [
  1501. '<?php use function /* foo */Foo\bar;',
  1502. '<?php use function /* foo */Foo\bar;',
  1503. ];
  1504. }
  1505. /**
  1506. * @dataProvider provideFixWithGlobalCases
  1507. */
  1508. public function testFixWithGlobal(string $expected, ?string $input = null): void
  1509. {
  1510. $this->fixer->configure([
  1511. 'constructs_followed_by_a_single_space' => [
  1512. 'global',
  1513. ],
  1514. ]);
  1515. $this->doTest($expected, $input);
  1516. }
  1517. /**
  1518. * @return iterable<array{string, string}>
  1519. */
  1520. public static function provideFixWithGlobalCases(): iterable
  1521. {
  1522. yield [
  1523. '<?php function foo() { global $bar; }',
  1524. '<?php function foo() { global$bar; }',
  1525. ];
  1526. yield [
  1527. '<?php function foo() { global $bar; }',
  1528. '<?php function foo() { global $bar; }',
  1529. ];
  1530. yield [
  1531. '<?php function foo() { global $bar; }',
  1532. '<?php function foo() { global
  1533. $bar; }',
  1534. ];
  1535. yield [
  1536. '<?php function foo() { global /* foo */$bar; }',
  1537. '<?php function foo() { global/* foo */$bar; }',
  1538. ];
  1539. yield [
  1540. '<?php function foo() { global /* foo */$bar; }',
  1541. '<?php function foo() { global /* foo */$bar; }',
  1542. ];
  1543. }
  1544. /**
  1545. * @dataProvider provideFixWithGotoCases
  1546. */
  1547. public function testFixWithGoto(string $expected, ?string $input = null): void
  1548. {
  1549. $this->fixer->configure([
  1550. 'constructs_followed_by_a_single_space' => [
  1551. 'goto',
  1552. ],
  1553. ]);
  1554. $this->doTest($expected, $input);
  1555. }
  1556. /**
  1557. * @return iterable<array{string, string}>
  1558. */
  1559. public static function provideFixWithGotoCases(): iterable
  1560. {
  1561. yield [
  1562. '<?php goto foo; foo: echo "Bar";',
  1563. '<?php goto foo; foo: echo "Bar";',
  1564. ];
  1565. yield [
  1566. '<?php goto foo; foo: echo "Bar";',
  1567. '<?php goto
  1568. foo; foo: echo "Bar";',
  1569. ];
  1570. yield [
  1571. '<?php goto /* foo */foo; foo: echo "Bar";',
  1572. '<?php goto/* foo */foo; foo: echo "Bar";',
  1573. ];
  1574. }
  1575. /**
  1576. * @dataProvider provideFixWithIfCases
  1577. */
  1578. public function testFixWithIf(string $expected, ?string $input = null): void
  1579. {
  1580. $this->fixer->configure([
  1581. 'constructs_followed_by_a_single_space' => [
  1582. 'if',
  1583. ],
  1584. ]);
  1585. $this->doTest($expected, $input);
  1586. }
  1587. /**
  1588. * @return iterable<array{string, string}>
  1589. */
  1590. public static function provideFixWithIfCases(): iterable
  1591. {
  1592. yield [
  1593. '<?php if ($foo === $bar) {}',
  1594. '<?php if($foo === $bar) {}',
  1595. ];
  1596. yield [
  1597. '<?php if ($foo === $bar) {}',
  1598. '<?php if ($foo === $bar) {}',
  1599. ];
  1600. yield [
  1601. '<?php if ($foo === $bar) {}',
  1602. '<?php if
  1603. ($foo === $bar) {}',
  1604. ];
  1605. yield [
  1606. '<?php if /* foo */($foo === $bar) {}',
  1607. '<?php if/* foo */($foo === $bar) {}',
  1608. ];
  1609. }
  1610. /**
  1611. * @dataProvider provideFixWithImplementsCases
  1612. */
  1613. public function testFixWithImplements(string $expected, ?string $input = null): void
  1614. {
  1615. $this->fixer->configure([
  1616. 'constructs_followed_by_a_single_space' => [
  1617. 'implements',
  1618. ],
  1619. ]);
  1620. $this->doTest($expected, $input);
  1621. }
  1622. /**
  1623. * @return iterable<array{0: string, 1?: string}>
  1624. */
  1625. public static function provideFixWithImplementsCases(): iterable
  1626. {
  1627. yield [
  1628. '<?php class Foo implements \Countable {}',
  1629. '<?php class Foo implements \Countable {}',
  1630. ];
  1631. yield [
  1632. '<?php class Foo implements \Countable {}',
  1633. '<?php class Foo implements
  1634. \Countable {}',
  1635. ];
  1636. yield [
  1637. '<?php class Foo implements /* foo */\Countable {}',
  1638. '<?php class Foo implements/* foo */\Countable {}',
  1639. ];
  1640. yield [
  1641. '<?php class Foo implements /* foo */\Countable {}',
  1642. '<?php class Foo implements /* foo */\Countable {}',
  1643. ];
  1644. yield [
  1645. '<?php class Foo implements
  1646. \Countable,
  1647. Bar,
  1648. Baz
  1649. {}',
  1650. ];
  1651. yield [
  1652. '<?php $foo = new class implements \Countable {};',
  1653. '<?php $foo = new class implements \Countable {};',
  1654. ];
  1655. yield [
  1656. '<?php $foo = new class implements \Countable {};',
  1657. '<?php $foo = new class implements
  1658. \Countable {};',
  1659. ];
  1660. yield [
  1661. '<?php $foo = new class implements /* foo */\Countable {};',
  1662. '<?php $foo = new class implements/* foo */\Countable {};',
  1663. ];
  1664. yield [
  1665. '<?php $foo = new class implements /* foo */\Countable {};',
  1666. '<?php $foo = new class implements /* foo */\Countable {};',
  1667. ];
  1668. }
  1669. /**
  1670. * @dataProvider provideFixWithIncludeCases
  1671. */
  1672. public function testFixWithInclude(string $expected, ?string $input = null): void
  1673. {
  1674. $this->fixer->configure([
  1675. 'constructs_followed_by_a_single_space' => [
  1676. 'include',
  1677. ],
  1678. ]);
  1679. $this->doTest($expected, $input);
  1680. }
  1681. /**
  1682. * @return iterable<array{string, string}>
  1683. */
  1684. public static function provideFixWithIncludeCases(): iterable
  1685. {
  1686. yield [
  1687. '<?php include "vendor/autoload.php";',
  1688. '<?php include"vendor/autoload.php";',
  1689. ];
  1690. yield [
  1691. '<?php include "vendor/autoload.php";',
  1692. '<?php include "vendor/autoload.php";',
  1693. ];
  1694. yield [
  1695. '<?php include "vendor/autoload.php";',
  1696. '<?php include
  1697. "vendor/autoload.php";',
  1698. ];
  1699. yield [
  1700. '<?php include /* foo */"vendor/autoload.php";',
  1701. '<?php include/* foo */"vendor/autoload.php";',
  1702. ];
  1703. }
  1704. /**
  1705. * @dataProvider provideFixWithIncludeOnceCases
  1706. */
  1707. public function testFixWithIncludeOnce(string $expected, ?string $input = null): void
  1708. {
  1709. $this->fixer->configure([
  1710. 'constructs_followed_by_a_single_space' => [
  1711. 'include_once',
  1712. ],
  1713. ]);
  1714. $this->doTest($expected, $input);
  1715. }
  1716. /**
  1717. * @return iterable<array{string, string}>
  1718. */
  1719. public static function provideFixWithIncludeOnceCases(): iterable
  1720. {
  1721. yield [
  1722. '<?php include_once "vendor/autoload.php";',
  1723. '<?php include_once"vendor/autoload.php";',
  1724. ];
  1725. yield [
  1726. '<?php include_once "vendor/autoload.php";',
  1727. '<?php include_once "vendor/autoload.php";',
  1728. ];
  1729. yield [
  1730. '<?php include_once "vendor/autoload.php";',
  1731. '<?php include_once
  1732. "vendor/autoload.php";',
  1733. ];
  1734. yield [
  1735. '<?php include_once /* foo */"vendor/autoload.php";',
  1736. '<?php include_once/* foo */"vendor/autoload.php";',
  1737. ];
  1738. }
  1739. /**
  1740. * @dataProvider provideFixWithInstanceofCases
  1741. */
  1742. public function testFixWithInstanceof(string $expected, ?string $input = null): void
  1743. {
  1744. $this->fixer->configure([
  1745. 'constructs_followed_by_a_single_space' => [
  1746. 'instanceof',
  1747. ],
  1748. ]);
  1749. $this->doTest($expected, $input);
  1750. }
  1751. /**
  1752. * @return iterable<array{string, string}>
  1753. */
  1754. public static function provideFixWithInstanceofCases(): iterable
  1755. {
  1756. yield [
  1757. '<?php $foo instanceof \stdClass;',
  1758. '<?php $foo instanceof \stdClass;',
  1759. ];
  1760. yield [
  1761. '<?php $foo instanceof \stdClass;',
  1762. '<?php $foo instanceof
  1763. \stdClass;',
  1764. ];
  1765. yield [
  1766. '<?php $foo instanceof /* foo */\stdClass;',
  1767. '<?php $foo instanceof/* foo */\stdClass;',
  1768. ];
  1769. yield [
  1770. '<?php $foo instanceof /* foo */\stdClass;',
  1771. '<?php $foo instanceof /* foo */\stdClass;',
  1772. ];
  1773. yield [
  1774. '<?php $foo instanceof $bar;',
  1775. '<?php $foo instanceof$bar;',
  1776. ];
  1777. }
  1778. /**
  1779. * @dataProvider provideFixWithInsteadofCases
  1780. */
  1781. public function testFixWithInsteadof(string $expected, ?string $input = null): void
  1782. {
  1783. $this->fixer->configure([
  1784. 'constructs_followed_by_a_single_space' => [
  1785. 'insteadof',
  1786. ],
  1787. ]);
  1788. $this->doTest($expected, $input);
  1789. }
  1790. /**
  1791. * @return iterable<array{string, string}>
  1792. */
  1793. public static function provideFixWithInsteadofCases(): iterable
  1794. {
  1795. yield [
  1796. '<?php
  1797. class Talker {
  1798. use A, B {
  1799. B::smallTalk insteadof A;
  1800. A::bigTalk insteadof B;
  1801. }
  1802. }',
  1803. '<?php
  1804. class Talker {
  1805. use A, B {
  1806. B::smallTalk insteadof A;
  1807. A::bigTalk insteadof B;
  1808. }
  1809. }',
  1810. ];
  1811. yield [
  1812. '<?php
  1813. class Talker {
  1814. use A, B {
  1815. B::smallTalk insteadof A;
  1816. A::bigTalk insteadof B;
  1817. }
  1818. }',
  1819. '<?php
  1820. class Talker {
  1821. use A, B {
  1822. B::smallTalk insteadof
  1823. A;
  1824. A::bigTalk insteadof B;
  1825. }
  1826. }',
  1827. ];
  1828. yield [
  1829. '<?php
  1830. class Talker {
  1831. use A, B {
  1832. B::smallTalk insteadof /* foo */A;
  1833. A::bigTalk insteadof B;
  1834. }
  1835. }',
  1836. '<?php
  1837. class Talker {
  1838. use A, B {
  1839. B::smallTalk insteadof/* foo */A;
  1840. A::bigTalk insteadof B;
  1841. }
  1842. }',
  1843. ];
  1844. yield [
  1845. '<?php
  1846. class Talker {
  1847. use A, B {
  1848. B::smallTalk insteadof /* foo */A;
  1849. A::bigTalk insteadof B;
  1850. }
  1851. }',
  1852. '<?php
  1853. class Talker {
  1854. use A, B {
  1855. B::smallTalk insteadof /* foo */A;
  1856. A::bigTalk insteadof B;
  1857. }
  1858. }',
  1859. ];
  1860. }
  1861. /**
  1862. * @dataProvider provideFixWithInterfaceCases
  1863. */
  1864. public function testFixWithInterface(string $expected, ?string $input = null): void
  1865. {
  1866. $this->fixer->configure([
  1867. 'constructs_followed_by_a_single_space' => [
  1868. 'interface',
  1869. ],
  1870. ]);
  1871. $this->doTest($expected, $input);
  1872. }
  1873. /**
  1874. * @return iterable<array{string, string}>
  1875. */
  1876. public static function provideFixWithInterfaceCases(): iterable
  1877. {
  1878. yield [
  1879. '<?php interface Foo {}',
  1880. '<?php interface Foo {}',
  1881. ];
  1882. yield [
  1883. '<?php interface Foo {}',
  1884. '<?php interface
  1885. Foo {}',
  1886. ];
  1887. yield [
  1888. '<?php interface /* foo */Foo {}',
  1889. '<?php interface /* foo */Foo {}',
  1890. ];
  1891. yield [
  1892. '<?php interface /* foo */Foo {}',
  1893. '<?php interface/* foo */Foo {}',
  1894. ];
  1895. }
  1896. /**
  1897. * @dataProvider provideFixWithNewCases
  1898. */
  1899. public function testFixWithNew(string $expected, ?string $input = null): void
  1900. {
  1901. $this->fixer->configure([
  1902. 'constructs_followed_by_a_single_space' => [
  1903. 'new',
  1904. ],
  1905. ]);
  1906. $this->doTest($expected, $input);
  1907. }
  1908. /**
  1909. * @return iterable<int|string, array{0: string, 1?: string}>
  1910. */
  1911. public static function provideFixWithNewCases(): iterable
  1912. {
  1913. yield [
  1914. '<?php new $foo();',
  1915. '<?php new$foo();',
  1916. ];
  1917. yield [
  1918. '<?php new Bar();',
  1919. '<?php new Bar();',
  1920. ];
  1921. yield [
  1922. '<?php new Bar();',
  1923. '<?php new
  1924. Bar();',
  1925. ];
  1926. yield [
  1927. '<?php new /* foo */Bar();',
  1928. '<?php new/* foo */Bar();',
  1929. ];
  1930. yield 'attribute on separate line' => [
  1931. <<<'EOF'
  1932. <?php
  1933. $a = new
  1934. #[FOO]
  1935. class() {};
  1936. EOF,
  1937. ];
  1938. yield 'phpdoc on separate line' => [
  1939. <<<'EOF'
  1940. <?php
  1941. $a = new
  1942. /**
  1943. @property string $x
  1944. */
  1945. class() {};
  1946. EOF,
  1947. ];
  1948. }
  1949. /**
  1950. * @dataProvider provideFixWithOpenTagWithEchoCases
  1951. */
  1952. public function testFixWithOpenTagWithEcho(string $expected, ?string $input = null): void
  1953. {
  1954. $this->fixer->configure([
  1955. 'constructs_followed_by_a_single_space' => [
  1956. 'open_tag_with_echo',
  1957. ],
  1958. ]);
  1959. $this->doTest($expected, $input);
  1960. }
  1961. /**
  1962. * @return iterable<array{string, string}>
  1963. */
  1964. public static function provideFixWithOpenTagWithEchoCases(): iterable
  1965. {
  1966. yield [
  1967. '<?= $foo ?>',
  1968. '<?=$foo ?>',
  1969. ];
  1970. yield [
  1971. '<?= $foo ?>',
  1972. '<?= $foo ?>',
  1973. ];
  1974. yield [
  1975. '<?= $foo ?>',
  1976. '<?=
  1977. $foo ?>',
  1978. ];
  1979. yield [
  1980. '<?= /* foo */$foo ?>',
  1981. '<?=/* foo */$foo ?>',
  1982. ];
  1983. yield [
  1984. '<?= /* foo */$foo ?>',
  1985. '<?= /* foo */$foo ?>',
  1986. ];
  1987. }
  1988. /**
  1989. * @dataProvider provideFixWithPrintCases
  1990. */
  1991. public function testFixWithPrint(string $expected, ?string $input = null): void
  1992. {
  1993. $this->fixer->configure([
  1994. 'constructs_followed_by_a_single_space' => [
  1995. 'print',
  1996. ],
  1997. ]);
  1998. $this->doTest($expected, $input);
  1999. }
  2000. /**
  2001. * @return iterable<array{string, string}>
  2002. */
  2003. public static function provideFixWithPrintCases(): iterable
  2004. {
  2005. yield [
  2006. '<?php print $foo;',
  2007. '<?php print$foo;',
  2008. ];
  2009. yield [
  2010. '<?php print 9000;',
  2011. '<?php print 9000;',
  2012. ];
  2013. yield [
  2014. '<?php print 9000;',
  2015. '<?php print
  2016. 9000;',
  2017. ];
  2018. yield [
  2019. '<?php print /* foo */9000;',
  2020. '<?php print/* foo */9000;',
  2021. ];
  2022. }
  2023. /**
  2024. * @dataProvider provideFixWithPrivateCases
  2025. */
  2026. public function testFixWithPrivate(string $expected, ?string $input = null): void
  2027. {
  2028. $this->fixer->configure([
  2029. 'constructs_followed_by_a_single_space' => [
  2030. 'private',
  2031. ],
  2032. ]);
  2033. $this->doTest($expected, $input);
  2034. }
  2035. /**
  2036. * @return iterable<array{string, string}>
  2037. */
  2038. public static function provideFixWithPrivateCases(): iterable
  2039. {
  2040. yield [
  2041. '<?php class Foo { private $bar; }',
  2042. '<?php class Foo { private$bar; }',
  2043. ];
  2044. yield [
  2045. '<?php class Foo { private $bar; }',
  2046. '<?php class Foo { private $bar; }',
  2047. ];
  2048. yield [
  2049. '<?php class Foo { private $bar; }',
  2050. '<?php class Foo { private
  2051. $bar; }',
  2052. ];
  2053. yield [
  2054. '<?php class Foo { private /* foo */$bar; }',
  2055. '<?php class Foo { private/* foo */$bar; }',
  2056. ];
  2057. yield [
  2058. '<?php class Foo { private /* foo */$bar; }',
  2059. '<?php class Foo { private /* foo */$bar; }',
  2060. ];
  2061. yield [
  2062. '<?php class Foo { private function bar() {} }',
  2063. '<?php class Foo { private function bar() {} }',
  2064. ];
  2065. yield [
  2066. '<?php class Foo { private function bar() {} }',
  2067. '<?php class Foo { private
  2068. function bar() {} }',
  2069. ];
  2070. yield [
  2071. '<?php class Foo { private /* foo */function bar() {} }',
  2072. '<?php class Foo { private/* foo */function bar() {} }',
  2073. ];
  2074. yield [
  2075. '<?php class Foo { private /* foo */function bar() {} }',
  2076. '<?php class Foo { private /* foo */function bar() {} }',
  2077. ];
  2078. yield [
  2079. '<?php class Foo { private CONST BAR = 9000; }',
  2080. '<?php class Foo { private CONST BAR = 9000; }',
  2081. ];
  2082. yield [
  2083. '<?php class Foo { private CONST BAR = 9000; }',
  2084. '<?php class Foo { private
  2085. CONST BAR = 9000; }',
  2086. ];
  2087. yield [
  2088. '<?php class Foo { private /* foo */CONST BAR = 9000; }',
  2089. '<?php class Foo { private/* foo */CONST BAR = 9000; }',
  2090. ];
  2091. yield [
  2092. '<?php class Foo { private /* foo */CONST BAR = 9000; }',
  2093. '<?php class Foo { private /* foo */CONST BAR = 9000; }',
  2094. ];
  2095. }
  2096. /**
  2097. * @dataProvider provideFixWithProtectedCases
  2098. */
  2099. public function testFixWithProtected(string $expected, ?string $input = null): void
  2100. {
  2101. $this->fixer->configure([
  2102. 'constructs_followed_by_a_single_space' => [
  2103. 'protected',
  2104. ],
  2105. ]);
  2106. $this->doTest($expected, $input);
  2107. }
  2108. /**
  2109. * @return iterable<array{string, string}>
  2110. */
  2111. public static function provideFixWithProtectedCases(): iterable
  2112. {
  2113. yield [
  2114. '<?php class Foo { protected $bar; }',
  2115. '<?php class Foo { protected$bar; }',
  2116. ];
  2117. yield [
  2118. '<?php class Foo { protected $bar; }',
  2119. '<?php class Foo { protected $bar; }',
  2120. ];
  2121. yield [
  2122. '<?php class Foo { protected $bar; }',
  2123. '<?php class Foo { protected
  2124. $bar; }',
  2125. ];
  2126. yield [
  2127. '<?php class Foo { protected /* foo */$bar; }',
  2128. '<?php class Foo { protected/* foo */$bar; }',
  2129. ];
  2130. yield [
  2131. '<?php class Foo { protected /* foo */$bar; }',
  2132. '<?php class Foo { protected /* foo */$bar; }',
  2133. ];
  2134. yield [
  2135. '<?php class Foo { protected function bar() {} }',
  2136. '<?php class Foo { protected function bar() {} }',
  2137. ];
  2138. yield [
  2139. '<?php class Foo { protected function bar() {} }',
  2140. '<?php class Foo { protected
  2141. function bar() {} }',
  2142. ];
  2143. yield [
  2144. '<?php class Foo { protected /* foo */function bar() {} }',
  2145. '<?php class Foo { protected/* foo */function bar() {} }',
  2146. ];
  2147. yield [
  2148. '<?php class Foo { protected /* foo */function bar() {} }',
  2149. '<?php class Foo { protected /* foo */function bar() {} }',
  2150. ];
  2151. yield [
  2152. '<?php class Foo { protected CONST BAR = 9000; }',
  2153. '<?php class Foo { protected CONST BAR = 9000; }',
  2154. ];
  2155. yield [
  2156. '<?php class Foo { protected CONST BAR = 9000; }',
  2157. '<?php class Foo { protected
  2158. CONST BAR = 9000; }',
  2159. ];
  2160. yield [
  2161. '<?php class Foo { protected /* foo */CONST BAR = 9000; }',
  2162. '<?php class Foo { protected/* foo */CONST BAR = 9000; }',
  2163. ];
  2164. yield [
  2165. '<?php class Foo { protected /* foo */CONST BAR = 9000; }',
  2166. '<?php class Foo { protected /* foo */CONST BAR = 9000; }',
  2167. ];
  2168. }
  2169. /**
  2170. * @dataProvider provideFixWithPublicCases
  2171. */
  2172. public function testFixWithPublic(string $expected, ?string $input = null): void
  2173. {
  2174. $this->fixer->configure([
  2175. 'constructs_followed_by_a_single_space' => [
  2176. 'public',
  2177. ],
  2178. ]);
  2179. $this->doTest($expected, $input);
  2180. }
  2181. /**
  2182. * @return iterable<array{string, string}>
  2183. */
  2184. public static function provideFixWithPublicCases(): iterable
  2185. {
  2186. yield [
  2187. '<?php class Foo { public $bar; }',
  2188. '<?php class Foo { public$bar; }',
  2189. ];
  2190. yield [
  2191. '<?php class Foo { Public $bar; }',
  2192. '<?php class Foo { Public $bar; }',
  2193. ];
  2194. yield [
  2195. '<?php class Foo { public $bar; }',
  2196. '<?php class Foo { public
  2197. $bar; }',
  2198. ];
  2199. yield [
  2200. '<?php class Foo { public /* foo */$bar; }',
  2201. '<?php class Foo { public/* foo */$bar; }',
  2202. ];
  2203. yield [
  2204. '<?php class Foo { public /* foo */$bar; }',
  2205. '<?php class Foo { public /* foo */$bar; }',
  2206. ];
  2207. yield [
  2208. '<?php class Foo { public function bar() {} }',
  2209. '<?php class Foo { public function bar() {} }',
  2210. ];
  2211. yield [
  2212. '<?php class Foo { public function bar() {} }',
  2213. '<?php class Foo { public
  2214. function bar() {} }',
  2215. ];
  2216. yield [
  2217. '<?php class Foo { public /* foo */function bar() {} }',
  2218. '<?php class Foo { public/* foo */function bar() {} }',
  2219. ];
  2220. yield [
  2221. '<?php class Foo { public /* foo */function bar() {} }',
  2222. '<?php class Foo { public /* foo */function bar() {} }',
  2223. ];
  2224. yield [
  2225. '<?php class Foo { public CONST BAR = 9000; }',
  2226. '<?php class Foo { public CONST BAR = 9000; }',
  2227. ];
  2228. yield [
  2229. '<?php class Foo { public CONST BAR = 9000; }',
  2230. '<?php class Foo { public
  2231. CONST BAR = 9000; }',
  2232. ];
  2233. yield [
  2234. '<?php class Foo { public /* foo */CONST BAR = 9000; }',
  2235. '<?php class Foo { public/* foo */CONST BAR = 9000; }',
  2236. ];
  2237. yield [
  2238. '<?php class Foo { public /* foo */CONST BAR = 9000; }',
  2239. '<?php class Foo { public /* foo */CONST BAR = 9000; }',
  2240. ];
  2241. }
  2242. /**
  2243. * @dataProvider provideFixWithRequireCases
  2244. */
  2245. public function testFixWithRequire(string $expected, ?string $input = null): void
  2246. {
  2247. $this->fixer->configure([
  2248. 'constructs_followed_by_a_single_space' => [
  2249. 'require',
  2250. ],
  2251. ]);
  2252. $this->doTest($expected, $input);
  2253. }
  2254. /**
  2255. * @return iterable<array{string, string}>
  2256. */
  2257. public static function provideFixWithRequireCases(): iterable
  2258. {
  2259. yield [
  2260. '<?php require "vendor/autoload.php";',
  2261. '<?php require"vendor/autoload.php";',
  2262. ];
  2263. yield [
  2264. '<?php require "vendor/autoload.php";',
  2265. '<?php require "vendor/autoload.php";',
  2266. ];
  2267. yield [
  2268. '<?php require "vendor/autoload.php";',
  2269. '<?php require
  2270. "vendor/autoload.php";',
  2271. ];
  2272. yield [
  2273. '<?php require /* foo */"vendor/autoload.php";',
  2274. '<?php require/* foo */"vendor/autoload.php";',
  2275. ];
  2276. }
  2277. /**
  2278. * @dataProvider provideFixWithRequireOnceCases
  2279. */
  2280. public function testFixWithRequireOnce(string $expected, ?string $input = null): void
  2281. {
  2282. $this->fixer->configure([
  2283. 'constructs_followed_by_a_single_space' => [
  2284. 'require_once',
  2285. ],
  2286. ]);
  2287. $this->doTest($expected, $input);
  2288. }
  2289. /**
  2290. * @return iterable<array{string, string}>
  2291. */
  2292. public static function provideFixWithRequireOnceCases(): iterable
  2293. {
  2294. yield [
  2295. '<?php require_once "vendor/autoload.php";',
  2296. '<?php require_once"vendor/autoload.php";',
  2297. ];
  2298. yield [
  2299. '<?php require_once "vendor/autoload.php";',
  2300. '<?php require_once "vendor/autoload.php";',
  2301. ];
  2302. yield [
  2303. '<?php require_once "vendor/autoload.php";',
  2304. '<?php require_once
  2305. "vendor/autoload.php";',
  2306. ];
  2307. yield [
  2308. '<?php require_once /* foo */"vendor/autoload.php";',
  2309. '<?php require_once/* foo */"vendor/autoload.php";',
  2310. ];
  2311. }
  2312. /**
  2313. * @dataProvider provideFixWithReturnCases
  2314. */
  2315. public function testFixWithReturn(string $expected, ?string $input = null): void
  2316. {
  2317. $this->fixer->configure([
  2318. 'constructs_followed_by_a_single_space' => [
  2319. 'return',
  2320. ],
  2321. ]);
  2322. $this->doTest($expected, $input);
  2323. }
  2324. /**
  2325. * @return iterable<array{0: string, 1?: string}>
  2326. */
  2327. public static function provideFixWithReturnCases(): iterable
  2328. {
  2329. yield [
  2330. '<?php return;',
  2331. ];
  2332. yield [
  2333. '<?php return /* foo */;',
  2334. '<?php return/* foo */;',
  2335. ];
  2336. yield [
  2337. '<?php return /* foo */;',
  2338. '<?php return /* foo */;',
  2339. ];
  2340. yield [
  2341. '<?php return $foo;',
  2342. '<?php return$foo;',
  2343. ];
  2344. yield [
  2345. '<?php return 9000;',
  2346. '<?php return 9000;',
  2347. ];
  2348. yield [
  2349. '<?php return 9000;',
  2350. '<?php return
  2351. 9000;',
  2352. ];
  2353. yield [
  2354. '<?php return /* */ 9000 + 1 /* foo */ ?>',
  2355. '<?php return
  2356. /* */ 9000 + 1 /* foo */ ?>',
  2357. ];
  2358. yield [
  2359. '<?php return /* foo */9000;',
  2360. '<?php return/* foo */9000;',
  2361. ];
  2362. yield [
  2363. '<?php return $foo && $bar || $baz;',
  2364. '<?php return
  2365. $foo && $bar || $baz;',
  2366. ];
  2367. yield [
  2368. '<?php
  2369. return
  2370. $foo
  2371. && $bar
  2372. || $baz;',
  2373. ];
  2374. yield [
  2375. '<?php
  2376. return
  2377. $foo &&
  2378. $bar ||
  2379. $baz;',
  2380. ];
  2381. yield [
  2382. '<?php
  2383. return
  2384. $foo
  2385. + $bar
  2386. - $baz;',
  2387. ];
  2388. yield [
  2389. '<?php
  2390. return
  2391. $foo +
  2392. $bar -
  2393. $baz;',
  2394. ];
  2395. yield [
  2396. '<?php
  2397. return
  2398. $foo ?
  2399. $bar :
  2400. $baz;',
  2401. ];
  2402. yield [
  2403. '<?php
  2404. return
  2405. $foo
  2406. ? $bar
  2407. : baz;',
  2408. ];
  2409. yield [
  2410. '<?php
  2411. return
  2412. $foo ?:
  2413. $bar;',
  2414. ];
  2415. yield [
  2416. '<?php
  2417. return
  2418. $foo
  2419. ?: $bar;',
  2420. ];
  2421. yield [
  2422. '<?php
  2423. return
  2424. $foo
  2425. ?: $bar?>',
  2426. ];
  2427. }
  2428. /**
  2429. * @dataProvider provideFixWithStaticCases
  2430. */
  2431. public function testFixWithStatic(string $expected, ?string $input = null): void
  2432. {
  2433. $this->fixer->configure([
  2434. 'constructs_followed_by_a_single_space' => [
  2435. 'static',
  2436. ],
  2437. ]);
  2438. $this->doTest($expected, $input);
  2439. }
  2440. /**
  2441. * @return iterable<array{0: string, 1?: string}>
  2442. */
  2443. public static function provideFixWithStaticCases(): iterable
  2444. {
  2445. yield [
  2446. '<?php function foo() { static $bar; }',
  2447. '<?php function foo() { static$bar; }',
  2448. ];
  2449. yield [
  2450. '<?php function foo() { static $bar; }',
  2451. '<?php function foo() { static $bar; }',
  2452. ];
  2453. yield [
  2454. '<?php function foo() { static $bar; }',
  2455. '<?php function foo() { static
  2456. $bar; }',
  2457. ];
  2458. yield [
  2459. '<?php function foo() { static /* foo */$bar; }',
  2460. '<?php function foo() { static/* foo */$bar; }',
  2461. ];
  2462. yield [
  2463. '<?php function foo() { static /* foo */$bar; }',
  2464. '<?php function foo() { static /* foo */$bar; }',
  2465. ];
  2466. yield [
  2467. '<?php class Foo { static function bar() {} }',
  2468. '<?php class Foo { static function bar() {} }',
  2469. ];
  2470. yield [
  2471. '<?php class Foo { static function bar() {} }',
  2472. '<?php class Foo { static
  2473. function bar() {} }',
  2474. ];
  2475. yield [
  2476. '<?php class Foo { static /* foo */function bar() {} }',
  2477. '<?php class Foo { static/* foo */function bar() {} }',
  2478. ];
  2479. yield [
  2480. '<?php class Foo { static /* foo */function bar() {} }',
  2481. '<?php class Foo { static /* foo */function bar() {} }',
  2482. ];
  2483. yield [
  2484. '<?php class Foo { public static ?int $x; }',
  2485. '<?php class Foo { public static?int $x; }',
  2486. ];
  2487. yield [
  2488. '<?php class Foo { public static ?int $x; }',
  2489. '<?php class Foo { public static ?int $x; }',
  2490. ];
  2491. yield [
  2492. '<?php class Foo { public static int $x; }',
  2493. '<?php class Foo { public static int $x; }',
  2494. ];
  2495. yield [
  2496. '<?php class Foo { public static \Closure $a; }',
  2497. '<?php class Foo { public static \Closure $a; }',
  2498. ];
  2499. yield [
  2500. '<?php class Foo { public static array $c; }',
  2501. '<?php class Foo { public static
  2502. array $c; }',
  2503. ];
  2504. yield [
  2505. '<?php $a = static fn(): bool => true;',
  2506. '<?php $a = static fn(): bool => true;',
  2507. ];
  2508. yield [
  2509. '<?php class Foo { function bar() { return new static(); } }',
  2510. ];
  2511. yield [
  2512. '<?php class Foo { function bar() { return static::class; } }',
  2513. ];
  2514. }
  2515. /**
  2516. * @dataProvider provideFixWithThrowCases
  2517. */
  2518. public function testFixWithThrow(string $expected, ?string $input = null): void
  2519. {
  2520. $this->fixer->configure([
  2521. 'constructs_followed_by_a_single_space' => [
  2522. 'throw',
  2523. ],
  2524. ]);
  2525. $this->doTest($expected, $input);
  2526. }
  2527. /**
  2528. * @return iterable<array{string, string}>
  2529. */
  2530. public static function provideFixWithThrowCases(): iterable
  2531. {
  2532. yield [
  2533. '<?php throw $foo;',
  2534. '<?php throw$foo;',
  2535. ];
  2536. yield [
  2537. '<?php throw new Exception();',
  2538. '<?php throw new Exception();',
  2539. ];
  2540. yield [
  2541. '<?php throw new Exception();',
  2542. '<?php throw
  2543. new Exception();',
  2544. ];
  2545. yield [
  2546. '<?php throw /* foo */new Exception();',
  2547. '<?php throw/* foo */new Exception();',
  2548. ];
  2549. }
  2550. /**
  2551. * @dataProvider provideFixWithTraitCases
  2552. */
  2553. public function testFixWithTrait(string $expected, ?string $input = null): void
  2554. {
  2555. $this->fixer->configure([
  2556. 'constructs_followed_by_a_single_space' => [
  2557. 'trait',
  2558. ],
  2559. ]);
  2560. $this->doTest($expected, $input);
  2561. }
  2562. /**
  2563. * @return iterable<array{string, string}>
  2564. */
  2565. public static function provideFixWithTraitCases(): iterable
  2566. {
  2567. yield [
  2568. '<?php trait Foo {}',
  2569. '<?php trait Foo {}',
  2570. ];
  2571. yield [
  2572. '<?php trait Foo {}',
  2573. '<?php trait
  2574. Foo {}',
  2575. ];
  2576. yield [
  2577. '<?php trait /* foo */Foo {}',
  2578. '<?php trait /* foo */Foo {}',
  2579. ];
  2580. yield [
  2581. '<?php trait /* foo */Foo {}',
  2582. '<?php trait/* foo */Foo {}',
  2583. ];
  2584. }
  2585. /**
  2586. * @dataProvider provideFixWithTryCases
  2587. */
  2588. public function testFixWithTry(string $expected, ?string $input = null): void
  2589. {
  2590. $this->fixer->configure([
  2591. 'constructs_followed_by_a_single_space' => [
  2592. 'try',
  2593. ],
  2594. ]);
  2595. $this->doTest($expected, $input);
  2596. }
  2597. /**
  2598. * @return iterable<array{string, string}>
  2599. */
  2600. public static function provideFixWithTryCases(): iterable
  2601. {
  2602. yield [
  2603. '<?php try {} catch (\Exception $exception) {}',
  2604. '<?php try{} catch (\Exception $exception) {}',
  2605. ];
  2606. yield [
  2607. '<?php try {} catch (\Exception $exception) {}',
  2608. '<?php try {} catch (\Exception $exception) {}',
  2609. ];
  2610. yield [
  2611. '<?php try {} catch (\Exception $exception) {}',
  2612. '<?php try
  2613. {} catch (\Exception $exception) {}',
  2614. ];
  2615. yield [
  2616. '<?php try /* foo */{} catch (\Exception $exception) {}',
  2617. '<?php try/* foo */{} catch (\Exception $exception) {}',
  2618. ];
  2619. yield [
  2620. '<?php try /* foo */{} catch (\Exception $exception) {}',
  2621. '<?php try /* foo */{} catch (\Exception $exception) {}',
  2622. ];
  2623. }
  2624. /**
  2625. * @dataProvider provideFixWithUseCases
  2626. */
  2627. public function testFixWithUse(string $expected, ?string $input = null): void
  2628. {
  2629. $this->fixer->configure([
  2630. 'constructs_followed_by_a_single_space' => [
  2631. 'use',
  2632. ],
  2633. ]);
  2634. $this->doTest($expected, $input);
  2635. }
  2636. /**
  2637. * @return iterable<array{string, string}>
  2638. */
  2639. public static function provideFixWithUseCases(): iterable
  2640. {
  2641. yield [
  2642. '<?php use Foo\Bar;',
  2643. '<?php use Foo\Bar;',
  2644. ];
  2645. yield [
  2646. '<?php use Foo\Bar;',
  2647. '<?php use
  2648. Foo\Bar;',
  2649. ];
  2650. yield [
  2651. '<?php use /* foo */Foo\Bar;',
  2652. '<?php use/* foo */Foo\Bar;',
  2653. ];
  2654. yield [
  2655. '<?php use /* foo */Foo\Bar;',
  2656. '<?php use /* foo */Foo\Bar;',
  2657. ];
  2658. yield [
  2659. '<?php use const Foo\BAR;',
  2660. '<?php use const Foo\BAR;',
  2661. ];
  2662. yield [
  2663. '<?php use const Foo\BAR;',
  2664. '<?php use
  2665. const Foo\BAR;',
  2666. ];
  2667. yield [
  2668. '<?php use /* foo */const Foo\BAR;',
  2669. '<?php use/* foo */const Foo\BAR;',
  2670. ];
  2671. yield [
  2672. '<?php use function Foo\bar;',
  2673. '<?php use function Foo\bar;',
  2674. ];
  2675. yield [
  2676. '<?php use function Foo\bar;',
  2677. '<?php use
  2678. function Foo\bar;',
  2679. ];
  2680. yield [
  2681. '<?php use /* foo */function Foo\bar;',
  2682. '<?php use/* foo */function Foo\bar;',
  2683. ];
  2684. }
  2685. /**
  2686. * @dataProvider provideFixWithUseLambdaCases
  2687. *
  2688. * @param _AutogeneratedInputConfiguration $configuration
  2689. */
  2690. public function testFixWithUseLambda(string $expected, ?string $input = null, ?array $configuration = null): void
  2691. {
  2692. $this->fixer->configure($configuration);
  2693. $this->doTest($expected, $input);
  2694. }
  2695. public static function provideFixWithUseLambdaCases(): iterable
  2696. {
  2697. yield [
  2698. '<?php $foo = function () use($bar) {};',
  2699. '<?php $foo = function ()use($bar) {};',
  2700. [
  2701. 'constructs_preceded_by_a_single_space' => ['use_lambda'],
  2702. 'constructs_followed_by_a_single_space' => [],
  2703. ],
  2704. ];
  2705. yield [
  2706. '<?php $foo = function ()use ($bar) {};',
  2707. '<?php $foo = function ()use($bar) {};',
  2708. [
  2709. 'constructs_preceded_by_a_single_space' => [],
  2710. 'constructs_followed_by_a_single_space' => ['use_lambda'],
  2711. ],
  2712. ];
  2713. yield [
  2714. '<?php $foo = function () use ($bar) {};',
  2715. '<?php $foo = function ()use($bar) {};',
  2716. [
  2717. 'constructs_preceded_by_a_single_space' => ['use_lambda'],
  2718. 'constructs_followed_by_a_single_space' => ['use_lambda'],
  2719. ],
  2720. ];
  2721. yield [
  2722. '<?php $foo = function () use ($bar) {};',
  2723. '<?php $foo = function () use ($bar) {};',
  2724. [
  2725. 'constructs_preceded_by_a_single_space' => [],
  2726. 'constructs_followed_by_a_single_space' => ['use_lambda'],
  2727. ],
  2728. ];
  2729. yield [
  2730. '<?php $foo = function () use ($bar) {};',
  2731. '<?php $foo = function () use
  2732. ($bar) {};',
  2733. [
  2734. 'constructs_preceded_by_a_single_space' => [],
  2735. 'constructs_followed_by_a_single_space' => ['use_lambda'],
  2736. ],
  2737. ];
  2738. yield [
  2739. '<?php $foo = function () use /* foo */($bar) {};',
  2740. '<?php $foo = function () use/* foo */($bar) {};',
  2741. [
  2742. 'constructs_preceded_by_a_single_space' => [],
  2743. 'constructs_followed_by_a_single_space' => ['use_lambda'],
  2744. ],
  2745. ];
  2746. yield [
  2747. '<?php $foo = function () use /* foo */($bar) {};',
  2748. '<?php $foo = function () use /* foo */($bar) {};',
  2749. [
  2750. 'constructs_preceded_by_a_single_space' => [],
  2751. 'constructs_followed_by_a_single_space' => ['use_lambda'],
  2752. ],
  2753. ];
  2754. }
  2755. /**
  2756. * @dataProvider provideFixWithUseTraitCases
  2757. */
  2758. public function testFixWithUseTrait(string $expected, ?string $input = null): void
  2759. {
  2760. $this->fixer->configure([
  2761. 'constructs_followed_by_a_single_space' => [
  2762. 'use_trait',
  2763. ],
  2764. ]);
  2765. $this->doTest($expected, $input);
  2766. }
  2767. /**
  2768. * @return iterable<array{string, string}>
  2769. */
  2770. public static function provideFixWithUseTraitCases(): iterable
  2771. {
  2772. yield [
  2773. '<?php class Foo { use Bar; }',
  2774. '<?php class Foo { use Bar; }',
  2775. ];
  2776. yield [
  2777. '<?php class Foo { use Bar; }',
  2778. '<?php class Foo { use
  2779. Bar; }',
  2780. ];
  2781. yield [
  2782. '<?php class Foo { use /* foo */Bar; }',
  2783. '<?php class Foo { use/* foo */Bar; }',
  2784. ];
  2785. yield [
  2786. '<?php class Foo { use /* foo */Bar; }',
  2787. '<?php class Foo { use /* foo */Bar; }',
  2788. ];
  2789. }
  2790. /**
  2791. * @dataProvider provideFixWithVarCases
  2792. */
  2793. public function testFixWithVar(string $expected, ?string $input = null): void
  2794. {
  2795. $this->fixer->configure([
  2796. 'constructs_followed_by_a_single_space' => [
  2797. 'var',
  2798. ],
  2799. ]);
  2800. $this->doTest($expected, $input);
  2801. }
  2802. /**
  2803. * @return iterable<array{string, string}>
  2804. */
  2805. public static function provideFixWithVarCases(): iterable
  2806. {
  2807. yield [
  2808. '<?php class Foo { var $bar; }',
  2809. '<?php class Foo { var$bar; }',
  2810. ];
  2811. yield [
  2812. '<?php class Foo { var $bar; }',
  2813. '<?php class Foo { var $bar; }',
  2814. ];
  2815. yield [
  2816. '<?php class Foo { var $bar; }',
  2817. '<?php class Foo { var
  2818. $bar; }',
  2819. ];
  2820. yield [
  2821. '<?php class Foo { var /* foo */$bar; }',
  2822. '<?php class Foo { var/* foo */$bar; }',
  2823. ];
  2824. yield [
  2825. '<?php class Foo { var /* foo */$bar; }',
  2826. '<?php class Foo { var /* foo */$bar; }',
  2827. ];
  2828. }
  2829. /**
  2830. * @dataProvider provideFixWithWhileCases
  2831. */
  2832. public function testFixWithWhile(string $expected, ?string $input = null): void
  2833. {
  2834. $this->fixer->configure([
  2835. 'constructs_followed_by_a_single_space' => [
  2836. 'while',
  2837. ],
  2838. ]);
  2839. $this->doTest($expected, $input);
  2840. }
  2841. /**
  2842. * @return iterable<array{string, string}>
  2843. */
  2844. public static function provideFixWithWhileCases(): iterable
  2845. {
  2846. yield [
  2847. '<?php do {} while (true);',
  2848. '<?php do {} while(true);',
  2849. ];
  2850. yield [
  2851. '<?php do {} while (true);',
  2852. '<?php do {} while (true);',
  2853. ];
  2854. yield [
  2855. '<?php do {} while (true);',
  2856. '<?php do {} while
  2857. (true);',
  2858. ];
  2859. yield [
  2860. '<?php do {} while /* foo */(true);',
  2861. '<?php do {} while/* foo */(true);',
  2862. ];
  2863. yield [
  2864. '<?php do {} while /* foo */(true);',
  2865. '<?php do {} while /* foo */(true);',
  2866. ];
  2867. }
  2868. /**
  2869. * @dataProvider provideFixWithYieldCases
  2870. */
  2871. public function testFixWithYield(string $expected, ?string $input = null): void
  2872. {
  2873. $this->fixer->configure([
  2874. 'constructs_followed_by_a_single_space' => [
  2875. 'yield',
  2876. ],
  2877. ]);
  2878. $this->doTest($expected, $input);
  2879. }
  2880. /**
  2881. * @return iterable<array{string, string}>
  2882. */
  2883. public static function provideFixWithYieldCases(): iterable
  2884. {
  2885. yield [
  2886. '<?php function foo() { yield $foo; }',
  2887. '<?php function foo() { yield$foo; }',
  2888. ];
  2889. yield [
  2890. '<?php function foo() { yield "Foo"; }',
  2891. '<?php function foo() { yield "Foo"; }',
  2892. ];
  2893. yield [
  2894. '<?php function foo() { yield "Foo"; }',
  2895. '<?php function foo() { yield
  2896. "Foo"; }',
  2897. ];
  2898. yield [
  2899. '<?php function foo() { yield /* foo */"Foo"; }',
  2900. '<?php function foo() { yield/* foo */"Foo"; }',
  2901. ];
  2902. }
  2903. /**
  2904. * @dataProvider provideFixWithYieldFromCases
  2905. *
  2906. * @param _AutogeneratedInputConfiguration $configuration
  2907. */
  2908. public function testFixWithYieldFrom(string $expected, ?string $input = null, ?array $configuration = []): void
  2909. {
  2910. $this->fixer->configure($configuration);
  2911. $this->doTest($expected, $input);
  2912. }
  2913. public static function provideFixWithYieldFromCases(): iterable
  2914. {
  2915. $configFollowed = [
  2916. 'constructs_contain_a_single_space' => [
  2917. ],
  2918. 'constructs_followed_by_a_single_space' => [
  2919. 'yield_from',
  2920. ],
  2921. ];
  2922. $configContain = [
  2923. 'constructs_contain_a_single_space' => [
  2924. 'yield_from',
  2925. ],
  2926. 'constructs_followed_by_a_single_space' => [
  2927. ],
  2928. ];
  2929. $configAll = [
  2930. 'constructs_contain_a_single_space' => [
  2931. 'yield_from',
  2932. ],
  2933. 'constructs_followed_by_a_single_space' => [
  2934. 'yield_from',
  2935. ],
  2936. ];
  2937. yield [
  2938. '<?php function foo() { yield from $foo; }',
  2939. '<?php function foo() { yield from$foo; }',
  2940. $configFollowed,
  2941. ];
  2942. yield [
  2943. '<?php function foo() { yield from baz(); }',
  2944. '<?php function foo() { yield from baz(); }',
  2945. $configFollowed,
  2946. ];
  2947. yield [
  2948. '<?php function foo() { yIeLd fRoM baz(); }',
  2949. '<?php function foo() { yIeLd fRoM baz(); }',
  2950. $configFollowed,
  2951. ];
  2952. yield [
  2953. '<?php function foo() { yield from baz(); }',
  2954. '<?php function foo() { yield from
  2955. baz(); }',
  2956. $configFollowed,
  2957. ];
  2958. yield [
  2959. '<?php function foo() { yield from /* foo */baz(); }',
  2960. '<?php function foo() { yield from/* foo */baz(); }',
  2961. $configFollowed,
  2962. ];
  2963. yield [
  2964. '<?php function foo() { yield from /* foo */baz(); }',
  2965. '<?php function foo() { yield from /* foo */baz(); }',
  2966. $configFollowed,
  2967. ];
  2968. yield [
  2969. '<?php function foo() { yield from /* foo */baz(); }',
  2970. '<?php function foo() { yield from
  2971. /* foo */baz(); }',
  2972. $configFollowed,
  2973. ];
  2974. yield [
  2975. '<?php function foo() { yield from baz(); }',
  2976. '<?php function foo() { yield from baz(); }',
  2977. $configFollowed,
  2978. ];
  2979. yield [
  2980. '<?php function foo() { yield from baz(); }',
  2981. '<?php function foo() { yield from baz(); }',
  2982. $configContain,
  2983. ];
  2984. yield [
  2985. '<?php function foo() { yield from baz(); }',
  2986. '<?php function foo() { yield from baz(); }',
  2987. $configAll,
  2988. ];
  2989. yield [
  2990. '<?php function foo() { yIeLd fRoM baz(); }',
  2991. '<?php function foo() { yIeLd fRoM baz(); }',
  2992. $configAll,
  2993. ];
  2994. yield [
  2995. '<?php function foo() { yield from baz(); }',
  2996. '<?php function foo() { yield
  2997. from baz(); }',
  2998. $configContain,
  2999. ];
  3000. yield [
  3001. '<?php function foo() { yield from baz(); }',
  3002. '<?php function foo() { yield
  3003. from
  3004. baz(); }',
  3005. $configAll,
  3006. ];
  3007. }
  3008. /**
  3009. * @dataProvider provideFixWithPhpOpenCases
  3010. */
  3011. public function testFixWithPhpOpen(string $expected, ?string $input = null): void
  3012. {
  3013. $this->fixer->configure([
  3014. 'constructs_followed_by_a_single_space' => [
  3015. 'php_open',
  3016. ],
  3017. ]);
  3018. $this->doTest($expected, $input);
  3019. }
  3020. /**
  3021. * @return iterable<array{0: string, 1?: string}>
  3022. */
  3023. public static function provideFixWithPhpOpenCases(): iterable
  3024. {
  3025. yield [
  3026. '<?php echo 1;',
  3027. '<?php echo 1;',
  3028. ];
  3029. yield [
  3030. "<?php\necho 1;",
  3031. ];
  3032. yield [
  3033. "<?php\n echo 1;",
  3034. ];
  3035. yield [
  3036. '<?php ',
  3037. ];
  3038. yield [
  3039. "<?php\n",
  3040. ];
  3041. yield [
  3042. "<?php \necho 1;",
  3043. ];
  3044. yield [
  3045. "<?php \n\necho 1;",
  3046. ];
  3047. }
  3048. /**
  3049. * @dataProvider provideCommentsCases
  3050. */
  3051. public function testComments(string $expected, ?string $input = null): void
  3052. {
  3053. $this->fixer->configure([
  3054. 'constructs_followed_by_a_single_space' => [
  3055. 'comment',
  3056. 'php_doc',
  3057. ],
  3058. ]);
  3059. $this->doTest($expected, $input);
  3060. }
  3061. /**
  3062. * @return iterable<int|string, array{0: string, 1?: string}>
  3063. */
  3064. public static function provideCommentsCases(): iterable
  3065. {
  3066. yield [
  3067. '<?php
  3068. $a /* 1 */ = /**/ 1;
  3069. $a /** 1 */ = /** 2 */ 1;
  3070. $a = 3; # 3
  3071. $a = 4; /** 4 */
  3072. echo 1;
  3073. ',
  3074. '<?php
  3075. $a /* 1 */= /**/1;
  3076. $a /** 1 */= /** 2 */1;
  3077. $a = 3; # 3
  3078. $a = 4; /** 4 */
  3079. echo 1;
  3080. ',
  3081. ];
  3082. yield 'exceptions' => [
  3083. '<?php
  3084. new Dummy(/* a */);
  3085. new Dummy(/** b */);
  3086. foo(/* c */);
  3087. foo($a /* d */, $b);
  3088. $arr = [/* empty */];
  3089. ',
  3090. ];
  3091. yield 'before_destructuring_square_brace_close' => [
  3092. '<?php
  3093. foreach ($fields as [$field/** @var string*/]) {
  3094. }
  3095. ',
  3096. ];
  3097. }
  3098. /**
  3099. * @dataProvider provideWithNamespaceCases
  3100. */
  3101. public function testWithNamespace(string $expected, ?string $input = null): void
  3102. {
  3103. $this->fixer->configure([
  3104. 'constructs_followed_by_a_single_space' => [
  3105. 'namespace',
  3106. ],
  3107. ]);
  3108. $this->doTest($expected, $input);
  3109. }
  3110. /**
  3111. * @return iterable<string, array{0: string, 1?: string}>
  3112. */
  3113. public static function provideWithNamespaceCases(): iterable
  3114. {
  3115. yield 'simple' => [
  3116. '<?php
  3117. namespace Foo;
  3118. namespace Bar;',
  3119. '<?php
  3120. namespace Foo;
  3121. namespace Bar;',
  3122. ];
  3123. yield 'simple with newlines' => [
  3124. '<?php
  3125. namespace Foo;
  3126. namespace Bar;',
  3127. '<?php
  3128. namespace
  3129. Foo;
  3130. namespace
  3131. Bar;',
  3132. ];
  3133. yield 'braces' => [
  3134. '<?php
  3135. namespace Foo {}
  3136. namespace Bar {}',
  3137. '<?php
  3138. namespace Foo {}
  3139. namespace Bar {}',
  3140. ];
  3141. yield 'braces with newlines' => [
  3142. '<?php
  3143. namespace Foo {}
  3144. namespace Bar {}',
  3145. '<?php
  3146. namespace
  3147. Foo {}
  3148. namespace
  3149. Bar {}',
  3150. ];
  3151. yield 'with // comment' => [
  3152. '<?php
  3153. namespace // comment
  3154. Foo;',
  3155. ];
  3156. yield 'with /* comment */' => [
  3157. '<?php
  3158. namespace /* comment */ Foo;',
  3159. '<?php
  3160. namespace/* comment */ Foo;',
  3161. ];
  3162. }
  3163. /**
  3164. * @dataProvider provideFix80Cases
  3165. *
  3166. * @requires PHP 8.0
  3167. */
  3168. public function testFix80(string $expected, string $input): void
  3169. {
  3170. $this->doTest($expected, $input);
  3171. }
  3172. /**
  3173. * @return iterable<string, array{string, string}>
  3174. */
  3175. public static function provideFix80Cases(): iterable
  3176. {
  3177. yield 'match 1' => [
  3178. '<?php echo match ($x) {
  3179. 1, 2 => "Same for 1 and 2",
  3180. };',
  3181. '<?php echo match ($x) {
  3182. 1, 2 => "Same for 1 and 2",
  3183. };',
  3184. ];
  3185. yield 'match 2' => [
  3186. '<?php echo match ($x) {
  3187. 1, 2 => "Same for 1 and 2",
  3188. };',
  3189. '<?php echo match($x) {
  3190. 1, 2 => "Same for 1 and 2",
  3191. };',
  3192. ];
  3193. yield 'constructor property promotion' => [
  3194. '<?php
  3195. class Point {
  3196. public function __construct(
  3197. public float $x = 0.0,
  3198. protected float $y = 0.0,
  3199. private float $z = 0.0,
  3200. ) {}
  3201. }
  3202. ',
  3203. "<?php
  3204. class Point {
  3205. public function __construct(
  3206. public float \$x = 0.0,
  3207. protected\tfloat \$y = 0.0,
  3208. private\nfloat \$z = 0.0,
  3209. ) {}
  3210. }
  3211. ",
  3212. ];
  3213. yield 'attribute' => [
  3214. '<?php class Foo {
  3215. #[Required] // foo
  3216. public $bar1;
  3217. #[Required]
  3218. public $bar2;
  3219. }',
  3220. '<?php class Foo {
  3221. #[Required]// foo
  3222. public $bar1;
  3223. #[Required]
  3224. public $bar2;
  3225. }',
  3226. ];
  3227. yield 'named argument' => [
  3228. '<?php $foo(test: 1);',
  3229. '<?php $foo(test: 1);',
  3230. ];
  3231. }
  3232. /**
  3233. * @dataProvider provideFix81Cases
  3234. *
  3235. * @requires PHP 8.1
  3236. */
  3237. public function testFix81(string $expected, string $input): void
  3238. {
  3239. $this->doTest($expected, $input);
  3240. }
  3241. /**
  3242. * @return iterable<int|string, array{string, string}>
  3243. */
  3244. public static function provideFix81Cases(): iterable
  3245. {
  3246. yield 'readonly' => [
  3247. '<?php
  3248. final class Foo
  3249. {
  3250. public readonly string $prop;
  3251. public function __construct(
  3252. public readonly float $x = 0.0,
  3253. ) {}
  3254. }
  3255. ',
  3256. '<?php
  3257. final class Foo
  3258. {
  3259. public readonly string $prop;
  3260. public function __construct(
  3261. public readonly float $x = 0.0,
  3262. ) {}
  3263. }
  3264. ',
  3265. ];
  3266. yield 'enum' => [
  3267. '<?php
  3268. enum Suit {
  3269. case Hearts;
  3270. }
  3271. ',
  3272. '<?php
  3273. enum Suit {
  3274. case Hearts;
  3275. }
  3276. ',
  3277. ];
  3278. yield 'enum full caps' => [
  3279. '<?php
  3280. ENUM Suit {
  3281. case Hearts;
  3282. }
  3283. ',
  3284. '<?php
  3285. ENUM Suit {
  3286. case Hearts;
  3287. }
  3288. ',
  3289. ];
  3290. yield [
  3291. '<?php class Foo
  3292. {
  3293. final public const X = "foo";
  3294. }',
  3295. '<?php class Foo
  3296. {
  3297. final public const X = "foo";
  3298. }',
  3299. ];
  3300. yield [
  3301. '<?php
  3302. class Test {
  3303. public function __construct(
  3304. public $prop = new Foo,
  3305. ) {}
  3306. }
  3307. ',
  3308. '<?php
  3309. class Test {
  3310. public function __construct(
  3311. public $prop = new Foo,
  3312. ) {}
  3313. }
  3314. ',
  3315. ];
  3316. }
  3317. /**
  3318. * @dataProvider provideFixWithSwitchCases
  3319. */
  3320. public function testFixWithSwitch(string $expected, string $input): void
  3321. {
  3322. $this->fixer->configure([
  3323. 'constructs_followed_by_a_single_space' => [
  3324. 'switch',
  3325. ],
  3326. ]);
  3327. $this->doTest($expected, $input);
  3328. }
  3329. /**
  3330. * @return iterable<array{string, string}>
  3331. */
  3332. public static function provideFixWithSwitchCases(): iterable
  3333. {
  3334. yield [
  3335. '<?php
  3336. switch ($a){ case 1: echo 123; }
  3337. switch ($b){ case 1: echo 123; }
  3338. ',
  3339. '<?php
  3340. switch($a){ case 1: echo 123; }
  3341. switch ($b){ case 1: echo 123; }
  3342. ',
  3343. ];
  3344. }
  3345. /**
  3346. * @dataProvider provideTypeColonCases
  3347. */
  3348. public function testTypeColon(string $expected, string $input): void
  3349. {
  3350. $this->fixer->configure([
  3351. 'constructs_followed_by_a_single_space' => [
  3352. 'type_colon',
  3353. ],
  3354. ]);
  3355. $this->doTest($expected, $input);
  3356. }
  3357. /**
  3358. * @return iterable<array{string, string}>
  3359. */
  3360. public static function provideTypeColonCases(): iterable
  3361. {
  3362. yield [
  3363. '<?php function foo(): array { return []; }',
  3364. "<?php function foo():\narray { return []; }",
  3365. ];
  3366. yield [
  3367. '<?php interface F { public function foo(): array; }',
  3368. "<?php interface F { public function foo():\tarray; }",
  3369. ];
  3370. yield [
  3371. '<?php $a=1; $f = function () use($a): array {};',
  3372. '<?php $a=1; $f = function () use($a):array {};',
  3373. ];
  3374. yield [
  3375. '<?php fn() : array => [];',
  3376. '<?php fn() : array => [];',
  3377. ];
  3378. yield [
  3379. '<?php $a=1; $f = fn (): array => [];',
  3380. '<?php $a=1; $f = fn (): array => [];',
  3381. ];
  3382. }
  3383. /**
  3384. * @dataProvider provideEnumTypeColonCases
  3385. *
  3386. * @requires PHP 8.1
  3387. */
  3388. public function testEnumTypeColon(string $expected, string $input): void
  3389. {
  3390. $this->fixer->configure([
  3391. 'constructs_followed_by_a_single_space' => [
  3392. 'type_colon',
  3393. ],
  3394. ]);
  3395. $this->doTest($expected, $input);
  3396. }
  3397. /**
  3398. * @return iterable<array{string, string}>
  3399. */
  3400. public static function provideEnumTypeColonCases(): iterable
  3401. {
  3402. yield [
  3403. '<?php enum Foo: int {}',
  3404. "<?php enum Foo:\nint {}",
  3405. ];
  3406. yield [
  3407. '<?php enum Foo: string {}',
  3408. '<?php enum Foo:string {}',
  3409. ];
  3410. }
  3411. }