Passed
Push — master ( a6da77...d17d59 )
by William
04:24 queued 11s
created

CLI   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 290
Duplicated Lines 0 %

Test Coverage

Coverage 97.9%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 127
dl 0
loc 290
ccs 140
cts 143
cp 0.979
rs 9.28
c 2
b 0
f 0
wmc 39

12 Methods

Rating   Name   Duplication   Size   Complexity  
A usageTokenize() 0 4 1
A getopt() 0 3 1
A mergeLongOpts() 0 9 3
A usageLint() 0 4 1
A usageHighlight() 0 4 1
B runTokenize() 0 47 8
B runLint() 0 48 9
A readStdin() 0 17 2
B runHighlight() 0 39 7
A parseLint() 0 12 1
A parseHighlight() 0 25 4
A parseTokenize() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Utils;
6
7
use PhpMyAdmin\SqlParser\Context;
8
use PhpMyAdmin\SqlParser\Lexer;
9
use PhpMyAdmin\SqlParser\Parser;
10
11
use function count;
12
use function getopt;
13
use function implode;
14
use function in_array;
15
use function rtrim;
16
use function stream_get_contents;
17
use function stream_select;
18
use function var_export;
19
20
use const STDIN;
21
22
/**
23
 * CLI interface.
24
 */
25
class CLI
26
{
27
    /**
28
     * @param string[]|false[] $params
29
     * @param string[]         $longopts
30
     *
31
     * @return void
32
     */
33 140
    public function mergeLongOpts(&$params, &$longopts)
34
    {
35 140
        foreach ($longopts as $value) {
36 140
            $value = rtrim($value, ':');
37 140
            if (! isset($params[$value])) {
38 140
                continue;
39
            }
40
41 12
            $params[$value[0]] = $params[$value];
42
        }
43
    }
44
45
    /**
46
     * @return void
47
     */
48 16
    public function usageHighlight()
49
    {
50 16
        echo "Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]\n";
51 16
        echo "       cat file.sql | highlight-query\n";
52
    }
53
54
    /**
55
     * @param string   $opt
56
     * @param string[] $long
57
     *
58
     * @return string[]|false[]|false
59
     */
60 4
    public function getopt($opt, $long)
61
    {
62 4
        return getopt($opt, $long);
63
    }
64
65
    /**
66
     * @return mixed|false
67
     */
68 60
    public function parseHighlight()
69
    {
70 60
        $longopts = [
71 60
            'help',
72 60
            'query:',
73 60
            'format:',
74 60
            'ansi',
75 60
        ];
76 60
        $params = $this->getopt('hq:f:a', $longopts);
77 60
        if ($params === false) {
78 8
            return false;
79
        }
80
81 52
        $this->mergeLongOpts($params, $longopts);
82 52
        if (! isset($params['f'])) {
83 28
            $params['f'] = 'cli';
84
        }
85
86 52
        if (! in_array($params['f'], ['html', 'cli', 'text'])) {
87 8
            echo "ERROR: Invalid value for format!\n";
88
89 8
            return false;
90
        }
91
92 44
        return $params;
93
    }
94
95
    /**
96
     * @return int
97
     */
98 60
    public function runHighlight()
99
    {
100 60
        $params = $this->parseHighlight();
101 60
        if ($params === false) {
102 16
            return 1;
103
        }
104
105 44
        if (isset($params['h'])) {
106 8
            $this->usageHighlight();
107
108 8
            return 0;
109
        }
110
111 36
        if (! isset($params['q'])) {
112 20
            $stdIn = $this->readStdin();
113
114 20
            if ($stdIn) {
115 12
                $params['q'] = $stdIn;
116
            }
117
        }
118
119 36
        if (isset($params['a'])) {
120
            Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
121
        }
122
123 36
        if (isset($params['q'])) {
124 28
            echo Formatter::format(
125 28
                $params['q'],
126 28
                ['type' => $params['f']]
127 28
            );
128 28
            echo "\n";
129
130 28
            return 0;
131
        }
132
133 8
        echo "ERROR: Missing parameters!\n";
134 8
        $this->usageHighlight();
135
136 8
        return 1;
137
    }
138
139
    /**
140
     * @return void
141
     */
142 16
    public function usageLint()
143
    {
144 16
        echo "Usage: lint-query --query SQL [--ansi]\n";
145 16
        echo "       cat file.sql | lint-query\n";
146
    }
147
148
    /**
149
     * @return mixed
150
     */
151 52
    public function parseLint()
152
    {
153 52
        $longopts = [
154 52
            'help',
155 52
            'query:',
156 52
            'context:',
157 52
            'ansi',
158 52
        ];
159 52
        $params = $this->getopt('hq:c:a', $longopts);
160 52
        $this->mergeLongOpts($params, $longopts);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type false; however, parameter $params of PhpMyAdmin\SqlParser\Utils\CLI::mergeLongOpts() does only seem to accept array<mixed,false>|string[], 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

160
        $this->mergeLongOpts(/** @scrutinizer ignore-type */ $params, $longopts);
Loading history...
161
162 52
        return $params;
163
    }
164
165
    /**
166
     * @return int
167
     */
168 52
    public function runLint()
169
    {
170 52
        $params = $this->parseLint();
171 52
        if ($params === false) {
172 8
            return 1;
173
        }
174
175 44
        if (isset($params['h'])) {
176 8
            $this->usageLint();
177
178 8
            return 0;
179
        }
180
181 36
        if (isset($params['c'])) {
182 8
            Context::load($params['c']);
183
        }
184
185 36
        if (! isset($params['q'])) {
186 20
            $stdIn = $this->readStdin();
187
188 20
            if ($stdIn) {
189 12
                $params['q'] = $stdIn;
190
            }
191
        }
192
193 36
        if (isset($params['a'])) {
194
            Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
195
        }
196
197 36
        if (isset($params['q'])) {
198 28
            $lexer = new Lexer($params['q'], false);
199 28
            $parser = new Parser($lexer->list);
200 28
            $errors = Error::get([$lexer, $parser]);
0 ignored issues
show
Bug introduced by
array($lexer, $parser) of type array<integer,PhpMyAdmin...Admin\SqlParser\Parser> is incompatible with the type array<integer|string,Php...Admin\SqlParser\Parser> expected by parameter $objs of PhpMyAdmin\SqlParser\Utils\Error::get(). ( Ignorable by Annotation )

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

200
            $errors = Error::get(/** @scrutinizer ignore-type */ [$lexer, $parser]);
Loading history...
201 28
            if (count($errors) === 0) {
202 12
                return 0;
203
            }
204
205 16
            $output = Error::format($errors);
206 16
            echo implode("\n", $output);
207 16
            echo "\n";
208
209 16
            return 10;
210
        }
211
212 8
        echo "ERROR: Missing parameters!\n";
213 8
        $this->usageLint();
214
215 8
        return 1;
216
    }
217
218
    /**
219
     * @return void
220
     */
221 16
    public function usageTokenize()
222
    {
223 16
        echo "Usage: tokenize-query --query SQL [--ansi]\n";
224 16
        echo "       cat file.sql | tokenize-query\n";
225
    }
226
227
    /**
228
     * @return mixed
229
     */
230 36
    public function parseTokenize()
231
    {
232 36
        $longopts = [
233 36
            'help',
234 36
            'query:',
235 36
            'ansi',
236 36
        ];
237 36
        $params = $this->getopt('hq:a', $longopts);
238 36
        $this->mergeLongOpts($params, $longopts);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type false; however, parameter $params of PhpMyAdmin\SqlParser\Utils\CLI::mergeLongOpts() does only seem to accept array<mixed,false>|string[], 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

238
        $this->mergeLongOpts(/** @scrutinizer ignore-type */ $params, $longopts);
Loading history...
239
240 36
        return $params;
241
    }
242
243
    /**
244
     * @return int
245
     */
246 36
    public function runTokenize()
247
    {
248 36
        $params = $this->parseTokenize();
249 36
        if ($params === false) {
250 8
            return 1;
251
        }
252
253 28
        if (isset($params['h'])) {
254 8
            $this->usageTokenize();
255
256 8
            return 0;
257
        }
258
259 20
        if (! isset($params['q'])) {
260 12
            $stdIn = $this->readStdin();
261
262 12
            if ($stdIn) {
263 4
                $params['q'] = $stdIn;
264
            }
265
        }
266
267 20
        if (isset($params['a'])) {
268
            Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
269
        }
270
271 20
        if (isset($params['q'])) {
272 12
            $lexer = new Lexer($params['q'], false);
273 12
            foreach ($lexer->list->tokens as $idx => $token) {
274 12
                echo '[TOKEN ', $idx, "]\n";
275 12
                echo 'Type = ', $token->type, "\n";
276 12
                echo 'Flags = ', $token->flags, "\n";
277 12
                echo 'Value = ';
278 12
                var_export($token->value);
279 12
                echo "\n";
280 12
                echo 'Token = ';
281 12
                var_export($token->token);
282 12
                echo "\n";
283 12
                echo "\n";
284
            }
285
286 12
            return 0;
287
        }
288
289 8
        echo "ERROR: Missing parameters!\n";
290 8
        $this->usageTokenize();
291
292 8
        return 1;
293
    }
294
295
    /**
296
     * @return string|false
297
     */
298 12
    public function readStdin()
299
    {
300 12
        $read = [STDIN];
301 12
        $write = [];
302 12
        $except = [];
303
304
        // Assume there's nothing to be read from STDIN.
305 12
        $stdin = null;
306
307
        // Try to read from STDIN.  Wait 0.2 second before timing out.
308 12
        $result = stream_select($read, $write, $except, 0, 2000);
309
310 12
        if ($result > 0) {
311 12
            $stdin = stream_get_contents(STDIN);
312
        }
313
314 12
        return $stdin;
315
    }
316
}
317