Repository navigation
Expand file tree
/
Copy pathRandomFunctionRector.php
More file actions
130 lines (108 loc) · 3.45 KB
/
Copy pathRandomFunctionRector.php
File metadata and controls
130 lines (108 loc) · 3.45 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?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 PHPStan\Type\Constant\ConstantIntegerType;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
/**
* @see \Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\RandomFunctionRectorTest
*/
final class RandomFunctionRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var array<string, string>
*/
private const array OLD_TO_NEW_FUNCTION_NAMES = [
'getrandmax' => 'mt_getrandmax',
'srand' => 'mt_srand',
'rand' => 'random_int',
];
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder
) {
}
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): FuncCall|null
{
if ($node->isFirstClassCallable()) {
return null;
}
foreach (self::OLD_TO_NEW_FUNCTION_NAMES as $oldFunctionName => $newFunctionName) {
if (! $this->isName($node, $oldFunctionName)) {
continue;
}
if ($newFunctionName === 'random_int' && ! $this->refactorRandArgs($node)) {
return null;
}
$node->name = new Name($newFunctionName);
return $node;
}
return null;
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_70;
}
/**
* random_int() ignores the seed and throws on min > max, unlike rand()
*/
private function refactorRandArgs(FuncCall $funcCall): bool
{
if ($this->isSeededFile()) {
return false;
}
$args = $funcCall->getArgs();
// special case: random_int(); → random_int(0, getrandmax());
if ($args === []) {
$funcCall->args[0] = new Arg(new Int_(0));
$funcCall->args[1] = new Arg($this->nodeFactory->createFuncCall('mt_getrandmax'));
return true;
}
if (count($args) !== 2) {
return true;
}
$minType = $this->getType($args[0]->value);
$maxType = $this->getType($args[1]->value);
if (! $minType instanceof ConstantIntegerType || ! $maxType instanceof ConstantIntegerType) {
return false;
}
if ($minType->getValue() > $maxType->getValue()) {
$funcCall->args = [$funcCall->args[1], $funcCall->args[0]];
}
return true;
}
private function isSeededFile(): bool
{
$seedFuncCall = $this->betterNodeFinder->findFirst(
$this->getFile()
->getNewStmts(),
fn (Node $node): bool => $node instanceof FuncCall && $this->isNames($node, ['srand', 'mt_srand'])
);
return $seedFuncCall instanceof FuncCall;
}
}