Passed
Push — master ( 470cec...646bff )
by Adam
01:33
created

dieOnDenyUserConfirm()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Functions for receiving input from User.
4
 *
5
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
6
 */
7
8
namespace Includes\Input;
9
10
use Includes\Text;
1 ignored issue
show
Bug introduced by
The type Includes\Text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Get number from User.
14
 *
15
 * @param string    $message Message for User what he must type
16
 * @param int|float $default Default number for empty input. Default is 0
17
 *
18
 * @return float Inserted number
19
 */
20
function getNumberInput($message, $default = 0)
21
{
22
    echo $message.' [Default: '.$default.']: ';
23
24
    do {
25
        $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\Input\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

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

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

89
        $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
90
91
        // Default is YES
92
        if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') {
93
            return true;
94
        } elseif (strtolower($input) == 'n' || strtolower($input) == 'no') {
95
            return false;
96
        }
97
    }
98
99
    // Fix missing return statement warning. Return true...
100
    return true;
101
}
102