Conditions | 6 |
Paths | 6 |
Total Lines | 30 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Tests | 3 |
CRAP Score | 25.3095 |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
48 | 1 | private static function random($min, $max) |
|
49 | { |
||
50 | 1 | if (function_exists('random_int')) { |
|
51 | 1 | return random_int($min, $max); |
|
52 | } |
||
53 | |||
54 | if (!function_exists('mcrypt_create_iv')) { |
||
55 | throw new \Exception('mcrypt must be loaded for random_int to work'); |
||
56 | } |
||
57 | |||
58 | $range = $counter = $max - $min; |
||
59 | $bits = 1; |
||
60 | |||
61 | while ($counter >>= 1) { |
||
62 | ++$bits; |
||
63 | } |
||
64 | |||
65 | $bytes = (int) max(ceil($bits / 8), 1); |
||
66 | $bitmask = pow(2, $bits) - 1; |
||
67 | |||
68 | if ($bitmask >= PHP_INT_MAX) { |
||
69 | $bitmask = PHP_INT_MAX; |
||
70 | } |
||
71 | |||
72 | do { |
||
73 | $result = hexdec(bin2hex(mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM))) & $bitmask; |
||
74 | } while ($result > $range); |
||
75 | |||
76 | return $result + $min; |
||
77 | } |
||
78 | } |
||
79 |