SingleSpaceAroundConstructFixerTest.php 88 KB

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