ModernizeTypesCastingFixerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 $x = /**/(int) /**/ /** x*/(/**//** */mt_rand(0, 100)/***/)/*xx*/;',
  115. '<?php $x = /**/intval/**/ /** x*/(/**//** */mt_rand(0, 100)/***/)/*xx*/;',
  116. ],
  117. [
  118. '<?php $x = (string) ((int) ((int) $x + (float) $x));',
  119. '<?php $x = strval(intval(intval($x) + floatval($x)));',
  120. ],
  121. [
  122. '<?php intval();intval(1,2,3);',
  123. ],
  124. [
  125. '<?php
  126. interface Test
  127. {
  128. public function floatval($a);
  129. public function &doubleval($a);
  130. }',
  131. ],
  132. [
  133. '<?php $a = #
  134. #
  135. #
  136. (int) #
  137. (
  138. #
  139. $b#
  140. )#
  141. ;#',
  142. '<?php $a = #
  143. #
  144. \
  145. #
  146. intval#
  147. (
  148. #
  149. $b#
  150. )#
  151. ;#',
  152. ],
  153. ];
  154. }
  155. }