BinaryOperatorSpacesFixerTest.php 75 KB

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