SingleSpaceAfterConstructFixerTest.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275
  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 provideConfigureRejectsInvalidControlStatementCases
  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 static function provideConfigureRejectsInvalidControlStatementCases(): iterable
  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 static function provideFixWithAbstractCases(): iterable
  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 static function provideFixWithBreakCases(): iterable
  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 static function provideFixWithAsCases(): iterable
  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 static function provideFixWithCaseCases(): iterable
  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 static function provideFixWithCatchCases(): iterable
  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 static function provideFixWithClassCases(): iterable
  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 static function provideFixWithContinueCases(): iterable
  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 static function provideFixWithConstCases(): iterable
  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. ['<?php class Foo {
  530. const
  531. FOO = 9000,
  532. BAR = 10000;
  533. }',
  534. ],
  535. [
  536. '<?php
  537. const
  538. A = 3,
  539. B = 3
  540. ?>',
  541. ],
  542. [
  543. '<?php
  544. const A = 3 ?>
  545. <?php
  546. [ ,
  547. ,
  548. ,$z
  549. ] = foo() ;',
  550. '<?php
  551. const A = 3 ?>
  552. <?php
  553. [ ,
  554. ,
  555. ,$z
  556. ] = foo() ;',
  557. ],
  558. [
  559. '<?php
  560. const A
  561. =
  562. 1;
  563. ',
  564. '<?php
  565. const
  566. A
  567. =
  568. 1;
  569. ',
  570. ],
  571. ];
  572. }
  573. /**
  574. * @dataProvider provideFixWithConstImportCases
  575. */
  576. public function testFixWithConstImport(string $expected, ?string $input = null): void
  577. {
  578. $this->fixer->configure([
  579. 'constructs' => [
  580. 'const_import',
  581. ],
  582. ]);
  583. $this->doTest($expected, $input);
  584. }
  585. public static function provideFixWithConstImportCases(): iterable
  586. {
  587. return [
  588. [
  589. '<?php use const FOO\BAR;',
  590. '<?php use const FOO\BAR;',
  591. ],
  592. [
  593. '<?php use const FOO\BAR;',
  594. '<?php use const
  595. FOO\BAR;',
  596. ],
  597. [
  598. '<?php use const /* foo */FOO\BAR;',
  599. '<?php use const/* foo */FOO\BAR;',
  600. ],
  601. [
  602. '<?php use const /* foo */FOO\BAR;',
  603. '<?php use const /* foo */FOO\BAR;',
  604. ],
  605. ];
  606. }
  607. /**
  608. * @dataProvider provideFixWithCloneCases
  609. */
  610. public function testFixWithClone(string $expected, ?string $input = null): void
  611. {
  612. $this->fixer->configure([
  613. 'constructs' => [
  614. 'clone',
  615. ],
  616. ]);
  617. $this->doTest($expected, $input);
  618. }
  619. public static function provideFixWithCloneCases(): iterable
  620. {
  621. return [
  622. [
  623. '<?php clone $foo;',
  624. '<?php clone$foo;',
  625. ],
  626. [
  627. '<?php clone $foo;',
  628. '<?php clone $foo;',
  629. ],
  630. [
  631. '<?php clone $foo;',
  632. '<?php clone
  633. $foo;',
  634. ],
  635. [
  636. '<?php clone /* foo */$foo;',
  637. '<?php clone/* foo */$foo;',
  638. ],
  639. ];
  640. }
  641. /**
  642. * @dataProvider provideFixWithDoCases
  643. */
  644. public function testFixWithDo(string $expected, ?string $input = null): void
  645. {
  646. $this->fixer->configure([
  647. 'constructs' => [
  648. 'do',
  649. ],
  650. ]);
  651. $this->doTest($expected, $input);
  652. }
  653. public static function provideFixWithDoCases(): iterable
  654. {
  655. return [
  656. [
  657. '<?php do {} while (true);',
  658. '<?php do{} while (true);',
  659. ],
  660. [
  661. '<?php DO {} while (true);',
  662. '<?php DO{} while (true);',
  663. ],
  664. [
  665. '<?php do {} while (true);',
  666. '<?php do {} while (true);',
  667. ],
  668. [
  669. '<?php do {} while (true);',
  670. '<?php do
  671. {} while (true);',
  672. ],
  673. [
  674. '<?php do /* foo*/{} while (true);',
  675. '<?php do/* foo*/{} while (true);',
  676. ],
  677. [
  678. '<?php do /* foo*/{} while (true);',
  679. '<?php do /* foo*/{} while (true);',
  680. ],
  681. ];
  682. }
  683. /**
  684. * @dataProvider provideFixWithEchoCases
  685. */
  686. public function testFixWithEcho(string $expected, ?string $input = null): void
  687. {
  688. $this->fixer->configure([
  689. 'constructs' => [
  690. 'echo',
  691. ],
  692. ]);
  693. $this->doTest($expected, $input);
  694. }
  695. public static function provideFixWithEchoCases(): iterable
  696. {
  697. return [
  698. [
  699. '<?php echo $foo;',
  700. '<?php echo$foo;',
  701. ],
  702. [
  703. '<?php echo 9000;',
  704. '<?php echo 9000;',
  705. ],
  706. [
  707. '<?php echo 9000;',
  708. '<?php echo
  709. 9000;',
  710. ],
  711. [
  712. '<?php ECHO /* foo */9000;',
  713. '<?php ECHO/* foo */9000;',
  714. ],
  715. ];
  716. }
  717. /**
  718. * @dataProvider provideFixWithElseCases
  719. */
  720. public function testFixWithElse(string $expected, ?string $input = null): void
  721. {
  722. $this->fixer->configure([
  723. 'constructs' => [
  724. 'else',
  725. ],
  726. ]);
  727. $this->doTest($expected, $input);
  728. }
  729. public static function provideFixWithElseCases(): iterable
  730. {
  731. return [
  732. [
  733. '<?php if (true) {} else {}',
  734. '<?php if (true) {} else{}',
  735. ],
  736. [
  737. '<?php if (true) {} else {}',
  738. '<?php if (true) {} else {}',
  739. ],
  740. [
  741. '<?php if (true) {} else {}',
  742. '<?php if (true) {} else
  743. {}',
  744. ],
  745. [
  746. '<?php if (true) {} else /* foo */{}',
  747. '<?php if (true) {} else/* foo */{}',
  748. ],
  749. ];
  750. }
  751. /**
  752. * @dataProvider provideFixWithElseIfCases
  753. */
  754. public function testFixWithElseIf(string $expected, ?string $input = null): void
  755. {
  756. $this->fixer->configure([
  757. 'constructs' => [
  758. 'elseif',
  759. ],
  760. ]);
  761. $this->doTest($expected, $input);
  762. }
  763. public static function provideFixWithElseIfCases(): iterable
  764. {
  765. return [
  766. [
  767. '<?php if (true) {} elseif (false) {}',
  768. '<?php if (true) {} elseif(false) {}',
  769. ],
  770. [
  771. '<?php if (true) {} elseif (false) {}',
  772. '<?php if (true) {} elseif (false) {}',
  773. ],
  774. [
  775. '<?php if (true) {} elseif (false) {}',
  776. '<?php if (true) {} elseif
  777. (false) {}',
  778. ],
  779. [
  780. '<?php if (true) {} elseif /* foo */(false) {}',
  781. '<?php if (true) {} elseif/* foo */(false) {}',
  782. ],
  783. ];
  784. }
  785. /**
  786. * @dataProvider provideFixWithExtendsCases
  787. */
  788. public function testFixWithExtends(string $expected, ?string $input = null): void
  789. {
  790. $this->fixer->configure([
  791. 'constructs' => [
  792. 'extends',
  793. ],
  794. ]);
  795. $this->doTest($expected, $input);
  796. }
  797. public static function provideFixWithExtendsCases(): iterable
  798. {
  799. return [
  800. [
  801. '<?php class Foo extends \InvalidArgumentException {}',
  802. '<?php class Foo extends \InvalidArgumentException {}',
  803. ],
  804. [
  805. '<?php class Foo extends \InvalidArgumentException {}',
  806. '<?php class Foo extends
  807. \InvalidArgumentException {}',
  808. ],
  809. [
  810. '<?php class Foo extends /* foo */\InvalidArgumentException {}',
  811. '<?php class Foo extends/* foo */\InvalidArgumentException {}',
  812. ],
  813. [
  814. '<?php class Foo extends /* foo */\InvalidArgumentException {}',
  815. '<?php class Foo extends /* foo */\InvalidArgumentException {}',
  816. ],
  817. [
  818. '<?php interface Foo extends Bar1 {}',
  819. '<?php interface Foo extends Bar1 {}',
  820. ],
  821. [
  822. '<?php interface Foo extends Bar2 {}',
  823. '<?php interface Foo extends
  824. Bar2 {}',
  825. ],
  826. [
  827. '<?php interface Foo extends /* foo */Bar3 {}',
  828. '<?php interface Foo extends/* foo */Bar3 {}',
  829. ],
  830. [
  831. '<?php interface Foo extends /* foo */Bar4 {}',
  832. '<?php interface Foo extends /* foo */Bar4 {}',
  833. ],
  834. [
  835. '<?php interface Foo extends Bar5, Baz, Qux {}',
  836. '<?php interface Foo extends Bar5, Baz, Qux {}',
  837. ],
  838. [
  839. '<?php interface Foo extends Bar6, Baz, Qux {}',
  840. '<?php interface Foo extends
  841. Bar6, Baz, Qux {}',
  842. ],
  843. [
  844. '<?php interface Foo extends /* foo */Bar7, Baz, Qux {}',
  845. '<?php interface Foo extends/* foo */Bar7, Baz, Qux {}',
  846. ],
  847. [
  848. '<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
  849. '<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
  850. ],
  851. [
  852. '<?php interface Foo extends
  853. Bar9,
  854. Baz,
  855. Qux
  856. {}',
  857. ],
  858. [
  859. '<?php $foo = new class extends \InvalidArgumentException {};',
  860. '<?php $foo = new class extends \InvalidArgumentException {};',
  861. ],
  862. [
  863. '<?php $foo = new class extends \InvalidArgumentException {};',
  864. '<?php $foo = new class extends
  865. \InvalidArgumentException {};',
  866. ],
  867. [
  868. '<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
  869. '<?php $foo = new class extends/* foo */\InvalidArgumentException {};',
  870. ],
  871. [
  872. '<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
  873. '<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
  874. ],
  875. ];
  876. }
  877. /**
  878. * @dataProvider provideFixWithFinalCases
  879. */
  880. public function testFixWithFinal(string $expected, ?string $input = null): void
  881. {
  882. $this->fixer->configure([
  883. 'constructs' => [
  884. 'final',
  885. ],
  886. ]);
  887. $this->doTest($expected, $input);
  888. }
  889. public static function provideFixWithFinalCases(): iterable
  890. {
  891. return [
  892. [
  893. '<?php final class Foo {}',
  894. '<?php final class Foo {}',
  895. ],
  896. [
  897. '<?php final class Foo {}',
  898. '<?php final
  899. class Foo {}',
  900. ],
  901. [
  902. '<?php final /* foo */class Foo {}',
  903. '<?php final/* foo */class Foo {}',
  904. ],
  905. [
  906. '<?php final /* foo */class Foo {}',
  907. '<?php final /* foo */class Foo {}',
  908. ],
  909. [
  910. '<?php
  911. class Foo
  912. {
  913. final function bar() {}
  914. }',
  915. '<?php
  916. class Foo
  917. {
  918. final function bar() {}
  919. }',
  920. ],
  921. [
  922. '<?php
  923. class Foo
  924. {
  925. final function bar() {}
  926. }',
  927. '<?php
  928. class Foo
  929. {
  930. final
  931. function bar() {}
  932. }',
  933. ],
  934. [
  935. '<?php
  936. class Foo
  937. {
  938. final /* foo */function bar() {}
  939. }',
  940. '<?php
  941. class Foo
  942. {
  943. final/* foo */function bar() {}
  944. }',
  945. ],
  946. [
  947. '<?php
  948. class Foo
  949. {
  950. final /* foo */function bar() {}
  951. }',
  952. '<?php
  953. class Foo
  954. {
  955. final /* foo */function bar() {}
  956. }',
  957. ],
  958. ];
  959. }
  960. /**
  961. * @dataProvider provideFixWithFinallyCases
  962. */
  963. public function testFixWithFinally(string $expected, ?string $input = null): void
  964. {
  965. $this->fixer->configure([
  966. 'constructs' => [
  967. 'finally',
  968. ],
  969. ]);
  970. $this->doTest($expected, $input);
  971. }
  972. public static function provideFixWithFinallyCases(): iterable
  973. {
  974. return [
  975. [
  976. '<?php try {} finally {}',
  977. '<?php try {} finally{}',
  978. ],
  979. [
  980. '<?php try {} finally {}',
  981. '<?php try {} finally {}',
  982. ],
  983. [
  984. '<?php try {} finally {}',
  985. '<?php try {} finally
  986. {}',
  987. ],
  988. [
  989. '<?php try {} finally /* foo */{}',
  990. '<?php try {} finally/* foo */{}',
  991. ],
  992. [
  993. '<?php try {} finally /* foo */{}',
  994. '<?php try {} finally /* foo */{}',
  995. ],
  996. ];
  997. }
  998. /**
  999. * @dataProvider provideFixWithForCases
  1000. */
  1001. public function testFixWithFor(string $expected, ?string $input = null): void
  1002. {
  1003. $this->fixer->configure([
  1004. 'constructs' => [
  1005. 'for',
  1006. ],
  1007. ]);
  1008. $this->doTest($expected, $input);
  1009. }
  1010. public static function provideFixWithForCases(): iterable
  1011. {
  1012. return [
  1013. [
  1014. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1015. '<?php for($i = 0; $i < 3; ++$i) {}',
  1016. ],
  1017. [
  1018. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1019. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1020. ],
  1021. [
  1022. '<?php for ($i = 0; $i < 3; ++$i) {}',
  1023. '<?php for
  1024. ($i = 0; $i < 3; ++$i) {}',
  1025. ],
  1026. [
  1027. '<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
  1028. '<?php for/* foo */($i = 0; $i < 3; ++$i) {}',
  1029. ],
  1030. [
  1031. '<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
  1032. '<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
  1033. ],
  1034. ];
  1035. }
  1036. /**
  1037. * @dataProvider provideFixWithForeachCases
  1038. */
  1039. public function testFixWithForeach(string $expected, ?string $input = null): void
  1040. {
  1041. $this->fixer->configure([
  1042. 'constructs' => [
  1043. 'foreach',
  1044. ],
  1045. ]);
  1046. $this->doTest($expected, $input);
  1047. }
  1048. public static function provideFixWithForeachCases(): iterable
  1049. {
  1050. return [
  1051. [
  1052. '<?php foreach ($foo as $bar) {}',
  1053. '<?php foreach($foo as $bar) {}',
  1054. ],
  1055. [
  1056. '<?php foreach ($foo as $bar) {}',
  1057. '<?php foreach ($foo as $bar) {}',
  1058. ],
  1059. [
  1060. '<?php foreach ($foo as $bar) {}',
  1061. '<?php foreach
  1062. ($foo as $bar) {}',
  1063. ],
  1064. [
  1065. '<?php foreach /* foo */($foo as $bar) {}',
  1066. '<?php foreach/* foo */($foo as $bar) {}',
  1067. ],
  1068. [
  1069. '<?php foreach /* foo */($foo as $bar) {}',
  1070. '<?php foreach /* foo */($foo as $bar) {}',
  1071. ],
  1072. ];
  1073. }
  1074. /**
  1075. * @dataProvider provideFixWithFunctionCases
  1076. */
  1077. public function testFixWithFunction(string $expected, ?string $input = null): void
  1078. {
  1079. $this->fixer->configure([
  1080. 'constructs' => [
  1081. 'function',
  1082. ],
  1083. ]);
  1084. $this->doTest($expected, $input);
  1085. }
  1086. public static function provideFixWithFunctionCases(): iterable
  1087. {
  1088. return [
  1089. [
  1090. '<?php function foo() {}',
  1091. '<?php function foo() {}',
  1092. ],
  1093. [
  1094. '<?php function foo() {}',
  1095. '<?php function
  1096. foo() {}',
  1097. ],
  1098. [
  1099. '<?php function /* foo */foo() {}',
  1100. '<?php function/* foo */foo() {}',
  1101. ],
  1102. [
  1103. '<?php function /* foo */foo() {}',
  1104. '<?php function /* foo */foo() {}',
  1105. ],
  1106. [
  1107. '<?php
  1108. class Foo
  1109. {
  1110. function bar() {}
  1111. }
  1112. ',
  1113. '<?php
  1114. class Foo
  1115. {
  1116. function bar() {}
  1117. }
  1118. ',
  1119. ],
  1120. [
  1121. '<?php
  1122. class Foo
  1123. {
  1124. function bar() {}
  1125. }
  1126. ',
  1127. '<?php
  1128. class Foo
  1129. {
  1130. function
  1131. bar() {}
  1132. }
  1133. ',
  1134. ],
  1135. [
  1136. '<?php
  1137. class Foo
  1138. {
  1139. function /* foo */bar() {}
  1140. }
  1141. ',
  1142. '<?php
  1143. class Foo
  1144. {
  1145. function/* foo */bar() {}
  1146. }
  1147. ',
  1148. ],
  1149. [
  1150. '<?php
  1151. class Foo
  1152. {
  1153. function /* foo */bar() {}
  1154. }
  1155. ',
  1156. '<?php
  1157. class Foo
  1158. {
  1159. function /* foo */bar() {}
  1160. }
  1161. ',
  1162. ],
  1163. ];
  1164. }
  1165. /**
  1166. * @dataProvider provideFixWithFunctionImportCases
  1167. */
  1168. public function testFixWithFunctionImport(string $expected, ?string $input = null): void
  1169. {
  1170. $this->fixer->configure([
  1171. 'constructs' => [
  1172. 'function_import',
  1173. ],
  1174. ]);
  1175. $this->doTest($expected, $input);
  1176. }
  1177. public static function provideFixWithFunctionImportCases(): iterable
  1178. {
  1179. return [
  1180. [
  1181. '<?php use function Foo\bar;',
  1182. '<?php use function Foo\bar;',
  1183. ],
  1184. [
  1185. '<?php use function Foo\bar;',
  1186. '<?php use function
  1187. Foo\bar;',
  1188. ],
  1189. [
  1190. '<?php use function /* foo */Foo\bar;',
  1191. '<?php use function/* foo */Foo\bar;',
  1192. ],
  1193. [
  1194. '<?php use function /* foo */Foo\bar;',
  1195. '<?php use function /* foo */Foo\bar;',
  1196. ],
  1197. ];
  1198. }
  1199. /**
  1200. * @dataProvider provideFixWithGlobalCases
  1201. */
  1202. public function testFixWithGlobal(string $expected, ?string $input = null): void
  1203. {
  1204. $this->fixer->configure([
  1205. 'constructs' => [
  1206. 'global',
  1207. ],
  1208. ]);
  1209. $this->doTest($expected, $input);
  1210. }
  1211. public static function provideFixWithGlobalCases(): iterable
  1212. {
  1213. return [
  1214. [
  1215. '<?php function foo() { global $bar; }',
  1216. '<?php function foo() { global$bar; }',
  1217. ],
  1218. [
  1219. '<?php function foo() { global $bar; }',
  1220. '<?php function foo() { global $bar; }',
  1221. ],
  1222. [
  1223. '<?php function foo() { global $bar; }',
  1224. '<?php function foo() { global
  1225. $bar; }',
  1226. ],
  1227. [
  1228. '<?php function foo() { global /* foo */$bar; }',
  1229. '<?php function foo() { global/* foo */$bar; }',
  1230. ],
  1231. [
  1232. '<?php function foo() { global /* foo */$bar; }',
  1233. '<?php function foo() { global /* foo */$bar; }',
  1234. ],
  1235. ];
  1236. }
  1237. /**
  1238. * @dataProvider provideFixWithGotoCases
  1239. */
  1240. public function testFixWithGoto(string $expected, ?string $input = null): void
  1241. {
  1242. $this->fixer->configure([
  1243. 'constructs' => [
  1244. 'goto',
  1245. ],
  1246. ]);
  1247. $this->doTest($expected, $input);
  1248. }
  1249. public static function provideFixWithGotoCases(): iterable
  1250. {
  1251. return [
  1252. [
  1253. '<?php goto foo; foo: echo "Bar";',
  1254. '<?php goto foo; foo: echo "Bar";',
  1255. ],
  1256. [
  1257. '<?php goto foo; foo: echo "Bar";',
  1258. '<?php goto
  1259. foo; foo: echo "Bar";',
  1260. ],
  1261. [
  1262. '<?php goto /* foo */foo; foo: echo "Bar";',
  1263. '<?php goto/* foo */foo; foo: echo "Bar";',
  1264. ],
  1265. ];
  1266. }
  1267. /**
  1268. * @dataProvider provideFixWithIfCases
  1269. */
  1270. public function testFixWithIf(string $expected, ?string $input = null): void
  1271. {
  1272. $this->fixer->configure([
  1273. 'constructs' => [
  1274. 'if',
  1275. ],
  1276. ]);
  1277. $this->doTest($expected, $input);
  1278. }
  1279. public static function provideFixWithIfCases(): iterable
  1280. {
  1281. return [
  1282. [
  1283. '<?php if ($foo === $bar) {}',
  1284. '<?php if($foo === $bar) {}',
  1285. ],
  1286. [
  1287. '<?php if ($foo === $bar) {}',
  1288. '<?php if ($foo === $bar) {}',
  1289. ],
  1290. [
  1291. '<?php if ($foo === $bar) {}',
  1292. '<?php if
  1293. ($foo === $bar) {}',
  1294. ],
  1295. [
  1296. '<?php if /* foo */($foo === $bar) {}',
  1297. '<?php if/* foo */($foo === $bar) {}',
  1298. ],
  1299. ];
  1300. }
  1301. /**
  1302. * @dataProvider provideFixWithImplementsCases
  1303. */
  1304. public function testFixWithImplements(string $expected, ?string $input = null): void
  1305. {
  1306. $this->fixer->configure([
  1307. 'constructs' => [
  1308. 'implements',
  1309. ],
  1310. ]);
  1311. $this->doTest($expected, $input);
  1312. }
  1313. public static function provideFixWithImplementsCases(): iterable
  1314. {
  1315. return [
  1316. [
  1317. '<?php class Foo implements \Countable {}',
  1318. '<?php class Foo implements \Countable {}',
  1319. ],
  1320. [
  1321. '<?php class Foo implements \Countable {}',
  1322. '<?php class Foo implements
  1323. \Countable {}',
  1324. ],
  1325. [
  1326. '<?php class Foo implements /* foo */\Countable {}',
  1327. '<?php class Foo implements/* foo */\Countable {}',
  1328. ],
  1329. [
  1330. '<?php class Foo implements /* foo */\Countable {}',
  1331. '<?php class Foo implements /* foo */\Countable {}',
  1332. ],
  1333. [
  1334. '<?php class Foo implements
  1335. \Countable,
  1336. Bar,
  1337. Baz
  1338. {}',
  1339. ],
  1340. [
  1341. '<?php $foo = new class implements \Countable {};',
  1342. '<?php $foo = new class implements \Countable {};',
  1343. ],
  1344. [
  1345. '<?php $foo = new class implements \Countable {};',
  1346. '<?php $foo = new class implements
  1347. \Countable {};',
  1348. ],
  1349. [
  1350. '<?php $foo = new class implements /* foo */\Countable {};',
  1351. '<?php $foo = new class implements/* foo */\Countable {};',
  1352. ],
  1353. [
  1354. '<?php $foo = new class implements /* foo */\Countable {};',
  1355. '<?php $foo = new class implements /* foo */\Countable {};',
  1356. ],
  1357. ];
  1358. }
  1359. /**
  1360. * @dataProvider provideFixWithIncludeCases
  1361. */
  1362. public function testFixWithInclude(string $expected, ?string $input = null): void
  1363. {
  1364. $this->fixer->configure([
  1365. 'constructs' => [
  1366. 'include',
  1367. ],
  1368. ]);
  1369. $this->doTest($expected, $input);
  1370. }
  1371. public static function provideFixWithIncludeCases(): iterable
  1372. {
  1373. return [
  1374. [
  1375. '<?php include "vendor/autoload.php";',
  1376. '<?php include"vendor/autoload.php";',
  1377. ],
  1378. [
  1379. '<?php include "vendor/autoload.php";',
  1380. '<?php include "vendor/autoload.php";',
  1381. ],
  1382. [
  1383. '<?php include "vendor/autoload.php";',
  1384. '<?php include
  1385. "vendor/autoload.php";',
  1386. ],
  1387. [
  1388. '<?php include /* foo */"vendor/autoload.php";',
  1389. '<?php include/* foo */"vendor/autoload.php";',
  1390. ],
  1391. ];
  1392. }
  1393. /**
  1394. * @dataProvider provideFixWithIncludeOnceCases
  1395. */
  1396. public function testFixWithIncludeOnce(string $expected, ?string $input = null): void
  1397. {
  1398. $this->fixer->configure([
  1399. 'constructs' => [
  1400. 'include_once',
  1401. ],
  1402. ]);
  1403. $this->doTest($expected, $input);
  1404. }
  1405. public static function provideFixWithIncludeOnceCases(): iterable
  1406. {
  1407. return [
  1408. [
  1409. '<?php include_once "vendor/autoload.php";',
  1410. '<?php include_once"vendor/autoload.php";',
  1411. ],
  1412. [
  1413. '<?php include_once "vendor/autoload.php";',
  1414. '<?php include_once "vendor/autoload.php";',
  1415. ],
  1416. [
  1417. '<?php include_once "vendor/autoload.php";',
  1418. '<?php include_once
  1419. "vendor/autoload.php";',
  1420. ],
  1421. [
  1422. '<?php include_once /* foo */"vendor/autoload.php";',
  1423. '<?php include_once/* foo */"vendor/autoload.php";',
  1424. ],
  1425. ];
  1426. }
  1427. /**
  1428. * @dataProvider provideFixWithInstanceofCases
  1429. */
  1430. public function testFixWithInstanceof(string $expected, ?string $input = null): void
  1431. {
  1432. $this->fixer->configure([
  1433. 'constructs' => [
  1434. 'instanceof',
  1435. ],
  1436. ]);
  1437. $this->doTest($expected, $input);
  1438. }
  1439. public static function provideFixWithInstanceofCases(): iterable
  1440. {
  1441. return [
  1442. [
  1443. '<?php $foo instanceof \stdClass;',
  1444. '<?php $foo instanceof \stdClass;',
  1445. ],
  1446. [
  1447. '<?php $foo instanceof \stdClass;',
  1448. '<?php $foo instanceof
  1449. \stdClass;',
  1450. ],
  1451. [
  1452. '<?php $foo instanceof /* foo */\stdClass;',
  1453. '<?php $foo instanceof/* foo */\stdClass;',
  1454. ],
  1455. [
  1456. '<?php $foo instanceof /* foo */\stdClass;',
  1457. '<?php $foo instanceof /* foo */\stdClass;',
  1458. ],
  1459. [
  1460. '<?php $foo instanceof $bar;',
  1461. '<?php $foo instanceof$bar;',
  1462. ],
  1463. ];
  1464. }
  1465. /**
  1466. * @dataProvider provideFixWithInsteadofCases
  1467. */
  1468. public function testFixWithInsteadof(string $expected, ?string $input = null): void
  1469. {
  1470. $this->fixer->configure([
  1471. 'constructs' => [
  1472. 'insteadof',
  1473. ],
  1474. ]);
  1475. $this->doTest($expected, $input);
  1476. }
  1477. public static function provideFixWithInsteadofCases(): iterable
  1478. {
  1479. return [
  1480. [
  1481. '<?php
  1482. class Talker {
  1483. use A, B {
  1484. B::smallTalk insteadof A;
  1485. A::bigTalk insteadof B;
  1486. }
  1487. }',
  1488. '<?php
  1489. class Talker {
  1490. use A, B {
  1491. B::smallTalk insteadof A;
  1492. A::bigTalk insteadof B;
  1493. }
  1494. }',
  1495. ],
  1496. [
  1497. '<?php
  1498. class Talker {
  1499. use A, B {
  1500. B::smallTalk insteadof A;
  1501. A::bigTalk insteadof B;
  1502. }
  1503. }',
  1504. '<?php
  1505. class Talker {
  1506. use A, B {
  1507. B::smallTalk insteadof
  1508. A;
  1509. A::bigTalk insteadof B;
  1510. }
  1511. }',
  1512. ],
  1513. [
  1514. '<?php
  1515. class Talker {
  1516. use A, B {
  1517. B::smallTalk insteadof /* foo */A;
  1518. A::bigTalk insteadof B;
  1519. }
  1520. }',
  1521. '<?php
  1522. class Talker {
  1523. use A, B {
  1524. B::smallTalk insteadof/* foo */A;
  1525. A::bigTalk insteadof B;
  1526. }
  1527. }',
  1528. ],
  1529. [
  1530. '<?php
  1531. class Talker {
  1532. use A, B {
  1533. B::smallTalk insteadof /* foo */A;
  1534. A::bigTalk insteadof B;
  1535. }
  1536. }',
  1537. '<?php
  1538. class Talker {
  1539. use A, B {
  1540. B::smallTalk insteadof /* foo */A;
  1541. A::bigTalk insteadof B;
  1542. }
  1543. }',
  1544. ],
  1545. ];
  1546. }
  1547. /**
  1548. * @dataProvider provideFixWithInterfaceCases
  1549. */
  1550. public function testFixWithInterface(string $expected, ?string $input = null): void
  1551. {
  1552. $this->fixer->configure([
  1553. 'constructs' => [
  1554. 'interface',
  1555. ],
  1556. ]);
  1557. $this->doTest($expected, $input);
  1558. }
  1559. public static function provideFixWithInterfaceCases(): iterable
  1560. {
  1561. return [
  1562. [
  1563. '<?php interface Foo {}',
  1564. '<?php interface Foo {}',
  1565. ],
  1566. [
  1567. '<?php interface Foo {}',
  1568. '<?php interface
  1569. Foo {}',
  1570. ],
  1571. [
  1572. '<?php interface /* foo */Foo {}',
  1573. '<?php interface /* foo */Foo {}',
  1574. ],
  1575. [
  1576. '<?php interface /* foo */Foo {}',
  1577. '<?php interface/* foo */Foo {}',
  1578. ],
  1579. ];
  1580. }
  1581. /**
  1582. * @dataProvider provideFixWithNewCases
  1583. */
  1584. public function testFixWithNew(string $expected, ?string $input = null): void
  1585. {
  1586. $this->fixer->configure([
  1587. 'constructs' => [
  1588. 'new',
  1589. ],
  1590. ]);
  1591. $this->doTest($expected, $input);
  1592. }
  1593. public static function provideFixWithNewCases(): iterable
  1594. {
  1595. return [
  1596. [
  1597. '<?php new $foo();',
  1598. '<?php new$foo();',
  1599. ],
  1600. [
  1601. '<?php new Bar();',
  1602. '<?php new Bar();',
  1603. ],
  1604. [
  1605. '<?php new Bar();',
  1606. '<?php new
  1607. Bar();',
  1608. ],
  1609. [
  1610. '<?php new /* foo */Bar();',
  1611. '<?php new/* foo */Bar();',
  1612. ],
  1613. ];
  1614. }
  1615. /**
  1616. * @dataProvider provideFixWithOpenTagWithEchoCases
  1617. */
  1618. public function testFixWithOpenTagWithEcho(string $expected, ?string $input = null): void
  1619. {
  1620. $this->fixer->configure([
  1621. 'constructs' => [
  1622. 'open_tag_with_echo',
  1623. ],
  1624. ]);
  1625. $this->doTest($expected, $input);
  1626. }
  1627. public static function provideFixWithOpenTagWithEchoCases(): iterable
  1628. {
  1629. return [
  1630. [
  1631. '<?= $foo ?>',
  1632. '<?=$foo ?>',
  1633. ],
  1634. [
  1635. '<?= $foo ?>',
  1636. '<?= $foo ?>',
  1637. ],
  1638. [
  1639. '<?= $foo ?>',
  1640. '<?=
  1641. $foo ?>',
  1642. ],
  1643. [
  1644. '<?= /* foo */$foo ?>',
  1645. '<?=/* foo */$foo ?>',
  1646. ],
  1647. [
  1648. '<?= /* foo */$foo ?>',
  1649. '<?= /* foo */$foo ?>',
  1650. ],
  1651. ];
  1652. }
  1653. /**
  1654. * @dataProvider provideFixWithPrintCases
  1655. */
  1656. public function testFixWithPrint(string $expected, ?string $input = null): void
  1657. {
  1658. $this->fixer->configure([
  1659. 'constructs' => [
  1660. 'print',
  1661. ],
  1662. ]);
  1663. $this->doTest($expected, $input);
  1664. }
  1665. public static function provideFixWithPrintCases(): iterable
  1666. {
  1667. return [
  1668. [
  1669. '<?php print $foo;',
  1670. '<?php print$foo;',
  1671. ],
  1672. [
  1673. '<?php print 9000;',
  1674. '<?php print 9000;',
  1675. ],
  1676. [
  1677. '<?php print 9000;',
  1678. '<?php print
  1679. 9000;',
  1680. ],
  1681. [
  1682. '<?php print /* foo */9000;',
  1683. '<?php print/* foo */9000;',
  1684. ],
  1685. ];
  1686. }
  1687. /**
  1688. * @dataProvider provideFixWithPrivateCases
  1689. */
  1690. public function testFixWithPrivate(string $expected, ?string $input = null): void
  1691. {
  1692. $this->fixer->configure([
  1693. 'constructs' => [
  1694. 'private',
  1695. ],
  1696. ]);
  1697. $this->doTest($expected, $input);
  1698. }
  1699. public static function provideFixWithPrivateCases(): iterable
  1700. {
  1701. return [
  1702. [
  1703. '<?php class Foo { private $bar; }',
  1704. '<?php class Foo { private$bar; }',
  1705. ],
  1706. [
  1707. '<?php class Foo { private $bar; }',
  1708. '<?php class Foo { private $bar; }',
  1709. ],
  1710. [
  1711. '<?php class Foo { private $bar; }',
  1712. '<?php class Foo { private
  1713. $bar; }',
  1714. ],
  1715. [
  1716. '<?php class Foo { private /* foo */$bar; }',
  1717. '<?php class Foo { private/* foo */$bar; }',
  1718. ],
  1719. [
  1720. '<?php class Foo { private /* foo */$bar; }',
  1721. '<?php class Foo { private /* foo */$bar; }',
  1722. ],
  1723. [
  1724. '<?php class Foo { private function bar() {} }',
  1725. '<?php class Foo { private function bar() {} }',
  1726. ],
  1727. [
  1728. '<?php class Foo { private function bar() {} }',
  1729. '<?php class Foo { private
  1730. function bar() {} }',
  1731. ],
  1732. [
  1733. '<?php class Foo { private /* foo */function bar() {} }',
  1734. '<?php class Foo { private/* foo */function bar() {} }',
  1735. ],
  1736. [
  1737. '<?php class Foo { private /* foo */function bar() {} }',
  1738. '<?php class Foo { private /* foo */function bar() {} }',
  1739. ],
  1740. [
  1741. '<?php class Foo { private CONST BAR = 9000; }',
  1742. '<?php class Foo { private CONST BAR = 9000; }',
  1743. ],
  1744. [
  1745. '<?php class Foo { private CONST BAR = 9000; }',
  1746. '<?php class Foo { private
  1747. CONST BAR = 9000; }',
  1748. ],
  1749. [
  1750. '<?php class Foo { private /* foo */CONST BAR = 9000; }',
  1751. '<?php class Foo { private/* foo */CONST BAR = 9000; }',
  1752. ],
  1753. [
  1754. '<?php class Foo { private /* foo */CONST BAR = 9000; }',
  1755. '<?php class Foo { private /* foo */CONST BAR = 9000; }',
  1756. ],
  1757. ];
  1758. }
  1759. /**
  1760. * @dataProvider provideFixWithProtectedCases
  1761. */
  1762. public function testFixWithProtected(string $expected, ?string $input = null): void
  1763. {
  1764. $this->fixer->configure([
  1765. 'constructs' => [
  1766. 'protected',
  1767. ],
  1768. ]);
  1769. $this->doTest($expected, $input);
  1770. }
  1771. public static function provideFixWithProtectedCases(): iterable
  1772. {
  1773. return [
  1774. [
  1775. '<?php class Foo { protected $bar; }',
  1776. '<?php class Foo { protected$bar; }',
  1777. ],
  1778. [
  1779. '<?php class Foo { protected $bar; }',
  1780. '<?php class Foo { protected $bar; }',
  1781. ],
  1782. [
  1783. '<?php class Foo { protected $bar; }',
  1784. '<?php class Foo { protected
  1785. $bar; }',
  1786. ],
  1787. [
  1788. '<?php class Foo { protected /* foo */$bar; }',
  1789. '<?php class Foo { protected/* foo */$bar; }',
  1790. ],
  1791. [
  1792. '<?php class Foo { protected /* foo */$bar; }',
  1793. '<?php class Foo { protected /* foo */$bar; }',
  1794. ],
  1795. [
  1796. '<?php class Foo { protected function bar() {} }',
  1797. '<?php class Foo { protected function bar() {} }',
  1798. ],
  1799. [
  1800. '<?php class Foo { protected function bar() {} }',
  1801. '<?php class Foo { protected
  1802. function bar() {} }',
  1803. ],
  1804. [
  1805. '<?php class Foo { protected /* foo */function bar() {} }',
  1806. '<?php class Foo { protected/* foo */function bar() {} }',
  1807. ],
  1808. [
  1809. '<?php class Foo { protected /* foo */function bar() {} }',
  1810. '<?php class Foo { protected /* foo */function bar() {} }',
  1811. ],
  1812. [
  1813. '<?php class Foo { protected CONST BAR = 9000; }',
  1814. '<?php class Foo { protected CONST BAR = 9000; }',
  1815. ],
  1816. [
  1817. '<?php class Foo { protected CONST BAR = 9000; }',
  1818. '<?php class Foo { protected
  1819. 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. '<?php class Foo { protected /* foo */CONST BAR = 9000; }',
  1827. '<?php class Foo { protected /* foo */CONST BAR = 9000; }',
  1828. ],
  1829. ];
  1830. }
  1831. /**
  1832. * @dataProvider provideFixWithPublicCases
  1833. */
  1834. public function testFixWithPublic(string $expected, ?string $input = null): void
  1835. {
  1836. $this->fixer->configure([
  1837. 'constructs' => [
  1838. 'public',
  1839. ],
  1840. ]);
  1841. $this->doTest($expected, $input);
  1842. }
  1843. public static function provideFixWithPublicCases(): iterable
  1844. {
  1845. return [
  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 $bar; }',
  1853. ],
  1854. [
  1855. '<?php class Foo { public $bar; }',
  1856. '<?php class Foo { public
  1857. $bar; }',
  1858. ],
  1859. [
  1860. '<?php class Foo { public /* foo */$bar; }',
  1861. '<?php class Foo { public/* foo */$bar; }',
  1862. ],
  1863. [
  1864. '<?php class Foo { public /* foo */$bar; }',
  1865. '<?php class Foo { public /* foo */$bar; }',
  1866. ],
  1867. [
  1868. '<?php class Foo { public function bar() {} }',
  1869. '<?php class Foo { public function bar() {} }',
  1870. ],
  1871. [
  1872. '<?php class Foo { public function bar() {} }',
  1873. '<?php class Foo { public
  1874. function bar() {} }',
  1875. ],
  1876. [
  1877. '<?php class Foo { public /* foo */function bar() {} }',
  1878. '<?php class Foo { public/* foo */function bar() {} }',
  1879. ],
  1880. [
  1881. '<?php class Foo { public /* foo */function bar() {} }',
  1882. '<?php class Foo { public /* foo */function bar() {} }',
  1883. ],
  1884. [
  1885. '<?php class Foo { public CONST BAR = 9000; }',
  1886. '<?php class Foo { public CONST BAR = 9000; }',
  1887. ],
  1888. [
  1889. '<?php class Foo { public CONST BAR = 9000; }',
  1890. '<?php class Foo { public
  1891. CONST BAR = 9000; }',
  1892. ],
  1893. [
  1894. '<?php class Foo { public /* foo */CONST BAR = 9000; }',
  1895. '<?php class Foo { public/* foo */CONST BAR = 9000; }',
  1896. ],
  1897. [
  1898. '<?php class Foo { public /* foo */CONST BAR = 9000; }',
  1899. '<?php class Foo { public /* foo */CONST BAR = 9000; }',
  1900. ],
  1901. ];
  1902. }
  1903. /**
  1904. * @dataProvider provideFixWithRequireCases
  1905. */
  1906. public function testFixWithRequire(string $expected, ?string $input = null): void
  1907. {
  1908. $this->fixer->configure([
  1909. 'constructs' => [
  1910. 'require',
  1911. ],
  1912. ]);
  1913. $this->doTest($expected, $input);
  1914. }
  1915. public static function provideFixWithRequireCases(): iterable
  1916. {
  1917. return [
  1918. [
  1919. '<?php require "vendor/autoload.php";',
  1920. '<?php require"vendor/autoload.php";',
  1921. ],
  1922. [
  1923. '<?php require "vendor/autoload.php";',
  1924. '<?php require "vendor/autoload.php";',
  1925. ],
  1926. [
  1927. '<?php require "vendor/autoload.php";',
  1928. '<?php require
  1929. "vendor/autoload.php";',
  1930. ],
  1931. [
  1932. '<?php require /* foo */"vendor/autoload.php";',
  1933. '<?php require/* foo */"vendor/autoload.php";',
  1934. ],
  1935. ];
  1936. }
  1937. /**
  1938. * @dataProvider provideFixWithRequireOnceCases
  1939. */
  1940. public function testFixWithRequireOnce(string $expected, ?string $input = null): void
  1941. {
  1942. $this->fixer->configure([
  1943. 'constructs' => [
  1944. 'require_once',
  1945. ],
  1946. ]);
  1947. $this->doTest($expected, $input);
  1948. }
  1949. public static function provideFixWithRequireOnceCases(): iterable
  1950. {
  1951. return [
  1952. [
  1953. '<?php require_once "vendor/autoload.php";',
  1954. '<?php require_once"vendor/autoload.php";',
  1955. ],
  1956. [
  1957. '<?php require_once "vendor/autoload.php";',
  1958. '<?php require_once "vendor/autoload.php";',
  1959. ],
  1960. [
  1961. '<?php require_once "vendor/autoload.php";',
  1962. '<?php require_once
  1963. "vendor/autoload.php";',
  1964. ],
  1965. [
  1966. '<?php require_once /* foo */"vendor/autoload.php";',
  1967. '<?php require_once/* foo */"vendor/autoload.php";',
  1968. ],
  1969. ];
  1970. }
  1971. /**
  1972. * @dataProvider provideFixWithReturnCases
  1973. */
  1974. public function testFixWithReturn(string $expected, ?string $input = null): void
  1975. {
  1976. $this->fixer->configure([
  1977. 'constructs' => [
  1978. 'return',
  1979. ],
  1980. ]);
  1981. $this->doTest($expected, $input);
  1982. }
  1983. public static function provideFixWithReturnCases(): iterable
  1984. {
  1985. return [
  1986. [
  1987. '<?php return;',
  1988. ],
  1989. [
  1990. '<?php return /* foo */;',
  1991. '<?php return/* foo */;',
  1992. ],
  1993. [
  1994. '<?php return /* foo */;',
  1995. '<?php return /* foo */;',
  1996. ],
  1997. [
  1998. '<?php return $foo;',
  1999. '<?php return$foo;',
  2000. ],
  2001. [
  2002. '<?php return 9000;',
  2003. '<?php return 9000;',
  2004. ],
  2005. [
  2006. '<?php return 9000;',
  2007. '<?php return
  2008. 9000;',
  2009. ],
  2010. [
  2011. '<?php return /* */ 9000 + 1 /* foo */ ?>',
  2012. '<?php return
  2013. /* */ 9000 + 1 /* foo */ ?>',
  2014. ],
  2015. [
  2016. '<?php return /* foo */9000;',
  2017. '<?php return/* foo */9000;',
  2018. ],
  2019. [
  2020. '<?php return $foo && $bar || $baz;',
  2021. '<?php return
  2022. $foo && $bar || $baz;',
  2023. ],
  2024. [
  2025. '<?php
  2026. return
  2027. $foo
  2028. && $bar
  2029. || $baz;',
  2030. ],
  2031. [
  2032. '<?php
  2033. return
  2034. $foo &&
  2035. $bar ||
  2036. $baz;',
  2037. ],
  2038. [
  2039. '<?php
  2040. return
  2041. $foo
  2042. + $bar
  2043. - $baz;',
  2044. ],
  2045. [
  2046. '<?php
  2047. return
  2048. $foo +
  2049. $bar -
  2050. $baz;',
  2051. ],
  2052. [
  2053. '<?php
  2054. return
  2055. $foo ?
  2056. $bar :
  2057. $baz;',
  2058. ],
  2059. [
  2060. '<?php
  2061. return
  2062. $foo
  2063. ? $bar
  2064. : baz;',
  2065. ],
  2066. [
  2067. '<?php
  2068. return
  2069. $foo ?:
  2070. $bar;',
  2071. ],
  2072. [
  2073. '<?php
  2074. return
  2075. $foo
  2076. ?: $bar;',
  2077. ],
  2078. [
  2079. '<?php
  2080. return
  2081. $foo
  2082. ?: $bar?>',
  2083. ],
  2084. ];
  2085. }
  2086. /**
  2087. * @dataProvider provideFixWithStaticCases
  2088. */
  2089. public function testFixWithStatic(string $expected, ?string $input = null): void
  2090. {
  2091. $this->fixer->configure([
  2092. 'constructs' => [
  2093. 'static',
  2094. ],
  2095. ]);
  2096. $this->doTest($expected, $input);
  2097. }
  2098. public static function provideFixWithStaticCases(): iterable
  2099. {
  2100. return [
  2101. [
  2102. '<?php function foo() { static $bar; }',
  2103. '<?php function foo() { static$bar; }',
  2104. ],
  2105. [
  2106. '<?php function foo() { static $bar; }',
  2107. '<?php function foo() { static $bar; }',
  2108. ],
  2109. [
  2110. '<?php function foo() { static $bar; }',
  2111. '<?php function foo() { static
  2112. $bar; }',
  2113. ],
  2114. [
  2115. '<?php function foo() { static /* foo */$bar; }',
  2116. '<?php function foo() { static/* foo */$bar; }',
  2117. ],
  2118. [
  2119. '<?php function foo() { static /* foo */$bar; }',
  2120. '<?php function foo() { static /* foo */$bar; }',
  2121. ],
  2122. [
  2123. '<?php class Foo { static function bar() {} }',
  2124. '<?php class Foo { static function bar() {} }',
  2125. ],
  2126. [
  2127. '<?php class Foo { static function bar() {} }',
  2128. '<?php class Foo { static
  2129. function bar() {} }',
  2130. ],
  2131. [
  2132. '<?php class Foo { static /* foo */function bar() {} }',
  2133. '<?php class Foo { static/* foo */function bar() {} }',
  2134. ],
  2135. [
  2136. '<?php class Foo { static /* foo */function bar() {} }',
  2137. '<?php class Foo { static /* foo */function bar() {} }',
  2138. ],
  2139. [
  2140. '<?php class Foo { function bar() { return new static(); } }',
  2141. ],
  2142. [
  2143. '<?php class Foo { function bar() { return static::class; } }',
  2144. ],
  2145. ];
  2146. }
  2147. /**
  2148. * @dataProvider provideFixWithThrowCases
  2149. */
  2150. public function testFixWithThrow(string $expected, ?string $input = null): void
  2151. {
  2152. $this->fixer->configure([
  2153. 'constructs' => [
  2154. 'throw',
  2155. ],
  2156. ]);
  2157. $this->doTest($expected, $input);
  2158. }
  2159. public static function provideFixWithThrowCases(): iterable
  2160. {
  2161. return [
  2162. [
  2163. '<?php throw $foo;',
  2164. '<?php throw$foo;',
  2165. ],
  2166. [
  2167. '<?php throw new Exception();',
  2168. '<?php throw new Exception();',
  2169. ],
  2170. [
  2171. '<?php throw new Exception();',
  2172. '<?php throw
  2173. new Exception();',
  2174. ],
  2175. [
  2176. '<?php throw /* foo */new Exception();',
  2177. '<?php throw/* foo */new Exception();',
  2178. ],
  2179. ];
  2180. }
  2181. /**
  2182. * @dataProvider provideFixWithTraitCases
  2183. */
  2184. public function testFixWithTrait(string $expected, ?string $input = null): void
  2185. {
  2186. $this->fixer->configure([
  2187. 'constructs' => [
  2188. 'trait',
  2189. ],
  2190. ]);
  2191. $this->doTest($expected, $input);
  2192. }
  2193. public static function provideFixWithTraitCases(): iterable
  2194. {
  2195. return [
  2196. [
  2197. '<?php trait Foo {}',
  2198. '<?php trait Foo {}',
  2199. ],
  2200. [
  2201. '<?php trait Foo {}',
  2202. '<?php trait
  2203. Foo {}',
  2204. ],
  2205. [
  2206. '<?php trait /* foo */Foo {}',
  2207. '<?php trait /* foo */Foo {}',
  2208. ],
  2209. [
  2210. '<?php trait /* foo */Foo {}',
  2211. '<?php trait/* foo */Foo {}',
  2212. ],
  2213. ];
  2214. }
  2215. /**
  2216. * @dataProvider provideFixWithTryCases
  2217. */
  2218. public function testFixWithTry(string $expected, ?string $input = null): void
  2219. {
  2220. $this->fixer->configure([
  2221. 'constructs' => [
  2222. 'try',
  2223. ],
  2224. ]);
  2225. $this->doTest($expected, $input);
  2226. }
  2227. public static function provideFixWithTryCases(): iterable
  2228. {
  2229. return [
  2230. [
  2231. '<?php try {} catch (\Exception $exception) {}',
  2232. '<?php try{} catch (\Exception $exception) {}',
  2233. ],
  2234. [
  2235. '<?php try {} catch (\Exception $exception) {}',
  2236. '<?php try {} catch (\Exception $exception) {}',
  2237. ],
  2238. [
  2239. '<?php try {} catch (\Exception $exception) {}',
  2240. '<?php try
  2241. {} catch (\Exception $exception) {}',
  2242. ],
  2243. [
  2244. '<?php try /* foo */{} catch (\Exception $exception) {}',
  2245. '<?php try/* foo */{} catch (\Exception $exception) {}',
  2246. ],
  2247. [
  2248. '<?php try /* foo */{} catch (\Exception $exception) {}',
  2249. '<?php try /* foo */{} catch (\Exception $exception) {}',
  2250. ],
  2251. ];
  2252. }
  2253. /**
  2254. * @dataProvider provideFixWithUseCases
  2255. */
  2256. public function testFixWithUse(string $expected, ?string $input = null): void
  2257. {
  2258. $this->fixer->configure([
  2259. 'constructs' => [
  2260. 'use',
  2261. ],
  2262. ]);
  2263. $this->doTest($expected, $input);
  2264. }
  2265. public static function provideFixWithUseCases(): iterable
  2266. {
  2267. return [
  2268. [
  2269. '<?php use Foo\Bar;',
  2270. '<?php use Foo\Bar;',
  2271. ],
  2272. [
  2273. '<?php use Foo\Bar;',
  2274. '<?php use
  2275. Foo\Bar;',
  2276. ],
  2277. [
  2278. '<?php use /* foo */Foo\Bar;',
  2279. '<?php use/* foo */Foo\Bar;',
  2280. ],
  2281. [
  2282. '<?php use /* foo */Foo\Bar;',
  2283. '<?php use /* foo */Foo\Bar;',
  2284. ],
  2285. [
  2286. '<?php use const Foo\BAR;',
  2287. '<?php use const Foo\BAR;',
  2288. ],
  2289. [
  2290. '<?php use const Foo\BAR;',
  2291. '<?php use
  2292. const Foo\BAR;',
  2293. ],
  2294. [
  2295. '<?php use /* foo */const Foo\BAR;',
  2296. '<?php use/* foo */const Foo\BAR;',
  2297. ],
  2298. [
  2299. '<?php use /* foo */const Foo\BAR;',
  2300. '<?php use/* foo */const Foo\BAR;',
  2301. ],
  2302. [
  2303. '<?php use function Foo\bar;',
  2304. '<?php use function Foo\bar;',
  2305. ],
  2306. [
  2307. '<?php use function Foo\bar;',
  2308. '<?php use
  2309. function Foo\bar;',
  2310. ],
  2311. [
  2312. '<?php use /* foo */function Foo\bar;',
  2313. '<?php use/* foo */function Foo\bar;',
  2314. ],
  2315. [
  2316. '<?php use /* foo */function Foo\bar;',
  2317. '<?php use/* foo */function Foo\bar;',
  2318. ],
  2319. ];
  2320. }
  2321. /**
  2322. * @dataProvider provideFixWithUseLambdaCases
  2323. */
  2324. public function testFixWithUseLambda(string $expected, ?string $input = null): void
  2325. {
  2326. $this->fixer->configure([
  2327. 'constructs' => [
  2328. 'use_lambda',
  2329. ],
  2330. ]);
  2331. $this->doTest($expected, $input);
  2332. }
  2333. public static function provideFixWithUseLambdaCases(): iterable
  2334. {
  2335. return [
  2336. [
  2337. '<?php $foo = function () use ($bar) {};',
  2338. '<?php $foo = function () use($bar) {};',
  2339. ],
  2340. [
  2341. '<?php $foo = function () use ($bar) {};',
  2342. '<?php $foo = function () use ($bar) {};',
  2343. ],
  2344. [
  2345. '<?php $foo = function () use ($bar) {};',
  2346. '<?php $foo = function () use
  2347. ($bar) {};',
  2348. ],
  2349. [
  2350. '<?php $foo = function () use /* foo */($bar) {};',
  2351. '<?php $foo = function () use/* foo */($bar) {};',
  2352. ],
  2353. [
  2354. '<?php $foo = function () use /* foo */($bar) {};',
  2355. '<?php $foo = function () use /* foo */($bar) {};',
  2356. ],
  2357. ];
  2358. }
  2359. /**
  2360. * @dataProvider provideFixWithUseTraitCases
  2361. */
  2362. public function testFixWithUseTrait(string $expected, ?string $input = null): void
  2363. {
  2364. $this->fixer->configure([
  2365. 'constructs' => [
  2366. 'use_trait',
  2367. ],
  2368. ]);
  2369. $this->doTest($expected, $input);
  2370. }
  2371. public static function provideFixWithUseTraitCases(): iterable
  2372. {
  2373. return [
  2374. [
  2375. '<?php class Foo { use Bar; }',
  2376. '<?php class Foo { use Bar; }',
  2377. ],
  2378. [
  2379. '<?php class Foo { use Bar; }',
  2380. '<?php class Foo { use
  2381. Bar; }',
  2382. ],
  2383. [
  2384. '<?php class Foo { use /* foo */Bar; }',
  2385. '<?php class Foo { use/* foo */Bar; }',
  2386. ],
  2387. [
  2388. '<?php class Foo { use /* foo */Bar; }',
  2389. '<?php class Foo { use /* foo */Bar; }',
  2390. ],
  2391. ];
  2392. }
  2393. /**
  2394. * @dataProvider provideFixWithVarCases
  2395. */
  2396. public function testFixWithVar(string $expected, ?string $input = null): void
  2397. {
  2398. $this->fixer->configure([
  2399. 'constructs' => [
  2400. 'var',
  2401. ],
  2402. ]);
  2403. $this->doTest($expected, $input);
  2404. }
  2405. public static function provideFixWithVarCases(): iterable
  2406. {
  2407. return [
  2408. [
  2409. '<?php class Foo { var $bar; }',
  2410. '<?php class Foo { var$bar; }',
  2411. ],
  2412. [
  2413. '<?php class Foo { var $bar; }',
  2414. '<?php class Foo { var $bar; }',
  2415. ],
  2416. [
  2417. '<?php class Foo { var $bar; }',
  2418. '<?php class Foo { var
  2419. $bar; }',
  2420. ],
  2421. [
  2422. '<?php class Foo { var /* foo */$bar; }',
  2423. '<?php class Foo { var/* foo */$bar; }',
  2424. ],
  2425. [
  2426. '<?php class Foo { var /* foo */$bar; }',
  2427. '<?php class Foo { var /* foo */$bar; }',
  2428. ],
  2429. ];
  2430. }
  2431. /**
  2432. * @dataProvider provideFixWithWhileCases
  2433. */
  2434. public function testFixWithWhile(string $expected, ?string $input = null): void
  2435. {
  2436. $this->fixer->configure([
  2437. 'constructs' => [
  2438. 'while',
  2439. ],
  2440. ]);
  2441. $this->doTest($expected, $input);
  2442. }
  2443. public static function provideFixWithWhileCases(): iterable
  2444. {
  2445. return [
  2446. [
  2447. '<?php do {} while (true);',
  2448. '<?php do {} while(true);',
  2449. ],
  2450. [
  2451. '<?php do {} while (true);',
  2452. '<?php do {} while (true);',
  2453. ],
  2454. [
  2455. '<?php do {} while (true);',
  2456. '<?php do {} while
  2457. (true);',
  2458. ],
  2459. [
  2460. '<?php do {} while /* foo */(true);',
  2461. '<?php do {} while/* foo */(true);',
  2462. ],
  2463. [
  2464. '<?php do {} while /* foo */(true);',
  2465. '<?php do {} while /* foo */(true);',
  2466. ],
  2467. ];
  2468. }
  2469. /**
  2470. * @dataProvider provideFixWithYieldCases
  2471. */
  2472. public function testFixWithYield(string $expected, ?string $input = null): void
  2473. {
  2474. $this->fixer->configure([
  2475. 'constructs' => [
  2476. 'yield',
  2477. ],
  2478. ]);
  2479. $this->doTest($expected, $input);
  2480. }
  2481. public static function provideFixWithYieldCases(): iterable
  2482. {
  2483. return [
  2484. [
  2485. '<?php function foo() { yield $foo; }',
  2486. '<?php function foo() { yield$foo; }',
  2487. ],
  2488. [
  2489. '<?php function foo() { yield "Foo"; }',
  2490. '<?php function foo() { yield "Foo"; }',
  2491. ],
  2492. [
  2493. '<?php function foo() { yield "Foo"; }',
  2494. '<?php function foo() { yield
  2495. "Foo"; }',
  2496. ],
  2497. [
  2498. '<?php function foo() { yield /* foo */"Foo"; }',
  2499. '<?php function foo() { yield/* foo */"Foo"; }',
  2500. ],
  2501. ];
  2502. }
  2503. /**
  2504. * @dataProvider provideFixWithYieldFromCases
  2505. */
  2506. public function testFixWithYieldFrom(string $expected, ?string $input = null): void
  2507. {
  2508. $this->fixer->configure([
  2509. 'constructs' => [
  2510. 'yield_from',
  2511. ],
  2512. ]);
  2513. $this->doTest($expected, $input);
  2514. }
  2515. public static function provideFixWithYieldFromCases(): iterable
  2516. {
  2517. return [
  2518. [
  2519. '<?php function foo() { yield from baz(); }',
  2520. '<?php function foo() { yield from baz(); }',
  2521. ],
  2522. [
  2523. '<?php function foo() { yield from $foo; }',
  2524. '<?php function foo() { yield from$foo; }',
  2525. ],
  2526. [
  2527. '<?php function foo() { yield from baz(); }',
  2528. '<?php function foo() { yield from baz(); }',
  2529. ],
  2530. [
  2531. '<?php function foo() { yield from baz(); }',
  2532. '<?php function foo() { yield from baz(); }',
  2533. ],
  2534. [
  2535. '<?php function foo() { yIeLd fRoM baz(); }',
  2536. '<?php function foo() { yIeLd fRoM baz(); }',
  2537. ],
  2538. [
  2539. '<?php function foo() { yield from baz(); }',
  2540. '<?php function foo() { yield
  2541. from baz(); }',
  2542. ],
  2543. [
  2544. '<?php function foo() { yield from baz(); }',
  2545. '<?php function foo() { yield from
  2546. baz(); }',
  2547. ],
  2548. [
  2549. '<?php function foo() { yield from baz(); }',
  2550. '<?php function foo() { yield
  2551. from
  2552. baz(); }',
  2553. ],
  2554. [
  2555. '<?php function foo() { yield from /* foo */baz(); }',
  2556. '<?php function foo() { yield from/* foo */baz(); }',
  2557. ],
  2558. [
  2559. '<?php function foo() { yield from /* foo */baz(); }',
  2560. '<?php function foo() { yield from /* foo */baz(); }',
  2561. ],
  2562. [
  2563. '<?php function foo() { yield from /* foo */baz(); }',
  2564. '<?php function foo() { yield from
  2565. /* foo */baz(); }',
  2566. ],
  2567. ];
  2568. }
  2569. /**
  2570. * @dataProvider provideFixWithPhpOpenCases
  2571. */
  2572. public function testFixWithPhpOpen(string $expected, ?string $input = null): void
  2573. {
  2574. $this->fixer->configure([
  2575. 'constructs' => [
  2576. 'php_open',
  2577. ],
  2578. ]);
  2579. $this->doTest($expected, $input);
  2580. }
  2581. public static function provideFixWithPhpOpenCases(): iterable
  2582. {
  2583. return [
  2584. [
  2585. '<?php echo 1;',
  2586. '<?php echo 1;',
  2587. ],
  2588. [
  2589. "<?php\necho 1;",
  2590. ],
  2591. [
  2592. "<?php\n echo 1;",
  2593. ],
  2594. [
  2595. '<?php ',
  2596. ],
  2597. [
  2598. "<?php\n",
  2599. ],
  2600. [
  2601. "<?php \necho 1;",
  2602. ],
  2603. [
  2604. "<?php \n\necho 1;",
  2605. ],
  2606. ];
  2607. }
  2608. /**
  2609. * @dataProvider provideCommentsCases
  2610. */
  2611. public function testComments(string $expected, ?string $input = null): void
  2612. {
  2613. $this->fixer->configure([
  2614. 'constructs' => [
  2615. 'comment',
  2616. 'php_doc',
  2617. ],
  2618. ]);
  2619. $this->doTest($expected, $input);
  2620. }
  2621. public static function provideCommentsCases(): iterable
  2622. {
  2623. yield [
  2624. '<?php
  2625. $a /* 1 */ = /**/ 1;
  2626. $a /** 1 */ = /** 2 */ 1;
  2627. $a = 3; # 3
  2628. $a = 4; /** 4 */
  2629. echo 1;
  2630. ',
  2631. '<?php
  2632. $a /* 1 */= /**/1;
  2633. $a /** 1 */= /** 2 */1;
  2634. $a = 3; # 3
  2635. $a = 4; /** 4 */
  2636. echo 1;
  2637. ',
  2638. ];
  2639. yield 'exceptions' => [
  2640. '<?php
  2641. new Dummy(/* a */);
  2642. new Dummy(/** b */);
  2643. foo(/* c */);
  2644. foo($a /* d */, $b);
  2645. $arr = [/* empty */];
  2646. ',
  2647. ];
  2648. yield 'before_destructuring_square_brace_close' => [
  2649. '<?php
  2650. foreach ($fields as [$field/** @var string*/]) {
  2651. }
  2652. ',
  2653. ];
  2654. }
  2655. /**
  2656. * @dataProvider provideWithNamespaceCases
  2657. */
  2658. public function testWithNamespace(string $expected, ?string $input = null): void
  2659. {
  2660. $this->fixer->configure([
  2661. 'constructs' => [
  2662. 'namespace',
  2663. ],
  2664. ]);
  2665. $this->doTest($expected, $input);
  2666. }
  2667. public static function provideWithNamespaceCases(): iterable
  2668. {
  2669. yield 'simple' => [
  2670. '<?php
  2671. namespace Foo;
  2672. namespace Bar;',
  2673. '<?php
  2674. namespace Foo;
  2675. namespace Bar;',
  2676. ];
  2677. yield 'simple with newlines' => [
  2678. '<?php
  2679. namespace Foo;
  2680. namespace Bar;',
  2681. '<?php
  2682. namespace
  2683. Foo;
  2684. namespace
  2685. Bar;',
  2686. ];
  2687. yield 'braces' => [
  2688. '<?php
  2689. namespace Foo {}
  2690. namespace Bar {}',
  2691. '<?php
  2692. namespace Foo {}
  2693. namespace Bar {}',
  2694. ];
  2695. yield 'braces with newlines' => [
  2696. '<?php
  2697. namespace Foo {}
  2698. namespace Bar {}',
  2699. '<?php
  2700. namespace
  2701. Foo {}
  2702. namespace
  2703. Bar {}',
  2704. ];
  2705. yield 'with // comment' => [
  2706. '<?php
  2707. namespace // comment
  2708. Foo;',
  2709. ];
  2710. yield 'with /* comment */' => [
  2711. '<?php
  2712. namespace /* comment */ Foo;',
  2713. '<?php
  2714. namespace/* comment */ Foo;',
  2715. ];
  2716. }
  2717. /**
  2718. * @dataProvider provideFix80Cases
  2719. *
  2720. * @requires PHP 8.0
  2721. */
  2722. public function testFix80(string $expected, string $input): void
  2723. {
  2724. $this->doTest($expected, $input);
  2725. }
  2726. public static function provideFix80Cases(): iterable
  2727. {
  2728. yield 'match 1' => [
  2729. '<?php echo match ($x) {
  2730. 1, 2 => "Same for 1 and 2",
  2731. };',
  2732. '<?php echo match ($x) {
  2733. 1, 2 => "Same for 1 and 2",
  2734. };',
  2735. ];
  2736. yield 'match 2' => [
  2737. '<?php echo match ($x) {
  2738. 1, 2 => "Same for 1 and 2",
  2739. };',
  2740. '<?php echo match($x) {
  2741. 1, 2 => "Same for 1 and 2",
  2742. };',
  2743. ];
  2744. yield 'constructor property promotion' => [
  2745. '<?php
  2746. class Point {
  2747. public function __construct(
  2748. public float $x = 0.0,
  2749. protected float $y = 0.0,
  2750. private float $z = 0.0,
  2751. ) {}
  2752. }
  2753. ',
  2754. "<?php
  2755. class Point {
  2756. public function __construct(
  2757. public float \$x = 0.0,
  2758. protected\tfloat \$y = 0.0,
  2759. private\nfloat \$z = 0.0,
  2760. ) {}
  2761. }
  2762. ",
  2763. ];
  2764. yield 'attribute' => [
  2765. '<?php class Foo {
  2766. #[Required] // foo
  2767. public $bar1;
  2768. #[Required]
  2769. public $bar2;
  2770. }',
  2771. '<?php class Foo {
  2772. #[Required]// foo
  2773. public $bar1;
  2774. #[Required]
  2775. public $bar2;
  2776. }',
  2777. ];
  2778. yield 'named argument' => [
  2779. '<?php $foo(test: 1);',
  2780. '<?php $foo(test: 1);',
  2781. ];
  2782. }
  2783. /**
  2784. * @dataProvider provideFix81Cases
  2785. *
  2786. * @requires PHP 8.1
  2787. */
  2788. public function testFix81(string $expected, string $input): void
  2789. {
  2790. $this->doTest($expected, $input);
  2791. }
  2792. public static function provideFix81Cases(): iterable
  2793. {
  2794. yield 'readonly' => [
  2795. '<?php
  2796. final class Foo
  2797. {
  2798. public readonly string $prop;
  2799. public function __construct(
  2800. public readonly float $x = 0.0,
  2801. ) {}
  2802. }
  2803. ',
  2804. '<?php
  2805. final class Foo
  2806. {
  2807. public readonly string $prop;
  2808. public function __construct(
  2809. public readonly float $x = 0.0,
  2810. ) {}
  2811. }
  2812. ',
  2813. ];
  2814. yield 'enum' => [
  2815. '<?php
  2816. enum Suit {
  2817. case Hearts;
  2818. }
  2819. ',
  2820. '<?php
  2821. enum Suit {
  2822. case Hearts;
  2823. }
  2824. ',
  2825. ];
  2826. yield 'enum full caps' => [
  2827. '<?php
  2828. ENUM Suit {
  2829. case Hearts;
  2830. }
  2831. ',
  2832. '<?php
  2833. ENUM Suit {
  2834. case Hearts;
  2835. }
  2836. ',
  2837. ];
  2838. yield [
  2839. '<?php class Foo
  2840. {
  2841. final public const X = "foo";
  2842. }',
  2843. '<?php class Foo
  2844. {
  2845. final public const X = "foo";
  2846. }',
  2847. ];
  2848. yield [
  2849. '<?php
  2850. class Test {
  2851. public function __construct(
  2852. public $prop = new Foo,
  2853. ) {}
  2854. }
  2855. ',
  2856. '<?php
  2857. class Test {
  2858. public function __construct(
  2859. public $prop = new Foo,
  2860. ) {}
  2861. }
  2862. ',
  2863. ];
  2864. }
  2865. /**
  2866. * @dataProvider provideFixWithSwitchCases
  2867. */
  2868. public function testFixWithSwitch(string $expected, string $input): void
  2869. {
  2870. $this->fixer->configure([
  2871. 'constructs' => [
  2872. 'switch',
  2873. ],
  2874. ]);
  2875. $this->doTest($expected, $input);
  2876. }
  2877. public static function provideFixWithSwitchCases(): iterable
  2878. {
  2879. yield [
  2880. '<?php
  2881. switch ($a){ case 1: echo 123; }
  2882. switch ($b){ case 1: echo 123; }
  2883. ',
  2884. '<?php
  2885. switch($a){ case 1: echo 123; }
  2886. switch ($b){ case 1: echo 123; }
  2887. ',
  2888. ];
  2889. }
  2890. /**
  2891. * @dataProvider provideTypeColonCases
  2892. */
  2893. public function testTypeColon(string $expected, string $input): void
  2894. {
  2895. $this->fixer->configure([
  2896. 'constructs' => [
  2897. 'type_colon',
  2898. ],
  2899. ]);
  2900. $this->doTest($expected, $input);
  2901. }
  2902. public static function provideTypeColonCases(): iterable
  2903. {
  2904. yield [
  2905. '<?php function foo(): array { return []; }',
  2906. "<?php function foo():\narray { return []; }",
  2907. ];
  2908. yield [
  2909. '<?php interface F { public function foo(): array; }',
  2910. "<?php interface F { public function foo():\tarray; }",
  2911. ];
  2912. yield [
  2913. '<?php $a=1; $f = function () use($a): array {};',
  2914. '<?php $a=1; $f = function () use($a):array {};',
  2915. ];
  2916. yield [
  2917. '<?php fn() : array => [];',
  2918. '<?php fn() : array => [];',
  2919. ];
  2920. yield [
  2921. '<?php $a=1; $f = fn (): array => [];',
  2922. '<?php $a=1; $f = fn (): array => [];',
  2923. ];
  2924. }
  2925. /**
  2926. * @dataProvider provideEnumTypeColonCases
  2927. *
  2928. * @requires PHP 8.1
  2929. */
  2930. public function testEnumTypeColon(string $expected, string $input): void
  2931. {
  2932. $this->fixer->configure([
  2933. 'constructs' => [
  2934. 'type_colon',
  2935. ],
  2936. ]);
  2937. $this->doTest($expected, $input);
  2938. }
  2939. public static function provideEnumTypeColonCases(): iterable
  2940. {
  2941. yield [
  2942. '<?php enum Foo: int {}',
  2943. "<?php enum Foo:\nint {}",
  2944. ];
  2945. yield [
  2946. '<?php enum Foo: string {}',
  2947. '<?php enum Foo:string {}',
  2948. ];
  2949. }
  2950. }