ArrayPushFixerTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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\Alias;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Alias\ArrayPushFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\ArrayPushFixer>
  20. */
  21. final class ArrayPushFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'minimal' => [
  36. '<?php $a[] =$b;',
  37. '<?php array_push($a,$b);',
  38. ];
  39. yield 'simple' => [
  40. '<?php $a[] = $b;',
  41. '<?php array_push($a, $b);',
  42. ];
  43. yield 'simple, spaces' => [
  44. '<?php $a [] =$b ;',
  45. '<?php array_push( $a , $b );',
  46. ];
  47. yield 'simple 25x times' => [
  48. '<?php '.str_repeat('$a[] = $b;', 25),
  49. '<?php '.str_repeat('array_push($a, $b);', 25),
  50. ];
  51. yield 'simple namespaced' => [
  52. '<?php $a[] = $b1;',
  53. '<?php \array_push($a, $b1);',
  54. ];
  55. yield '; before' => [
  56. '<?php ; $a1[] = $b2 ?>',
  57. '<?php ; array_push($a1, $b2) ?>',
  58. ];
  59. yield ') before' => [
  60. '<?php
  61. if ($c) $a[] = $b;
  62. while (--$c > 0) $a[] = $c;
  63. ',
  64. '<?php
  65. if ($c) array_push($a, $b);
  66. while (--$c > 0) array_push($a, $c);
  67. ',
  68. ];
  69. yield '} before' => [
  70. '<?php $b3 = []; { $a = 1; } $b5[] = 1;',
  71. '<?php $b3 = []; { $a = 1; } \array_push($b5, 1);',
  72. ];
  73. yield '{ before' => [
  74. '<?php { $a[] = $b8; }',
  75. '<?php { array_push($a, $b8); }',
  76. ];
  77. yield 'comments and PHPDoc' => [
  78. '<?php /* */ $a2[] = $b3 /** */;',
  79. '<?php /* */ array_push($a2, $b3) /** */;',
  80. ];
  81. yield [
  82. '<?php $a4[1][] = $b6[2];',
  83. '<?php array_push($a4[1], $b6[2]);',
  84. ];
  85. yield 'case insensitive and precedence' => [
  86. '<?php
  87. $a[] = $b--;
  88. $a[] = ++$b;
  89. $a[] = !$b;
  90. $a[] = $b + $c;
  91. $a[] = 1 ** $c / 2 || !b && c(1,2,3) ^ $a[1];
  92. ',
  93. '<?php
  94. array_push($a, $b--);
  95. ARRAY_push($a, ++$b);
  96. array_PUSH($a, !$b);
  97. ARRAY_PUSH($a, $b + $c);
  98. \array_push($a, 1 ** $c / 2 || !b && c(1,2,3) ^ $a[1]);
  99. ',
  100. ];
  101. yield 'simple traditional array' => [
  102. '<?php $a[] = array($b, $c);',
  103. '<?php array_push($a, array($b, $c));',
  104. ];
  105. yield 'simple short array' => [
  106. '<?php $a[] = [$b];',
  107. '<?php array_push($a, [$b]);',
  108. ];
  109. yield 'multiple element short array' => [
  110. '<?php $a[] = [[], [], $b, $c];',
  111. '<?php array_push($a, [[], [], $b, $c]);',
  112. ];
  113. yield 'second argument wrapped in `(` `)`' => [
  114. '<?php $a::$c[] = ($b);',
  115. '<?php array_push($a::$c, ($b));',
  116. ];
  117. yield [
  118. '<?php $a::$c[] = $b;',
  119. '<?php array_push($a::$c, $b);',
  120. ];
  121. yield [
  122. '<?php $a[foo(1,2,3)][] = $b[foo(1,2,3)];',
  123. '<?php array_push($a[foo(1,2,3)], $b[foo(1,2,3)]);',
  124. ];
  125. yield [
  126. '<?php \A\B::$foo[] = 1;',
  127. '<?php array_push(\A\B::$foo, 1);',
  128. ];
  129. yield [
  130. '<?php static::$foo[] = 1;',
  131. '<?php array_push(static::$foo, 1);',
  132. ];
  133. yield [
  134. '<?php namespace\A::$foo[] = 1;',
  135. '<?php array_push(namespace\A::$foo, 1);',
  136. ];
  137. yield [
  138. '<?php foo()->bar[] = 1;',
  139. '<?php array_push(foo()->bar, 1);',
  140. ];
  141. yield [
  142. '<?php foo()[] = 1;',
  143. '<?php array_push(foo(), 1);',
  144. ];
  145. yield [
  146. '<?php $a->$c[] = $b;',
  147. '<?php array_push($a->$c, $b);',
  148. ];
  149. yield [
  150. '<?php $a->$c[1]->$d[$a--]->$a[7][] = $b;',
  151. '<?php array_push($a->$c[1]->$d[$a--]->$a[7], $b);',
  152. ];
  153. yield 'push multiple' => [
  154. '<?php array_push($a6, $b9, $c);',
  155. ];
  156. yield 'push multiple II' => [
  157. '<?php ; array_push($a6a, $b9->$a(1,2), $c);',
  158. ];
  159. yield 'push multiple short' => [
  160. '<?php array_push($a6, [$b,$c], []);',
  161. ];
  162. yield 'returns number of elements in the array I' => [
  163. '<?php foo(array_push($a7, $b10)); // ;array_push($a, $b);',
  164. ];
  165. yield 'returns number of elements in the array II' => [
  166. '<?php $a = 3 * array_push($a8, $b11);',
  167. ];
  168. yield 'returns number of elements in the array III' => [
  169. '<?php $a = foo($z, array_push($a9, $b12));',
  170. ];
  171. yield 'returns number of elements in the array IV' => [
  172. '<?php $z = array_push($a00, $b13);',
  173. ];
  174. yield 'function declare in different namespace' => [
  175. '<?php namespace Foo; function array_push($a11, $b14){};',
  176. ];
  177. yield 'overridden detect I' => [
  178. '<?php namespace Foo; array_push(1, $a15);',
  179. ];
  180. yield 'overridden detect II' => [
  181. '<?php namespace Foo; array_push($a + 1, $a16);',
  182. ];
  183. yield 'different namespace and not a function call' => [
  184. '<?php
  185. A\array_push($a, $b17);
  186. A::array_push($a, $b18);
  187. $a->array_push($a, $b19);
  188. ',
  189. ];
  190. yield 'open echo' => [
  191. '<?= array_push($a, $b20) ?> <?= array_push($a, $b20); ?>',
  192. ];
  193. yield 'ellipsis' => [
  194. '<?php array_push($a, ...$b21);',
  195. ];
  196. $precedenceCases = [
  197. '$b and $c',
  198. '$b or $c',
  199. '$b xor $c',
  200. ];
  201. foreach ($precedenceCases as $precedenceCase) {
  202. yield [
  203. \sprintf(
  204. '<?php array_push($a, %s);',
  205. $precedenceCase
  206. ),
  207. ];
  208. }
  209. yield [
  210. '<?php
  211. while (foo()) $a[] = $b;
  212. foreach (foo() as $C) $a[] = $b;
  213. if (foo()) $a[] = $b;
  214. if ($b) {} elseif (foo()) $a[] = $b;
  215. ',
  216. '<?php
  217. while (foo()) array_push($a, $b);
  218. foreach (foo() as $C) array_push($a, $b);
  219. if (foo()) array_push($a, $b);
  220. if ($b) {} elseif (foo()) array_push($a, $b);
  221. ',
  222. ];
  223. }
  224. /**
  225. * @dataProvider provideFixPre80Cases
  226. *
  227. * @requires PHP <8.0
  228. */
  229. public function testFixPre80(string $expected, ?string $input = null): void
  230. {
  231. $this->doTest($expected, $input);
  232. }
  233. /**
  234. * @return iterable<array{string, string}>
  235. */
  236. public static function provideFixPre80Cases(): iterable
  237. {
  238. yield [
  239. '<?php $a5{1*3}[2+1][] = $b4{2+1};',
  240. '<?php array_push($a5{1*3}[2+1], $b4{2+1});',
  241. ];
  242. yield [
  243. '<?php $a5{1*3}[2+1][] = $b7{2+1};',
  244. '<?php array_push($a5{1*3}[2+1], $b7{2+1});',
  245. ];
  246. }
  247. /**
  248. * @dataProvider provideFix80Cases
  249. *
  250. * @requires PHP 8.0
  251. */
  252. public function testFix80(string $expected, ?string $input = null): void
  253. {
  254. $this->doTest($expected, $input);
  255. }
  256. /**
  257. * @return iterable<array{string}>
  258. */
  259. public static function provideFix80Cases(): iterable
  260. {
  261. yield [
  262. '<?php array_push($b?->c[2], $b19);',
  263. ];
  264. }
  265. /**
  266. * @dataProvider provideFix81Cases
  267. *
  268. * @requires PHP 8.1
  269. */
  270. public function testFix81(string $expected, string $input): void
  271. {
  272. $this->doTest($expected, $input);
  273. }
  274. /**
  275. * @return iterable<string, array{string, string}>
  276. */
  277. public static function provideFix81Cases(): iterable
  278. {
  279. yield 'simple 8.1' => [
  280. '<?php
  281. $a[] = $b;
  282. $a = array_push(...);
  283. ',
  284. '<?php
  285. array_push($a, $b);
  286. $a = array_push(...);
  287. ',
  288. ];
  289. }
  290. /**
  291. * @dataProvider provideFixPre84Cases
  292. *
  293. * @requires PHP <8.4
  294. */
  295. public function testFixPre84(string $expected, string $input): void
  296. {
  297. $this->doTest($expected, $input);
  298. }
  299. /**
  300. * @return iterable<array{string, string}>
  301. */
  302. public static function provideFixPre84Cases(): iterable
  303. {
  304. yield [
  305. '<?php $a->$c[1]->$d{$a--}->$a[7][] = $b;',
  306. '<?php array_push($a->$c[1]->$d{$a--}->$a[7], $b);',
  307. ];
  308. }
  309. }