RandomApiMigrationFixerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. return [
  65. [
  66. '<?php random_int(0, getrandmax());',
  67. '<?php rand();',
  68. ['replacements' => ['rand' => 'random_int']],
  69. ],
  70. [
  71. '<?php random_int#1
  72. #2
  73. (0, getrandmax()#3
  74. #4
  75. )#5
  76. ;',
  77. '<?php rand#1
  78. #2
  79. (#3
  80. #4
  81. )#5
  82. ;',
  83. ['replacements' => ['rand' => 'random_int']],
  84. ],
  85. ['<?php $smth->srand($a);'],
  86. ['<?php srandSmth($a);'],
  87. ['<?php smth_srand($a);'],
  88. ['<?php new srand($a);'],
  89. ['<?php new Smth\\srand($a);'],
  90. ['<?php Smth\\srand($a);'],
  91. ['<?php namespace\\srand($a);'],
  92. ['<?php Smth::srand($a);'],
  93. ['<?php new srand\\smth($a);'],
  94. ['<?php srand::smth($a);'],
  95. ['<?php srand\\smth($a);'],
  96. ['<?php "SELECT ... srand(\$a) ...";'],
  97. ['<?php "SELECT ... SRAND($a) ...";'],
  98. ["<?php 'test'.'srand' . 'in concatenation';"],
  99. ['<?php "test" . "srand"."in concatenation";'],
  100. [
  101. '<?php
  102. class SrandClass
  103. {
  104. const srand = 1;
  105. public function srand($srand)
  106. {
  107. if (!defined("srand") || $srand instanceof srand) {
  108. echo srand;
  109. }
  110. }
  111. }
  112. class srand extends SrandClass{
  113. const srand = "srand";
  114. }
  115. ',
  116. ],
  117. ['<?php mt_srand($a);', '<?php srand($a);'],
  118. ['<?php \\mt_srand($a);', '<?php \\srand($a);'],
  119. ['<?php $a = &mt_srand($a);', '<?php $a = &srand($a);'],
  120. ['<?php $a = &\\mt_srand($a);', '<?php $a = &\\srand($a);'],
  121. ['<?php /* foo */ mt_srand /** bar */ ($a);', '<?php /* foo */ srand /** bar */ ($a);'],
  122. ['<?php a(mt_getrandmax ());', '<?php a(getrandmax ());'],
  123. ['<?php a(mt_rand());', '<?php a(rand());'],
  124. ['<?php a(mt_srand());', '<?php a(srand());'],
  125. ['<?php a(\\mt_srand());', '<?php a(\\srand());'],
  126. [
  127. '<?php rand(rand($a));',
  128. null,
  129. ['replacements' => ['rand' => 'random_int']],
  130. ],
  131. [
  132. '<?php random_int($d, random_int($a,$b));',
  133. '<?php rand($d, rand($a,$b));',
  134. ['replacements' => ['rand' => 'random_int']],
  135. ],
  136. [
  137. '<?php random_int($a, \Other\Scope\mt_rand($a));',
  138. '<?php rand($a, \Other\Scope\mt_rand($a));',
  139. ['replacements' => ['rand' => 'random_int']],
  140. ],
  141. [
  142. '<?php $a = random_int(1,2) + random_int(3,4);',
  143. '<?php $a = rand(1,2) + mt_rand(3,4);',
  144. ['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
  145. ],
  146. [
  147. '<?php
  148. interface Test
  149. {
  150. public function getrandmax();
  151. public function &rand();
  152. }',
  153. null,
  154. ['replacements' => ['rand' => 'random_int']],
  155. ],
  156. [
  157. '<?php rand($d, rand($a,$b));',
  158. null,
  159. ['replacements' => ['rand' => 'rand']],
  160. ],
  161. [
  162. '<?php $a = random_int(1,2,) + random_int(3,4,);',
  163. '<?php $a = rand(1,2,) + mt_rand(3,4,);',
  164. ['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
  165. ],
  166. [
  167. '<?php mt_srand($a,);',
  168. '<?php srand($a,);',
  169. ],
  170. ];
  171. }
  172. /**
  173. * @dataProvider provideFix81Cases
  174. *
  175. * @requires PHP 8.1
  176. */
  177. public function testFix81(string $expected, string $input = null): void
  178. {
  179. $this->doTest($expected, $input);
  180. }
  181. public static function provideFix81Cases(): iterable
  182. {
  183. yield 'simple 8.1' => [
  184. '<?php $f = srand(...);',
  185. ];
  186. }
  187. }