BinaryOperatorSpacesFixerTest.php 48 KB

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