BinaryOperatorSpacesFixerTest.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Operator;
  12. use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. * @author Gregor Harlan <gharlan@web.de>
  17. * @author Carlos Cirello <carlos.cirello.nl@gmail.com>
  18. * @author SpacePossum
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer
  23. */
  24. final class BinaryOperatorSpacesFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @param string $expected
  28. * @param null|string $input
  29. *
  30. * @dataProvider provideWithTabsCases
  31. */
  32. public function testWithTabs($expected, $input = null, array $configuration = [])
  33. {
  34. $this->fixer->configure($configuration);
  35. $this->doTest($expected, $input);
  36. }
  37. public function provideWithTabsCases()
  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 string $expected
  74. * @param null|string $input
  75. *
  76. * @dataProvider provideTestCases
  77. */
  78. public function testConfigured($expected, $input = null, array $configuration = [])
  79. {
  80. $this->fixer->configure($configuration);
  81. $this->doTest($expected, $input);
  82. }
  83. public function provideTestCases()
  84. {
  85. return [
  86. [
  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. [
  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. [
  117. '<?php
  118. $a//
  119. = 1;
  120. ',
  121. '<?php
  122. $a//
  123. = 1;
  124. ',
  125. ['operators' => ['=' => BinaryOperatorSpacesFixer::SINGLE_SPACE]],
  126. ],
  127. [
  128. '<?php
  129. $var = [];
  130. foreach ([
  131. 1 => 2,
  132. 2 => 3,
  133. ] as $k => $v) {
  134. $var[] = [$i => $bar];
  135. }',
  136. '<?php
  137. $var = [];
  138. foreach ([
  139. 1=> 2,
  140. 2 =>3,
  141. ] as $k => $v) {
  142. $var[] = [$i => $bar];
  143. }',
  144. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  145. ],
  146. [
  147. '<?php $a = array(
  148. 1 => 2, 4 => 5,
  149. 5 => 2, 6 => 5, 7 => 8, 9 => 10, 11 => 1222,
  150. );',
  151. '<?php $a = array(
  152. 1=>2, 4=>5,
  153. 5=>2, 6 => 5, 7=>8, 9=>10, 11=>1222,
  154. );',
  155. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  156. ],
  157. [
  158. '<?php $a = array(1 => 2, 4 => 5);',
  159. '<?php $a = array(1=>2, 4 => 5);',
  160. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  161. ],
  162. [
  163. '<?php $a = array(1 => 2, 4 => 5 && $b, 5 => 5 && $b, 6 => 5 && $b, 7 => 5 && $b);',
  164. '<?php $a = array(1 => 2, 4 => 5&&$b, 5 => 5 && $b, 6 => 5&& $b, 7 => 5 &&$b);',
  165. ['operators' => ['&&' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  166. ],
  167. [
  168. '<?php
  169. [1 => "foo"];
  170. [2 => "foo"];
  171. [3 => "foo"];
  172. ',
  173. '<?php
  174. [1 => "foo"];
  175. [2 =>"foo"];
  176. [3=>"foo"];
  177. ',
  178. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  179. ],
  180. [
  181. '<?php
  182. [1 => "foo"];
  183. [2 => "foo"];
  184. [3 => "foo"];
  185. ',
  186. '<?php
  187. [1 => "foo"];
  188. [2 =>"foo"];
  189. [3=>"foo"];
  190. ',
  191. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  192. ],
  193. [
  194. '<?php $a += 1;',
  195. '<?php $a+=1;',
  196. ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  197. ],
  198. [
  199. '<?php $a += 1;',
  200. '<?php $a+=1;',
  201. ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  202. ],
  203. [
  204. '<?php $a+=1;',
  205. null,
  206. ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN]],
  207. ],
  208. [
  209. '<?php
  210. $ade = $b !== $a;
  211. $b = $b !== $a;
  212. $c = $b !== $a;
  213. ',
  214. '<?php
  215. $ade = $b!== $a;
  216. $b = $b!== $a;
  217. $c = $b!==$a;
  218. ',
  219. ['operators' => ['!==' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  220. ],
  221. [
  222. '<?php
  223. $aab = $b !== $e;
  224. $b = $b !== $c;
  225. $c = $b !== $d;
  226. ',
  227. '<?php
  228. $aab = $b !==$e;
  229. $b = $b !==$c;
  230. $c = $b !==$d;
  231. ',
  232. ['operators' => ['!==' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  233. ],
  234. [
  235. '<?php
  236. $aaa*= 11;
  237. $b *= 21;
  238. $c *=31;
  239. $d = $e and $f;
  240. $d = $g or $h;
  241. ',
  242. '<?php
  243. $aaa*= 11;
  244. $b *= 21;
  245. $c*=31;
  246. $d = $e and $f;
  247. $d = $g or $h;
  248. ',
  249. [
  250. 'operators' => [
  251. 'and' => BinaryOperatorSpacesFixer::SINGLE_SPACE,
  252. '*=' => BinaryOperatorSpacesFixer::ALIGN,
  253. 'or' => null,
  254. ],
  255. ],
  256. ],
  257. [
  258. '<?php
  259. $abc = $b !== $a;
  260. $b = $b !== $a;
  261. $c = $b !== $a;
  262. ',
  263. '<?php
  264. $abc = $b !== $a;
  265. $b = $b !== $a;
  266. $c = $b !== $a;
  267. ',
  268. ['operators' => ['!==' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  269. ],
  270. [
  271. '<?php $a = [
  272. 1 => 2,
  273. 2 => 3,
  274. ];',
  275. '<?php $a = [
  276. 1=>2,
  277. 2 => 3,
  278. ];',
  279. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  280. ],
  281. [
  282. '<?php
  283. [1 => "foo",
  284. 2 => "foo"];
  285. ',
  286. '<?php
  287. [1 => "foo",
  288. 2 => "foo"];
  289. ',
  290. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  291. ],
  292. [
  293. '<?php
  294. [1 => "foo"];
  295. $i += 1;
  296. ',
  297. '<?php
  298. [1 => "foo"];
  299. $i+= 1;
  300. ',
  301. ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  302. ],
  303. [
  304. '<?php $a = 1 + 2; $b = array(
  305. 13 =>3,
  306. 4 => 3,
  307. 5=>2,
  308. );',
  309. null,
  310. ['default' => null],
  311. ],
  312. [
  313. '<?php $a = 1 + 2; $b = array(
  314. $øøø => $ø0ø0ø,
  315. $ø4 => $ø1ø1ø,
  316. $ø5 => $ø2ø2ø,
  317. );
  318. $a = 12 + 1;
  319. $a = 13 + 41;
  320. ',
  321. '<?php $a = 1 + 2; $b = array(
  322. $øøø =>$ø0ø0ø,
  323. $ø4 => $ø1ø1ø,
  324. $ø5=>$ø2ø2ø,
  325. );
  326. $a = 12 + 1;
  327. $a = 13+41;
  328. ',
  329. ['default' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL],
  330. ],
  331. 'do not align with nor touch strings' => [
  332. '<?php
  333. \putenv("{$name}= {$value}");
  334. $b = $c + 1;
  335. $b = $c - 1;
  336. ',
  337. '<?php
  338. \putenv("{$name}= {$value}");
  339. $b =$c+1;
  340. $b =$c - 1;
  341. ',
  342. ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  343. ],
  344. 'do not align with declare' => [
  345. '<?php
  346. declare(ticks=1);
  347. $a = 1;
  348. $b = 1;
  349. ',
  350. '<?php
  351. declare(ticks=1);
  352. $a = 1;
  353. $b = 1;
  354. ',
  355. ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  356. ],
  357. 'do not align with multibyte character in array key' => [
  358. '<?php
  359. $map = [
  360. "ø" => "oe",
  361. ];
  362. ',
  363. null,
  364. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  365. ],
  366. 'align correctly with multibyte characters in array key' => [
  367. '<?php
  368. $inflect_male = array(
  369. "aitė\b" => "øasø",
  370. "ytė\b" => "øisø",
  371. "iūtė\b" => "øiusø",
  372. "utė\b" => array(
  373. "aitė\b" => "øas",
  374. "ytė\b" => "øis",
  375. "iūtė\b" => $øøius,
  376. "utė\b" => "us",
  377. ),
  378. );',
  379. '<?php
  380. $inflect_male = array(
  381. "aitė\b" => "øasø",
  382. "ytė\b" => "øisø",
  383. "iūtė\b" => "øiusø",
  384. "utė\b" => array(
  385. "aitė\b" => "øas",
  386. "ytė\b" => "øis",
  387. "iūtė\b" => $øøius,
  388. "utė\b" => "us",
  389. ),
  390. );',
  391. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  392. ],
  393. [
  394. '<?php
  395. $foo = 1+$bar;
  396. ',
  397. '<?php
  398. $foo = 1 + $bar;
  399. ',
  400. [
  401. 'default' => BinaryOperatorSpacesFixer::NO_SPACE,
  402. 'operators' => ['=' => BinaryOperatorSpacesFixer::SINGLE_SPACE],
  403. ],
  404. ],
  405. [
  406. '<?php
  407. $foo = 1 + $bar|$a;
  408. ',
  409. '<?php
  410. $foo = 1 + $bar | $a;
  411. ',
  412. [
  413. 'default' => null,
  414. 'operators' => [
  415. '=' => BinaryOperatorSpacesFixer::SINGLE_SPACE,
  416. '|' => BinaryOperatorSpacesFixer::NO_SPACE,
  417. ],
  418. ],
  419. ],
  420. [
  421. '<?php
  422. $foo = $d #
  423. |
  424. #
  425. $a| // foo
  426. $b#
  427. |$d;
  428. ',
  429. '<?php
  430. $foo = $d #
  431. |
  432. #
  433. $a | // foo
  434. $b#
  435. | $d;
  436. ',
  437. [
  438. 'operators' => ['|' => BinaryOperatorSpacesFixer::NO_SPACE],
  439. ],
  440. ],
  441. ];
  442. }
  443. /**
  444. * @param string $expected
  445. * @param null|string $input
  446. *
  447. * @dataProvider provideFixCases
  448. */
  449. public function testFixDefaults($expected, $input = null)
  450. {
  451. $this->doTest($expected, $input);
  452. }
  453. public function provideFixCases()
  454. {
  455. return [
  456. [
  457. '<?php $a + /** */
  458. $b;',
  459. '<?php $a + /** */
  460. $b;',
  461. ],
  462. [
  463. '<?php '.'
  464. $a
  465. + $b
  466. + $d;
  467. ;',
  468. '<?php '.'
  469. $a
  470. +$b
  471. + $d;
  472. ;',
  473. ],
  474. [
  475. '<?php
  476. $a
  477. /***/ + $b
  478. /***/ + $d;
  479. ;',
  480. '<?php
  481. $a
  482. /***/+ $b
  483. /***/ +$d;
  484. ;',
  485. ],
  486. [
  487. '<?php $a + $b;',
  488. '<?php $a+$b;',
  489. ],
  490. [
  491. '<?php 1 + $b;',
  492. '<?php 1+$b;',
  493. ],
  494. [
  495. '<?php 0.2 + $b;',
  496. '<?php 0.2+$b;',
  497. ],
  498. [
  499. '<?php $a[1] + $b;',
  500. '<?php $a[1]+$b;',
  501. ],
  502. [
  503. '<?php FOO + $b;',
  504. '<?php FOO+$b;',
  505. ],
  506. [
  507. '<?php foo() + $b;',
  508. '<?php foo()+$b;',
  509. ],
  510. [
  511. '<?php ${"foo"} + $b;',
  512. '<?php ${"foo"}+$b;',
  513. ],
  514. [
  515. '<?php $a & $b;',
  516. '<?php $a&$b;',
  517. ],
  518. [
  519. '<?php $a &= $b;',
  520. '<?php $a&=$b;',
  521. ],
  522. [
  523. '<?php $a &= $b;',
  524. '<?php $a &=$b;',
  525. ],
  526. [
  527. '<?php $a &= $b;',
  528. '<?php $a&= $b;',
  529. ],
  530. [
  531. '<?php $a &= $b;',
  532. '<?php $a &= $b;',
  533. ],
  534. [
  535. '<?php $a &=
  536. $b;',
  537. ],
  538. [
  539. '<?php $a
  540. &= $b;',
  541. '<?php $a
  542. &=$b;',
  543. ],
  544. [
  545. '<?php (1) and 2;',
  546. '<?php (1)and 2;',
  547. ],
  548. [
  549. '<?php 1 or ($b - $c);',
  550. '<?php 1 or($b-$c);',
  551. ],
  552. [
  553. '<?php "a" xor (2);',
  554. '<?php "a"xor(2);',
  555. ],
  556. [
  557. '<?php $a * -$b;',
  558. '<?php $a*-$b;',
  559. ],
  560. [
  561. '<?php $a = -2 / +5;',
  562. '<?php $a=-2/+5;',
  563. ],
  564. [
  565. '<?php $a = &$b;',
  566. '<?php $a=&$b;',
  567. ],
  568. [
  569. '<?php $a++ + $b;',
  570. '<?php $a+++$b;',
  571. ],
  572. [
  573. '<?php __LINE__ - 1;',
  574. '<?php __LINE__-1;',
  575. ],
  576. [
  577. '<?php `echo 1` + 1;',
  578. '<?php `echo 1`+1;',
  579. ],
  580. [
  581. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  582. ],
  583. [
  584. '<?php $a = 1 //
  585. || 2;
  586. ',
  587. ],
  588. [
  589. '<?php $a =
  590. 2;',
  591. ],
  592. [
  593. '<?php declare(ticks=1);',
  594. ],
  595. [
  596. '<?php declare(ticks = 1);',
  597. ],
  598. [
  599. '<?php $a = 1;declare(ticks = 1);$b = 1;',
  600. '<?php $a=1;declare(ticks = 1);$b=1;',
  601. ],
  602. [
  603. '<?php $a = array("b" => "c", );',
  604. '<?php $a = array("b"=>"c", );',
  605. ],
  606. [
  607. '<?php $a = array("b" => "c", );',
  608. '<?php $a = array("b" =>"c", );',
  609. ],
  610. [
  611. '<?php $a = array("b" => "c", );',
  612. '<?php $a = array("b"=> "c", );',
  613. ],
  614. [
  615. '<?php [1, 2] + [3, 4];',
  616. '<?php [1, 2]+[3, 4];',
  617. ],
  618. [
  619. '<?php [1, 2] + [3, 4];',
  620. '<?php [1, 2] + [3, 4];',
  621. ],
  622. [
  623. '<?php [1, 2] + // '.'
  624. [3, 4];',
  625. '<?php [1, 2] + // '.'
  626. [3, 4];',
  627. ],
  628. [
  629. '<?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;',
  630. '<?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;',
  631. ],
  632. [
  633. '<?php
  634. $c =
  635. $a
  636. +
  637. $b;
  638. ',
  639. ],
  640. ];
  641. }
  642. /**
  643. * @param string $expected
  644. * @param null|string $input
  645. *
  646. * @dataProvider provideUnalignEqualsCases
  647. */
  648. public function testUnalignEquals($expected, $input = null)
  649. {
  650. $this->doTest($expected, $input);
  651. }
  652. public function provideUnalignEqualsCases()
  653. {
  654. return [
  655. [
  656. '<?php $a = "c"?>',
  657. '<?php $a="c"?>',
  658. ],
  659. [
  660. '<?php $a = "c";',
  661. '<?php $a ="c";',
  662. ],
  663. [
  664. '<?php $a = "c";',
  665. '<?php $a= "c";',
  666. ],
  667. [
  668. '<?php $d = $c + $a/**/ + //
  669. $b;',
  670. '<?php $d = $c+$a/**/+ //
  671. $b;',
  672. ],
  673. [
  674. '<?php
  675. $a = 1;
  676. $bbbb = \'
  677. $cccccccc = 3;
  678. \';',
  679. '<?php
  680. $a = 1;
  681. $bbbb = \'
  682. $cccccccc = 3;
  683. \';',
  684. ],
  685. [
  686. '<?php
  687. $ccc = 1;
  688. $bb = 1;
  689. $a = 1;
  690. /*
  691. Others alignments
  692. */
  693. $a[$b = 1] = 1;
  694. $ab[$bc = 1] = 1;
  695. $abc[$bcd = 1] = 1;
  696. $a[$b] = 1;
  697. $ab[$bc] = 1;
  698. $abc[$bcd] = 1;
  699. if ($a = 1) {
  700. $ccc = 1;
  701. $bb = 1;
  702. $a = 1;
  703. }
  704. function a($a = 1, $b = 2, $c = 3)
  705. {
  706. $a[$b = 1] = 1;
  707. $ab[$bc = 1] = 1;
  708. $abc[$bcd = 1] = 1;
  709. }
  710. function b(
  711. $a = 1,
  712. $bbb = 2,
  713. $c = 3
  714. ) {
  715. $a[$b = 1] = 1;
  716. $ab[$bc = 1] = 1;
  717. $abc[$bcd = 1] = 1;
  718. }
  719. while (false) {
  720. $aa = 2;
  721. $a[$b] = array();
  722. }
  723. for ($i = 0; $i < 10; $i++) {
  724. $aa = 2;
  725. $a[$b] = array();
  726. }',
  727. '<?php
  728. $ccc = 1;
  729. $bb = 1;
  730. $a = 1;
  731. /*
  732. Others alignments
  733. */
  734. $a[$b = 1] = 1;
  735. $ab[$bc = 1] = 1;
  736. $abc[$bcd = 1] = 1;
  737. $a[$b] = 1;
  738. $ab[$bc] = 1;
  739. $abc[$bcd] = 1;
  740. if ($a = 1) {
  741. $ccc = 1;
  742. $bb = 1;
  743. $a = 1;
  744. }
  745. function a($a = 1, $b = 2, $c = 3)
  746. {
  747. $a[$b = 1] = 1;
  748. $ab[$bc = 1] = 1;
  749. $abc[$bcd = 1] = 1;
  750. }
  751. function b(
  752. $a = 1,
  753. $bbb = 2,
  754. $c = 3
  755. ) {
  756. $a[$b = 1] = 1;
  757. $ab[$bc = 1] = 1;
  758. $abc[$bcd = 1] = 1;
  759. }
  760. while (false) {
  761. $aa = 2;
  762. $a[$b] = array();
  763. }
  764. for ($i = 0; $i < 10; $i++) {
  765. $aa = 2;
  766. $a[$b] = array();
  767. }',
  768. ],
  769. ];
  770. }
  771. public function testWrongConfigItem()
  772. {
  773. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  774. $this->expectExceptionMessageRegExp(
  775. '/^\[binary_operator_spaces\] Invalid configuration: The option "foo" does not exist\. Defined options are: "default", "operators"\.$/'
  776. );
  777. $this->fixer->configure(['foo' => true]);
  778. }
  779. public function testWrongConfigTypeForOperators()
  780. {
  781. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  782. $this->expectExceptionMessageRegExp(
  783. '/^\[binary_operator_spaces\] Invalid configuration: The option "operators" with value true is expected to be of type "array", but is of type "(bool|boolean)"\.$/'
  784. );
  785. $this->fixer->configure(['operators' => true]);
  786. }
  787. public function testWrongConfigTypeForOperatorsKey()
  788. {
  789. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  790. $this->expectExceptionMessageRegExp('/^\[binary_operator_spaces\] Invalid configuration: Unexpected "operators" key, expected any of ".*", got "integer#123"\.$/');
  791. $this->fixer->configure(['operators' => [123 => 1]]);
  792. }
  793. public function testWrongConfigTypeForOperatorsKeyValue()
  794. {
  795. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  796. $this->expectExceptionMessageRegExp('/^\[binary_operator_spaces\] Invalid configuration: Unexpected value for operator "\+", expected any of ".*", got "string#abc"\.$/');
  797. $this->fixer->configure(['operators' => ['+' => 'abc']]);
  798. }
  799. /**
  800. * @param string $expected
  801. * @param null|string $input
  802. *
  803. * @dataProvider provideUnalignDoubleArrowCases
  804. */
  805. public function testUnalignDoubleArrow($expected, $input = null)
  806. {
  807. $this->doTest($expected, $input);
  808. }
  809. public function provideUnalignDoubleArrowCases()
  810. {
  811. return [
  812. [
  813. '<?php
  814. $data = [
  815. "foo" => "Bar",
  816. "main" => array(
  817. [
  818. "baz" => "Test",
  819. "bazaa" => $a->{"Test"},
  820. "bazaa" => $a["Test"],
  821. "bazaaaa" => b("Test"),
  822. ]
  823. ),
  824. "bar" => array(),
  825. ];',
  826. '<?php
  827. $data = [
  828. "foo" => "Bar",
  829. "main" => array(
  830. [
  831. "baz" => "Test",
  832. "bazaa" => $a->{"Test"},
  833. "bazaa" => $a["Test"],
  834. "bazaaaa" => b("Test"),
  835. ]
  836. ),
  837. "bar" => array(),
  838. ];',
  839. ],
  840. [
  841. '<?php
  842. $data = [
  843. "foo" => "Bar",
  844. "main" => [array("baz" => "Test")],
  845. "bar" => array(),
  846. ];
  847. $data = array(
  848. "foo" => "Bar",
  849. "main" => array("baz" => "Test"),
  850. "bar" => array(),
  851. );
  852. $var = [];
  853. foreach ($foo as $i => $bar) {
  854. $var[] = /* Comment */ [$i => $bar];
  855. }',
  856. '<?php
  857. $data = [
  858. "foo" => "Bar",
  859. "main" => [array("baz" => "Test")],
  860. "bar" => array(),
  861. ];
  862. $data = array(
  863. "foo" => "Bar",
  864. "main" => array("baz" => "Test"),
  865. "bar" => array(),
  866. );
  867. $var = [];
  868. foreach ($foo as $i => $bar) {
  869. $var[] = /* Comment */ [$i => $bar];
  870. }',
  871. ],
  872. [
  873. '<?php
  874. $data = [
  875. "foo" => "Bar",
  876. "main" => [array("baz" => "Test")],
  877. "bar" => array(),
  878. ];',
  879. '<?php
  880. $data = [
  881. "foo" => "Bar",
  882. "main" => [array("baz" => "Test")],
  883. "bar" => array(),
  884. ];',
  885. ],
  886. [
  887. '<?php
  888. $data = array(
  889. "foo" => "Bar",
  890. "main" => array("baz" => "Test"),
  891. "bar" => array(),
  892. );',
  893. '<?php
  894. $data = array(
  895. "foo" => "Bar",
  896. "main" => array("baz" => "Test"),
  897. "bar" => array(),
  898. );',
  899. ],
  900. [
  901. '<?php
  902. $data = array(
  903. "foo" => "Bar",
  904. "main" => array(array("baz" => "Test")),
  905. "bar" => array(),
  906. );',
  907. '<?php
  908. $data = array(
  909. "foo" => "Bar",
  910. "main" => array(array("baz" => "Test")),
  911. "bar" => array(),
  912. );',
  913. ],
  914. [
  915. '<?php
  916. $var = [];
  917. foreach ($foo as $i => $bar) {
  918. $var[] = /* Comment */ [$i => $bar];
  919. }',
  920. '<?php
  921. $var = [];
  922. foreach ($foo as $i => $bar) {
  923. $var[] = /* Comment */ [$i => $bar];
  924. }',
  925. ],
  926. [
  927. '<?php
  928. $var = [];
  929. foreach ($foo as $i => $bar) {
  930. $var[] = [$i => $bar];
  931. }',
  932. ],
  933. [
  934. '<?php
  935. $var = [];
  936. foreach ([1 => 2] as $k => $v) {
  937. $var[] = [$i => $bar];
  938. }',
  939. ],
  940. [
  941. '<?php
  942. $var = [];
  943. foreach (fncCall() as $k => $v){
  944. $var[] = [$i => $bar];
  945. }',
  946. ],
  947. [
  948. '<?php
  949. $var = [];
  950. foreach ($foo as $bar) {
  951. $var[] = [
  952. $i => $bar,
  953. $iaaa => $bar,
  954. ];
  955. }',
  956. '<?php
  957. $var = [];
  958. foreach ($foo as $bar) {
  959. $var[] = [
  960. $i => $bar,
  961. $iaaa => $bar,
  962. ];
  963. }',
  964. ],
  965. [
  966. '<?php
  967. $data = [
  968. "foo" => "Bar",
  969. "main" => [["baz" => "Test", "bar" => "Test2"]],
  970. "bar" => [],
  971. ];',
  972. '<?php
  973. $data = [
  974. "foo" => "Bar",
  975. "main" => [["baz" => "Test", "bar" => "Test2"]],
  976. "bar" => [],
  977. ];',
  978. ],
  979. [
  980. '<?php
  981. $a = [
  982. 0 => 1,
  983. 10 /*Comment*/ => [
  984. 1 => 2,
  985. 22 => 3,
  986. ],
  987. 100 => [
  988. 1 => 2,
  989. 22 => 3,
  990. ]
  991. ];',
  992. '<?php
  993. $a = [
  994. 0 => 1,
  995. 10 /*Comment*/ => [
  996. 1 => 2,
  997. 22 => 3,
  998. ],
  999. 100 => [
  1000. 1 => 2,
  1001. 22 => 3,
  1002. ]
  1003. ];',
  1004. ],
  1005. [
  1006. '<?php
  1007. $a = array(
  1008. 0 => 1,
  1009. 10 => array(
  1010. 1 => 2,
  1011. 22 => 3,
  1012. ),
  1013. 100 => array(
  1014. 1 => 2,
  1015. 22 => 3,
  1016. )
  1017. );',
  1018. '<?php
  1019. $a = array(
  1020. 0 => 1,
  1021. 10 => array(
  1022. 1 => 2,
  1023. 22 => 3,
  1024. ),
  1025. 100 => array(
  1026. 1 => 2,
  1027. 22 => 3,
  1028. )
  1029. );',
  1030. ],
  1031. [
  1032. '<?php
  1033. $arr = array(
  1034. $a => 1,
  1035. $bbbb => \'
  1036. $cccccccc = 3;
  1037. \',
  1038. );',
  1039. '<?php
  1040. $arr = array(
  1041. $a => 1,
  1042. $bbbb => \'
  1043. $cccccccc = 3;
  1044. \',
  1045. );',
  1046. ],
  1047. [
  1048. '<?php
  1049. $arr = [
  1050. $a => 1,
  1051. $bbbb => \'
  1052. $cccccccc = 3;
  1053. \',
  1054. ];',
  1055. '<?php
  1056. $arr = [
  1057. $a => 1,
  1058. $bbbb => \'
  1059. $cccccccc = 3;
  1060. \',
  1061. ];',
  1062. ],
  1063. [
  1064. '<?php
  1065. foreach($arr as $k => $v){
  1066. $arr = array($k => 1,
  1067. $a => 1,
  1068. $bbbb => \'
  1069. $cccccccc = 3;
  1070. \',
  1071. );
  1072. }',
  1073. '<?php
  1074. foreach($arr as $k => $v){
  1075. $arr = array($k => 1,
  1076. $a => 1,
  1077. $bbbb => \'
  1078. $cccccccc = 3;
  1079. \',
  1080. );
  1081. }',
  1082. ],
  1083. [
  1084. '<?php
  1085. $a = array(
  1086. 10 => 11,
  1087. 20 => 22,
  1088. 30 => 33,
  1089. 40
  1090. =>
  1091. 44,
  1092. );',
  1093. '<?php
  1094. $a = array(
  1095. 10 => 11,
  1096. 20 => 22,
  1097. 30=>33,
  1098. 40
  1099. =>
  1100. 44,
  1101. );',
  1102. ],
  1103. [
  1104. '<?php
  1105. return array(
  1106. " " => "", "\t" => "",
  1107. "\n" => "", "\r" => "",
  1108. "\0" => "", "\x0B" => "",
  1109. );',
  1110. '<?php
  1111. return array(
  1112. " " => "", "\t" => "",
  1113. "\n" => "", "\r" => "",
  1114. "\0" => "", "\x0B" => "",
  1115. );',
  1116. ],
  1117. [
  1118. '<?php
  1119. return $this->grabAttribsBeforeToken(
  1120. $tokens,
  1121. $index,
  1122. $tokenAttribsMap,
  1123. array(
  1124. "abstract" => null,
  1125. "final" => null,
  1126. "visibility" => new Token(array(T_PUBLIC, "public")),
  1127. "static" => null,
  1128. )
  1129. );',
  1130. '<?php
  1131. return $this->grabAttribsBeforeToken(
  1132. $tokens,
  1133. $index,
  1134. $tokenAttribsMap,
  1135. array(
  1136. "abstract" => null,
  1137. "final" => null,
  1138. "visibility" => new Token(array(T_PUBLIC, "public")),
  1139. "static" => null,
  1140. )
  1141. );',
  1142. ],
  1143. [
  1144. '<?php
  1145. return array(
  1146. self::STATUS_UNKNOWN_0 => array("symbol" => "?", "description" => "unknown"),
  1147. self::STATUS_INVALID_0 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1148. );',
  1149. '<?php
  1150. return array(
  1151. self::STATUS_UNKNOWN_0 => array("symbol" => "?", "description" => "unknown"),
  1152. self::STATUS_INVALID_0 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1153. );',
  1154. ],
  1155. [
  1156. '<?php
  1157. $array = array(
  1158. "bazab" => b(array(
  1159. 1 => 2,
  1160. 5 => [
  1161. 6 => 7,
  1162. 8 => 9,
  1163. ],
  1164. 3 => 4,
  1165. 10 => 11,
  1166. )),
  1167. );',
  1168. '<?php
  1169. $array = array(
  1170. "bazab" => b(array(
  1171. 1 => 2,
  1172. 5 => [
  1173. 6 => 7,
  1174. 8 => 9,
  1175. ],
  1176. 3 => 4,
  1177. 10 => 11,
  1178. )),
  1179. );',
  1180. ],
  1181. [
  1182. '<?php
  1183. Foo::test()->aaa(array(1 => 2))->bbb("a", "b");
  1184. ',
  1185. ],
  1186. [
  1187. '<?php
  1188. function foo() {
  1189. yield 1 => 2;
  1190. }',
  1191. ],
  1192. ];
  1193. }
  1194. /**
  1195. * @param string $expected
  1196. * @param null|string $input
  1197. *
  1198. * @dataProvider provideAlignEqualsCases
  1199. */
  1200. public function testFixAlignEquals($expected, $input = null)
  1201. {
  1202. $this->fixer->configure(['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN]]);
  1203. $this->doTest($expected, $input);
  1204. }
  1205. public function provideAlignEqualsCases()
  1206. {
  1207. return [
  1208. [
  1209. '<?php
  1210. $a = 1;
  1211. $bbbb = \'
  1212. $ddcccccc1 = 3;
  1213. \';',
  1214. '<?php
  1215. $a = 1;
  1216. $bbbb = \'
  1217. $ddcccccc1 = 3;
  1218. \';',
  1219. ],
  1220. [
  1221. '<?php
  1222. $ccc = 1;
  1223. $bb = 1;
  1224. $a = 1;
  1225. /*
  1226. Others alignments
  1227. */
  1228. $a[$b = 1] = 1;
  1229. $ab[$bc = 1] = 1;
  1230. $abc[$bcd = 1] = 1;
  1231. $a[$b] = 1;
  1232. $ab[$bc] = 1;
  1233. $abc[$bcd] = 1;
  1234. if ($a = 1) {
  1235. $ccc = 1;
  1236. $bb = 1;
  1237. $a = 1;
  1238. }
  1239. function a($a = 1, $b = 2, $c = 3)
  1240. {
  1241. $a[$b = 1] = 1;
  1242. $ab[$bc = 1] = 1;
  1243. $abc[$bcd = 1] = 1;
  1244. }
  1245. function b(
  1246. $a = 1,
  1247. $bbb = 2,
  1248. $c = 3
  1249. ) {
  1250. $a[$b = 1] = 1;
  1251. $ab[$bc = 1] = 1;
  1252. $abc[$bcd = 1] = 1;
  1253. }
  1254. while (false) {
  1255. $aa = 2;
  1256. $a[$b] = array();
  1257. }
  1258. for ($i = 0; $i < 10; $i++) {
  1259. $aa = 2;
  1260. $a[$b] = array();
  1261. }',
  1262. '<?php
  1263. $ccc = 1;
  1264. $bb = 1;
  1265. $a = 1;
  1266. /*
  1267. Others alignments
  1268. */
  1269. $a[$b = 1] = 1;
  1270. $ab[$bc = 1] = 1;
  1271. $abc[$bcd = 1] = 1;
  1272. $a[$b] = 1;
  1273. $ab[$bc] = 1;
  1274. $abc[$bcd] = 1;
  1275. if ($a = 1) {
  1276. $ccc = 1;
  1277. $bb = 1;
  1278. $a = 1;
  1279. }
  1280. function a($a = 1, $b = 2, $c = 3)
  1281. {
  1282. $a[$b = 1] = 1;
  1283. $ab[$bc = 1] = 1;
  1284. $abc[$bcd = 1] = 1;
  1285. }
  1286. function b(
  1287. $a = 1,
  1288. $bbb = 2,
  1289. $c = 3
  1290. ) {
  1291. $a[$b = 1] = 1;
  1292. $ab[$bc = 1] = 1;
  1293. $abc[$bcd = 1] = 1;
  1294. }
  1295. while (false) {
  1296. $aa = 2;
  1297. $a[$b] = array();
  1298. }
  1299. for ($i = 0; $i < 10; $i++) {
  1300. $aa = 2;
  1301. $a[$b] = array();
  1302. }',
  1303. ],
  1304. ];
  1305. }
  1306. /**
  1307. * @param string $expected
  1308. * @param null|string $input
  1309. *
  1310. * @dataProvider provideAlignDoubleArrowCases
  1311. */
  1312. public function testFixAlignDoubleArrow($expected, $input = null)
  1313. {
  1314. $this->fixer->configure(['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN]]);
  1315. $this->doTest($expected, $input);
  1316. }
  1317. public function provideAlignDoubleArrowCases()
  1318. {
  1319. return [
  1320. [
  1321. '<?php
  1322. switch ($a) {
  1323. case "prod":
  1324. break;
  1325. }
  1326. ',
  1327. ],
  1328. [
  1329. '<?php
  1330. $array = array(
  1331. "closure" => function ($param1, $param2) {
  1332. return;
  1333. }
  1334. );',
  1335. ],
  1336. [
  1337. '<?php
  1338. return new JsonResponse(array(
  1339. "result" => "OK",
  1340. "html" => 1, /**/array(
  1341. "foo" => "bar",
  1342. "foofoo" => array(
  1343. "a" => 1,
  1344. "b" => 2
  1345. )
  1346. ),)
  1347. );',
  1348. '<?php
  1349. return new JsonResponse(array(
  1350. "result" => "OK",
  1351. "html" => 1, /**/array(
  1352. "foo" => "bar",
  1353. "foofoo" => array(
  1354. "a" => 1,
  1355. "b" => 2
  1356. )
  1357. ),)
  1358. );',
  1359. ],
  1360. [
  1361. '<?php
  1362. return new JsonResponse([
  1363. "result" => "OK",
  1364. "html" => renderView("views/my_view.html.twig", array(
  1365. "foo" => "bar",
  1366. "foofoo" => 43,
  1367. )),
  1368. ]);',
  1369. '<?php
  1370. return new JsonResponse([
  1371. "result" => "OK",
  1372. "html" => renderView("views/my_view.html.twig", array(
  1373. "foo" => "bar",
  1374. "foofoo" => 43,
  1375. )),
  1376. ]);',
  1377. ],
  1378. [
  1379. '<?php
  1380. return new JsonResponse([
  1381. "result" => "OK",
  1382. "html" => renderView("views/my_view.html.twig", [
  1383. "foo" => "bar",
  1384. "foofoo" => 42,
  1385. ]),
  1386. "baz" => "OK",
  1387. ]);',
  1388. '<?php
  1389. return new JsonResponse([
  1390. "result" => "OK",
  1391. "html" => renderView("views/my_view.html.twig", [
  1392. "foo" => "bar",
  1393. "foofoo" => 42,
  1394. ]),
  1395. "baz" => "OK",
  1396. ]);',
  1397. ],
  1398. [
  1399. '<?php
  1400. $data = [
  1401. "foo" => "Bar",
  1402. "main" => array(
  1403. [
  1404. "baz" => "Test",
  1405. "bazaa" => $a->{"Test"},
  1406. "bazaa" => $a["Test"],
  1407. "bazaaaa" => b("Test"),
  1408. ]
  1409. ),
  1410. "bar" => array(),
  1411. ];',
  1412. '<?php
  1413. $data = [
  1414. "foo" => "Bar",
  1415. "main" => array(
  1416. [
  1417. "baz" => "Test",
  1418. "bazaa" => $a->{"Test"},
  1419. "bazaa" => $a["Test"],
  1420. "bazaaaa" => b("Test"),
  1421. ]
  1422. ),
  1423. "bar" => array(),
  1424. ];',
  1425. ],
  1426. [
  1427. '<?php
  1428. $data = [
  1429. "foo" => "Bar",
  1430. "main" => [array("baz" => "Test")],
  1431. "bar" => array(),
  1432. ];
  1433. $data = array(
  1434. "foo" => "Bar",
  1435. "main" => array("baz" => "Test"),
  1436. "bar" => array(),
  1437. );
  1438. $var = [];
  1439. foreach ($foo as $i => $bar) {
  1440. $var[] = /* Comment */ [$i => $bar];
  1441. }',
  1442. ],
  1443. [
  1444. '<?php
  1445. $data = [
  1446. "foo" => "Bar",
  1447. "main" => [array("baz" => "Test")],
  1448. "bar" => array(),
  1449. ];',
  1450. ],
  1451. [
  1452. '<?php
  1453. $data = array(
  1454. "foo" => "Bar",
  1455. "main" => array("baz" => "Test"),
  1456. "bar" => array(),
  1457. );',
  1458. ],
  1459. [
  1460. '<?php
  1461. $data = array(
  1462. "foo" => "Bar",
  1463. "main" => array(array("baz" => "Test")),
  1464. "bar" => array(),
  1465. );',
  1466. ],
  1467. [
  1468. '<?php
  1469. $var = [];
  1470. foreach ($foo as $i => $bar) {
  1471. $var[] = /* Comment */ [$i => $bar];
  1472. }',
  1473. ],
  1474. [
  1475. '<?php
  1476. $var = [];
  1477. foreach ($foo as $i => $bar) {
  1478. $var[] = [$i => $bar];
  1479. }',
  1480. ],
  1481. [
  1482. '<?php
  1483. $var = [];
  1484. foreach ([1 => 2] as $k => $v) {
  1485. $var[] = [$i => $bar];
  1486. }',
  1487. ],
  1488. [
  1489. '<?php
  1490. $var = [];
  1491. foreach (fncCall() as $k => $v){
  1492. $var[] = [$i => $bar];
  1493. }',
  1494. ],
  1495. [
  1496. '<?php
  1497. $var = [];
  1498. foreach ($foo as $bar) {
  1499. $var[] = [
  1500. $i => $bar,
  1501. $iaaa => $bar,
  1502. ];
  1503. }',
  1504. ],
  1505. [
  1506. '<?php
  1507. $data = [
  1508. "foo" => "Bar",
  1509. "main" => [["baz" => "Test", "bar" => "Test2"]],
  1510. "bar" => [],
  1511. ];',
  1512. ],
  1513. [
  1514. '<?php
  1515. $data = [
  1516. "foo" => "Bar",
  1517. "main" => ["baz" => "Test"],
  1518. "bar" => [],
  1519. ];',
  1520. ],
  1521. [
  1522. '<?php
  1523. $a = [
  1524. 0 => 1,
  1525. 10 /*Comment*/ => [
  1526. 1 => 2,
  1527. 22 => 3,
  1528. ],
  1529. 100 => [
  1530. 1 => 2,
  1531. 22 => 3,
  1532. ]
  1533. ];',
  1534. '<?php
  1535. $a = [
  1536. 0 => 1,
  1537. 10 /*Comment*/ => [
  1538. 1 => 2,
  1539. 22 => 3,
  1540. ],
  1541. 100 => [
  1542. 1 => 2,
  1543. 22 => 3,
  1544. ]
  1545. ];',
  1546. ],
  1547. [
  1548. '<?php
  1549. $a = array(
  1550. 0 => 1,
  1551. 10 => array(
  1552. 1 => 2,
  1553. 22 => 3,
  1554. ),
  1555. 100 => array(
  1556. 1 => 2,
  1557. 22 => 3,
  1558. )
  1559. );',
  1560. ],
  1561. [
  1562. '<?php
  1563. $arr = array(
  1564. $a => 1,
  1565. $bbbb => \'
  1566. $cccccccc2 = 3;
  1567. \',
  1568. );',
  1569. '<?php
  1570. $arr = array(
  1571. $a => 1,
  1572. $bbbb => \'
  1573. $cccccccc2 = 3;
  1574. \',
  1575. );',
  1576. ],
  1577. [
  1578. '<?php
  1579. $arr = [
  1580. $a => 1,
  1581. $bbbb => \'
  1582. $cccccccc3 = 3;
  1583. \',
  1584. ];',
  1585. '<?php
  1586. $arr = [
  1587. $a => 1,
  1588. $bbbb => \'
  1589. $cccccccc3 = 3;
  1590. \',
  1591. ];',
  1592. ],
  1593. [
  1594. '<?php
  1595. foreach($arr as $k => $v){
  1596. $arr = array($k => 1,
  1597. $a => 1,
  1598. $bbbb => \'
  1599. $cccccccc4 = 3;
  1600. \',
  1601. );
  1602. }',
  1603. ],
  1604. [
  1605. '<?php
  1606. $a = array(
  1607. 10 => 11,
  1608. 20 => 22,
  1609. 30 => 33,
  1610. 40
  1611. =>
  1612. 44,
  1613. );',
  1614. '<?php
  1615. $a = array(
  1616. 10 => 11,
  1617. 20 => 22,
  1618. 30=>33,
  1619. 40
  1620. =>
  1621. 44,
  1622. );',
  1623. ],
  1624. [
  1625. '<?php
  1626. return array(
  1627. " " => "", "\t" => "",
  1628. "\n" => "", "\r" => "",
  1629. "\0" => "", "\x0B" => "",
  1630. );',
  1631. '<?php
  1632. return array(
  1633. " " => "", "\t" => "",
  1634. "\n" => "", "\r" => "",
  1635. "\0" => "", "\x0B" => "",
  1636. );',
  1637. ],
  1638. [
  1639. '<?php
  1640. return $this->grabAttribsBeforeToken(
  1641. $tokens,
  1642. $index,
  1643. $tokenAttribsMap,
  1644. array(
  1645. "abstract" => null,
  1646. "final" => null,
  1647. "visibility" => new Token(array(T_PUBLIC, "public")),
  1648. "static" => null,
  1649. )
  1650. );',
  1651. '<?php
  1652. return $this->grabAttribsBeforeToken(
  1653. $tokens,
  1654. $index,
  1655. $tokenAttribsMap,
  1656. array(
  1657. "abstract" => null,
  1658. "final" => null,
  1659. "visibility" => new Token(array(T_PUBLIC, "public")),
  1660. "static" => null,
  1661. )
  1662. );',
  1663. ],
  1664. [
  1665. '<?php
  1666. return array(
  1667. self::STATUS_UNKNOWN_1 => array("symbol" => "?", "description" => "unknown"),
  1668. self::STATUS_INVALID_1 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1669. );',
  1670. '<?php
  1671. return array(
  1672. self::STATUS_UNKNOWN_1 => array("symbol" => "?", "description" => "unknown"),
  1673. self::STATUS_INVALID_1 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1674. );',
  1675. ],
  1676. [
  1677. '<?php
  1678. $array = array(
  1679. "bazab" => b(array(
  1680. 1 => 2,
  1681. 5 => [
  1682. 6 => 7,
  1683. 8 => 9,
  1684. ],
  1685. 3 => 4,
  1686. 10 => 11,
  1687. )),
  1688. );',
  1689. '<?php
  1690. $array = array(
  1691. "bazab" => b(array(
  1692. 1 => 2,
  1693. 5 => [
  1694. 6 => 7,
  1695. 8 => 9,
  1696. ],
  1697. 3 => 4,
  1698. 10 => 11,
  1699. )),
  1700. );',
  1701. ],
  1702. [
  1703. '<?php
  1704. Foo::test()->aaa(array(1 => 2))->bbb("a", "b");
  1705. ',
  1706. ],
  1707. [
  1708. '<?php
  1709. $inflect_male = array(
  1710. "aitė\b" => "as",
  1711. "ytė\b" => "is",
  1712. "iūtė\b" => "ius",
  1713. "utė\b" => "us",
  1714. );',
  1715. '<?php
  1716. $inflect_male = array(
  1717. "aitė\b" => "as",
  1718. "ytė\b" => "is",
  1719. "iūtė\b" => "ius",
  1720. "utė\b" => "us",
  1721. );',
  1722. ],
  1723. [
  1724. '<?php
  1725. $formMapper
  1726. ->add(\'foo\', null, [\'required\' => false])
  1727. ->add(\'dummy_field\', null, [\'required\' => false])
  1728. ;
  1729. ',
  1730. ],
  1731. [
  1732. '<?php
  1733. $formMapper
  1734. ->add(\'foo\', null, array(\'required\' => false))
  1735. ->add(\'dummy_field\', null, array(\'required\' => false))
  1736. ;
  1737. ',
  1738. ],
  1739. [
  1740. '<?php
  1741. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy(["server1" => $object], ["addedAt" => "DESC"], 5);
  1742. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy(["server2" => $object], ["checkedAt" => "desc"], 50);
  1743. ',
  1744. ],
  1745. [
  1746. '<?php
  1747. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy(array("server1" => $object), array("addedAt" => "DESC"), 5);
  1748. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy(array("server2" => $object), array("checkedAt" => "desc"), 50);
  1749. ',
  1750. ],
  1751. [
  1752. '<?php
  1753. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy($foo[123]);
  1754. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy($foo[123]);
  1755. ',
  1756. ],
  1757. [
  1758. '<?php
  1759. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy([1, 2, 3]);
  1760. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy([1, 2, 3]);
  1761. ',
  1762. ],
  1763. [
  1764. '<?php
  1765. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy((1 + 2));
  1766. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy((1 + 2));
  1767. ',
  1768. ],
  1769. [
  1770. '<?php
  1771. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy(array(1, 2));
  1772. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy(array(1, 2));
  1773. ',
  1774. ],
  1775. [
  1776. '<?php
  1777. function foo() {}
  1778. $bar = 42;
  1779. $foo = [
  1780. "test123" => "foo",
  1781. "foo" => $bar[123],
  1782. "a" => foo(),
  1783. "b" => 1,
  1784. ];
  1785. ',
  1786. '<?php
  1787. function foo() {}
  1788. $bar = 42;
  1789. $foo = [
  1790. "test123" => "foo",
  1791. "foo" => $bar[123],
  1792. "a" => foo(),
  1793. "b" => 1,
  1794. ];
  1795. ',
  1796. ],
  1797. [
  1798. '<?php
  1799. return array(
  1800. self::STATUS_UNKNOWN_2 => array("symbol" => "?", "description" => "unknown"),
  1801. self::STATUS_INVALID_2 => array("symbol123" => "III", "description" => "invalid file syntax, file ignored"),
  1802. );',
  1803. '<?php
  1804. return array(
  1805. self::STATUS_UNKNOWN_2 => array("symbol" => "?", "description" => "unknown"),
  1806. self::STATUS_INVALID_2 => array("symbol123" => "III", "description" => "invalid file syntax, file ignored"),
  1807. );',
  1808. ],
  1809. [
  1810. '<?php
  1811. return array(
  1812. self::STATUS_UNKNOWN_3 => array((1 + 11)=> "?", "description" => "unknown"),
  1813. self::STATUS_INVALID_3 => array((2 + 3)=> "III", "description" => "invalid file syntax, file ignored"),
  1814. );',
  1815. '<?php
  1816. return array(
  1817. self::STATUS_UNKNOWN_3 => array((1+11)=> "?", "description" => "unknown"),
  1818. self::STATUS_INVALID_3 => array((2+3)=> "III", "description" => "invalid file syntax, file ignored"),
  1819. );',
  1820. ],
  1821. [
  1822. '<?php
  1823. return [
  1824. self::STATUS_UNKNOWN_4 => ["symbol" => "?", "description" => "unknown"],
  1825. self::STATUS_INVALID_4 => ["symbol123" => "III", "description" => "invalid file syntax, file ignored"],
  1826. ];',
  1827. '<?php
  1828. return [
  1829. self::STATUS_UNKNOWN_4 => ["symbol" => "?", "description" => "unknown"],
  1830. self::STATUS_INVALID_4 => ["symbol123" => "III", "description" => "invalid file syntax, file ignored"],
  1831. ];',
  1832. ],
  1833. [
  1834. '<?php
  1835. return [
  1836. self::STATUS_UNKNOWN_7 => [(1 + 11)=> "?", "description" => "unknown"],
  1837. self::STATUS_INVALID_7 => [(2 + 3)=> "III", "description" => "invalid file syntax, file ignored"],
  1838. ];',
  1839. '<?php
  1840. return [
  1841. self::STATUS_UNKNOWN_7 => [(1+11)=> "?", "description" => "unknown"],
  1842. self::STATUS_INVALID_7 => [(2+3)=> "III", "description" => "invalid file syntax, file ignored"],
  1843. ];',
  1844. ],
  1845. ];
  1846. }
  1847. public function testDoNotTouchEqualsAndArrowByConfig()
  1848. {
  1849. $this->fixer->configure(
  1850. [
  1851. 'operators' => [
  1852. '=' => null,
  1853. '=>' => null,
  1854. ],
  1855. ]
  1856. );
  1857. $this->doTest(
  1858. '<?php
  1859. $a = 1;
  1860. $aa = 1;
  1861. $aaa = 1;
  1862. $aaB = 1;
  1863. array(
  1864. 1 => 5,
  1865. 2 => 4,
  1866. 3 => 3,
  1867. 4 => 2,
  1868. 5 => 1,
  1869. 6 => 7,
  1870. );
  1871. '
  1872. );
  1873. }
  1874. /**
  1875. * @requires PHP 7.0
  1876. */
  1877. public function testPHP70Cases()
  1878. {
  1879. $this->fixer->configure(['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE, '??' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]]);
  1880. $this->doTest(
  1881. '<?php declare(strict_types=1);
  1882. $a = 1;
  1883. echo 1 <=> 1;
  1884. echo 1 <=> 2;
  1885. echo 2 <=> 1;
  1886. echo 2 <=> 1;
  1887. $a = $a ?? $b;
  1888. $a = $ab ?? $b;
  1889. $a = $ac ?? $b;
  1890. $a = $ad ?? $b;
  1891. $a = $ae ?? $b;
  1892. ',
  1893. '<?php declare(strict_types=1);
  1894. $a = 1;
  1895. echo 1<=>1;
  1896. echo 1 <=>2;
  1897. echo 2<=> 1;
  1898. echo 2 <=> 1;
  1899. $a = $a ?? $b;
  1900. $a = $ab ?? $b;
  1901. $a = $ac ?? $b;
  1902. $a = $ad ?? $b;
  1903. $a = $ae?? $b;
  1904. '
  1905. );
  1906. }
  1907. /**
  1908. * @requires PHP 7.1
  1909. *
  1910. * @param string $expected
  1911. * @param null|string $input
  1912. *
  1913. * @dataProvider providePHP71Cases
  1914. */
  1915. public function testPHP71Cases($expected, $input = null, array $configuration = [])
  1916. {
  1917. $this->fixer->configure($configuration);
  1918. $this->doTest($expected, $input);
  1919. }
  1920. public function providePHP71Cases()
  1921. {
  1922. return [
  1923. 'align array destruction' => [
  1924. '<?php
  1925. $c = [$d] = $e[1];
  1926. function A(){}[$a] = $a[$c];
  1927. $b = 1;
  1928. ',
  1929. '<?php
  1930. $c = [$d] = $e[1];
  1931. function A(){}[$a] = $a[$c];
  1932. $b = 1;
  1933. ',
  1934. ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN]],
  1935. ],
  1936. 'align array destruction with assignments' => [
  1937. '<?php
  1938. $d = [
  1939. "a" => $a,
  1940. "b" => $b,
  1941. "c" => $c
  1942. ] = $array;
  1943. ',
  1944. '<?php
  1945. $d = [
  1946. "a"=>$a,
  1947. "b" => $b,
  1948. "c" => $c
  1949. ] = $array;
  1950. ',
  1951. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  1952. ],
  1953. 'multiple exceptions catch, default config' => [
  1954. '<?php try {} catch (A | B $e) {}',
  1955. '<?php try {} catch (A | B $e) {}',
  1956. ],
  1957. 'multiple exceptions catch, no space config' => [
  1958. '<?php try {} catch (A|B $e) {}',
  1959. '<?php try {} catch (A | B $e) {}',
  1960. ['operators' => ['|' => BinaryOperatorSpacesFixer::NO_SPACE]],
  1961. ],
  1962. ];
  1963. }
  1964. /**
  1965. * @param string $expected
  1966. * @param null|string $input
  1967. *
  1968. * @dataProvider provideFixPhp74Cases
  1969. * @requires PHP 7.4
  1970. */
  1971. public function testFixPhp74($expected, $input = null, array $configuration = null)
  1972. {
  1973. if (null !== $configuration) {
  1974. $this->fixer->configure($configuration);
  1975. }
  1976. $this->doTest($expected, $input);
  1977. }
  1978. public function provideFixPhp74Cases()
  1979. {
  1980. return [
  1981. [
  1982. '<?php
  1983. $a = fn() => null;
  1984. $b = fn() => null;
  1985. ',
  1986. '<?php
  1987. $a = fn() => null;
  1988. $b = fn() => null;
  1989. ',
  1990. null,
  1991. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  1992. ],
  1993. [
  1994. '<?php $a ??= 1;',
  1995. '<?php $a??=1;',
  1996. ['operators' => ['??=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]],
  1997. ],
  1998. ];
  1999. }
  2000. }