*/ private const OLD_TO_NEW_FUNCTION_NAMES = ['getrandmax' => 'mt_getrandmax', 'srand' => 'mt_srand', 'rand' => 'random_int']; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Changes rand, srand, and getrandmax to newer alternatives', [new CodeSample('rand();', 'random_int();')]); } /** * @return array> */ public function getNodeTypes() : array { return [FuncCall::class]; } /** * @param FuncCall $node */ public function refactor(Node $node) : ?\PhpParser\Node\Expr\FuncCall { 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' && $node->args === []) { $node->args[0] = new Arg(new LNumber(0)); $node->args[1] = new Arg($this->nodeFactory->createFuncCall('mt_getrandmax')); } return $node; } } return null; } public function provideMinPhpVersion() : int { return PhpVersionFeature::CSPRNG_FUNCTIONS; } }