| Conditions | 6 |
| Paths | 9 |
| Total Lines | 55 |
| Code Lines | 29 |
| 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 |
||
| 40 | public static function generateRandomNumbers($min, $max, $decimalPlaces, $maxFileSize) |
||
| 41 | { |
||
| 42 | $range = $max - $min; |
||
| 43 | $outputString = ''; |
||
| 44 | |||
| 45 | $minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
||
| 46 | |||
| 47 | if ($minMaxNumberSize['max'] > $maxFileSize) { |
||
| 48 | die('Error: Cannot generate any number due to too low file size.'); |
||
| 49 | } |
||
| 50 | |||
| 51 | // Maximum iteration without spaces |
||
| 52 | $maximumIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
||
| 53 | Text::debug('First maximum iteration: '.$maximumIteration); |
||
| 54 | |||
| 55 | $findingStart = microtime(true); |
||
| 56 | |||
| 57 | Text::message('Finding maximum iteration for loop...'); |
||
| 58 | |||
| 59 | for ($i = 0; ; $i++) { |
||
| 60 | $tempMaxIteration = $maximumIteration - $i; |
||
| 61 | $maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - 1; |
||
| 62 | |||
| 63 | if ($maxFileSize >= $maximumBytes) { |
||
| 64 | $maximumIteration = $tempMaxIteration; |
||
| 65 | Text::debug('Found right max iteration for loop'); |
||
| 66 | break; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | Text::printTimeDuration($findingStart); |
||
| 71 | |||
| 72 | Text::message('GENERATING...'); |
||
| 73 | Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maximumIteration."\n"); |
||
| 74 | |||
| 75 | $generateStart = microtime(true); |
||
| 76 | |||
| 77 | for ($i = 1; $i <= $maximumIteration; $i++) { |
||
| 78 | // Print progress and move cursor back to position 0 |
||
| 79 | if (!self::getTesting()) { |
||
| 80 | echo 'Progress: '.$i.'/'.$maximumIteration."\r"; |
||
| 81 | } |
||
| 82 | |||
| 83 | // Random number |
||
| 84 | $number = $min + $range * (mt_rand() / mt_getrandmax()); |
||
| 85 | // Format with trailing zeros ie. 8.00 |
||
| 86 | $number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
||
| 87 | $outputString .= $number.' '; |
||
| 88 | } |
||
| 89 | |||
| 90 | Text::printTimeDuration($generateStart); |
||
| 91 | Text::debug('Output string has '.strlen(trim($outputString)).' bytes.'); |
||
| 92 | |||
| 93 | // Remove last space |
||
| 94 | return trim($outputString); |
||
| 95 | } |
||
| 173 |