Completed
Push — 4.9 ( f399a5...eb33e7 )
by Mikhail
01:38
created

Terminal::autocomplete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace terminal;
4
5
/**
6
 * class for terminal/cli/bash output
7
 */
8
final class Terminal {
9
    private $buffer = [];
10
    private $argv;
11
    private $argc;
12
    private $arguments = [];
13
14
    public function __construct()
15
    {
16
        global $argv;
17
        global $argc;
18
19
        $this->argv = $argv;
20
        $this->argc = $argc;
21
22
        $this->parseArguments();
23
    }
24
25
    public function addBuffer(string $buffer)
26
    {
27
        $this->buffer[] = $buffer;
28
    }
29
30
    public function getBuffer(): string
31
    {
32
        return implode(PHP_EOL, $this->buffer);
33
    }
34
35
    public function echo(string $string, bool $to_buffer = false, bool $return = false)
36
    {
37
        if ($return) {
38
            return $string;
39
        }
40
41
        if ($to_buffer) {
42
            $this->addBuffer($string);
43
        } else {
44
            echo $string . PHP_EOL;
45
        }
46
    }
47
48
    public function diff(string $string, bool $to_buffer = false, bool $return = false)
0 ignored issues
show
Unused Code introduced by
The parameter $return is not used and could be removed. ( Ignorable by Annotation )

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

48
    public function diff(string $string, bool $to_buffer = false, /** @scrutinizer ignore-unused */ bool $return = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $result_string = '';
51
        $separator = "\r\n";
52
        $line = strtok($string, $separator);
53
54
        while ($line !== false) {
55
            if (strpos($line, '-') === 0) {
56
                $result_string .= $this->error($line, false, true);
57
            } else if (strpos($line, '+') === 0) {
58
                $result_string .= $this->success($line, false, true);
59
            } else {
60
                $result_string .= $line;
61
            }
62
63
            $result_string .= PHP_EOL;
64
65
            $line = strtok($separator);
66
        }
67
68
        if ($to_buffer) {
69
            $this->setBuffer($result_string);
0 ignored issues
show
Bug introduced by
The method setBuffer() does not exist on terminal\Terminal. ( Ignorable by Annotation )

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

69
            $this->/** @scrutinizer ignore-call */ 
70
                   setBuffer($result_string);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        } else {
71
            echo $result_string;
72
        }
73
    }
74
75
    public function info(string $string, bool $to_buffer = false, bool $return = false)
76
    {
77
        $output = "\e[46m" . $string . "\e[0m";
78
79
        if ($return) {
80
            return $output;
81
        } else {
82
            $this->echo($output, $to_buffer);
83
        }
84
    }
85
86
    public function success(string $string, bool $to_buffer = false, bool $return = false)
87
    {
88
        $output = "\e[32m" . $string . "\e[0m";
89
90
        if ($return) {
91
            return $output;
92
        } else {
93
            $this->echo($output, $to_buffer);
94
        }
95
    }
96
97
    public function warning(string $string, bool $to_buffer = false, bool $return = false)
98
    {
99
        $output = "\e[43m" . $string . "\e[0m";
100
101
        if ($return) {
102
            return $output;
103
        } else {
104
            $this->echo($output, $to_buffer);
105
        }
106
    }
107
108
    public function error(string $string, bool $to_buffer = false, bool $return = false)
109
    {
110
        $output = "\e[1;31m" . $string . "\e[0m";
111
112
        if ($return) {
113
            return $output;
114
        } else {
115
            $this->echo($output, $to_buffer);
116
        }
117
    }
118
119
    public function forceOutput()
120
    {
121
        $this->echo(
122
            $this->getBuffer()
123
        );
124
    }
125
126
    /**
127
     * Requests user for confirm his action
128
     * 
129
     * @param callable $success_action
130
     * @param callable $cancel_action
131
     * @param string $question
132
     * @param string $confirmation_word
133
     */
134
    public function confirm(
135
        callable $success_action,
136
        callable $cancel_action = null,
137
        string $question = "Are you sure?\n Type 'Y' to continue: ",
138
        string $confirmation_word = 'Y'
139
    ): void
140
    {
141
        $this->warning($question);
142
        $handle = fopen('php://stdin', 'r');
143
        $line   = fgets($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, 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

143
        $line   = fgets(/** @scrutinizer ignore-type */ $handle);
Loading history...
144
145
        if(trim($line) != $confirmation_word) {
146
            $this->warning('ABORTING');
147
            call_user_func($cancel_action);
148
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
149
        }
150
151
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

151
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
152
        call_user_func($success_action);
153
    }
154
155
    /**
156
     * Requests config item from user 
157
     */
158
    public function requestSetting(
159
        string $name,
160
        callable $result_action,
161
        $default = ''
162
    ): void
163
    {
164
        $this->echo("Please, set up $name");
165
        $handle = fopen('php://stdin', 'r');
166
        $line   = fgets($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, 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

166
        $line   = fgets(/** @scrutinizer ignore-type */ $handle);
Loading history...
167
        $result = trim($line);
168
169
        call_user_func($result_action, $result ?: $default);
170
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

170
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
171
    }
172
173
    public function expectParams(
174
        array $opts,
0 ignored issues
show
Unused Code introduced by
The parameter $opts is not used and could be removed. ( Ignorable by Annotation )

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

174
        /** @scrutinizer ignore-unused */ array $opts,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
        callable $result_action
0 ignored issues
show
Unused Code introduced by
The parameter $result_action is not used and could be removed. ( Ignorable by Annotation )

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

175
        /** @scrutinizer ignore-unused */ callable $result_action

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    )
177
    {
178
//         $options = getopt('', ['addon.id:']);
179
//         print_r($options);
180
// die();
181
//         $options = getopt(implode('', array_keys($opts)), $opts);
182
// print_r($options);
183
//         array_walk($options, function($option) use ($result_action) {
184
//             echo 2;
185
//             call_user_func($result_action, $option);
186
//         });
187
    }
188
189
    /**
190
     * Set arguments
191
     * 
192
     * @param array  $arguments
193
     * 
194
     * @return void
195
     */
196
    public function setArguments(array $arguments): void
197
    {
198
        $this->arguments = $arguments;
199
    }
200
201
    /**
202
     * Get arguments
203
     * 
204
     * @return array
205
     */
206
    public function getArguments(): array
207
    {
208
        return $this->arguments;
209
    }
210
211
    /**
212
     * Parse arguments from terminal to array
213
     * 
214
     * @return void
215
     */
216
    private function parseArguments(): void
217
    {
218
        $arguments = arguments(implode(' ', $this->argv));
219
        $controller = @$this->argv[1] ?: '';
220
        list($generator, $command) = array_pad(explode('/', $controller), 2, '');
221
        $generator = (1 === preg_match('/^[\w]+/ui', $generator)) ? $generator : '';
222
223
        $arguments['generator'] = to_camel_case($generator);
224
        $arguments['command'] = $command;
225
        $this->setArguments($arguments);
226
    }
227
228
    /**
229
     * Cheks is terminal at autocomplete mode
230
     * 
231
     * @return bool
232
     */
233
    public function isAutocomplete()
234
    {
235
        return (
236
            isset($this->arguments['autocomplete'])
237
            && $this->arguments['autocomplete'] === 'y'
238
        );
239
    }
240
241
    /**
242
     * Echo autocompletes to terminal
243
     * 
244
     * @param array
245
     * 
246
     * @return Terminal
247
     */
248
    public function autocomplete(array $variants)
249
    {
250
        $this->echo(implode(' ', $variants)) . ' ';
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->echo(implode(' ', $variants)) targeting terminal\Terminal::echo() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
251
252
        return $this;
253
    }
254
255
    /**
256
     * Closes program execution
257
     */
258
    public function exit()
259
    {
260
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
261
    }
262
}
263