NoSpacesAroundOffsetFixerTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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\Whitespace;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Javier Spagnoletti <phansys@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer
  21. */
  22. final class NoSpacesAroundOffsetFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixSpaceInsideOffsetCases
  26. */
  27. public function testFixSpaceInsideOffset(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. /**
  32. * @dataProvider provideFixSpaceOutsideOffsetCases
  33. */
  34. public function testFixSpaceOutsideOffset(string $expected, ?string $input = null): void
  35. {
  36. $this->doTest($expected, $input);
  37. }
  38. public function testLeaveNewLinesAlone(): void
  39. {
  40. $expected = <<<'EOF'
  41. <?php
  42. class Foo
  43. {
  44. private function bar()
  45. {
  46. if ([1, 2, 3] && [
  47. 'foo',
  48. 'bar' ,
  49. 'baz'// a comment just to mix things up
  50. ]) {
  51. return 1;
  52. };
  53. }
  54. }
  55. EOF;
  56. $this->doTest($expected);
  57. }
  58. /**
  59. * @dataProvider provideCommentsCases
  60. */
  61. public function testComments(string $expected, ?string $input = null): void
  62. {
  63. $this->doTest($expected, $input);
  64. }
  65. public static function provideCommentsCases(): iterable
  66. {
  67. yield [
  68. '<?php
  69. $withComments[0] // here is a comment
  70. [1] // and here is another
  71. [2] = 3;',
  72. ];
  73. yield [
  74. '<?php
  75. $a = $b[# z
  76. 1#z
  77. ];',
  78. '<?php
  79. $a = $b[ # z
  80. 1#z
  81. ];',
  82. ];
  83. }
  84. public function testLeaveComplexString(): void
  85. {
  86. $expected = <<<'EOF'
  87. <?php
  88. echo "I am printing some spaces here {$foo->bar[1]} {$foo->bar[1]}.";
  89. EOF;
  90. $this->doTest($expected);
  91. }
  92. public function testLeaveFunctions(): void
  93. {
  94. $expected = <<<'EOF'
  95. <?php
  96. function someFunc() { $someVar = []; }
  97. EOF;
  98. $this->doTest($expected);
  99. }
  100. public static function provideFixSpaceOutsideOffsetCases(): iterable
  101. {
  102. yield [
  103. '<?php
  104. $a = $b[0] ;',
  105. '<?php
  106. $a = $b [0] ;',
  107. ];
  108. yield [
  109. '<?php
  110. $a = array($b[0] , $b[0] );',
  111. '<?php
  112. $a = array($b [0] , $b [0] );',
  113. ];
  114. yield [
  115. '<?php
  116. $withComments[0] // here is a comment
  117. [1] // and here is another
  118. [2][3] = 4;',
  119. '<?php
  120. $withComments [0] // here is a comment
  121. [1] // and here is another
  122. [2] [3] = 4;',
  123. ];
  124. yield [
  125. '<?php
  126. $c = SOME_CONST[0][1][2];',
  127. '<?php
  128. $c = SOME_CONST [0] [1] [2];',
  129. ];
  130. yield [
  131. '<?php
  132. $f = someFunc()[0][1][2];',
  133. '<?php
  134. $f = someFunc() [0] [1] [2];',
  135. ];
  136. yield [
  137. '<?php
  138. $foo[][0][1][2] = 3;',
  139. '<?php
  140. $foo [] [0] [1] [2] = 3;',
  141. ];
  142. yield [
  143. '<?php
  144. $foo[0][1][2] = 3;',
  145. '<?php
  146. $foo [0] [1] [2] = 3;',
  147. ];
  148. yield [
  149. '<?php
  150. $bar = $foo[0][1][2];',
  151. '<?php
  152. $bar = $foo [0] [1] [2];',
  153. ];
  154. yield [
  155. '<?php
  156. $baz[0][1][2] = 3;',
  157. '<?php
  158. $baz [0]
  159. [1]
  160. [2] = 3;',
  161. ];
  162. if (\PHP_VERSION_ID < 8_00_00) {
  163. yield [
  164. '<?php
  165. $foo{0}{1}{2} = 3;',
  166. '<?php
  167. $foo {0} {1} {2} = 3;',
  168. ];
  169. yield [
  170. '<?php
  171. $foobar = $foo{0}[1]{2};',
  172. '<?php
  173. $foobar = $foo {0} [1] {2};',
  174. ];
  175. yield [
  176. '<?php
  177. $var = $arr[0]{0
  178. };',
  179. '<?php
  180. $var = $arr[0]{ 0
  181. };',
  182. ];
  183. }
  184. }
  185. public static function provideFixSpaceInsideOffsetCases(): iterable
  186. {
  187. yield [
  188. '<?php
  189. $foo = array(1, 2, 3);
  190. $var = $foo[1];',
  191. '<?php
  192. $foo = array(1, 2, 3);
  193. $var = $foo[ 1 ];',
  194. ];
  195. yield [
  196. '<?php
  197. $arr = [2, 2 , ];
  198. $var = $arr[0];',
  199. '<?php
  200. $arr = [2, 2 , ];
  201. $var = $arr[ 0 ];',
  202. ];
  203. yield [
  204. '<?php
  205. $arr[2] = 3;',
  206. '<?php
  207. $arr[ 2 ] = 3;',
  208. ];
  209. yield [
  210. '<?php
  211. $arr[] = 3;',
  212. '<?php
  213. $arr[ ] = 3;',
  214. ];
  215. yield [
  216. '<?php
  217. $arr[]["some_offset"][] = 3;',
  218. '<?php
  219. $arr[ ][ "some_offset" ][ ] = 3;',
  220. ];
  221. yield [
  222. '<?php
  223. $arr[]["some offset with spaces"][] = 3;',
  224. '<?php
  225. $arr[ ][ "some offset with spaces" ][ ] = 3;',
  226. ];
  227. yield [
  228. '<?php
  229. $var = $arr[0];',
  230. '<?php
  231. $var = $arr[ 0 ];',
  232. ];
  233. yield [
  234. '<?php
  235. $var = $arr[0][0];',
  236. '<?php
  237. $var = $arr[ 0 ][ 0 ];',
  238. ];
  239. yield [
  240. '<?php
  241. $var = $arr[$a[$b]];',
  242. '<?php
  243. $var = $arr[ $a [ $b ] ];',
  244. ];
  245. yield [
  246. '<?php
  247. $var = $arr[$a[$b]];',
  248. '<?php
  249. $var = $arr[ $a [ $b ] ];',
  250. ];
  251. yield [
  252. '<?php
  253. $var = $arr[0][
  254. 0];',
  255. '<?php
  256. $var = $arr[0][
  257. 0 ];',
  258. ];
  259. yield [
  260. '<?php
  261. $var = $arr[0][0
  262. ];',
  263. '<?php
  264. $var = $arr[0][ 0
  265. ];',
  266. ];
  267. }
  268. /**
  269. * @param list<string> $configuration
  270. *
  271. * @dataProvider provideFixWithConfigurationCases
  272. */
  273. public function testFixWithConfiguration(array $configuration, string $expected, string $input): void
  274. {
  275. $this->fixer->configure(['positions' => $configuration]);
  276. $this->doTest($expected, $input);
  277. }
  278. public static function provideFixWithConfigurationCases(): iterable
  279. {
  280. $tests = [
  281. [
  282. ['inside', 'outside'],
  283. <<<'EOT'
  284. <?php
  285. $arr1[]["some_offset"][]{"foo"} = 3;
  286. EOT
  287. ,
  288. <<<'EOT'
  289. <?php
  290. $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3;
  291. EOT
  292. ,
  293. ],
  294. [
  295. ['inside'],
  296. <<<'EOT'
  297. <?php
  298. $arr1[] ["some_offset"] [] {"foo"} = 3;
  299. EOT
  300. ,
  301. <<<'EOT'
  302. <?php
  303. $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3;
  304. EOT
  305. ,
  306. ],
  307. [
  308. ['outside'],
  309. <<<'EOT'
  310. <?php
  311. $arr1[ ][ "some_offset" ][ ]{ "foo" } = 3;
  312. EOT
  313. ,
  314. <<<'EOT'
  315. <?php
  316. $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3;
  317. EOT
  318. ,
  319. ],
  320. ];
  321. foreach ($tests as $index => $test) {
  322. if (\PHP_VERSION_ID >= 8_00_00) {
  323. $test[1] = str_replace('{', '[', $test[1]);
  324. $test[1] = str_replace('}', ']', $test[1]);
  325. $test[2] = str_replace('{', '[', $test[2]);
  326. $test[2] = str_replace('}', ']', $test[2]);
  327. }
  328. yield $index => $test;
  329. }
  330. yield 'Config "default".' => [
  331. ['inside', 'outside'],
  332. '<?php [ $a ] = $a;
  333. if ($controllerName = $request->attributes->get(1)) {
  334. return false;
  335. }
  336. [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName);
  337. $a = $b[0];
  338. ',
  339. '<?php [ $a ] = $a;
  340. if ($controllerName = $request->attributes->get(1)) {
  341. return false;
  342. }
  343. [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName);
  344. $a = $b [0];
  345. ',
  346. ];
  347. }
  348. public function testWrongConfig(): void
  349. {
  350. $this->expectException(InvalidFixerConfigurationException::class);
  351. $this->expectExceptionMessageMatches('/^\[no_spaces_around_offset\] Invalid configuration: The option "positions" .*\.$/');
  352. $this->fixer->configure(['positions' => ['foo']]);
  353. }
  354. }