Conditions | 3 |
Paths | 2 |
Total Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
53 | private static function secureCrypt($min, $max) |
||
54 | { |
||
55 | $range = $max - $min; |
||
56 | |||
57 | if ($range < 0) { |
||
58 | return $min; // not so random... |
||
59 | } |
||
60 | |||
61 | $log = log($range, 2); |
||
62 | $bytes = (int) ($log / 8) + 1; // length in bytes |
||
63 | $bits = (int) $log + 1; // length in bits |
||
64 | $filter = (int) (1 << $bits) - 1; // set all lower bits to 1 |
||
65 | do { |
||
66 | $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
||
67 | $rnd = $rnd & $filter; // discard irrelevant bits |
||
68 | } while ($rnd >= $range); |
||
69 | |||
70 | return $min + $rnd; |
||
71 | } |
||
72 | |||
89 |
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: