RandomApiMigrationFixerTest.php 6.8 KB

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