LongToShorthandOperatorFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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\Operator;
  13. use PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\AbstractShortOperatorFixer
  19. * @covers \PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer>
  22. */
  23. final class LongToShorthandOperatorFixerTest 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 'simple I' => [
  38. '<?php $a += 123;',
  39. '<?php $a = $a + 123;',
  40. ];
  41. yield 'simple II' => [
  42. '<?php $b[0] *= 789;',
  43. '<?php $b[0] = ($b[0]) * 789;',
  44. ];
  45. yield 'simple III' => [
  46. '<?php ($b *= 789);',
  47. '<?php ($b = $b * 789);',
  48. ];
  49. yield 'simple IV' => [
  50. '<?php foo($c /= 1234, 1);',
  51. '<?php foo($c = $c / 1234, 1);',
  52. ];
  53. yield 'simple V' => [
  54. '<?php foo(1, $x *= 1235, 1);',
  55. '<?php foo(1, $x = $x * 1235, 1);',
  56. ];
  57. yield 'simple II\' array' => [
  58. '<?php $aa[1] %= 963;',
  59. '<?php $aa[1] = $aa[1] % 963;',
  60. ];
  61. yield 'simple III array' => [
  62. '<?php $a[1][2] -= 852;',
  63. '<?php $a[1][2] = $a[1][2] - 852;',
  64. ];
  65. yield 'simple IV array' => [
  66. '<?php {$a[0][1][122] ^= $a;}',
  67. '<?php {$a[0][1][122] = $a[0][1][122] ^ $a;}',
  68. ];
  69. yield [
  70. '<?php $xa .= $b;',
  71. '<?php $xa = $xa . $b;',
  72. ];
  73. $constants = ['"foo"', "'foo'", '1', '1.1'];
  74. foreach ($constants as $i => $constant) {
  75. yield 'c #'.$i => [
  76. \sprintf('<?php $fa .= %s;', $constant),
  77. \sprintf('<?php $fa = $fa . %s;', $constant),
  78. ];
  79. yield 'c reverse #'.$i => [
  80. \sprintf('<?php $ga *= %s ;', $constant),
  81. \sprintf('<?php $ga = %s * $ga;', $constant),
  82. ];
  83. }
  84. foreach (['-', '/', '.', '%'] as $nonCommutativeKind) {
  85. yield \sprintf('non commutative kind "%s"', $nonCommutativeKind) => [
  86. \sprintf('<?php $nck = 5 %s $nck;', $nonCommutativeKind),
  87. ];
  88. }
  89. foreach (['*' => '*=', '|' => '|=', '&' => '&=', '^' => '^='] as $operator => $shortHand) {
  90. yield \sprintf('commutative operator "%s".', $operator) => [
  91. \sprintf('<?php $a3 %s "456" ;', $shortHand),
  92. \sprintf('<?php $a3 = "456" %s $a3;', $operator),
  93. ];
  94. }
  95. // array index
  96. yield 'simple I array' => [
  97. '<?php $ai[1] += 566;',
  98. '<?php $ai[1] = $ai[1] + 566;',
  99. ];
  100. yield 'simple II array' => [
  101. '<?php $p[1] += 789;',
  102. '<?php $p[1] = $p[1] + 789;',
  103. ];
  104. // minimal and multiple
  105. yield 'minimal' => [
  106. '<?php $a += 1;',
  107. '<?php $a=$a+1;',
  108. ];
  109. yield 'minimal, multiple' => [
  110. '<?php $a += 1;$a += 1;$a += 1;$a += 1;',
  111. '<?php $a=$a+1;$a=$a+1;$a=$a+1;$a=$a+1;',
  112. ];
  113. // test simple with all operators
  114. $operators = \Closure::bind(static fn (): array => LongToShorthandOperatorFixer::OPERATORS, null, LongToShorthandOperatorFixer::class)();
  115. foreach ($operators as $operator => $info) {
  116. $shortHand = $info[1];
  117. yield \sprintf('Simple test with operator "%s" var/var.', $operator) => [
  118. \sprintf('<?php $a1 %s $b;', $shortHand),
  119. \sprintf('<?php $a1 = $a1 %s $b;', $operator),
  120. ];
  121. yield \sprintf('Simple test with operator "%s" var/const.', $operator) => [
  122. \sprintf('<?php $a2 %s 1;', $shortHand),
  123. \sprintf('<?php $a2 = $a2 %s 1;', $operator),
  124. ];
  125. }
  126. // odds and ends
  127. yield [
  128. '<?php $a4 += ++$b;',
  129. '<?php $a4 = $a4 + ++$b;',
  130. ];
  131. yield [
  132. '<?php $a5 .= '.'
  133. <<<EOD
  134. EOD
  135. ;',
  136. '<?php $a5 = $a5 .
  137. <<<EOD
  138. EOD
  139. ;',
  140. ];
  141. yield [
  142. '<?php $a6 .= '.'
  143. <<<\'EOD\'
  144. EOD
  145. ?>',
  146. '<?php $a6 = $a6 .
  147. <<<\'EOD\'
  148. EOD
  149. ?>',
  150. ];
  151. yield [
  152. '<?php
  153. $t += 1;
  154. $t1 -= 1;
  155. $t2 *= 1;
  156. $t3 /= 1;
  157. $t4 .= /* */ 1;',
  158. '<?php
  159. $t = ((($t))) + 1;
  160. $t1 = ($t1) - 1;
  161. $t2 = $t2 * 1;
  162. $t3 = ($t3) / 1;
  163. $t4 = ($t4) /* */ . 1;',
  164. ];
  165. // before assignment var
  166. yield 'minus itself' => [
  167. '<?php ;$a -= $a;',
  168. '<?php ;$a = $a - $a;',
  169. ];
  170. yield 'after not needed block' => [
  171. '<?php {echo 1;} $a &= $a;',
  172. '<?php {echo 1;} $a = $a & $a;',
  173. ];
  174. yield 'after if' => [
  175. '<?php if($z){echo 2;} $a |= $a;',
  176. '<?php if($z){echo 2;} $a = $a | $a;',
  177. ];
  178. yield 'fn minus itself' => [
  179. '<?php foo(1, $an -= $an);',
  180. '<?php foo(1, $an = $an - $an);',
  181. ];
  182. yield 'simple, before ) I' => [
  183. '<?php if ($a) $a .= "X"?>',
  184. '<?php if ($a) $a = $a . "X"?>',
  185. ];
  186. yield [
  187. '<?php
  188. $a1 /= +$b1;
  189. $a2 /= -$b2;
  190. ',
  191. '<?php
  192. $a1 = $a1 / +$b1;
  193. $a2 = $a2 / -$b2;
  194. ',
  195. ];
  196. // do not fix
  197. yield 'do not fix various' => ['<?php
  198. $a = ${foo} . 1;
  199. $a = ${foo}++ + 1;
  200. $a = $a[1] * 1;
  201. $a = $a(1 + 2) . 1;
  202. $a = $a[1][2] . 1;
  203. $a = $a[1][2][3][foo()][$a++][1+$a][${"foo"}][99] . 1;
  204. $a = ${foo}++ . 1;
  205. $a = ($a /* */ /* */ /* */ /* */ + 1 /* */ ) + 1;
  206. $a = 1 . 1 + foo();
  207. $a = 1 . foo() + 1;
  208. $a = 1 . foo();
  209. $a = 1 . foo(1, ++$a);
  210. $a = foo() . 1;
  211. $a = foo(1, ++$a) . 1;
  212. $a = $a[1] * 1;
  213. $a[1] = $a[0] * 1;
  214. $a = $a(1 + 2) . 1;
  215. foo($b, ${foo} + 1);
  216. foo($a + 1);
  217. $a++ + 2;
  218. 2 + $a++;
  219. $a = 7 . (int) $a;
  220. $a = (int) $a . 7;
  221. (int) $a = 7 . (int) $a;
  222. (int) $a = (int) $a . 7;
  223. $a = 1 . $a + foo();
  224. $a = $a instanceof \Foo & $b;
  225. $a = $a + $b instanceof \Foo;
  226. $a = $d / $a + $b;
  227. $d + $a = $a - $e;
  228. $a = $a >= $b;
  229. $a[1] = $a[1] instanceof \Foo & $b;
  230. '];
  231. yield ['<?php $a = 123 + $a + $c ?>'];
  232. yield ['<?php $a = $a + 123 + $c ?>'];
  233. // do not fix; not assignment
  234. yield ['<?php ($a + 123);'];
  235. yield ['<?php while(true){$a + 123;}'];
  236. yield ['<?php $a + 123;'];
  237. yield ['<?php ; $a + 123;'];
  238. // do not fix; precedence
  239. yield [
  240. '<?php
  241. $a = 1;
  242. $b = 3;
  243. $a = $a + $b ? 1 : 2;
  244. var_dump($a);
  245. $a = 1;
  246. $b = 3;
  247. $a += $b ? 1 : 2;
  248. var_dump($a);
  249. //---------------------
  250. $a = 2;
  251. $b = null;
  252. $a = $a + $b ?? 3;
  253. var_dump($a);
  254. $a = 2;
  255. $b = null;
  256. $a += $b ?? 3;
  257. var_dump($a);
  258. //---------------------
  259. $a = 3;
  260. $b = null;
  261. $a = $a + $b === null ? 3 : 1;
  262. var_dump($a);
  263. $a = 3;
  264. $b = null;
  265. $a += $b === null ? 3 : 1;
  266. var_dump($a);
  267. //---------------------
  268. $a = $a & $a ^ true;
  269. $a = $a ^ true & $a;
  270. $a = 1 . $a + foo();
  271. //---------------------
  272. $a = 1;
  273. $b = false;
  274. $z = true;
  275. $a = $a + $b || $z;
  276. var_dump($a);
  277. ',
  278. ];
  279. yield ['<?php {echo 1;} $a = new class{} & $a;'];
  280. // reverse
  281. yield 'simple I reverse' => [
  282. '<?php $a *= 9988 ?>',
  283. '<?php $a = 9988 * $a ?>',
  284. ];
  285. yield 'simple V, comments, reverse' => [
  286. '<?php foo(1, /*1*/$x /*2*/*= /*3*/123/*4*//*5*//*6*/, 1);',
  287. '<?php foo(1, /*1*/$x/*2*/=/*3*/123/*4*/*/*5*/$x/*6*/, 1);',
  288. ];
  289. yield 'simple VI, `)`, reverse' => [
  290. '<?php foo(1, $x *= 123);',
  291. '<?php foo(1, $x=123*$x);',
  292. ];
  293. yield [
  294. '<?php $a99 .= // foo
  295. <<<EOD
  296. EOD
  297. ;',
  298. '<?php $a99 = $a99 . // foo
  299. <<<EOD
  300. EOD
  301. ;',
  302. ];
  303. yield [
  304. '<?php $a00 .= // foo2
  305. <<<\'EOD\'
  306. EOD
  307. ;',
  308. '<?php $a00 = $a00 . // foo2
  309. <<<\'EOD\'
  310. EOD
  311. ;',
  312. ];
  313. yield 'do bother with to much mess' => [
  314. '<?php
  315. $a = 1 + $a + 2 + $a;
  316. $a = $a + 1 + $a + 2;
  317. ',
  318. ];
  319. yield [
  320. '<?php
  321. $r[1] = [&$r[1]];
  322. $r[1] = [$r[1],&$r[1]];
  323. ',
  324. ];
  325. yield 'switch case & default' => [
  326. '<?php
  327. switch(foo()) {
  328. case \'X\':
  329. $pX -= 789;
  330. break;
  331. default:
  332. $pY -= $b5;
  333. }
  334. ',
  335. '<?php
  336. switch(foo()) {
  337. case \'X\':
  338. $pX = $pX - 789;
  339. break;
  340. default:
  341. $pY = $pY - $b5;
  342. }
  343. ',
  344. ];
  345. yield 'operator precedence' => [
  346. '<?php $x = $z ? $b : $a = $a + 123;',
  347. ];
  348. yield 'alternative syntax' => [
  349. '<?php foreach([1, 2, 3] as $i): $a += $i; endforeach;',
  350. '<?php foreach([1, 2, 3] as $i): $a = $a + $i; endforeach;',
  351. ];
  352. yield 'assign and return' => [
  353. '<?php
  354. class Foo
  355. {
  356. private int $test = 1;
  357. public function bar(int $i): int
  358. {
  359. return $this->test += $i;
  360. }
  361. }',
  362. '<?php
  363. class Foo
  364. {
  365. private int $test = 1;
  366. public function bar(int $i): int
  367. {
  368. return $this->test = $this->test + $i;
  369. }
  370. }',
  371. ];
  372. }
  373. /**
  374. * @requires PHP <8.0
  375. *
  376. * @dataProvider provideFixPre80Cases
  377. */
  378. public function testFixPre80(string $expected, ?string $input = null): void
  379. {
  380. $this->doTest($expected, $input);
  381. }
  382. /**
  383. * @return iterable<int|string, array{0: string, 1?: string}>
  384. */
  385. public static function provideFixPre80Cases(): iterable
  386. {
  387. yield [
  388. '<?php
  389. $a = $a[1]{2} . 1;
  390. $a = $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99] . 1;
  391. $a = 1 . $a[1]{2};
  392. $a = 1 . $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99];',
  393. ];
  394. yield 'simple I\' array' => [
  395. '<?php $a[1] += 963;',
  396. '<?php $a[1] = $a{1} + 963;',
  397. ];
  398. yield 'simple II array' => [
  399. '<?php $a[1]{1} += 852;',
  400. '<?php $a[1]{1} = $a[1]{1} + 852;',
  401. ];
  402. yield 'simple III array' => [
  403. '<?php $a{7} += 742;',
  404. '<?php $a{7} = $a[7] + 742;',
  405. ];
  406. yield 'simple IV array' => [
  407. '<?php {$a[0]{1}[1] ^= $azz;} ?>',
  408. '<?php {$a[0]{1}[1] = $a[0][1]{1} ^ $azz;} ?>',
  409. ];
  410. }
  411. }