Conditions | 6 |
Paths | 6 |
Total Lines | 32 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 6.2373 |
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 | 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 | 1 | $range = $counter = $max - $min; |
|
62 | 1 | $bits = 1; |
|
63 | |||
64 | 1 | while ($counter >>= 1) { |
|
65 | 1 | ++$bits; |
|
66 | 1 | } |
|
67 | |||
68 | 1 | $bytes = (int) max(ceil($bits / 8), 1); |
|
69 | 1 | $bitmask = pow(2, $bits) - 1; |
|
70 | |||
71 | 1 | if ($bitmask >= PHP_INT_MAX) { |
|
72 | $bitmask = PHP_INT_MAX; |
||
73 | } |
||
74 | |||
75 | do { |
||
76 | 1 | $result = hexdec(bin2hex(mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM))) & $bitmask; |
|
77 | 1 | } while ($result > $range); |
|
78 | |||
79 | 1 | return $result + $min; |
|
80 | } |
||
81 | } |
||
82 |