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 |
||
48 | 1 | private static function random($min, $max) |
|
49 | { |
||
50 | 1 | if (function_exists('random_int')) { |
|
51 | return random_int($min, $max); |
||
52 | } |
||
53 | |||
54 | // @codeCoverageIgnoreStart |
||
55 | if (!function_exists('mcrypt_create_iv')) { |
||
56 | throw new \Exception('mcrypt must be loaded for random_int to work'); |
||
57 | } |
||
58 | // @codeCoverageIgnoreEnd |
||
59 | |||
60 | 1 | $range = $counter = $max - $min; |
|
61 | 1 | $bits = 1; |
|
62 | |||
63 | 1 | while ($counter >>= 1) { |
|
64 | 1 | ++$bits; |
|
65 | 1 | } |
|
66 | |||
67 | 1 | $bytes = (int) max(ceil($bits / 8), 1); |
|
68 | 1 | $bitmask = pow(2, $bits) - 1; |
|
69 | |||
70 | 1 | if ($bitmask >= PHP_INT_MAX) { |
|
71 | $bitmask = PHP_INT_MAX; |
||
72 | } |
||
73 | |||
74 | do { |
||
75 | 1 | $result = hexdec(bin2hex(mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM))) & $bitmask; |
|
76 | 1 | } while ($result > $range); |
|
77 | |||
78 | 1 | return $result + $min; |
|
79 | } |
||
80 | } |
||
81 |