ModernizeTypesCastingFixerTest.php 5.9 KB

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