Passed
Push — master ( d8f613...6e2a7a )
by Adam
01:58
created

checkSwitchGlobalMinMax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 12
rs 9.4285
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 string $min           Minimum allowed number to generate
15
 * @param string $max           Maximum allowed number to generate
16
 * @param string $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.' DecimalPlaces: '.$decimalPlaces);
24
25
    $range = $max - $min;
26
    $outputString = '';
27
    $howManyIterations = 10;
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
        debug($number);
41
        $outputString .= $number.' ';
42
    }
43
44
    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

44
    printEndTime(/** @scrutinizer ignore-type */ $GENERATE_START);
Loading history...
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
 *
55
 * @return string Inserted number
56
 */
57
function getNumberInput($message)
58
{
59
    echo $message.': ';
60
61
    do {
62
        $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

62
        $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
63
        debug('User input: '.$input);
64
65
        $isInputWrong = is_null($input) || !is_numeric($input);
66
67
        // TODO: Use default numbers?
68
        if ($isInputWrong) {
69
            echo 'Please input number: ';
70
        }
71
    } while ($isInputWrong);
72
73
    return $input;
74
}
75
76
/**
77
 * Switch Globals min and max if min > max.
78
 */
79
function checkSwitchGlobalMinMax()
80
{
81
    if ($GLOBALS['min'] > $GLOBALS['max']) {
82
        debug('!! Switching min and max !!');
83
84
        $tempMin = $GLOBALS['min'];
85
86
        $GLOBALS['min'] = $GLOBALS['max'];
87
        $GLOBALS['max'] = $tempMin;
88
89
        debug('Min: '.$GLOBALS['min']);
90
        debug('Max: '.$GLOBALS['max']);
91
    }
92
}
93