Completed
Push — master ( 092c93...6df846 )
by Adam
04:10
created

Generate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generateRandomNumbers() 0 28 2
A checkSwitchMinMax() 0 15 2
1
<?php
2
/**
3
 * Includes/Generate.php.
4
 *
5
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
6
 * @license MIT
7
 *
8
 * @link    https://github.com/Saibamen/Generate-Sort-Numbers
9
 */
10
/**
11
 * Functions for 'Generate' script.
12
 *
13
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
14
 */
15
16
namespace Includes;
17
18
require_once 'Text.php';
19
20
/**
21
 * Functions for 'Generate' script.
22
 */
23
class Generate
24
{
25
    /**
26
     * Generate string with randomized numbers.
27
     *
28
     * @param int|float $min           Minimum allowed number to generate
29
     * @param int|float $max           Maximum allowed number to generate
30
     * @param int|float $decimalPlaces Number of decimal places
31
     *
32
     * @return string Generated string without trailing spaces
33
     */
34
    public static function generateRandomNumbers($min, $max, $decimalPlaces)
35
    {
36
        Text::message('GENERATING...');
37
        Text::debug("Generating string.\nMin: ".$min.' Max: '.$max.' Decimal places: '.$decimalPlaces);
38
39
        $range = $max - $min;
40
        $outputString = '';
41
        $howManyIterations = 100;
42
43
        $generateStart = microtime(true);
44
45
        // TODO: Show percent progress based on iteration
46
        // TODO: Calculate output size for generate
47
        for ($i = 0; $i <= $howManyIterations; $i++) {
48
            // Print progress and move cursor back to position 0
49
            echo 'Progress: '.$i.'/'.$howManyIterations."\r";
50
51
            $number = $min + $range * (mt_rand() / mt_getrandmax());
52
            // Format with trailing zeros ie. 8.00
53
            $number = number_format((float) $number, (int) $decimalPlaces, '.', '');
54
            $outputString .= $number.' ';
55
        }
56
57
        Text::printTimeDuration($generateStart);
58
        Text::debug($outputString);
59
60
        // Remove last space
61
        return trim($outputString);
62
    }
63
64
    /**
65
     * Switch min and max if min > max.
66
     *
67
     * @param float $min Minimum
68
     * @param float $max Maximum
69
     *
70
     * @return array
71
     */
72
    public static function checkSwitchMinMax($min, $max)
73
    {
74
        if ($min > $max) {
75
            Text::debug('!! Switching min and max !!');
76
77
            $tempMin = $min;
78
79
            $min = $max;
80
            $max = $tempMin;
81
82
            Text::debug('Min: '.$min);
83
            Text::debug('Max: '.$max);
84
        }
85
86
        return array('min' => $min, 'max' => $max);
87
    }
88
}
89