| Conditions | 14 |
| Paths | 51 |
| Total Lines | 135 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 36 | public static function randomInt( |
||
| 37 | int|string|DecimalInterface $min, |
||
| 38 | int|string|DecimalInterface $max, |
||
| 39 | RandomMode $mode = RandomMode::Entropy, |
||
| 40 | ?int $seed = null |
||
| 41 | ): ImmutableDecimal |
||
| 42 | { |
||
| 43 | $minDecimal = Numbers::makeOrDont(Numbers::IMMUTABLE, $min); |
||
| 44 | $maxDecimal = Numbers::makeOrDont(Numbers::IMMUTABLE, $max); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * We want to prevent providing non-integer values for min and max, even in cases where |
||
| 48 | * the supplied value is a string or a DecimalInterface. |
||
| 49 | */ |
||
| 50 | if ($minDecimal->isFloat() || $maxDecimal->isFloat()) { |
||
|
|
|||
| 51 | throw new IntegrityConstraint( |
||
| 52 | 'Random integers cannot be generated with boundaries which are floats', |
||
| 53 | 'Provide only whole number, integer values for min and max.', |
||
| 54 | 'An attempt was made to generate a random integer with boundaries which are non-integers. Min Provided: '.$min->getValue(NumberBase::Ten).' -- Max Provided: '.$max->getValue(NumberBase::Ten) |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Because of optimistic optimizing with the rand() and random_int() functions, we do |
||
| 60 | * need the arguments to be provided in the correct order. |
||
| 61 | */ |
||
| 62 | if ($minDecimal->isGreaterThan($maxDecimal)) { |
||
| 63 | $tempDecimal = $minDecimal; |
||
| 64 | $minDecimal = $maxDecimal; |
||
| 65 | $maxDecimal = $tempDecimal; |
||
| 66 | unset($tempDecimal); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * For some applications it might be better to throw an exception here, however it |
||
| 71 | * would probably be hard to recover in most applications from a situation which |
||
| 72 | * resulted in this situation. |
||
| 73 | * |
||
| 74 | * So instead we will trigger a language level warning and return the only valid |
||
| 75 | * value for the parameters given. |
||
| 76 | */ |
||
| 77 | if ($minDecimal->isEqual($maxDecimal)) { |
||
| 78 | trigger_error( |
||
| 79 | 'Attempted to get a random value for a range of no size, with minimum of '.$minDecimal->getValue(NumberBase::Ten).' and maximum of '.$maxDecimal->getValue(NumberBase::Ten), |
||
| 80 | E_USER_WARNING |
||
| 81 | ); |
||
| 82 | |||
| 83 | return $minDecimal; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($minDecimal->isLessThanOrEqualTo(PHP_INT_MAX) && $minDecimal->isGreaterThanOrEqualTo(PHP_INT_MIN)) { |
||
| 87 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 88 | $min = $minDecimal->asInt(); |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($maxDecimal->isLessThanOrEqualTo(PHP_INT_MAX) && $maxDecimal->isGreaterThanOrEqualTo(PHP_INT_MIN)) { |
||
| 92 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 93 | $max = $maxDecimal->asInt(); |
||
| 94 | } |
||
| 95 | |||
| 96 | $randomizer = self::getRandomizer($mode, $seed); |
||
| 97 | |||
| 98 | if (is_int($min) && is_int($max)) { |
||
| 99 | $num = $randomizer->getInt($min, $max); |
||
| 100 | return new ImmutableDecimal($num); |
||
| 101 | } else { |
||
| 102 | /** |
||
| 103 | * We only need to request enough bytes to find a number within the range, since we |
||
| 104 | * will be adding the minimum value to it at the end. |
||
| 105 | */ |
||
| 106 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 107 | $range = $maxDecimal->subtract($minDecimal); |
||
| 108 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 109 | $bitsNeeded = $range->ln(2)->divide(Numbers::makeNaturalLog2(2), 2)->floor()->add(1); |
||
| 110 | $bytesNeeded = $bitsNeeded->divide(8, 2)->ceil(); |
||
| 111 | |||
| 112 | do { |
||
| 113 | try { |
||
| 114 | /** |
||
| 115 | * Returns random bytes based on sources of entropy within the system. |
||
| 116 | * |
||
| 117 | * For documentation on these sources please see: |
||
| 118 | * |
||
| 119 | * https://www.php.net/manual/en/function.random-bytes.php |
||
| 120 | */ |
||
| 121 | $entropyBytes = $randomizer->getBytes($bytesNeeded->asInt()); |
||
| 122 | $baseTwoBytes = ''; |
||
| 123 | for($i = 0; $i < strlen($entropyBytes); $i++){ |
||
| 124 | $baseTwoBytes .= decbin( ord( $entropyBytes[$i] ) ); |
||
| 125 | } |
||
| 126 | } catch (Exception $e) { |
||
| 127 | throw new OptionalExit( |
||
| 128 | 'System error from random_bytes().', |
||
| 129 | 'Ensure your system is configured correctly.', |
||
| 130 | 'A call to random_bytes() threw a system level exception. Most often this is due to a problem with entropy sources in your configuration. Original exception message: ' . $e->getMessage() |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Since the number of digits is equal to the bits needed, but random_bytes() only |
||
| 136 | * returns in chunks of 8 bits (duh, bytes), we can substr() from the right to |
||
| 137 | * select only the correct number of digits by multiplying the number of bits |
||
| 138 | * needed by -1 and using that as the starting point. |
||
| 139 | */ |
||
| 140 | $randomValue = BaseConversionProvider::convertStringToBaseTen( |
||
| 141 | substr($baseTwoBytes, $bitsNeeded->multiply(-1)->asInt()), |
||
| 142 | NumberBase::Two |
||
| 143 | ); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var ImmutableDecimal $num |
||
| 147 | */ |
||
| 148 | $num = Numbers::make( |
||
| 149 | type: Numbers::IMMUTABLE, |
||
| 150 | value: $randomValue |
||
| 151 | ); |
||
| 152 | } while ($num->isGreaterThan($range)); |
||
| 153 | /** |
||
| 154 | * It is strictly speaking possible for this to loop infinitely. In the worst case |
||
| 155 | * scenario where 50% of possible values are invalid, it takes 7 loops for there to |
||
| 156 | * be a less than a 1% chance of still not having an answer. |
||
| 157 | * |
||
| 158 | * After only 10 loops the chance is less than 1/1000. |
||
| 159 | * |
||
| 160 | * NOTE: Worst case scenario is a range of size 2^n + 1. |
||
| 161 | */ |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Add the minimum since we effectively subtracted it by finding a random number |
||
| 165 | * bounded between 0 and range. If our requested range included negative numbers, |
||
| 166 | * this operation will also return those values into our data by effectively |
||
| 167 | * shifting the result window. |
||
| 168 | */ |
||
| 169 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 170 | return $num->add($minDecimal); |
||
| 171 | } |
||
| 250 | } |