Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

final class ConstantTypeBounds
{
private const MAX = 10;

public function run()
{
$min = 5;
rand($min, self::MAX);

$high = 20;
rand($high, self::MAX);
}
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

final class ConstantTypeBounds
{
private const MAX = 10;

public function run()
{
$min = 5;
random_int($min, self::MAX);

$high = 20;
random_int(self::MAX, $high);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,10 @@ function randomFunction()
{
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);
}

Expand All @@ -37,24 +23,10 @@ function randomFunction()
{
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) + mt_rand(3, 4);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

function renameSrand()
{
srand($a);

$a = &srand($a);

/* foo */
srand/** bar */
($a);

a(srand());

a(getrandmax());
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

function renameSrand()
{
mt_srand($a);

$a = &mt_srand($a);

/* foo */
mt_srand/** bar */
($a);

a(mt_srand());

a(mt_getrandmax());
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

function seededKeepsRand()
{
srand(42);

$a = rand(1, 6);
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

function seededKeepsRand()
{
mt_srand(42);

$a = rand(1, 6);
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

function skipMtSrandSeeded()
{
mt_srand(42);

$a = rand(1, 6);
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\RandomFunctionRector\Fixture;

function skipUnorderedBounds(int $min, int $max, $a, $b, $d)
{
rand($min, $max);

rand($d, rand($a, $b));

rand($a, \Other\Scope\mt_rand($a));
}

?>
82 changes: 58 additions & 24 deletions rules/Php70/Rector/FuncCall/RandomFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use Rector\PhpParser\Node\Value\ValueResolver;
use PHPStan\Type\Constant\ConstantIntegerType;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand All @@ -31,7 +32,7 @@ final class RandomFunctionRector extends AbstractRector implements MinPhpVersion
];

public function __construct(
private readonly ValueResolver $valueResolver
private readonly BetterNodeFinder $betterNodeFinder
) {
}

Expand Down Expand Up @@ -61,29 +62,16 @@ public function refactor(Node $node): FuncCall|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;
if (! $this->isName($node, $oldFunctionName)) {
continue;
}

if ($newFunctionName === 'random_int' && ! $this->refactorRandArgs($node)) {
return null;
}

$node->name = new Name($newFunctionName);
return $node;
}

return null;
Expand All @@ -93,4 +81,50 @@ 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;
}
}
Loading