-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
RandomFunctionRector.php
81 lines (79 loc) · 2.91 KB
/
RandomFunctionRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare (strict_types=1);
namespace Rector\Php70\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\RandomFunctionRectorTest
*/
final class RandomFunctionRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
*/
private ValueResolver $valueResolver;
/**
* @var array<string, string>
*/
private const OLD_TO_NEW_FUNCTION_NAMES = ['getrandmax' => 'mt_getrandmax', 'srand' => 'mt_srand', 'rand' => 'random_int'];
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes rand, srand, and getrandmax to newer alternatives', [new CodeSample('rand();', 'random_int();')]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node) : ?\PhpParser\Node\Expr\FuncCall
{
if ($node->isFirstClassCallable()) {
return null;
}
foreach (self::OLD_TO_NEW_FUNCTION_NAMES as $oldFunctionName => $newFunctionName) {
if ($this->isName($node, $oldFunctionName)) {
$node->name = new Name($newFunctionName);
// special case: random_int(); → random_int(0, getrandmax());
if ($newFunctionName === 'random_int') {
$args = $node->getArgs();
if ($args === []) {
$node->args[0] = new Arg(new Int_(0));
$node->args[1] = new Arg($this->nodeFactory->createFuncCall('mt_getrandmax'));
} elseif (\count($args) === 2) {
$minValue = $this->valueResolver->getValue($args[0]->value);
$maxValue = $this->valueResolver->getValue($args[1]->value);
if (\is_int($minValue) && \is_int($maxValue) && $minValue > $maxValue) {
$temp = $node->args[0];
$node->args[0] = $node->args[1];
$node->args[1] = $temp;
}
}
}
return $node;
}
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::CSPRNG_FUNCTIONS;
}
}