SingleSpaceAfterConstructFixerTest.php 76 KB

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