TrimArraySpacesFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\ArrayNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Jared Henderson <jared@netrivet.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer>
  22. */
  23. final class TrimArraySpacesFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php $foo = array("foo");',
  39. '<?php $foo = array( "foo" );',
  40. ];
  41. yield [
  42. '<?php $foo = ["foo"];',
  43. '<?php $foo = [ "foo" ];',
  44. ];
  45. yield [
  46. '<?php $foo = array();',
  47. '<?php $foo = array( );',
  48. ];
  49. yield [
  50. '<?php $foo = [];',
  51. '<?php $foo = [ ];',
  52. ];
  53. yield [
  54. '<?php $foo = array("foo", "bar");',
  55. '<?php $foo = array( "foo", "bar" );',
  56. ];
  57. yield [
  58. '<?php $foo = array("foo", "bar", );',
  59. '<?php $foo = array( "foo", "bar", );',
  60. ];
  61. yield [
  62. '<?php $foo = ["foo", "bar", ];',
  63. '<?php $foo = [ "foo", "bar", ];',
  64. ];
  65. yield [
  66. "<?php \$foo = array('foo', 'bar');",
  67. "<?php \$foo = array(\t'foo', 'bar'\t);",
  68. ];
  69. yield [
  70. "<?php \$foo = array('foo', 'bar');",
  71. "<?php \$foo = array( \t 'foo', 'bar'\t );",
  72. ];
  73. yield [
  74. '<?php $foo = array("foo", "bar");',
  75. '<?php $foo = array( "foo", "bar" );',
  76. ];
  77. yield [
  78. '<?php $foo = ["foo", "bar"];',
  79. '<?php $foo = [ "foo", "bar" ];',
  80. ];
  81. yield [
  82. '<?php $foo = ["foo", "bar"];',
  83. '<?php $foo = [ "foo", "bar" ];',
  84. ];
  85. yield [
  86. "<?php \$foo = ['foo', 'bar'];",
  87. "<?php \$foo = [\t'foo', 'bar'\t];",
  88. ];
  89. yield [
  90. "<?php \$foo = ['foo', 'bar'];",
  91. "<?php \$foo = [ \t \t 'foo', 'bar'\t \t ];",
  92. ];
  93. yield [
  94. '<?php $foo = array("foo", "bar"); $bar = array("foo", "bar");',
  95. '<?php $foo = array( "foo", "bar" ); $bar = array( "foo", "bar" );',
  96. ];
  97. yield [
  98. '<?php $foo = ["foo", "bar"]; $bar = ["foo", "bar"];',
  99. '<?php $foo = [ "foo", "bar" ]; $bar = [ "foo", "bar" ];',
  100. ];
  101. yield [
  102. '<?php $foo = array("foo" => "bar");',
  103. '<?php $foo = array( "foo" => "bar" );',
  104. ];
  105. yield [
  106. '<?php $foo = ["foo" => "bar"];',
  107. '<?php $foo = [ "foo" => "bar" ];',
  108. ];
  109. yield [
  110. '<?php $foo = array($y ? true : false);',
  111. '<?php $foo = array( $y ? true : false );',
  112. ];
  113. yield [
  114. '<?php $foo = [$y ? true : false];',
  115. '<?php $foo = [ $y ? true : false ];',
  116. ];
  117. yield [
  118. '<?php $foo = array(array("foo"), array("bar"));',
  119. '<?php $foo = array( array( "foo" ), array( "bar" ) );',
  120. ];
  121. yield [
  122. '<?php $foo = [["foo"], ["bar"]];',
  123. '<?php $foo = [ [ "foo" ], [ "bar" ] ];',
  124. ];
  125. yield [
  126. '<?php function(array $foo = array("bar")) {};',
  127. '<?php function(array $foo = array( "bar" )) {};',
  128. ];
  129. yield [
  130. '<?php function(array $foo = ["bar"]) {};',
  131. '<?php function(array $foo = [ "bar" ]) {};',
  132. ];
  133. yield [
  134. '<?php $foo = array(function() {return "foo";});',
  135. '<?php $foo = array( function() {return "foo";} );',
  136. ];
  137. yield [
  138. '<?php $foo = [function() {return "foo";}];',
  139. '<?php $foo = [ function() {return "foo";} ];',
  140. ];
  141. yield [
  142. "<?php \$foo = [function( \$a = \tarray('foo') ) { return 'foo' ;}];",
  143. "<?php \$foo = [ function( \$a = \tarray( 'foo' ) ) { return 'foo' ;} ];",
  144. ];
  145. yield [
  146. "<?php \$foo = array(function( ) {\treturn 'foo' \t;\t});",
  147. "<?php \$foo = array( function( ) {\treturn 'foo' \t;\t} );",
  148. ];
  149. yield [
  150. "<?php \$foo = [function()\t{\t \treturn 'foo';\t}];",
  151. "<?php \$foo = [ function()\t{\t \treturn 'foo';\t} ];",
  152. ];
  153. yield [
  154. "<?php \$foo \t = array(function(\$a,\$b,\$c=array(3, 4))\t{\t \treturn 'foo';\t});",
  155. "<?php \$foo \t = array( function(\$a,\$b,\$c=array( 3, 4 ))\t{\t \treturn 'foo';\t} );",
  156. ];
  157. yield [
  158. '<?php $foo = array($bar->method(), Foo::doSomething());',
  159. '<?php $foo = array( $bar->method(), Foo::doSomething() );',
  160. ];
  161. yield [
  162. '<?php $foo = [$bar->method(), Foo::doSomething()];',
  163. '<?php $foo = [ $bar->method(), Foo::doSomething() ];',
  164. ];
  165. yield [
  166. "<?php \$foo = [\$bar->method( \$a,\$b, \$c,\t\t \$d ), Foo::doSomething()];",
  167. "<?php \$foo = [ \$bar->method( \$a,\$b, \$c,\t\t \$d ), Foo::doSomething() ];",
  168. ];
  169. yield [
  170. "<?php \$foo =\t array(\$bar->method( \$a,\$b, \$c,\t\t \$d ), \$bar -> doSomething( ['baz']));",
  171. "<?php \$foo =\t array( \$bar->method( \$a,\$b, \$c,\t\t \$d ), \$bar -> doSomething( [ 'baz']) );",
  172. ];
  173. yield [
  174. '<?php $foo = array(array("foo"), array("bar"));',
  175. '<?php $foo = array( array("foo"), array("bar") );',
  176. ];
  177. yield [
  178. '<?php $foo = [["foo"], ["bar"]];',
  179. '<?php $foo = [ ["foo"], ["bar"] ];',
  180. ];
  181. yield [
  182. '<?php $foo = array(array("foo"), array("bar"));',
  183. '<?php $foo = array(array( "foo" ), array( "bar" ));',
  184. ];
  185. yield [
  186. '<?php $foo = [["foo"], ["bar"]];',
  187. '<?php $foo = [[ "foo" ], [ "bar" ]];',
  188. ];
  189. yield [
  190. '<?php $foo = array(/* empty array */);',
  191. '<?php $foo = array( /* empty array */ );',
  192. ];
  193. yield [
  194. '<?php $foo = [/* empty array */];',
  195. '<?php $foo = [ /* empty array */ ];',
  196. ];
  197. yield [
  198. '<?php someFunc(array(/* empty array */));',
  199. '<?php someFunc(array( /* empty array */ ));',
  200. ];
  201. yield [
  202. '<?php someFunc([/* empty array */]);',
  203. '<?php someFunc([ /* empty array */ ]);',
  204. ];
  205. yield [
  206. '<?php
  207. someFunc(array(
  208. /* empty array */
  209. ));',
  210. ];
  211. yield [
  212. '<?php
  213. someFunc([
  214. /* empty array */
  215. ]);',
  216. ];
  217. yield [
  218. '<?php
  219. someFunc(array(
  220. /* empty
  221. array */));',
  222. '<?php
  223. someFunc(array(
  224. /* empty
  225. array */ ));',
  226. ];
  227. yield [
  228. '<?php
  229. someFunc([
  230. /* empty
  231. array */]);',
  232. '<?php
  233. someFunc([
  234. /* empty
  235. array */ ]);',
  236. ];
  237. yield [
  238. '<?php
  239. $a = array( // My array of:
  240. 1, // - first item
  241. 2, // - second item
  242. );',
  243. ];
  244. yield [
  245. '<?php
  246. $a = [ // My array of:
  247. 1, // - first item
  248. 2, // - second item
  249. ];',
  250. ];
  251. yield [
  252. '<?php
  253. $a = array(
  254. // My array of:
  255. 1, // - first item
  256. 2, // - second item
  257. );',
  258. ];
  259. yield [
  260. '<?php
  261. $a = [
  262. // My array of:
  263. 1, // - first item
  264. 2, // - second item
  265. ];',
  266. ];
  267. yield [
  268. '<?php
  269. $foo = array(/* comment */
  270. 1
  271. );',
  272. '<?php
  273. $foo = array( /* comment */
  274. 1
  275. );',
  276. ];
  277. yield [
  278. '<?php
  279. $foo = [/* comment */
  280. 1
  281. ];',
  282. '<?php
  283. $foo = [ /* comment */
  284. 1
  285. ];',
  286. ];
  287. // don't fix array syntax within comments
  288. yield [
  289. '<?php someFunc([/* array( "foo", "bar", [ "foo" ] ) */]);',
  290. '<?php someFunc([ /* array( "foo", "bar", [ "foo" ] ) */ ]);',
  291. ];
  292. yield [
  293. '<?php $foo = array($bar[ 4 ]);',
  294. '<?php $foo = array( $bar[ 4 ] );',
  295. ];
  296. yield [
  297. '<?php $foo = [$bar[ 4 ]];',
  298. '<?php $foo = [ $bar[ 4 ] ];',
  299. ];
  300. yield [
  301. '<?php // array( "foo", "bar" );',
  302. ];
  303. // multiple single line nested arrays on one line
  304. yield [
  305. '<?php $foo = array("foo", "bar", [1, 2, array(3)]); $baz = ["hash", 1, array("test")];',
  306. '<?php $foo = array( "foo", "bar", [ 1, 2, array( 3 )] ); $baz = [ "hash", 1, array( "test") ];',
  307. ];
  308. yield [
  309. "<?php \$foo = array( \n'bar'\n );",
  310. ];
  311. yield [
  312. "<?php \$foo = [ \n'bar'\n ];",
  313. ];
  314. yield [
  315. "<?php \$foo = array( \n'a', 'b',\n'c');",
  316. "<?php \$foo = array( \n'a', 'b',\n'c' );",
  317. ];
  318. yield [
  319. "<?php \$foo = [ \n'a', 'b',\n'c'];",
  320. "<?php \$foo = [ \n'a', 'b',\n'c' ];",
  321. ];
  322. yield [
  323. "<?php \$foo = array('a', 'b',\n'c'\n);",
  324. "<?php \$foo = array( 'a', 'b',\n'c'\n);",
  325. ];
  326. yield [
  327. "<?php \$foo = ['a', 'b',\n'c'\n];",
  328. "<?php \$foo = [ 'a', 'b',\n'c'\n];",
  329. ];
  330. // don't fix array syntax within string
  331. yield [
  332. '<?php $foo = [\'$bar = array( "foo" );\', array(1, 5)];',
  333. '<?php $foo = [ \'$bar = array( "foo" );\', array(1, 5 ) ];',
  334. ];
  335. // crazy nested garbage pile #1
  336. yield [
  337. "<?php \$foo = array(/* comment \$bar = array([ ], array( 'foo' ) ), */ function(\$a = array('foo'), \$b = [/* comment [] */]) {}, array('foo' => 'bar', 'baz' => \$x[ 4], 'hash' => array(1,2,3)));",
  338. "<?php \$foo = array( /* comment \$bar = array([ ], array( 'foo' ) ), */ function(\$a = array( 'foo' ), \$b = [ /* comment [] */ ]) {}, array( 'foo' => 'bar', 'baz' => \$x[ 4], 'hash' => array(1,2,3 )) );",
  339. ];
  340. // crazy nested garbage pile #2
  341. yield [
  342. '<?php $a = [array("foo", "bar ", [1, 4, function($x = ["foobar", 2]) {}, [/* array( 1) */]]), array("foo", [$y[ 3]()], \'bar\')];',
  343. '<?php $a = [ array("foo", "bar ", [ 1, 4, function($x = [ "foobar", 2 ]) {}, [/* array( 1) */] ] ), array("foo", [ $y[ 3]() ], \'bar\') ];',
  344. ];
  345. yield [
  346. '<?php
  347. $foo = array(
  348. 1 => 2, // comment
  349. );
  350. ',
  351. ];
  352. yield [
  353. '<?php
  354. function a()
  355. {
  356. yield array("a" => 1, "b" => 2);
  357. }',
  358. '<?php
  359. function a()
  360. {
  361. yield array( "a" => 1, "b" => 2 );
  362. }',
  363. ];
  364. yield [
  365. '<?php
  366. function a()
  367. {
  368. yield ["a" => 1, "b" => 2];
  369. }',
  370. '<?php
  371. function a()
  372. {
  373. yield [ "a" => 1, "b" => 2 ];
  374. }',
  375. ];
  376. yield 'array destructuring' => [
  377. "<?php ['url' => \$url] = \$data;",
  378. "<?php [ 'url' => \$url ] = \$data;",
  379. ];
  380. yield 'array destructuring with comments' => [
  381. "<?php [/* foo */ 'url' => \$url /* bar */] = \$data;",
  382. "<?php [ /* foo */ 'url' => \$url /* bar */ ] = \$data;",
  383. ];
  384. yield 'multiline array destructuring' => [
  385. '<?php
  386. [
  387. \'url\' => $url,
  388. \'token\' => $token,
  389. ] = $data;
  390. ',
  391. ];
  392. }
  393. }