Passed
Push — master ( 150fe8...37abc9 )
by Adam
01:53
created

checkSwitchMinMax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Functions for 'Generate' script.
4
 *
5
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
6
 */
7
8
/** Functions for inputs, saving file and printing text */
9
require_once 'console.inc.php';
10
11
/**
12
 * Generate string with randomized numbers.
13
 *
14
 * @param int|float $min           Minimum allowed number to generate
15
 * @param int|float $max           Maximum allowed number to generate
16
 * @param int|float $decimalPlaces Number of decimal places
17
 *
18
 * @return string Generated string without trailing spaces
19
 */
20
function generateRandomNumbers($min, $max, $decimalPlaces)
21
{
22
    text('GENERATING...');
23
    debug("Generating string.\nMin: ".$min.' Max: '.$max.' Decimal places: '.$decimalPlaces);
24
25
    $range = $max - $min;
26
    $outputString = '';
27
    $howManyIterations = 100;
28
29
    $GENERATE_START = microtime(true);
30
31
    // TODO: Show percent progress based on iteration
32
    // TODO: Calculate output size for generate
33
    for ($i = 0; $i <= $howManyIterations; $i++) {
34
        // Print progress and move cursor back to position 0
35
        echo 'Progress: '.$i.'/'.$howManyIterations."\r";
36
37
        $number = $min + $range * (mt_rand() / mt_getrandmax());
38
        // Format with trailing zeros ie. 8.00
39
        $number = number_format((float) $number, (int) $decimalPlaces, '.', '');
40
        $outputString .= $number.' ';
41
    }
42
43
    printEndTime($GENERATE_START);
0 ignored issues
show
Bug introduced by
It seems like $GENERATE_START can also be of type string; however, parameter $startTime of printEndTime() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
    printEndTime(/** @scrutinizer ignore-type */ $GENERATE_START);
Loading history...
44
    debug($outputString);
45
46
    // Remove last space
47
    return trim($outputString);
48
}
49
50
/**
51
 * Get number from User.
52
 *
53
 * @param string       $message Message for User what he must type
54
 * @param int|float    $default Default number for empty input. Default is 0
55
 *
56
 * @return float Inserted number
57
 */
58
function getNumberInput($message, $default = 0)
59
{
60
    echo $message.' [Default: '.$default.']: ';
61
62
    do {
63
        $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
STDIN of type string is incompatible with the type resource expected by parameter $handle of fgets(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
64
        debug('User input: '.$input);
65
66
        if (is_null($input) || empty($input)) {
67
            debug('Using default input: '.$default);
68
            $input = $default;
69
        } elseif (!is_numeric($input)) {
70
            echo 'Please input number: ';
71
        }
72
    } while (!is_numeric($input));
73
74
    return (float) $input;
75
}
76
77
/**
78
 * Switch min and max if min > max.
79
 *
80
 * @param float $min Minimum
81
 * @param float $max Maximum
82
 *
83
 * @return array
84
 */
85
function checkSwitchMinMax($min, $max)
86
{
87
    if ($min > $max) {
88
        debug('!! Switching min and max !!');
89
90
        $tempMin = $min;
91
92
        $min = $max;
93
        $max = $tempMin;
94
95
        debug('Min: '.$min);
96
        debug('Max: '.$max);
97
    }
98
99
    return array('min' => $min, 'max' => $max);
100
}
101