SingleSpaceAfterConstructFixerTest.php 74 KB

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