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