diff --git a/packages/Php/src/Rector/RandomFunctionRector.php b/packages/Php/src/Rector/RandomFunctionRector.php new file mode 100644 index 00000000000..a12fdb7e03f --- /dev/null +++ b/packages/Php/src/Rector/RandomFunctionRector.php @@ -0,0 +1,60 @@ + 'mt_getrandmax', + 'srand' => 'mt_srand', + 'mt_rand' => 'random_int', + 'rand' => 'random_int', + ]; + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition( + 'Changes rand, srand and getrandmax by new md_* alternatives.', + [new CodeSample('rand();', 'mt_rand();')] + ); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [FuncCall::class]; + } + + /** + * @param FuncCall $funcCallNode + */ + public function refactor(Node $funcCallNode): ?Node + { + foreach ($this->oldToNewFunctionNames as $oldFunctionName => $newFunctionName) { + if ((string) $funcCallNode->name === $oldFunctionName) { + $funcCallNode->name = new Node\Name($newFunctionName); + + // special case: random_int(); → random_int(0, getrandmax()); + if ($newFunctionName === 'random_int' && count($funcCallNode->args) === 0) { + $funcCallNode->args[0] = new Node\Arg(new Node\Scalar\LNumber(0)); + $funcCallNode->args[1] = new Node\Arg(new FuncCall(new Node\Name('mt_getrandmax'))); + } + + return $funcCallNode; + } + } + + return $funcCallNode; + } +} diff --git a/packages/Php/tests/Rector/PowToExpRector/PowToExpRectorTest.php b/packages/Php/tests/Rector/PowToExpRector/PowToExpRectorTest.php index de7ae84d597..3fd9bebfa83 100644 --- a/packages/Php/tests/Rector/PowToExpRector/PowToExpRectorTest.php +++ b/packages/Php/tests/Rector/PowToExpRector/PowToExpRectorTest.php @@ -8,7 +8,8 @@ use Rector\Testing\PHPUnit\AbstractRectorTestCase; /** * @covers \Rector\Php\Rector\PowToExpRector * - * Few tests copied from https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/14660432d9d0b66bf65135d793b52872cc6eccbc#diff-b412676c923661ef450f4a0903c5442a + * Some tests copied from: + * - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/14660432d9d0b66bf65135d793b52872cc6eccbc#diff-b412676c923661ef450f4a0903c5442a */ final class PowToExpRectorTest extends AbstractRectorTestCase { diff --git a/packages/Php/tests/Rector/RandomFunctionRector/Correct/correct.php.inc b/packages/Php/tests/Rector/RandomFunctionRector/Correct/correct.php.inc new file mode 100644 index 00000000000..3f725cbaf0e --- /dev/null +++ b/packages/Php/tests/Rector/RandomFunctionRector/Correct/correct.php.inc @@ -0,0 +1,21 @@ +doTestFileMatchesExpectedContent($wrong, $fixed); + } + + public function provideWrongToFixedFiles(): Iterator + { + yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; + } + + protected function provideConfig(): string + { + return __DIR__ . '/config.yml'; + } +} diff --git a/packages/Php/tests/Rector/RandomFunctionRector/Wrong/wrong.php.inc b/packages/Php/tests/Rector/RandomFunctionRector/Wrong/wrong.php.inc new file mode 100644 index 00000000000..65713de6a20 --- /dev/null +++ b/packages/Php/tests/Rector/RandomFunctionRector/Wrong/wrong.php.inc @@ -0,0 +1,21 @@ +