LongToShorthandOperatorFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. $reflection = new \ReflectionClass(LongToShorthandOperatorFixer::class);
  115. $operators = $reflection->getStaticProperties()['operators'];
  116. foreach ($operators as $operator => $info) {
  117. $shortHand = $info[1];
  118. yield \sprintf('Simple test with operator "%s" var/var.', $operator) => [
  119. \sprintf('<?php $a1 %s $b;', $shortHand),
  120. \sprintf('<?php $a1 = $a1 %s $b;', $operator),
  121. ];
  122. yield \sprintf('Simple test with operator "%s" var/const.', $operator) => [
  123. \sprintf('<?php $a2 %s 1;', $shortHand),
  124. \sprintf('<?php $a2 = $a2 %s 1;', $operator),
  125. ];
  126. }
  127. // odds and ends
  128. yield [
  129. '<?php $a4 += ++$b;',
  130. '<?php $a4 = $a4 + ++$b;',
  131. ];
  132. yield [
  133. '<?php $a5 .= '.'
  134. <<<EOD
  135. EOD
  136. ;',
  137. '<?php $a5 = $a5 .
  138. <<<EOD
  139. EOD
  140. ;',
  141. ];
  142. yield [
  143. '<?php $a6 .= '.'
  144. <<<\'EOD\'
  145. EOD
  146. ?>',
  147. '<?php $a6 = $a6 .
  148. <<<\'EOD\'
  149. EOD
  150. ?>',
  151. ];
  152. yield [
  153. '<?php
  154. $t += 1;
  155. $t1 -= 1;
  156. $t2 *= 1;
  157. $t3 /= 1;
  158. $t4 .= /* */ 1;',
  159. '<?php
  160. $t = ((($t))) + 1;
  161. $t1 = ($t1) - 1;
  162. $t2 = $t2 * 1;
  163. $t3 = ($t3) / 1;
  164. $t4 = ($t4) /* */ . 1;',
  165. ];
  166. // before assignment var
  167. yield 'minus itself' => [
  168. '<?php ;$a -= $a;',
  169. '<?php ;$a = $a - $a;',
  170. ];
  171. yield 'after not needed block' => [
  172. '<?php {echo 1;} $a &= $a;',
  173. '<?php {echo 1;} $a = $a & $a;',
  174. ];
  175. yield 'after if' => [
  176. '<?php if($z){echo 2;} $a |= $a;',
  177. '<?php if($z){echo 2;} $a = $a | $a;',
  178. ];
  179. yield 'fn minus itself' => [
  180. '<?php foo(1, $an -= $an);',
  181. '<?php foo(1, $an = $an - $an);',
  182. ];
  183. yield 'simple, before ) I' => [
  184. '<?php if ($a) $a .= "X"?>',
  185. '<?php if ($a) $a = $a . "X"?>',
  186. ];
  187. yield [
  188. '<?php
  189. $a1 /= +$b1;
  190. $a2 /= -$b2;
  191. ',
  192. '<?php
  193. $a1 = $a1 / +$b1;
  194. $a2 = $a2 / -$b2;
  195. ',
  196. ];
  197. // do not fix
  198. yield 'do not fix various' => ['<?php
  199. $a = ${foo} . 1;
  200. $a = ${foo}++ + 1;
  201. $a = $a[1] * 1;
  202. $a = $a(1 + 2) . 1;
  203. $a = $a[1][2] . 1;
  204. $a = $a[1][2][3][foo()][$a++][1+$a][${"foo"}][99] . 1;
  205. $a = ${foo}++ . 1;
  206. $a = ($a /* */ /* */ /* */ /* */ + 1 /* */ ) + 1;
  207. $a = 1 . 1 + foo();
  208. $a = 1 . foo() + 1;
  209. $a = 1 . foo();
  210. $a = 1 . foo(1, ++$a);
  211. $a = foo() . 1;
  212. $a = foo(1, ++$a) . 1;
  213. $a = $a[1] * 1;
  214. $a[1] = $a[0] * 1;
  215. $a = $a(1 + 2) . 1;
  216. foo($b, ${foo} + 1);
  217. foo($a + 1);
  218. $a++ + 2;
  219. 2 + $a++;
  220. $a = 7 . (int) $a;
  221. $a = (int) $a . 7;
  222. (int) $a = 7 . (int) $a;
  223. (int) $a = (int) $a . 7;
  224. $a = 1 . $a + foo();
  225. $a = $a instanceof \Foo & $b;
  226. $a = $a + $b instanceof \Foo;
  227. $a = $d / $a + $b;
  228. $d + $a = $a - $e;
  229. $a = $a >= $b;
  230. $a[1] = $a[1] instanceof \Foo & $b;
  231. '];
  232. yield ['<?php $a = 123 + $a + $c ?>'];
  233. yield ['<?php $a = $a + 123 + $c ?>'];
  234. // do not fix; not assignment
  235. yield ['<?php ($a + 123);'];
  236. yield ['<?php while(true){$a + 123;}'];
  237. yield ['<?php $a + 123;'];
  238. yield ['<?php ; $a + 123;'];
  239. // do not fix; precedence
  240. yield [
  241. '<?php
  242. $a = 1;
  243. $b = 3;
  244. $a = $a + $b ? 1 : 2;
  245. var_dump($a);
  246. $a = 1;
  247. $b = 3;
  248. $a += $b ? 1 : 2;
  249. var_dump($a);
  250. //---------------------
  251. $a = 2;
  252. $b = null;
  253. $a = $a + $b ?? 3;
  254. var_dump($a);
  255. $a = 2;
  256. $b = null;
  257. $a += $b ?? 3;
  258. var_dump($a);
  259. //---------------------
  260. $a = 3;
  261. $b = null;
  262. $a = $a + $b === null ? 3 : 1;
  263. var_dump($a);
  264. $a = 3;
  265. $b = null;
  266. $a += $b === null ? 3 : 1;
  267. var_dump($a);
  268. //---------------------
  269. $a = $a & $a ^ true;
  270. $a = $a ^ true & $a;
  271. $a = 1 . $a + foo();
  272. //---------------------
  273. $a = 1;
  274. $b = false;
  275. $z = true;
  276. $a = $a + $b || $z;
  277. var_dump($a);
  278. ',
  279. ];
  280. yield ['<?php {echo 1;} $a = new class{} & $a;'];
  281. // reverse
  282. yield 'simple I reverse' => [
  283. '<?php $a *= 9988 ?>',
  284. '<?php $a = 9988 * $a ?>',
  285. ];
  286. yield 'simple V, comments, reverse' => [
  287. '<?php foo(1, /*1*/$x /*2*/*= /*3*/123/*4*//*5*//*6*/, 1);',
  288. '<?php foo(1, /*1*/$x/*2*/=/*3*/123/*4*/*/*5*/$x/*6*/, 1);',
  289. ];
  290. yield 'simple VI, `)`, reverse' => [
  291. '<?php foo(1, $x *= 123);',
  292. '<?php foo(1, $x=123*$x);',
  293. ];
  294. yield [
  295. '<?php $a99 .= // foo
  296. <<<EOD
  297. EOD
  298. ;',
  299. '<?php $a99 = $a99 . // foo
  300. <<<EOD
  301. EOD
  302. ;',
  303. ];
  304. yield [
  305. '<?php $a00 .= // foo2
  306. <<<\'EOD\'
  307. EOD
  308. ;',
  309. '<?php $a00 = $a00 . // foo2
  310. <<<\'EOD\'
  311. EOD
  312. ;',
  313. ];
  314. yield 'do bother with to much mess' => [
  315. '<?php
  316. $a = 1 + $a + 2 + $a;
  317. $a = $a + 1 + $a + 2;
  318. ',
  319. ];
  320. yield [
  321. '<?php
  322. $r[1] = [&$r[1]];
  323. $r[1] = [$r[1],&$r[1]];
  324. ',
  325. ];
  326. yield 'switch case & default' => [
  327. '<?php
  328. switch(foo()) {
  329. case \'X\':
  330. $pX -= 789;
  331. break;
  332. default:
  333. $pY -= $b5;
  334. }
  335. ',
  336. '<?php
  337. switch(foo()) {
  338. case \'X\':
  339. $pX = $pX - 789;
  340. break;
  341. default:
  342. $pY = $pY - $b5;
  343. }
  344. ',
  345. ];
  346. yield 'operator precedence' => [
  347. '<?php $x = $z ? $b : $a = $a + 123;',
  348. ];
  349. yield 'alternative syntax' => [
  350. '<?php foreach([1, 2, 3] as $i): $a += $i; endforeach;',
  351. '<?php foreach([1, 2, 3] as $i): $a = $a + $i; endforeach;',
  352. ];
  353. yield 'assign and return' => [
  354. '<?php
  355. class Foo
  356. {
  357. private int $test = 1;
  358. public function bar(int $i): int
  359. {
  360. return $this->test += $i;
  361. }
  362. }',
  363. '<?php
  364. class Foo
  365. {
  366. private int $test = 1;
  367. public function bar(int $i): int
  368. {
  369. return $this->test = $this->test + $i;
  370. }
  371. }',
  372. ];
  373. }
  374. /**
  375. * @requires PHP <8.0
  376. *
  377. * @dataProvider provideFixPre80Cases
  378. */
  379. public function testFixPre80(string $expected, ?string $input = null): void
  380. {
  381. $this->doTest($expected, $input);
  382. }
  383. /**
  384. * @return iterable<int|string, array{0: string, 1?: string}>
  385. */
  386. public static function provideFixPre80Cases(): iterable
  387. {
  388. yield [
  389. '<?php
  390. $a = $a[1]{2} . 1;
  391. $a = $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99] . 1;
  392. $a = 1 . $a[1]{2};
  393. $a = 1 . $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99];',
  394. ];
  395. yield 'simple I\' array' => [
  396. '<?php $a[1] += 963;',
  397. '<?php $a[1] = $a{1} + 963;',
  398. ];
  399. yield 'simple II array' => [
  400. '<?php $a[1]{1} += 852;',
  401. '<?php $a[1]{1} = $a[1]{1} + 852;',
  402. ];
  403. yield 'simple III array' => [
  404. '<?php $a{7} += 742;',
  405. '<?php $a{7} = $a[7] + 742;',
  406. ];
  407. yield 'simple IV array' => [
  408. '<?php {$a[0]{1}[1] ^= $azz;} ?>',
  409. '<?php {$a[0]{1}[1] = $a[0][1]{1} ^ $azz;} ?>',
  410. ];
  411. }
  412. }