| Conditions | 13 |
| Paths | 32 |
| Total Lines | 130 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 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 |
||
| 41 | public static function random_int($min, $max) |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * Type and input logic checks |
||
| 45 | * |
||
| 46 | * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
||
| 47 | * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
||
| 48 | * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
||
| 49 | * lose precision, so the <= and => operators might accidentally let a float |
||
| 50 | * through. |
||
| 51 | */ |
||
| 52 | |||
| 53 | is_int($min) or $min = Paragonie_Util_Intval::intval($min, __FUNCTION__, 1); |
||
| 54 | is_int($max) or $max = Paragonie_Util_Intval::intval($max, __FUNCTION__, 2); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Now that we've verified our weak typing system has given us an integer, |
||
| 58 | * let's validate the logic then we can move forward with generating random |
||
| 59 | * integers along a given range. |
||
| 60 | */ |
||
| 61 | if ($min > $max) { |
||
| 62 | throw new Error( |
||
| 63 | 'Minimum value must be less than or equal to the maximum value' |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | if ($max === $min) { |
||
| 67 | return $min; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Initialize variables to 0 |
||
| 72 | * |
||
| 73 | * We want to store: |
||
| 74 | * $bytes => the number of random bytes we need |
||
| 75 | * $mask => an integer bitmask (for use with the &) operator |
||
| 76 | * so we can minimize the number of discards |
||
| 77 | */ |
||
| 78 | $attempts = $bits = $bytes = $mask = $valueShift = 0; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * At this point, $range is a positive number greater than 0. It might |
||
| 82 | * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to |
||
| 83 | * a float and we will lose some precision. |
||
| 84 | */ |
||
| 85 | $range = $max - $min; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Test for integer overflow: |
||
| 89 | */ |
||
| 90 | if (!is_int($range)) { |
||
| 91 | /** |
||
| 92 | * Still safely calculate wider ranges. |
||
| 93 | * Provided by @CodesInChaos, @oittaa |
||
| 94 | * |
||
| 95 | * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435 |
||
| 96 | * |
||
| 97 | * We use ~0 as a mask in this case because it generates all 1s |
||
| 98 | * |
||
| 99 | * @ref https://eval.in/400356 (32-bit) |
||
| 100 | * @ref http://3v4l.org/XX9r5 (64-bit) |
||
| 101 | */ |
||
| 102 | $bytes = PHP_INT_SIZE; |
||
| 103 | $mask = ~0; |
||
| 104 | } else { |
||
| 105 | /** |
||
| 106 | * $bits is effectively ceil(log($range, 2)) without dealing with |
||
| 107 | * type juggling |
||
| 108 | */ |
||
| 109 | while ($range > 0) { |
||
| 110 | if ($bits % 8 === 0) { |
||
| 111 | ++$bytes; |
||
| 112 | } |
||
| 113 | ++$bits; |
||
| 114 | $range >>= 1; |
||
| 115 | $mask = $mask << 1 | 1; |
||
| 116 | } |
||
| 117 | $valueShift = $min; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Now that we have our parameters set up, let's begin generating |
||
| 122 | * random integers until one falls between $min and $max |
||
| 123 | */ |
||
| 124 | do { |
||
| 125 | /** |
||
| 126 | * The rejection probability is at most 0.5, so this corresponds |
||
| 127 | * to a failure probability of 2^-128 for a working RNG |
||
| 128 | */ |
||
| 129 | if ($attempts > 128) { |
||
| 130 | throw new Exception( |
||
| 131 | 'Could not gather sufficient random data' |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Let's grab the necessary number of random bytes |
||
| 137 | */ |
||
| 138 | $randomByteString = random_bytes($bytes); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Let's turn $randomByteString into an integer |
||
| 142 | * |
||
| 143 | * This uses bitwise operators (<< and |) to build an integer |
||
| 144 | * out of the values extracted from ord() |
||
| 145 | * |
||
| 146 | * Example: [9F] | [6D] | [32] | [0C] => |
||
| 147 | * 159 + 27904 + 3276800 + 201326592 => |
||
| 148 | * 204631455 |
||
| 149 | */ |
||
| 150 | $val = 0; |
||
| 151 | for ($i = 0; $i < $bytes; ++$i) { |
||
| 152 | $val |= ord($randomByteString[$i]) << ($i * 8); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Apply mask |
||
| 157 | */ |
||
| 158 | $val &= $mask; |
||
| 159 | $val += $valueShift; |
||
| 160 | |||
| 161 | ++$attempts; |
||
| 162 | /** |
||
| 163 | * If $val overflows to a floating point number, |
||
| 164 | * ... or is larger than $max, |
||
| 165 | * ... or smaller than $min, |
||
| 166 | * then try again. |
||
| 167 | */ |
||
| 168 | } while (!is_int($val) || $val > $max || $val < $min); |
||
| 169 | return (int) $val; |
||
| 170 | } |
||
| 171 | } |
||
| 172 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.