ModernizeTypesCastingFixerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\CastNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Vladimir Reznichenko <kalessil@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  20. * @covers \PhpCsFixer\Fixer\CastNotation\ModernizeTypesCastingFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\ModernizeTypesCastingFixer>
  23. */
  24. final class ModernizeTypesCastingFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. $multiLinePatternToFix = <<<'FIX'
  39. <?php $x =
  40. intval
  41. (
  42. mt_rand
  43. (
  44. 0, 100
  45. )
  46. )
  47. ;
  48. FIX;
  49. $multiLinePatternFixed = <<<'FIXED'
  50. <?php $x =
  51. (int) (
  52. mt_rand
  53. (
  54. 0, 100
  55. )
  56. )
  57. ;
  58. FIXED;
  59. $overriddenFunction = <<<'OVERRIDDEN'
  60. <?php
  61. class overridesIntval
  62. {
  63. public function intval($x)
  64. {
  65. return \intval($x);
  66. }
  67. public function usesInval()
  68. {
  69. // that's why it is risky
  70. return intval(mt_rand(0, 100));
  71. }
  72. }
  73. OVERRIDDEN;
  74. $overriddenFunctionFixed = <<<'OVERRIDDEN'
  75. <?php
  76. class overridesIntval
  77. {
  78. public function intval($x)
  79. {
  80. return (int) $x;
  81. }
  82. public function usesInval()
  83. {
  84. // that's why it is risky
  85. return (int) (mt_rand(0, 100));
  86. }
  87. }
  88. OVERRIDDEN;
  89. yield ['<?php $x = "intval";'];
  90. yield ['<?php $x = ClassA::intval(mt_rand(0, 100));'];
  91. yield ['<?php $x = ScopeA\intval(mt_rand(0, 100));'];
  92. yield ['<?php $x = namespace\intval(mt_rand(0, 100));'];
  93. yield ['<?php $x = $object->intval(mt_rand(0, 100));'];
  94. yield ['<?php $x = new \intval(mt_rand(0, 100));'];
  95. yield ['<?php $x = new intval(mt_rand(0, 100));'];
  96. yield ['<?php $x = new ScopeB\intval(mt_rand(0, 100));'];
  97. yield ['<?php intvalSmth(mt_rand(0, 100));'];
  98. yield ['<?php smth_intval(mt_rand(0, 100));'];
  99. yield ['<?php "SELECT ... intval(mt_rand(0, 100)) ...";'];
  100. yield ['<?php "test" . "intval" . "in concatenation";'];
  101. yield ['<?php $x = intval($x, 16);'];
  102. yield ['<?php $x = intval($x, $options["base"]);'];
  103. yield ['<?php $x = intval($x, $options->get("base", 16));'];
  104. yield ['<?php $x = (int) $x;', '<?php $x = intval($x);'];
  105. yield ['<?php $x = (float) $x;', '<?php $x = floatval($x);'];
  106. yield ['<?php $x = (float) $x;', '<?php $x = doubleval($x);'];
  107. yield ['<?php $x = (string) $x;', '<?php $x = strval($x);'];
  108. yield ['<?php $x = (bool) $x;', '<?php $x = boolval ( $x );'];
  109. yield ['<?php $x = (int) (mt_rand(0, 100));', '<?php $x = intval(mt_rand(0, 100));'];
  110. yield ['<?php $x = (int) (mt_rand(0, 100));', '<?php $x = \intval(mt_rand(0, 100));'];
  111. yield ['<?php $x = (int) (mt_rand(0, 100)).".dist";', '<?php $x = intval(mt_rand(0, 100)).".dist";'];
  112. yield ['<?php $x = (int) (mt_rand(0, 100)).".dist";', '<?php $x = \intval(mt_rand(0, 100)).".dist";'];
  113. yield [$multiLinePatternFixed, $multiLinePatternToFix];
  114. yield [$overriddenFunctionFixed, $overriddenFunction];
  115. yield [
  116. '<?php $a = (string) ($b . $c);',
  117. '<?php $a = strval($b . $c);',
  118. ];
  119. yield [
  120. '<?php $x = /**/(int) /**/ /** x*/(/**//** */mt_rand(0, 100)/***/)/*xx*/;',
  121. '<?php $x = /**/intval/**/ /** x*/(/**//** */mt_rand(0, 100)/***/)/*xx*/;',
  122. ];
  123. yield [
  124. '<?php $x = (string) ((int) ((int) $x + (float) $x));',
  125. '<?php $x = strval(intval(intval($x) + floatval($x)));',
  126. ];
  127. yield [
  128. '<?php intval();intval(1,2,3);',
  129. ];
  130. yield [
  131. '<?php
  132. interface Test
  133. {
  134. public function floatval($a);
  135. public function &doubleval($a);
  136. }',
  137. ];
  138. yield [
  139. '<?php $foo = ((int) $x)**2;',
  140. '<?php $foo = intval($x)**2;',
  141. ];
  142. yield [
  143. '<?php $foo = ((string) $x)[0];',
  144. '<?php $foo = strval($x)[0];',
  145. ];
  146. yield [
  147. '<?php $foo = ((string) ($x + $y))[0];',
  148. '<?php $foo = strval($x + $y)[0];',
  149. ];
  150. yield [
  151. '<?php $a = (int) $b;',
  152. '<?php $a = intval($b, );',
  153. ];
  154. yield [
  155. '<?php $a = (int) $b;',
  156. '<?php $a = intval($b , );',
  157. ];
  158. yield [
  159. '<?php $a = (string) ($b . $c);',
  160. '<?php $a = strval($b . $c, );',
  161. ];
  162. }
  163. /**
  164. * @dataProvider provideFixPre80Cases
  165. *
  166. * @requires PHP <8.0
  167. */
  168. public function testFixPre80(string $expected, ?string $input = null): void
  169. {
  170. $this->doTest($expected, $input);
  171. }
  172. /**
  173. * @return iterable<array{string, string}>
  174. */
  175. public static function provideFixPre80Cases(): iterable
  176. {
  177. yield [
  178. '<?php $foo = ((string) ($x + $y)){0};',
  179. '<?php $foo = strval($x + $y){0};',
  180. ];
  181. yield [
  182. '<?php $a = #
  183. #
  184. #
  185. (int) #
  186. (
  187. #
  188. $b#
  189. )#
  190. ;#',
  191. '<?php $a = #
  192. #
  193. \
  194. #
  195. intval#
  196. (
  197. #
  198. $b#
  199. )#
  200. ;#',
  201. ];
  202. }
  203. /**
  204. * @dataProvider provideFix81Cases
  205. *
  206. * @requires PHP 8.1
  207. */
  208. public function testFix81(string $expected, ?string $input = null): void
  209. {
  210. $this->doTest($expected, $input);
  211. }
  212. /**
  213. * @return iterable<array{string}>
  214. */
  215. public static function provideFix81Cases(): iterable
  216. {
  217. yield [
  218. '<?php $x = intval(...);',
  219. ];
  220. }
  221. }