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