| Conditions | 9 |
| Paths | 16 |
| Total Lines | 77 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 40 | public static function generateAndSaveRandomNumbers($min, $max, $decimalPlaces, $maxFileSize, $filename, $fileExtension = '.dat', $delimiter = ' ') |
||
| 41 | { |
||
| 42 | $range = $max - $min; |
||
| 43 | |||
| 44 | $minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
||
| 45 | |||
| 46 | if ($minMaxNumberSize['max'] > $maxFileSize) { |
||
| 47 | die('Error: Cannot generate any number due to too low file size.'); |
||
|
|
|||
| 48 | } |
||
| 49 | |||
| 50 | File::createMissingDirectory($filename); |
||
| 51 | File::checkIfFileExistsAndDeleteContent($filename, $fileExtension); |
||
| 52 | |||
| 53 | // Maximum iteration without delimiter |
||
| 54 | $maxIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
||
| 55 | Text::debug('First maximum iteration: '.$maxIteration); |
||
| 56 | |||
| 57 | $findingStart = microtime(true); |
||
| 58 | Text::message('Finding maximum iteration for loop...'); |
||
| 59 | |||
| 60 | for ($i = 0; ; $i++) { |
||
| 61 | $tempMaxIteration = $maxIteration - $i; |
||
| 62 | $maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - strlen($delimiter); |
||
| 63 | |||
| 64 | if ($maxFileSize >= $maximumBytes) { |
||
| 65 | $maxIteration = $tempMaxIteration; |
||
| 66 | unset($tempMaxIteration); |
||
| 67 | break; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | Text::printTimeDuration($findingStart); |
||
| 72 | unset($findingStart); |
||
| 73 | |||
| 74 | Text::message('GENERATING...'); |
||
| 75 | Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maxIteration."\n"); |
||
| 76 | |||
| 77 | $generateStart = microtime(true); |
||
| 78 | |||
| 79 | $file = fopen($filename.$fileExtension, 'w'); |
||
| 80 | $chunkSize = 20; |
||
| 81 | $maxMainIteration = ceil((int) ($maxIteration / $chunkSize)); |
||
| 82 | $outputString = null; |
||
| 83 | $progress = 1; |
||
| 84 | |||
| 85 | for ($i = 0; $i <= $maxMainIteration; $i++) { |
||
| 86 | for ($j = 1; $j <= $chunkSize; $j++, $progress++) { |
||
| 87 | if ($progress > $maxIteration) { |
||
| 88 | break; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!self::getTesting()) { |
||
| 92 | // Print progress and move cursor back to position 0 |
||
| 93 | echo 'Progress: '.$progress.'/'.$maxIteration."\r"; |
||
| 94 | } |
||
| 95 | |||
| 96 | // Random number |
||
| 97 | $number = $min + $range * (mt_rand() / mt_getrandmax()); |
||
| 98 | // Format with trailing zeros ie. 8.00 |
||
| 99 | $number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
||
| 100 | |||
| 101 | $outputString .= $number.$delimiter; |
||
| 102 | } |
||
| 103 | |||
| 104 | // Remove last delimiter |
||
| 105 | if ($progress > $maxIteration) { |
||
| 106 | $outputString = rtrim($outputString, $delimiter); |
||
| 107 | } |
||
| 108 | |||
| 109 | fwrite($file, $outputString); |
||
| 110 | $outputString = null; |
||
| 111 | } |
||
| 112 | |||
| 113 | fclose($file); |
||
| 114 | |||
| 115 | Text::printTimeDuration($generateStart); |
||
| 116 | Text::message('Output file '.$filename.$fileExtension.' generated with '.filesize($filename.$fileExtension).' bytes.'); |
||
| 117 | } |
||
| 195 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.