BinaryOperatorSpacesFixerTest.php 76 KB

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