RandomApiMigrationFixerTest.php 6.8 KB

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