mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
[PHP] Add RandomFunctionRector
This commit is contained in:
parent
d14ab5d90c
commit
c8c82aabfd
60
packages/Php/src/Rector/RandomFunctionRector.php
Normal file
60
packages/Php/src/Rector/RandomFunctionRector.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class RandomFunctionRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $oldToNewFunctionNames = [
|
||||
'getrandmax' => '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;
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
|
@ -0,0 +1,21 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
random_int(0, mt_getrandmax());
|
||||
|
||||
mt_srand($a);
|
||||
|
||||
$a = &mt_srand($a);
|
||||
|
||||
/* foo */ mt_srand /** bar */ ($a);
|
||||
|
||||
a(mt_getrandmax ());
|
||||
|
||||
a(random_int(0, mt_getrandmax()));
|
||||
|
||||
a(mt_srand());
|
||||
|
||||
random_int($d, random_int($a,$b));
|
||||
|
||||
random_int($a, \Other\Scope\mt_rand($a));
|
||||
|
||||
$a = random_int(1,2) + random_int(3,4);
|
@ -0,0 +1,32 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Tests\Rector\RandomFunctionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @covers \Rector\Php\Rector\RandomFunctionRector
|
||||
*
|
||||
* Some tests copied from https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.12/tests/Fixer/Alias/RandomApiMigrationFixerTest.php
|
||||
*/
|
||||
final class RandomFunctionRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideWrongToFixedFiles()
|
||||
*/
|
||||
public function test(string $wrong, string $fixed): void
|
||||
{
|
||||
$this->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';
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
rand();
|
||||
|
||||
srand($a);
|
||||
|
||||
$a = &srand($a);
|
||||
|
||||
/* foo */ srand /** bar */ ($a);
|
||||
|
||||
a(getrandmax ());
|
||||
|
||||
a(rand());
|
||||
|
||||
a(srand());
|
||||
|
||||
rand($d, rand($a,$b));
|
||||
|
||||
rand($a, \Other\Scope\mt_rand($a));
|
||||
|
||||
$a = rand(1,2) + mt_rand(3,4);
|
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\Php\Rector\RandomFunctionRector: ~
|
Loading…
x
Reference in New Issue
Block a user