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

getFilenameInput()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.3142
1
<?php
2
/**
3
 * Functions for console scripts.
4
 *
5
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
6
 */
7
8
/**
9
 * Saves string to file.
10
 *
11
 * @param string $string        String to save
12
 * @param string $filename      Output filename without extension
13
 * @param string $fileExtension File extension. Default is '.dat'
14
 *
15
 * @see generateRandomNumbers()
16
 */
17
function saveStringToFile($string, $filename, $fileExtension = '.dat')
18
{
19
    // Create dir if not exists
20
    if (!is_dir(dirname($filename))) {
21
        debug('Creating missing directory: '.dirname($filename));
22
        mkdir(dirname($filename));
23
    }
24
25
    // Warn about overwriting file
26
    if (file_exists($filename.$fileExtension)) {
27
        text('File '.$filename.$fileExtension.' exists and it will be overwritten!');
28
    }
29
30
    text('Saving to file...');
31
32
    $outputFileBytes = file_put_contents($filename.$fileExtension, $string, LOCK_EX);
33
34
    text('Output file '.$filename.$fileExtension.' generated with '.$outputFileBytes.' bytes.');
35
}
36
37
/**
38
 * Get filename from User.
39
 *
40
 * @param string $message Message for User what he must type
41
 *
42
 * @return string Inserted filename
43
 */
44
function getFilenameInput($message)
45
{
46
    echo $message.': ';
47
48
    do {
49
        $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

49
        $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
50
        debug('User input: '.$input);
51
52
        /*
53
         * TODO: Invalid characters:
54
         * (Windows)    \/:*?"<>|   (check \ and / only at the end of string - need to test)
55
         * (Linux)      /
56
         */
57
        $isInputWrong = is_null($input);
58
59
        if ($isInputWrong) {
60
            echo 'Please input filename: ';
61
        }
62
    } while ($isInputWrong);
63
64
    return $input;
65
}
66
67
/**
68
 * Prints how much time took some action in milliseconds
69
 *
70
 * @param float $startTime Time when action started
71
 */
72
function printEndTime($startTime)
73
{
74
    $endTime = microtime(true) - (float) $startTime;
75
    $endTime = number_format((float) $endTime, 4, '.', '');
76
77
    text('It was done in '.$endTime.' ms.');
78
}
79
80
/**
81
 * Execute text() function if DEBUG is set to true.
82
 *
83
 * @global bool $DEBUG
84
 *
85
 * @param string $message Message to print if in DEBUG mode
86
 *
87
 * @see text()
88
 */
89
function debug($message)
90
{
91
    if (DEBUG) {
0 ignored issues
show
Bug introduced by
The constant DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
        text($message);
93
    }
94
}
95
96
/**
97
 * Prints message with new lines.
98
 *
99
 * @param string $message Message to print
100
 */
101
function text($message)
102
{
103
    echo "\n".$message."\n";
104
}
105