BinaryOperatorSpacesFixerTest.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  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: "align_double_arrow", "align_equals", "default", "operators"\.$/'
  730. );
  731. $this->fixer->configure(['foo' => true]);
  732. }
  733. public function testWrongConfigOldValue()
  734. {
  735. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  736. $this->expectExceptionMessageRegExp(
  737. '/^\[binary_operator_spaces\] Invalid configuration: The option "align_double_arrow" with value 123 is invalid\. Accepted values are: true, false, null\.$/'
  738. );
  739. $this->fixer->configure(['align_double_arrow' => 123]);
  740. }
  741. /**
  742. * @group legacy
  743. * @expectedDeprecation Given configuration is deprecated and will be removed in 3.0. Use configuration ['operators' => ['=' => 'align', '=>' => 'single_space']] as replacement for ['align_equals' => true, 'align_double_arrow' => false].
  744. */
  745. public function testWrongConfigOldDeprecated()
  746. {
  747. $this->fixer->configure([
  748. 'align_equals' => true,
  749. 'align_double_arrow' => false,
  750. ]);
  751. }
  752. /**
  753. * @group legacy
  754. * @expectedDeprecation Given configuration is deprecated and will be removed in 3.0. Use configuration ['operators' => ['=' => 'align']] as replacement for ['align_equals' => true, 'align_double_arrow' => null].
  755. */
  756. public function testWrongConfigOldDeprecated2()
  757. {
  758. $this->fixer->configure([
  759. 'align_equals' => true,
  760. 'align_double_arrow' => null,
  761. ]);
  762. }
  763. /**
  764. * @group legacy
  765. * @expectedDeprecation Given configuration is deprecated and will be removed in 3.0. Use configuration ['operators' => ['=>' => 'align']] as replacement for ['align_equals' => null, 'align_double_arrow' => true].
  766. */
  767. public function testWrongConfigOldDeprecated3()
  768. {
  769. $this->fixer->configure([
  770. 'align_equals' => null,
  771. 'align_double_arrow' => true,
  772. ]);
  773. }
  774. /**
  775. * @group legacy
  776. * @expectedDeprecation Given configuration is deprecated and will be removed in 3.0. Use configuration ['operators' => ['=' => 'single_space', '=>' => 'align']] as replacement for ['align_equals' => false, 'align_double_arrow' => true].
  777. */
  778. public function testWrongConfigOldDeprecated4()
  779. {
  780. $this->fixer->configure([
  781. 'align_equals' => false,
  782. 'align_double_arrow' => true,
  783. ]);
  784. }
  785. /**
  786. * @group legacy
  787. * @expectedDeprecation Given configuration is deprecated and will be removed in 3.0. Use configuration ['operators' => ['=' => 'align', '=>' => 'align']] as replacement for ['align_equals' => true, 'align_double_arrow' => true].
  788. */
  789. public function testWrongConfigOldDeprecated5()
  790. {
  791. $this->fixer->configure([
  792. 'align_equals' => true,
  793. 'align_double_arrow' => true,
  794. ]);
  795. // simple test to see if the old config is still used
  796. $this->doTest(
  797. '<?php
  798. $a = array(
  799. 1 => 2,
  800. 2 => 3,
  801. );
  802. $b = 1;
  803. $c = 2;
  804. ',
  805. '<?php
  806. $a = array(
  807. 1 => 2,
  808. 2 => 3,
  809. );
  810. $b = 1;
  811. $c = 2;
  812. '
  813. );
  814. }
  815. public function testWrongConfigOldAndNewMixed()
  816. {
  817. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  818. $this->expectExceptionMessageRegExp('/^\[binary_operator_spaces\] Mixing old configuration with new configuration is not allowed\.$/');
  819. $this->fixer->configure([
  820. 'align_double_arrow' => true,
  821. 'operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN],
  822. ]);
  823. }
  824. public function testWrongConfigTypeForOperators()
  825. {
  826. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  827. $this->expectExceptionMessageRegExp(
  828. '/^\[binary_operator_spaces\] Invalid configuration: The option "operators" with value true is expected to be of type "array", but is of type "boolean"\.$/'
  829. );
  830. $this->fixer->configure(['operators' => true]);
  831. }
  832. public function testWrongConfigTypeForOperatorsKey()
  833. {
  834. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  835. $this->expectExceptionMessageRegExp('/^\[binary_operator_spaces\] Invalid configuration: Unexpected "operators" key, expected any of ".*", got "integer#123"\.$/');
  836. $this->fixer->configure(['operators' => [123 => 1]]);
  837. }
  838. public function testWrongConfigTypeForOperatorsKeyValue()
  839. {
  840. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  841. $this->expectExceptionMessageRegExp('/^\[binary_operator_spaces\] Invalid configuration: Unexpected value for operator "\+", expected any of ".*", got "string#abc"\.$/');
  842. $this->fixer->configure(['operators' => ['+' => 'abc']]);
  843. }
  844. /**
  845. * @param string $expected
  846. * @param null|string $input
  847. *
  848. * @dataProvider provideUnalignDoubleArrowCases
  849. */
  850. public function testUnalignDoubleArrow($expected, $input = null)
  851. {
  852. $this->doTest($expected, $input);
  853. }
  854. public function provideUnalignDoubleArrowCases()
  855. {
  856. return [
  857. [
  858. '<?php
  859. $data = [
  860. "foo" => "Bar",
  861. "main" => array(
  862. [
  863. "baz" => "Test",
  864. "bazaa" => $a->{"Test"},
  865. "bazaa" => $a["Test"],
  866. "bazaaaa" => b("Test"),
  867. ]
  868. ),
  869. "bar" => array(),
  870. ];',
  871. '<?php
  872. $data = [
  873. "foo" => "Bar",
  874. "main" => array(
  875. [
  876. "baz" => "Test",
  877. "bazaa" => $a->{"Test"},
  878. "bazaa" => $a["Test"],
  879. "bazaaaa" => b("Test"),
  880. ]
  881. ),
  882. "bar" => array(),
  883. ];',
  884. ],
  885. [
  886. '<?php
  887. $data = [
  888. "foo" => "Bar",
  889. "main" => [array("baz" => "Test")],
  890. "bar" => array(),
  891. ];
  892. $data = array(
  893. "foo" => "Bar",
  894. "main" => array("baz" => "Test"),
  895. "bar" => array(),
  896. );
  897. $var = [];
  898. foreach ($foo as $i => $bar) {
  899. $var[] = /* Comment */ [$i => $bar];
  900. }',
  901. '<?php
  902. $data = [
  903. "foo" => "Bar",
  904. "main" => [array("baz" => "Test")],
  905. "bar" => array(),
  906. ];
  907. $data = array(
  908. "foo" => "Bar",
  909. "main" => array("baz" => "Test"),
  910. "bar" => array(),
  911. );
  912. $var = [];
  913. foreach ($foo as $i => $bar) {
  914. $var[] = /* Comment */ [$i => $bar];
  915. }',
  916. ],
  917. [
  918. '<?php
  919. $data = [
  920. "foo" => "Bar",
  921. "main" => [array("baz" => "Test")],
  922. "bar" => array(),
  923. ];',
  924. '<?php
  925. $data = [
  926. "foo" => "Bar",
  927. "main" => [array("baz" => "Test")],
  928. "bar" => array(),
  929. ];',
  930. ],
  931. [
  932. '<?php
  933. $data = array(
  934. "foo" => "Bar",
  935. "main" => array("baz" => "Test"),
  936. "bar" => array(),
  937. );',
  938. '<?php
  939. $data = array(
  940. "foo" => "Bar",
  941. "main" => array("baz" => "Test"),
  942. "bar" => array(),
  943. );',
  944. ],
  945. [
  946. '<?php
  947. $data = array(
  948. "foo" => "Bar",
  949. "main" => array(array("baz" => "Test")),
  950. "bar" => array(),
  951. );',
  952. '<?php
  953. $data = array(
  954. "foo" => "Bar",
  955. "main" => array(array("baz" => "Test")),
  956. "bar" => array(),
  957. );',
  958. ],
  959. [
  960. '<?php
  961. $var = [];
  962. foreach ($foo as $i => $bar) {
  963. $var[] = /* Comment */ [$i => $bar];
  964. }',
  965. '<?php
  966. $var = [];
  967. foreach ($foo as $i => $bar) {
  968. $var[] = /* Comment */ [$i => $bar];
  969. }',
  970. ],
  971. [
  972. '<?php
  973. $var = [];
  974. foreach ($foo as $i => $bar) {
  975. $var[] = [$i => $bar];
  976. }',
  977. ],
  978. [
  979. '<?php
  980. $var = [];
  981. foreach ([1 => 2] as $k => $v) {
  982. $var[] = [$i => $bar];
  983. }',
  984. ],
  985. [
  986. '<?php
  987. $var = [];
  988. foreach (fncCall() as $k => $v){
  989. $var[] = [$i => $bar];
  990. }',
  991. ],
  992. [
  993. '<?php
  994. $var = [];
  995. foreach ($foo as $bar) {
  996. $var[] = [
  997. $i => $bar,
  998. $iaaa => $bar,
  999. ];
  1000. }',
  1001. '<?php
  1002. $var = [];
  1003. foreach ($foo as $bar) {
  1004. $var[] = [
  1005. $i => $bar,
  1006. $iaaa => $bar,
  1007. ];
  1008. }',
  1009. ],
  1010. [
  1011. '<?php
  1012. $data = [
  1013. "foo" => "Bar",
  1014. "main" => [["baz" => "Test", "bar" => "Test2"]],
  1015. "bar" => [],
  1016. ];',
  1017. '<?php
  1018. $data = [
  1019. "foo" => "Bar",
  1020. "main" => [["baz" => "Test", "bar" => "Test2"]],
  1021. "bar" => [],
  1022. ];',
  1023. ],
  1024. [
  1025. '<?php
  1026. $a = [
  1027. 0 => 1,
  1028. 10 /*Comment*/ => [
  1029. 1 => 2,
  1030. 22 => 3,
  1031. ],
  1032. 100 => [
  1033. 1 => 2,
  1034. 22 => 3,
  1035. ]
  1036. ];',
  1037. '<?php
  1038. $a = [
  1039. 0 => 1,
  1040. 10 /*Comment*/ => [
  1041. 1 => 2,
  1042. 22 => 3,
  1043. ],
  1044. 100 => [
  1045. 1 => 2,
  1046. 22 => 3,
  1047. ]
  1048. ];',
  1049. ],
  1050. [
  1051. '<?php
  1052. $a = array(
  1053. 0 => 1,
  1054. 10 => array(
  1055. 1 => 2,
  1056. 22 => 3,
  1057. ),
  1058. 100 => array(
  1059. 1 => 2,
  1060. 22 => 3,
  1061. )
  1062. );',
  1063. '<?php
  1064. $a = array(
  1065. 0 => 1,
  1066. 10 => array(
  1067. 1 => 2,
  1068. 22 => 3,
  1069. ),
  1070. 100 => array(
  1071. 1 => 2,
  1072. 22 => 3,
  1073. )
  1074. );',
  1075. ],
  1076. [
  1077. '<?php
  1078. $arr = array(
  1079. $a => 1,
  1080. $bbbb => \'
  1081. $cccccccc = 3;
  1082. \',
  1083. );',
  1084. '<?php
  1085. $arr = array(
  1086. $a => 1,
  1087. $bbbb => \'
  1088. $cccccccc = 3;
  1089. \',
  1090. );',
  1091. ],
  1092. [
  1093. '<?php
  1094. $arr = [
  1095. $a => 1,
  1096. $bbbb => \'
  1097. $cccccccc = 3;
  1098. \',
  1099. ];',
  1100. '<?php
  1101. $arr = [
  1102. $a => 1,
  1103. $bbbb => \'
  1104. $cccccccc = 3;
  1105. \',
  1106. ];',
  1107. ],
  1108. [
  1109. '<?php
  1110. foreach($arr as $k => $v){
  1111. $arr = array($k => 1,
  1112. $a => 1,
  1113. $bbbb => \'
  1114. $cccccccc = 3;
  1115. \',
  1116. );
  1117. }',
  1118. '<?php
  1119. foreach($arr as $k => $v){
  1120. $arr = array($k => 1,
  1121. $a => 1,
  1122. $bbbb => \'
  1123. $cccccccc = 3;
  1124. \',
  1125. );
  1126. }',
  1127. ],
  1128. [
  1129. '<?php
  1130. $a = array(
  1131. 10 => 11,
  1132. 20 => 22,
  1133. 30 => 33,
  1134. 40
  1135. =>
  1136. 44,
  1137. );',
  1138. '<?php
  1139. $a = array(
  1140. 10 => 11,
  1141. 20 => 22,
  1142. 30=>33,
  1143. 40
  1144. =>
  1145. 44,
  1146. );',
  1147. ],
  1148. [
  1149. '<?php
  1150. return array(
  1151. " " => "", "\t" => "",
  1152. "\n" => "", "\r" => "",
  1153. "\0" => "", "\x0B" => "",
  1154. );',
  1155. '<?php
  1156. return array(
  1157. " " => "", "\t" => "",
  1158. "\n" => "", "\r" => "",
  1159. "\0" => "", "\x0B" => "",
  1160. );',
  1161. ],
  1162. [
  1163. '<?php
  1164. return $this->grabAttribsBeforeToken(
  1165. $tokens,
  1166. $index,
  1167. $tokenAttribsMap,
  1168. array(
  1169. "abstract" => null,
  1170. "final" => null,
  1171. "visibility" => new Token(array(T_PUBLIC, "public")),
  1172. "static" => null,
  1173. )
  1174. );',
  1175. '<?php
  1176. return $this->grabAttribsBeforeToken(
  1177. $tokens,
  1178. $index,
  1179. $tokenAttribsMap,
  1180. array(
  1181. "abstract" => null,
  1182. "final" => null,
  1183. "visibility" => new Token(array(T_PUBLIC, "public")),
  1184. "static" => null,
  1185. )
  1186. );',
  1187. ],
  1188. [
  1189. '<?php
  1190. return array(
  1191. self::STATUS_UNKNOWN_0 => array("symbol" => "?", "description" => "unknown"),
  1192. self::STATUS_INVALID_0 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1193. );',
  1194. '<?php
  1195. return array(
  1196. self::STATUS_UNKNOWN_0 => array("symbol" => "?", "description" => "unknown"),
  1197. self::STATUS_INVALID_0 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1198. );',
  1199. ],
  1200. [
  1201. '<?php
  1202. $array = array(
  1203. "bazab" => b(array(
  1204. 1 => 2,
  1205. 5 => [
  1206. 6 => 7,
  1207. 8 => 9,
  1208. ],
  1209. 3 => 4,
  1210. 10 => 11,
  1211. )),
  1212. );',
  1213. '<?php
  1214. $array = array(
  1215. "bazab" => b(array(
  1216. 1 => 2,
  1217. 5 => [
  1218. 6 => 7,
  1219. 8 => 9,
  1220. ],
  1221. 3 => 4,
  1222. 10 => 11,
  1223. )),
  1224. );',
  1225. ],
  1226. [
  1227. '<?php
  1228. Foo::test()->aaa(array(1 => 2))->bbb("a", "b");
  1229. ',
  1230. ],
  1231. [
  1232. '<?php
  1233. function foo() {
  1234. yield 1 => 2;
  1235. }',
  1236. ],
  1237. ];
  1238. }
  1239. /**
  1240. * @param string $expected
  1241. * @param null|string $input
  1242. *
  1243. * @dataProvider provideAlignEqualsCases
  1244. */
  1245. public function testFixAlignEquals($expected, $input = null)
  1246. {
  1247. $this->fixer->configure(['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN]]);
  1248. $this->doTest($expected, $input);
  1249. }
  1250. public function provideAlignEqualsCases()
  1251. {
  1252. return [
  1253. [
  1254. '<?php
  1255. $a = 1;
  1256. $bbbb = \'
  1257. $ddcccccc1 = 3;
  1258. \';',
  1259. '<?php
  1260. $a = 1;
  1261. $bbbb = \'
  1262. $ddcccccc1 = 3;
  1263. \';',
  1264. ],
  1265. [
  1266. '<?php
  1267. $ccc = 1;
  1268. $bb = 1;
  1269. $a = 1;
  1270. /*
  1271. Others alignments
  1272. */
  1273. $a[$b = 1] = 1;
  1274. $ab[$bc = 1] = 1;
  1275. $abc[$bcd = 1] = 1;
  1276. $a[$b] = 1;
  1277. $ab[$bc] = 1;
  1278. $abc[$bcd] = 1;
  1279. if ($a = 1) {
  1280. $ccc = 1;
  1281. $bb = 1;
  1282. $a = 1;
  1283. }
  1284. function a($a = 1, $b = 2, $c = 3)
  1285. {
  1286. $a[$b = 1] = 1;
  1287. $ab[$bc = 1] = 1;
  1288. $abc[$bcd = 1] = 1;
  1289. }
  1290. function b(
  1291. $a = 1,
  1292. $bbb = 2,
  1293. $c = 3
  1294. ) {
  1295. $a[$b = 1] = 1;
  1296. $ab[$bc = 1] = 1;
  1297. $abc[$bcd = 1] = 1;
  1298. }
  1299. while (false) {
  1300. $aa = 2;
  1301. $a[$b] = array();
  1302. }
  1303. for ($i = 0; $i < 10; $i++) {
  1304. $aa = 2;
  1305. $a[$b] = array();
  1306. }',
  1307. '<?php
  1308. $ccc = 1;
  1309. $bb = 1;
  1310. $a = 1;
  1311. /*
  1312. Others alignments
  1313. */
  1314. $a[$b = 1] = 1;
  1315. $ab[$bc = 1] = 1;
  1316. $abc[$bcd = 1] = 1;
  1317. $a[$b] = 1;
  1318. $ab[$bc] = 1;
  1319. $abc[$bcd] = 1;
  1320. if ($a = 1) {
  1321. $ccc = 1;
  1322. $bb = 1;
  1323. $a = 1;
  1324. }
  1325. function a($a = 1, $b = 2, $c = 3)
  1326. {
  1327. $a[$b = 1] = 1;
  1328. $ab[$bc = 1] = 1;
  1329. $abc[$bcd = 1] = 1;
  1330. }
  1331. function b(
  1332. $a = 1,
  1333. $bbb = 2,
  1334. $c = 3
  1335. ) {
  1336. $a[$b = 1] = 1;
  1337. $ab[$bc = 1] = 1;
  1338. $abc[$bcd = 1] = 1;
  1339. }
  1340. while (false) {
  1341. $aa = 2;
  1342. $a[$b] = array();
  1343. }
  1344. for ($i = 0; $i < 10; $i++) {
  1345. $aa = 2;
  1346. $a[$b] = array();
  1347. }',
  1348. ],
  1349. ];
  1350. }
  1351. /**
  1352. * @param string $expected
  1353. * @param null|string $input
  1354. *
  1355. * @dataProvider provideAlignDoubleArrowCases
  1356. */
  1357. public function testFixAlignDoubleArrow($expected, $input = null)
  1358. {
  1359. $this->fixer->configure(['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN]]);
  1360. $this->doTest($expected, $input);
  1361. }
  1362. public function provideAlignDoubleArrowCases()
  1363. {
  1364. return [
  1365. [
  1366. '<?php
  1367. switch ($a) {
  1368. case "prod":
  1369. break;
  1370. }
  1371. ',
  1372. ],
  1373. [
  1374. '<?php
  1375. $array = array(
  1376. "closure" => function ($param1, $param2) {
  1377. return;
  1378. }
  1379. );',
  1380. ],
  1381. [
  1382. '<?php
  1383. return new JsonResponse(array(
  1384. "result" => "OK",
  1385. "html" => 1, /**/array(
  1386. "foo" => "bar",
  1387. "foofoo" => array(
  1388. "a" => 1,
  1389. "b" => 2
  1390. )
  1391. ),)
  1392. );',
  1393. '<?php
  1394. return new JsonResponse(array(
  1395. "result" => "OK",
  1396. "html" => 1, /**/array(
  1397. "foo" => "bar",
  1398. "foofoo" => array(
  1399. "a" => 1,
  1400. "b" => 2
  1401. )
  1402. ),)
  1403. );',
  1404. ],
  1405. [
  1406. '<?php
  1407. return new JsonResponse([
  1408. "result" => "OK",
  1409. "html" => renderView("views/my_view.html.twig", array(
  1410. "foo" => "bar",
  1411. "foofoo" => 43,
  1412. )),
  1413. ]);',
  1414. '<?php
  1415. return new JsonResponse([
  1416. "result" => "OK",
  1417. "html" => renderView("views/my_view.html.twig", array(
  1418. "foo" => "bar",
  1419. "foofoo" => 43,
  1420. )),
  1421. ]);',
  1422. ],
  1423. [
  1424. '<?php
  1425. return new JsonResponse([
  1426. "result" => "OK",
  1427. "html" => renderView("views/my_view.html.twig", [
  1428. "foo" => "bar",
  1429. "foofoo" => 42,
  1430. ]),
  1431. "baz" => "OK",
  1432. ]);',
  1433. '<?php
  1434. return new JsonResponse([
  1435. "result" => "OK",
  1436. "html" => renderView("views/my_view.html.twig", [
  1437. "foo" => "bar",
  1438. "foofoo" => 42,
  1439. ]),
  1440. "baz" => "OK",
  1441. ]);',
  1442. ],
  1443. [
  1444. '<?php
  1445. $data = [
  1446. "foo" => "Bar",
  1447. "main" => array(
  1448. [
  1449. "baz" => "Test",
  1450. "bazaa" => $a->{"Test"},
  1451. "bazaa" => $a["Test"],
  1452. "bazaaaa" => b("Test"),
  1453. ]
  1454. ),
  1455. "bar" => array(),
  1456. ];',
  1457. '<?php
  1458. $data = [
  1459. "foo" => "Bar",
  1460. "main" => array(
  1461. [
  1462. "baz" => "Test",
  1463. "bazaa" => $a->{"Test"},
  1464. "bazaa" => $a["Test"],
  1465. "bazaaaa" => b("Test"),
  1466. ]
  1467. ),
  1468. "bar" => array(),
  1469. ];',
  1470. ],
  1471. [
  1472. '<?php
  1473. $data = [
  1474. "foo" => "Bar",
  1475. "main" => [array("baz" => "Test")],
  1476. "bar" => array(),
  1477. ];
  1478. $data = array(
  1479. "foo" => "Bar",
  1480. "main" => array("baz" => "Test"),
  1481. "bar" => array(),
  1482. );
  1483. $var = [];
  1484. foreach ($foo as $i => $bar) {
  1485. $var[] = /* Comment */ [$i => $bar];
  1486. }',
  1487. ],
  1488. [
  1489. '<?php
  1490. $data = [
  1491. "foo" => "Bar",
  1492. "main" => [array("baz" => "Test")],
  1493. "bar" => array(),
  1494. ];',
  1495. ],
  1496. [
  1497. '<?php
  1498. $data = array(
  1499. "foo" => "Bar",
  1500. "main" => array("baz" => "Test"),
  1501. "bar" => array(),
  1502. );',
  1503. ],
  1504. [
  1505. '<?php
  1506. $data = array(
  1507. "foo" => "Bar",
  1508. "main" => array(array("baz" => "Test")),
  1509. "bar" => array(),
  1510. );',
  1511. ],
  1512. [
  1513. '<?php
  1514. $var = [];
  1515. foreach ($foo as $i => $bar) {
  1516. $var[] = /* Comment */ [$i => $bar];
  1517. }',
  1518. ],
  1519. [
  1520. '<?php
  1521. $var = [];
  1522. foreach ($foo as $i => $bar) {
  1523. $var[] = [$i => $bar];
  1524. }',
  1525. ],
  1526. [
  1527. '<?php
  1528. $var = [];
  1529. foreach ([1 => 2] as $k => $v) {
  1530. $var[] = [$i => $bar];
  1531. }',
  1532. ],
  1533. [
  1534. '<?php
  1535. $var = [];
  1536. foreach (fncCall() as $k => $v){
  1537. $var[] = [$i => $bar];
  1538. }',
  1539. ],
  1540. [
  1541. '<?php
  1542. $var = [];
  1543. foreach ($foo as $bar) {
  1544. $var[] = [
  1545. $i => $bar,
  1546. $iaaa => $bar,
  1547. ];
  1548. }',
  1549. ],
  1550. [
  1551. '<?php
  1552. $data = [
  1553. "foo" => "Bar",
  1554. "main" => [["baz" => "Test", "bar" => "Test2"]],
  1555. "bar" => [],
  1556. ];',
  1557. ],
  1558. [
  1559. '<?php
  1560. $data = [
  1561. "foo" => "Bar",
  1562. "main" => ["baz" => "Test"],
  1563. "bar" => [],
  1564. ];',
  1565. ],
  1566. [
  1567. '<?php
  1568. $a = [
  1569. 0 => 1,
  1570. 10 /*Comment*/ => [
  1571. 1 => 2,
  1572. 22 => 3,
  1573. ],
  1574. 100 => [
  1575. 1 => 2,
  1576. 22 => 3,
  1577. ]
  1578. ];',
  1579. '<?php
  1580. $a = [
  1581. 0 => 1,
  1582. 10 /*Comment*/ => [
  1583. 1 => 2,
  1584. 22 => 3,
  1585. ],
  1586. 100 => [
  1587. 1 => 2,
  1588. 22 => 3,
  1589. ]
  1590. ];',
  1591. ],
  1592. [
  1593. '<?php
  1594. $a = array(
  1595. 0 => 1,
  1596. 10 => array(
  1597. 1 => 2,
  1598. 22 => 3,
  1599. ),
  1600. 100 => array(
  1601. 1 => 2,
  1602. 22 => 3,
  1603. )
  1604. );',
  1605. ],
  1606. [
  1607. '<?php
  1608. $arr = array(
  1609. $a => 1,
  1610. $bbbb => \'
  1611. $cccccccc2 = 3;
  1612. \',
  1613. );',
  1614. '<?php
  1615. $arr = array(
  1616. $a => 1,
  1617. $bbbb => \'
  1618. $cccccccc2 = 3;
  1619. \',
  1620. );',
  1621. ],
  1622. [
  1623. '<?php
  1624. $arr = [
  1625. $a => 1,
  1626. $bbbb => \'
  1627. $cccccccc3 = 3;
  1628. \',
  1629. ];',
  1630. '<?php
  1631. $arr = [
  1632. $a => 1,
  1633. $bbbb => \'
  1634. $cccccccc3 = 3;
  1635. \',
  1636. ];',
  1637. ],
  1638. [
  1639. '<?php
  1640. foreach($arr as $k => $v){
  1641. $arr = array($k => 1,
  1642. $a => 1,
  1643. $bbbb => \'
  1644. $cccccccc4 = 3;
  1645. \',
  1646. );
  1647. }',
  1648. ],
  1649. [
  1650. '<?php
  1651. $a = array(
  1652. 10 => 11,
  1653. 20 => 22,
  1654. 30 => 33,
  1655. 40
  1656. =>
  1657. 44,
  1658. );',
  1659. '<?php
  1660. $a = array(
  1661. 10 => 11,
  1662. 20 => 22,
  1663. 30=>33,
  1664. 40
  1665. =>
  1666. 44,
  1667. );',
  1668. ],
  1669. [
  1670. '<?php
  1671. return array(
  1672. " " => "", "\t" => "",
  1673. "\n" => "", "\r" => "",
  1674. "\0" => "", "\x0B" => "",
  1675. );',
  1676. '<?php
  1677. return array(
  1678. " " => "", "\t" => "",
  1679. "\n" => "", "\r" => "",
  1680. "\0" => "", "\x0B" => "",
  1681. );',
  1682. ],
  1683. [
  1684. '<?php
  1685. return $this->grabAttribsBeforeToken(
  1686. $tokens,
  1687. $index,
  1688. $tokenAttribsMap,
  1689. array(
  1690. "abstract" => null,
  1691. "final" => null,
  1692. "visibility" => new Token(array(T_PUBLIC, "public")),
  1693. "static" => null,
  1694. )
  1695. );',
  1696. '<?php
  1697. return $this->grabAttribsBeforeToken(
  1698. $tokens,
  1699. $index,
  1700. $tokenAttribsMap,
  1701. array(
  1702. "abstract" => null,
  1703. "final" => null,
  1704. "visibility" => new Token(array(T_PUBLIC, "public")),
  1705. "static" => null,
  1706. )
  1707. );',
  1708. ],
  1709. [
  1710. '<?php
  1711. return array(
  1712. self::STATUS_UNKNOWN_1 => array("symbol" => "?", "description" => "unknown"),
  1713. self::STATUS_INVALID_1 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1714. );',
  1715. '<?php
  1716. return array(
  1717. self::STATUS_UNKNOWN_1 => array("symbol" => "?", "description" => "unknown"),
  1718. self::STATUS_INVALID_1 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"),
  1719. );',
  1720. ],
  1721. [
  1722. '<?php
  1723. $array = array(
  1724. "bazab" => b(array(
  1725. 1 => 2,
  1726. 5 => [
  1727. 6 => 7,
  1728. 8 => 9,
  1729. ],
  1730. 3 => 4,
  1731. 10 => 11,
  1732. )),
  1733. );',
  1734. '<?php
  1735. $array = array(
  1736. "bazab" => b(array(
  1737. 1 => 2,
  1738. 5 => [
  1739. 6 => 7,
  1740. 8 => 9,
  1741. ],
  1742. 3 => 4,
  1743. 10 => 11,
  1744. )),
  1745. );',
  1746. ],
  1747. [
  1748. '<?php
  1749. Foo::test()->aaa(array(1 => 2))->bbb("a", "b");
  1750. ',
  1751. ],
  1752. [
  1753. '<?php
  1754. $inflect_male = array(
  1755. "aitė\b" => "as",
  1756. "ytė\b" => "is",
  1757. "iūtė\b" => "ius",
  1758. "utė\b" => "us",
  1759. );',
  1760. '<?php
  1761. $inflect_male = array(
  1762. "aitė\b" => "as",
  1763. "ytė\b" => "is",
  1764. "iūtė\b" => "ius",
  1765. "utė\b" => "us",
  1766. );',
  1767. ],
  1768. [
  1769. '<?php
  1770. $formMapper
  1771. ->add(\'foo\', null, [\'required\' => false])
  1772. ->add(\'dummy_field\', null, [\'required\' => false])
  1773. ;
  1774. ',
  1775. ],
  1776. [
  1777. '<?php
  1778. $formMapper
  1779. ->add(\'foo\', null, array(\'required\' => false))
  1780. ->add(\'dummy_field\', null, array(\'required\' => false))
  1781. ;
  1782. ',
  1783. ],
  1784. [
  1785. '<?php
  1786. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy(["server1" => $object], ["addedAt" => "DESC"], 5);
  1787. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy(["server2" => $object], ["checkedAt" => "desc"], 50);
  1788. ',
  1789. ],
  1790. [
  1791. '<?php
  1792. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy(array("server1" => $object), array("addedAt" => "DESC"), 5);
  1793. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy(array("server2" => $object), array("checkedAt" => "desc"), 50);
  1794. ',
  1795. ],
  1796. [
  1797. '<?php
  1798. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy($foo[123]);
  1799. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy($foo[123]);
  1800. ',
  1801. ],
  1802. [
  1803. '<?php
  1804. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy([1, 2, 3]);
  1805. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy([1, 2, 3]);
  1806. ',
  1807. ],
  1808. [
  1809. '<?php
  1810. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy((1 + 2));
  1811. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy((1 + 2));
  1812. ',
  1813. ],
  1814. [
  1815. '<?php
  1816. $dummy001 = $this->get("doctrine")->getRepository("AppBundle:Entity")->findBy(array(1, 2));
  1817. $foobar = $this->getDoctrine()->getRepository("AppBundle:Entity")->findBy(array(1, 2));
  1818. ',
  1819. ],
  1820. [
  1821. '<?php
  1822. function foo() {}
  1823. $bar = 42;
  1824. $foo = [
  1825. "test123" => "foo",
  1826. "foo" => $bar[123],
  1827. "a" => foo(),
  1828. "b" => 1,
  1829. ];
  1830. ',
  1831. '<?php
  1832. function foo() {}
  1833. $bar = 42;
  1834. $foo = [
  1835. "test123" => "foo",
  1836. "foo" => $bar[123],
  1837. "a" => foo(),
  1838. "b" => 1,
  1839. ];
  1840. ',
  1841. ],
  1842. [
  1843. '<?php
  1844. return array(
  1845. self::STATUS_UNKNOWN_2 => array("symbol" => "?", "description" => "unknown"),
  1846. self::STATUS_INVALID_2 => array("symbol123" => "III", "description" => "invalid file syntax, file ignored"),
  1847. );',
  1848. '<?php
  1849. return array(
  1850. self::STATUS_UNKNOWN_2 => array("symbol" => "?", "description" => "unknown"),
  1851. self::STATUS_INVALID_2 => array("symbol123" => "III", "description" => "invalid file syntax, file ignored"),
  1852. );',
  1853. ],
  1854. [
  1855. '<?php
  1856. return array(
  1857. self::STATUS_UNKNOWN_3 => array((1 + 11)=> "?", "description" => "unknown"),
  1858. self::STATUS_INVALID_3 => array((2 + 3)=> "III", "description" => "invalid file syntax, file ignored"),
  1859. );',
  1860. '<?php
  1861. return array(
  1862. self::STATUS_UNKNOWN_3 => array((1+11)=> "?", "description" => "unknown"),
  1863. self::STATUS_INVALID_3 => array((2+3)=> "III", "description" => "invalid file syntax, file ignored"),
  1864. );',
  1865. ],
  1866. [
  1867. '<?php
  1868. return [
  1869. self::STATUS_UNKNOWN_4 => ["symbol" => "?", "description" => "unknown"],
  1870. self::STATUS_INVALID_4 => ["symbol123" => "III", "description" => "invalid file syntax, file ignored"],
  1871. ];',
  1872. '<?php
  1873. return [
  1874. self::STATUS_UNKNOWN_4 => ["symbol" => "?", "description" => "unknown"],
  1875. self::STATUS_INVALID_4 => ["symbol123" => "III", "description" => "invalid file syntax, file ignored"],
  1876. ];',
  1877. ],
  1878. [
  1879. '<?php
  1880. return [
  1881. self::STATUS_UNKNOWN_7 => [(1 + 11)=> "?", "description" => "unknown"],
  1882. self::STATUS_INVALID_7 => [(2 + 3)=> "III", "description" => "invalid file syntax, file ignored"],
  1883. ];',
  1884. '<?php
  1885. return [
  1886. self::STATUS_UNKNOWN_7 => [(1+11)=> "?", "description" => "unknown"],
  1887. self::STATUS_INVALID_7 => [(2+3)=> "III", "description" => "invalid file syntax, file ignored"],
  1888. ];',
  1889. ],
  1890. ];
  1891. }
  1892. public function testDoNotTouchEqualsAndArrowByConfig()
  1893. {
  1894. $this->fixer->configure(
  1895. [
  1896. 'operators' => [
  1897. '=' => null,
  1898. '=>' => null,
  1899. ],
  1900. ]
  1901. );
  1902. $this->doTest(
  1903. '<?php
  1904. $a = 1;
  1905. $aa = 1;
  1906. $aaa = 1;
  1907. $aaB = 1;
  1908. array(
  1909. 1 => 5,
  1910. 2 => 4,
  1911. 3 => 3,
  1912. 4 => 2,
  1913. 5 => 1,
  1914. 6 => 7,
  1915. );
  1916. '
  1917. );
  1918. }
  1919. /**
  1920. * @requires PHP 7.0
  1921. */
  1922. public function testPHP70Cases()
  1923. {
  1924. $this->fixer->configure(['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE, '??' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]]);
  1925. $this->doTest(
  1926. '<?php declare(strict_types=1);
  1927. $a = 1;
  1928. echo 1 <=> 1;
  1929. echo 1 <=> 2;
  1930. echo 2 <=> 1;
  1931. echo 2 <=> 1;
  1932. $a = $a ?? $b;
  1933. $a = $ab ?? $b;
  1934. $a = $ac ?? $b;
  1935. $a = $ad ?? $b;
  1936. $a = $ae ?? $b;
  1937. ',
  1938. '<?php declare(strict_types=1);
  1939. $a = 1;
  1940. echo 1<=>1;
  1941. echo 1 <=>2;
  1942. echo 2<=> 1;
  1943. echo 2 <=> 1;
  1944. $a = $a ?? $b;
  1945. $a = $ab ?? $b;
  1946. $a = $ac ?? $b;
  1947. $a = $ad ?? $b;
  1948. $a = $ae?? $b;
  1949. '
  1950. );
  1951. }
  1952. /**
  1953. * @requires PHP 7.1
  1954. *
  1955. * @param string $expected
  1956. * @param null|string $input
  1957. * @param array $configuration
  1958. *
  1959. * @dataProvider providePHP71Cases
  1960. */
  1961. public function testPHP71Cases($expected, $input = null, array $configuration = [])
  1962. {
  1963. $this->fixer->configure($configuration);
  1964. $this->doTest($expected, $input);
  1965. }
  1966. public function providePHP71Cases()
  1967. {
  1968. return [
  1969. 'align array destruction' => [
  1970. '<?php
  1971. $c = [$d] = $e[1];
  1972. function A(){}[$a] = $a[$c];
  1973. $b = 1;
  1974. ',
  1975. '<?php
  1976. $c = [$d] = $e[1];
  1977. function A(){}[$a] = $a[$c];
  1978. $b = 1;
  1979. ',
  1980. ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN]],
  1981. ],
  1982. 'align array destruction with assignments' => [
  1983. '<?php
  1984. $d = [
  1985. "a" => $a,
  1986. "b" => $b,
  1987. "c" => $c
  1988. ] = $array;
  1989. ',
  1990. '<?php
  1991. $d = [
  1992. "a"=>$a,
  1993. "b" => $b,
  1994. "c" => $c
  1995. ] = $array;
  1996. ',
  1997. ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
  1998. ],
  1999. 'multiple exceptions catch' => [
  2000. '<?php try {} catch (A | B $e) {}',
  2001. '<?php try {} catch (A | B $e) {}',
  2002. ],
  2003. ];
  2004. }
  2005. }