PowToExponentiationFixerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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\AbstractFunctionReferenceFixer
  18. * @covers \PhpCsFixer\Fixer\Alias\PowToExponentiationFixer
  19. *
  20. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\PowToExponentiationFixer>
  21. */
  22. final class PowToExponentiationFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. /**
  32. * @return iterable<array{0: string, 1?: string}>
  33. */
  34. public static function provideFixCases(): iterable
  35. {
  36. yield [
  37. '<?php 1**2;',
  38. '<?php pow(1,2);',
  39. ];
  40. yield [
  41. '<?php 1**2?>',
  42. '<?php pow(1,2)?>',
  43. ];
  44. yield [
  45. '<?php 1.2**2.3;',
  46. '<?php pow(1.2,2.3);',
  47. ];
  48. yield [
  49. '<?php echo (-2)** 3;',
  50. '<?php echo pow(-2, 3);',
  51. ];
  52. yield [
  53. '<?php echo (-2)**( -3);',
  54. '<?php echo pow(-2, -3);',
  55. ];
  56. yield [
  57. '<?php echo (-2)**( 1-3);',
  58. '<?php echo pow(-2, 1-3);',
  59. ];
  60. yield [
  61. '<?php echo (-2)**( -1-3);',
  62. '<?php echo pow(-2, -1-3);',
  63. ];
  64. yield [
  65. '<?php $a = 3** +2;',
  66. '<?php $a = pow(3, +2);',
  67. ];
  68. yield [
  69. '<?php $a--**++$b;',
  70. '<?php pow($a--,++$b);',
  71. ];
  72. yield [
  73. '<?php 1//
  74. #
  75. **2/**/ /** */;',
  76. '<?php pow(1//
  77. #
  78. ,2/**/ /** */);',
  79. ];
  80. yield [
  81. '<?php /**/a(3/**/,4)**$pow;//pow(1,2);',
  82. '<?php pow/**/(a(3/**/,4),$pow);//pow(1,2);',
  83. ];
  84. yield [
  85. '<?php \a\pow(5,6);7**8?>',
  86. '<?php \a\pow(5,6);pow(7,8)?>',
  87. ];
  88. yield [
  89. '<?php (9**10)**(11**12);',
  90. '<?php pow(pow(9,10),pow(11,12));',
  91. ];
  92. yield [
  93. '<?php (1 + 2)**( 3 * 4);',
  94. '<?php pow(1 + 2, 3 * 4);',
  95. ];
  96. yield [
  97. '<?php ($b = 4)** 3;',
  98. '<?php pow($b = 4, 3);',
  99. ];
  100. yield [
  101. '<?php 13**14;',
  102. '<?php \pow(13,14);',
  103. ];
  104. yield [
  105. '<?php $a = 15 + (16** 17)** 18;',
  106. '<?php $a = 15 + \pow(\pow(16, 17), 18);',
  107. ];
  108. yield [
  109. '<?php $a = $b** $c($d + 1);',
  110. '<?php $a = pow($b, $c($d + 1));',
  111. ];
  112. yield [
  113. '<?php $a = ($a+$b)** ($c-$d);',
  114. '<?php $a = pow(($a+$b), ($c-$d));',
  115. ];
  116. yield [
  117. "<?php \$a = 2**( '1'.'2'). 2;",
  118. "<?php \$a = pow(2, '1'.'2'). 2;",
  119. ];
  120. yield [
  121. '<?php A::B** 2;\A\B\C::B** 2;',
  122. '<?php pow(A::B, 2);pow(\A\B\C::B, 2);',
  123. ];
  124. yield [
  125. '<?php $obj->{$bar}** $obj->{$foo};',
  126. '<?php pow($obj->{$bar}, $obj->{$foo});',
  127. ];
  128. yield [
  129. '<?php echo ${$bar}** ${$foo};',
  130. '<?php echo pow(${$bar}, ${$foo});',
  131. ];
  132. yield [
  133. '<?php echo $a[2^3+1]->test(1,2)** $b[2+$c];',
  134. '<?php echo pow($a[2^3+1]->test(1,2), $b[2+$c]);',
  135. ];
  136. yield [
  137. '<?php (int)"2"**(float)"3.0";',
  138. '<?php pow((int)"2",(float)"3.0");',
  139. ];
  140. yield [
  141. '<?php namespace\Foo::BAR** 2;',
  142. '<?php pow(namespace\Foo::BAR, 2);',
  143. ];
  144. yield [
  145. '<?php (-1)**( (-2)**( (-3)**( (-4)**( (-5)**( (-6)**( (-7)**( (-8)**( (-9)** 3))))))));',
  146. '<?php pow(-1, pow(-2, pow(-3, pow(-4, pow(-5, pow(-6, pow(-7, pow(-8, pow(-9, 3)))))))));',
  147. ];
  148. yield [
  149. '<?php
  150. $z = 1**2;
  151. $a = 1**( 2**( 3**( 4**( 5**( 6**( 7**( 8**( 9** 3))))))));
  152. $b = 1**( 2**( 3**( 4**( 5**( 6**( 7**( 8**( 9** 3))))))));
  153. $d = 1**2;
  154. ',
  155. '<?php
  156. $z = pow(1,2);
  157. $a = \pow(1, \poW(2, \pOw(3, \pOW(4, \Pow(5, \PoW(6, \POw(7, \POW(8, \pow(9, 3)))))))));
  158. $b = \pow(1, \pow(2, \pow(3, \pow(4, \pow(5, \pow(6, \pow(7, \pow(8, \pow(9, 3)))))))));
  159. $d = pow(1,2);
  160. ',
  161. ];
  162. yield [
  163. '<?php $b = 3** __LINE__;',
  164. '<?php $b = pow(3, __LINE__);',
  165. ];
  166. yield [
  167. '<?php
  168. ($a-$b)**(
  169. ($a-$b)**(
  170. ($a-$b)**(
  171. ($a-$b)**(
  172. ($a-$b)**($a-$b)
  173. ))));',
  174. '<?php
  175. pow($a-$b,
  176. pow($a-$b,
  177. pow($a-$b,
  178. pow($a-$b,
  179. pow($a-$b,$a-$b)
  180. ))));',
  181. ];
  182. yield [
  183. '<?php (-1)**( $a** pow(1,2,3, ($a-3)** 4));',
  184. '<?php pow(-1, pow($a, pow(1,2,3, pow($a-3, 4))));',
  185. ];
  186. yield [
  187. '<?php 1**2 /**/ ?>',
  188. '<?php pow(1,2) /**/ ?>',
  189. ];
  190. yield [
  191. '<?php ($$a)**( $$b);',
  192. '<?php pow($$a, $$b);',
  193. ];
  194. yield [
  195. '<?php [1, 2, 3, 4][$x]** 2;',
  196. '<?php pow([1, 2, 3, 4][$x], 2);',
  197. ];
  198. yield [
  199. '<?php echo +$a** 2;',
  200. '<?php echo pow(+$a, 2);',
  201. ];
  202. yield [
  203. '<?php
  204. interface Test
  205. {
  206. public function pow($a, $b);
  207. }',
  208. ];
  209. yield [
  210. '<?php
  211. interface Test
  212. {
  213. public function &pow($a, $b);
  214. }',
  215. ];
  216. yield [
  217. '<?php echo $a[1]** $b[2+5];',
  218. '<?php echo pow($a[1], $b[2+5]);',
  219. ];
  220. yield [
  221. '<?php pow($b, ...$a);',
  222. ];
  223. yield [
  224. '<?php echo +$a** 2;',
  225. '<?php echo pow(+$a, 2,);',
  226. ];
  227. yield [
  228. '<?php echo +$a** 2/*1*//*2*/;',
  229. '<?php echo pow(+$a, 2/*1*/,/*2*/);',
  230. ];
  231. yield [
  232. '<?php echo 10_0** 2;',
  233. '<?php echo pow(10_0, 2);',
  234. ];
  235. yield [
  236. '<?php pow(); ++$a;++$a;++$a;++$a;++$a;++$a;// pow(1,2);',
  237. ];
  238. yield [
  239. '<?php pow(5); ++$a;++$a;++$a;++$a;++$a;++$a;# pow(1,2);',
  240. ];
  241. yield [
  242. '<?php pow(5,1,1); ++$a;++$a;++$a;++$a;++$a;++$a;/* pow(1,2); */',
  243. ];
  244. yield [
  245. '<?php \a\pow(4,3); ++$a;++$a;++$a;++$a;++$a;++$a;/** pow(1,2); */',
  246. ];
  247. }
  248. /**
  249. * @dataProvider provideFixPre80Cases
  250. *
  251. * @requires PHP <8.0
  252. */
  253. public function testFixPre80(string $expected, ?string $input = null): void
  254. {
  255. $this->doTest($expected, $input);
  256. }
  257. /**
  258. * @return iterable<array{string, string}>
  259. */
  260. public static function provideFixPre80Cases(): iterable
  261. {
  262. yield [
  263. '<?php echo $a{1}** $b{2+5};',
  264. '<?php echo pow($a{1}, $b{2+5});',
  265. ];
  266. }
  267. /**
  268. * @requires PHP 8.0
  269. *
  270. * @dataProvider provideFix80Cases
  271. */
  272. public function testFix80(string $expected, string $input): void
  273. {
  274. $this->doTest($expected, $input);
  275. }
  276. /**
  277. * @return iterable<array{string, string}>
  278. */
  279. public static function provideFix80Cases(): iterable
  280. {
  281. yield [
  282. '<?php echo $a[2^3+1]?->test(1,2)** $b[2+$c];',
  283. '<?php echo pow($a[2^3+1]?->test(1,2), $b[2+$c]);',
  284. ];
  285. }
  286. }