Conditions | 3 |
Paths | 2 |
Total Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
47 | private static function secureCrypt($min, $max) |
||
48 | { |
||
49 | $range = $max - $min; |
||
50 | |||
51 | if ($range < 0) { |
||
52 | return $min; // not so random... |
||
53 | } |
||
54 | |||
55 | $log = log($range, 2); |
||
56 | $bytes = (int) ($log / 8) + 1; // length in bytes |
||
57 | $bits = (int) $log + 1; // length in bits |
||
58 | $filter = (int) (1 << $bits) - 1; // set all lower bits to 1 |
||
59 | do { |
||
60 | $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
||
61 | $rnd = $rnd & $filter; // discard irrelevant bits |
||
62 | } while ($rnd >= $range); |
||
63 | |||
64 | return $min + $rnd; |
||
65 | } |
||
66 | |||
83 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: