99use PhpParser \Node \Expr \FuncCall ;
1010use PhpParser \Node \Name ;
1111use PhpParser \Node \Scalar \Int_ ;
12- use Rector \PhpParser \Node \Value \ValueResolver ;
12+ use PHPStan \Type \Constant \ConstantIntegerType ;
13+ use Rector \PhpParser \Node \BetterNodeFinder ;
1314use Rector \Rector \AbstractRector ;
1415use Rector \RuleDoc \CodeSample \CodeSample ;
1516use 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