ModernizeTypesCastingFixerTest.php 6.4 KB

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