NoSpacesAroundOffsetFixerTest.php 8.2 KB

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