Skip to content

Commit 5164ebe

Browse files
authored
[Php70] Skip seeded files and unordered bounds in RandomFunctionRector (#9016)
1 parent 15165a1 commit 5164ebe

7 files changed

Lines changed: 189 additions & 52 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
4+
5+
final class ConstantTypeBounds
6+
{
7+
private const MAX = 10;
8+
9+
public function run()
10+
{
11+
$min = 5;
12+
rand($min, self::MAX);
13+
14+
$high = 20;
15+
rand($high, self::MAX);
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
24+
25+
final class ConstantTypeBounds
26+
{
27+
private const MAX = 10;
28+
29+
public function run()
30+
{
31+
$min = 5;
32+
random_int($min, self::MAX);
33+
34+
$high = 20;
35+
random_int(self::MAX, $high);
36+
}
37+
}
38+
39+
?>

‎rules-tests/Php70/Rector/FuncCall/RandomFunctionRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,10 @@ function randomFunction()
66
{
77
rand();
88

9-
srand($a);
10-
11-
$a = &srand($a);
12-
13-
/* foo */
14-
srand/** bar */
15-
($a);
16-
179
a(getrandmax());
1810

1911
a(rand());
2012

21-
a(srand());
22-
23-
rand($d, rand($a, $b));
24-
25-
rand($a, \Other\Scope\mt_rand($a));
26-
2713
$a = rand(1, 2) + mt_rand(3, 4);
2814
}
2915

@@ -37,24 +23,10 @@ function randomFunction()
3723
{
3824
random_int(0, mt_getrandmax());
3925

40-
mt_srand($a);
41-
42-
$a = &mt_srand($a);
43-
44-
/* foo */
45-
mt_srand/** bar */
46-
($a);
47-
4826
a(mt_getrandmax());
4927

5028
a(random_int(0, mt_getrandmax()));
5129

52-
a(mt_srand());
53-
54-
random_int($d, random_int($a, $b));
55-
56-
random_int($a, \Other\Scope\mt_rand($a));
57-
5830
$a = random_int(1, 2) + mt_rand(3, 4);
5931
}
6032

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
4+
5+
function renameSrand()
6+
{
7+
srand($a);
8+
9+
$a = &srand($a);
10+
11+
/* foo */
12+
srand/** bar */
13+
($a);
14+
15+
a(srand());
16+
17+
a(getrandmax());
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
25+
26+
function renameSrand()
27+
{
28+
mt_srand($a);
29+
30+
$a = &mt_srand($a);
31+
32+
/* foo */
33+
mt_srand/** bar */
34+
($a);
35+
36+
a(mt_srand());
37+
38+
a(mt_getrandmax());
39+
}
40+
41+
?>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
4+
5+
function seededKeepsRand()
6+
{
7+
srand(42);
8+
9+
$a = rand(1, 6);
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
17+
18+
function seededKeepsRand()
19+
{
20+
mt_srand(42);
21+
22+
$a = rand(1, 6);
23+
}
24+
25+
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
4+
5+
function skipMtSrandSeeded()
6+
{
7+
mt_srand(42);
8+
9+
$a = rand(1, 6);
10+
}
11+
12+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;
4+
5+
function skipUnorderedBounds(int $min, int $max, $a, $b, $d)
6+
{
7+
rand($min, $max);
8+
9+
rand($d, rand($a, $b));
10+
11+
rand($a, \Other\Scope\mt_rand($a));
12+
}
13+
14+
?>

‎rules/Php70/Rector/FuncCall/RandomFunctionRector.php‎

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use PhpParser\Node\Expr\FuncCall;
1010
use PhpParser\Node\Name;
1111
use PhpParser\Node\Scalar\Int_;
12-
use Rector\PhpParser\Node\Value\ValueResolver;
12+
use PHPStan\Type\Constant\ConstantIntegerType;
13+
use Rector\PhpParser\Node\BetterNodeFinder;
1314
use Rector\Rector\AbstractRector;
1415
use Rector\RuleDoc\CodeSample\CodeSample;
1516
use Rector\RuleDoc\RuleDefinition;
@@ -31,7 +32,7 @@ final class RandomFunctionRector extends AbstractRector implements MinPhpVersion
3132
];
3233

3334
public function __construct(
34-
private readonly ValueResolver $valueResolver
35+
private readonly BetterNodeFinder $betterNodeFinder
3536
) {
3637
}
3738

@@ -61,29 +62,16 @@ public function refactor(Node $node): FuncCall|null
6162
}
6263

6364
foreach (self::OLD_TO_NEW_FUNCTION_NAMES as $oldFunctionName => $newFunctionName) {
64-
if ($this->isName($node, $oldFunctionName)) {
65-
$node->name = new Name($newFunctionName);
66-
67-
// special case: random_int(); → random_int(0, getrandmax());
68-
if ($newFunctionName === 'random_int') {
69-
$args = $node->getArgs();
70-
if ($args === []) {
71-
$node->args[0] = new Arg(new Int_(0));
72-
$node->args[1] = new Arg($this->nodeFactory->createFuncCall('mt_getrandmax'));
73-
} elseif (count($args) === 2) {
74-
$minValue = $this->valueResolver->getValue($args[0]->value);
75-
$maxValue = $this->valueResolver->getValue($args[1]->value);
76-
77-
if (is_int($minValue) && is_int($maxValue) && $minValue > $maxValue) {
78-
$temp = $node->args[0];
79-
$node->args[0] = $node->args[1];
80-
$node->args[1] = $temp;
81-
}
82-
}
83-
}
84-
85-
return $node;
65+
if (! $this->isName($node, $oldFunctionName)) {
66+
continue;
8667
}
68+
69+
if ($newFunctionName === 'random_int' && ! $this->refactorRandArgs($node)) {
70+
return null;
71+
}
72+
73+
$node->name = new Name($newFunctionName);
74+
return $node;
8775
}
8876

8977
return null;
@@ -93,4 +81,50 @@ public function provideMinPhpVersion(): int
9381
{
9482
return PhpVersion::PHP_70;
9583
}
84+
85+
/**
86+
* random_int() ignores the seed and throws on min > max, unlike rand()
87+
*/
88+
private function refactorRandArgs(FuncCall $funcCall): bool
89+
{
90+
if ($this->isSeededFile()) {
91+
return false;
92+
}
93+
94+
$args = $funcCall->getArgs();
95+
96+
// special case: random_int(); → random_int(0, getrandmax());
97+
if ($args === []) {
98+
$funcCall->args[0] = new Arg(new Int_(0));
99+
$funcCall->args[1] = new Arg($this->nodeFactory->createFuncCall('mt_getrandmax'));
100+
return true;
101+
}
102+
103+
if (count($args) !== 2) {
104+
return true;
105+
}
106+
107+
$minType = $this->getType($args[0]->value);
108+
$maxType = $this->getType($args[1]->value);
109+
if (! $minType instanceof ConstantIntegerType || ! $maxType instanceof ConstantIntegerType) {
110+
return false;
111+
}
112+
113+
if ($minType->getValue() > $maxType->getValue()) {
114+
$funcCall->args = [$funcCall->args[1], $funcCall->args[0]];
115+
}
116+
117+
return true;
118+
}
119+
120+
private function isSeededFile(): bool
121+
{
122+
$seedFuncCall = $this->betterNodeFinder->findFirst(
123+
$this->getFile()
124+
->getNewStmts(),
125+
fn (Node $node): bool => $node instanceof FuncCall && $this->isNames($node, ['srand', 'mt_srand'])
126+
);
127+
128+
return $seedFuncCall instanceof FuncCall;
129+
}
96130
}

0 commit comments

Comments
 (0)