Conditions | 12 |
Paths | 5 |
Total Lines | 29 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
52 | public function extractEntropy(int $max, int $min = 0): int |
||
53 | { |
||
54 | Internal::assert($max <= PHP_INT_MAX && $min >= PHP_INT_MIN && $min <= $max); |
||
55 | $maxInt = (PHP_INT_MAX >> 1); |
||
56 | if (($max > $maxInt && ($min <= 0 || $max - $min > $maxInt)) || |
||
57 | ($min < -$maxInt && ($max >= 0 || $max - $min > $maxInt)) || ($max - $min) > $maxInt) { |
||
58 | //calculate floor(($max - $min) / 16) considering overflows |
||
59 | $max16 = $max % 16; |
||
60 | $min16 = $min % 16; |
||
61 | $max1 = (($max - $max16) >> 4) - (($min - $min16) >> 4); |
||
62 | $rest = $max16 - $min16; |
||
63 | if ($rest >= 16) { |
||
64 | $max1++; |
||
65 | $rest -= 16; |
||
66 | } |
||
67 | |||
68 | $z1 = $this->extractEntropy($max1); |
||
69 | $max2 = 15; |
||
70 | if ($z1 == $max1) { |
||
71 | $max2 = $rest; |
||
72 | } |
||
73 | $z2 = $this->extractEntropy($max2); |
||
74 | return (($min + 8 * $z1) + 8 * $z1) + $z2; |
||
75 | } |
||
76 | $range = $max - $min; |
||
77 | Internal::assert($range <= PHP_INT_MAX); |
||
78 | $bits = static::countBits($range); |
||
79 | |||
80 | return $min + ($this->extractEntropyByBits($bits) % ($range + 1)); |
||
81 | } |
||
142 | } |