| Conditions | 7 |
| Paths | 15 |
| Total Lines | 66 |
| Code Lines | 34 |
| 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 |
||
| 44 | public static function generateAndSaveRandomNumbers($min, $max, $decimalPlaces, $maxFileSize, $filename, $fileExtension = '.dat') |
||
| 45 | { |
||
| 46 | $range = $max - $min; |
||
| 47 | |||
| 48 | $minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
||
| 49 | |||
| 50 | if ($minMaxNumberSize['max'] > $maxFileSize) { |
||
| 51 | die('Error: Cannot generate any number due to too low file size.'); |
||
| 52 | } |
||
| 53 | |||
| 54 | File::createMissingDirectory($filename); |
||
| 55 | File::checkIfFileExists($filename, $fileExtension); |
||
| 56 | |||
| 57 | // Maximum iteration without spaces |
||
| 58 | $maximumIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
||
| 59 | Text::debug('First maximum iteration: '.$maximumIteration); |
||
| 60 | |||
| 61 | $findingStart = microtime(true); |
||
| 62 | |||
| 63 | Text::message('Finding maximum iteration for loop...'); |
||
| 64 | |||
| 65 | for ($i = 0; ; $i++) { |
||
| 66 | $tempMaxIteration = $maximumIteration - $i; |
||
| 67 | $maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - 1; |
||
| 68 | |||
| 69 | if ($maxFileSize >= $maximumBytes) { |
||
| 70 | $maximumIteration = $tempMaxIteration; |
||
| 71 | Text::debug('Found right max iteration for loop'); |
||
| 72 | break; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | Text::printTimeDuration($findingStart); |
||
| 77 | |||
| 78 | Text::message('GENERATING...'); |
||
| 79 | Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maximumIteration."\n"); |
||
| 80 | |||
| 81 | $generateStart = microtime(true); |
||
| 82 | |||
| 83 | $file = fopen($filename.$fileExtension, 'w'); |
||
| 84 | |||
| 85 | for ($i = 1; $i <= $maximumIteration; $i++) { |
||
| 86 | // Print progress and move cursor back to position 0 |
||
| 87 | if (!self::getTesting()) { |
||
| 88 | echo 'Progress: '.$i.'/'.$maximumIteration."\r"; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Random number |
||
| 92 | $number = $min + $range * (mt_rand() / mt_getrandmax()); |
||
| 93 | // Format with trailing zeros ie. 8.00 |
||
| 94 | $number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
||
| 95 | |||
| 96 | $outputString = $number.' '; |
||
| 97 | |||
| 98 | // Remove last space |
||
| 99 | if ($i === $maximumIteration) { |
||
| 100 | $outputString = trim($outputString); |
||
| 101 | } |
||
| 102 | |||
| 103 | fwrite($file, $outputString); |
||
| 104 | } |
||
| 105 | |||
| 106 | fclose($file); |
||
| 107 | |||
| 108 | Text::printTimeDuration($generateStart); |
||
| 109 | Text::message('Output file '.$filename.$fileExtension.' generated with '.filesize($filename.$fileExtension).' bytes.'); |
||
| 110 | } |
||
| 188 |