RandomApiMigrationFixerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Vladimir Reznichenko <kalessil@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  21. * @covers \PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer
  22. */
  23. final class RandomApiMigrationFixerTest extends AbstractFixerTestCase
  24. {
  25. public function testConfigureCheckSearchFunction(): void
  26. {
  27. $this->expectException(InvalidFixerConfigurationException::class);
  28. $this->expectExceptionMessageMatches('#^\[random_api_migration\] Invalid configuration: Function "is_null" is not handled by the fixer\.$#');
  29. $this->fixer->configure(['replacements' => ['is_null' => 'random_int']]);
  30. }
  31. public function testConfigureCheckReplacementType(): void
  32. {
  33. $this->expectException(InvalidFixerConfigurationException::class);
  34. $this->expectExceptionMessageMatches('#^\[random_api_migration\] Invalid configuration: Replacement for function "rand" must be a string, "null" given\.$#');
  35. $this->fixer->configure(['replacements' => ['rand' => null]]);
  36. }
  37. public function testConfigure(): void
  38. {
  39. $this->fixer->configure(['replacements' => ['rand' => 'random_int']]);
  40. $reflectionProperty = new \ReflectionProperty($this->fixer, 'configuration');
  41. $reflectionProperty->setAccessible(true);
  42. self::assertSame(
  43. ['replacements' => [
  44. 'rand' => ['alternativeName' => 'random_int', 'argumentCount' => [0, 2]], ],
  45. ],
  46. $reflectionProperty->getValue($this->fixer)
  47. );
  48. }
  49. /**
  50. * @param array<string, mixed> $config
  51. *
  52. * @dataProvider provideFixCases
  53. */
  54. public function testFix(string $expected, ?string $input = null, array $config = []): void
  55. {
  56. $this->fixer->configure($config);
  57. $this->doTest($expected, $input);
  58. }
  59. /**
  60. * @return array[]
  61. */
  62. public static function provideFixCases(): iterable
  63. {
  64. yield [
  65. '<?php random_int(0, getrandmax());',
  66. '<?php rand();',
  67. ['replacements' => ['rand' => 'random_int']],
  68. ];
  69. yield [
  70. '<?php random_int#1
  71. #2
  72. (0, getrandmax()#3
  73. #4
  74. )#5
  75. ;',
  76. '<?php rand#1
  77. #2
  78. (#3
  79. #4
  80. )#5
  81. ;',
  82. ['replacements' => ['rand' => 'random_int']],
  83. ];
  84. yield ['<?php $smth->srand($a);'];
  85. yield ['<?php srandSmth($a);'];
  86. yield ['<?php smth_srand($a);'];
  87. yield ['<?php new srand($a);'];
  88. yield ['<?php new Smth\\srand($a);'];
  89. yield ['<?php Smth\\srand($a);'];
  90. yield ['<?php namespace\\srand($a);'];
  91. yield ['<?php Smth::srand($a);'];
  92. yield ['<?php new srand\\smth($a);'];
  93. yield ['<?php srand::smth($a);'];
  94. yield ['<?php srand\\smth($a);'];
  95. yield ['<?php "SELECT ... srand(\$a) ...";'];
  96. yield ['<?php "SELECT ... SRAND($a) ...";'];
  97. yield ["<?php 'test'.'srand' . 'in concatenation';"];
  98. yield ['<?php "test" . "srand"."in concatenation";'];
  99. yield [
  100. '<?php
  101. class SrandClass
  102. {
  103. const srand = 1;
  104. public function srand($srand)
  105. {
  106. if (!defined("srand") || $srand instanceof srand) {
  107. echo srand;
  108. }
  109. }
  110. }
  111. class srand extends SrandClass{
  112. const srand = "srand";
  113. }
  114. ',
  115. ];
  116. yield ['<?php mt_srand($a);', '<?php srand($a);'];
  117. yield ['<?php \\mt_srand($a);', '<?php \\srand($a);'];
  118. yield ['<?php $a = &mt_srand($a);', '<?php $a = &srand($a);'];
  119. yield ['<?php $a = &\\mt_srand($a);', '<?php $a = &\\srand($a);'];
  120. yield ['<?php /* foo */ mt_srand /** bar */ ($a);', '<?php /* foo */ srand /** bar */ ($a);'];
  121. yield ['<?php a(mt_getrandmax ());', '<?php a(getrandmax ());'];
  122. yield ['<?php a(mt_rand());', '<?php a(rand());'];
  123. yield ['<?php a(mt_srand());', '<?php a(srand());'];
  124. yield ['<?php a(\\mt_srand());', '<?php a(\\srand());'];
  125. yield [
  126. '<?php rand(rand($a));',
  127. null,
  128. ['replacements' => ['rand' => 'random_int']],
  129. ];
  130. yield [
  131. '<?php random_int($d, random_int($a,$b));',
  132. '<?php rand($d, rand($a,$b));',
  133. ['replacements' => ['rand' => 'random_int']],
  134. ];
  135. yield [
  136. '<?php random_int($a, \Other\Scope\mt_rand($a));',
  137. '<?php rand($a, \Other\Scope\mt_rand($a));',
  138. ['replacements' => ['rand' => 'random_int']],
  139. ];
  140. yield [
  141. '<?php $a = random_int(1,2) + random_int(3,4);',
  142. '<?php $a = rand(1,2) + mt_rand(3,4);',
  143. ['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
  144. ];
  145. yield [
  146. '<?php
  147. interface Test
  148. {
  149. public function getrandmax();
  150. public function &rand();
  151. }',
  152. null,
  153. ['replacements' => ['rand' => 'random_int']],
  154. ];
  155. yield [
  156. '<?php rand($d, rand($a,$b));',
  157. null,
  158. ['replacements' => ['rand' => 'rand']],
  159. ];
  160. yield [
  161. '<?php $a = random_int(1,2,) + random_int(3,4,);',
  162. '<?php $a = rand(1,2,) + mt_rand(3,4,);',
  163. ['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
  164. ];
  165. yield [
  166. '<?php mt_srand($a,);',
  167. '<?php srand($a,);',
  168. ];
  169. }
  170. /**
  171. * @dataProvider provideFix81Cases
  172. *
  173. * @requires PHP 8.1
  174. */
  175. public function testFix81(string $expected, string $input = null): void
  176. {
  177. $this->doTest($expected, $input);
  178. }
  179. public static function provideFix81Cases(): iterable
  180. {
  181. yield 'simple 8.1' => [
  182. '<?php $f = srand(...);',
  183. ];
  184. }
  185. }