Passed
Push — master ( 2c7129...9d7cc4 )
by Adam
01:43
created

Input::getFilenameInput()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 23
rs 8.5906
1
<?php
2
/**
3
 * Includes/Input.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
namespace Includes;
12
13
/**
14
 * Functions for receiving input from User.
15
 */
16
class Input
17
{
18
    /**
19
     * Get number from User.
20
     *
21
     * @param string    $message Message for User what he must type
22
     * @param int|float $default Default number for empty input. Default is 0
23
     *
24
     * @return float Inserted number
25
     */
26
    public static function getNumberInput($message, $default = 0)
27
    {
28
        echo $message.' [Default: '.$default.']: ';
29
30
        do {
31
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\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

31
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
32
33
            if (is_null($input) || empty($input)) {
34
                Text::debug('Using default input: '.$default);
35
                $input = $default;
36
            } elseif (!is_numeric($input)) {
37
                echo 'Please input number: ';
38
            }
39
        } while (!is_numeric($input));
40
41
        return (float) $input;
42
    }
43
44
    /**
45
     * Get filename from User.
46
     *
47
     * @param string $message Message for User what he must type
48
     * @param string $default Default filename for empty input. Default is 'output'
49
     *
50
     * @return string Inserted filename
51
     */
52
    public static function getFilenameInput($message, $default = 'output')
53
    {
54
        echo $message.' [Default: '.$default.']: ';
55
56
        do {
57
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\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

57
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
58
59
            /*
60
             * TODO: Invalid characters:
61
             * (Windows)    \/:*?"<>|   (check \ and / only at the end of string - need to test)
62
             * (Linux)      /
63
             */
64
            $isInputWrong = substr($input, -1) === '/';
65
66
            if (is_null($input) || empty($input)) {
67
                Text::debug('Using default input: '.$default);
68
                $input = $default;
69
            } elseif ($isInputWrong) {
70
                echo 'Please input filename: ';
71
            }
72
        } while ($isInputWrong);
73
74
        return $input;
75
    }
76
77
    /**
78
     * Terminate script if User denied on confirmation.
79
     *
80
     * @see Input::getUserConfirm()
81
     */
82
    public static function dieOnDenyUserConfirm()
83
    {
84
        if (!self::getUserConfirm()) {
85
            die('Script terminated by user.');
86
        }
87
    }
88
89
    /**
90
     * Get User confirmation. Default is YES.
91
     *
92
     * @return bool Confirmation result
93
     */
94
    public static function getUserConfirm()
95
    {
96
        while (1) {
97
            echo 'Do you really want to continue? [Y/n]: ';
98
99
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\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

99
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
100
101
            // Default is YES
102
            if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') {
103
                return true;
104
            } elseif (strtolower($input) == 'n' || strtolower($input) == 'no') {
105
                return false;
106
            }
107
        }
108
109
        // Fix missing return statement warning. Return true...
110
        return true;
111
    }
112
}
113