Passed
Push — master ( 0efc01...ef206b )
by Maurício
02:53
created

CLI::parseLint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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

163
        $this->mergeLongOpts(/** @scrutinizer ignore-type */ $params, $longopts);
Loading history...
164
165 52
        return $params;
166
    }
167
168
    /**
169
     * @return int
170
     */
171 52
    public function runLint()
172
    {
173 52
        $params = $this->parseLint();
174 52
        if ($params === false) {
175 8
            return 1;
176
        }
177
178 44
        if (isset($params['h'])) {
179 8
            $this->usageLint();
180
181 8
            return 0;
182
        }
183
184 36
        if (isset($params['c'])) {
185 8
            Context::load($params['c']);
186
        }
187
188 36
        if (! isset($params['q'])) {
189 20
            $stdIn = $this->readStdin();
190
191 20
            if ($stdIn) {
192 12
                $params['q'] = $stdIn;
193
            }
194
        }
195
196 36
        if (isset($params['a'])) {
197
            Context::setMode('ANSI_QUOTES');
198
        }
199
200 36
        if (isset($params['q'])) {
201 28
            $lexer = new Lexer($params['q'], false);
202 28
            $parser = new Parser($lexer->list);
203 28
            $errors = Error::get([$lexer, $parser]);
204 28
            if (count($errors) === 0) {
205 12
                return 0;
206
            }
207
208 16
            $output = Error::format($errors);
209 16
            echo implode("\n", $output);
210 16
            echo "\n";
211
212 16
            return 10;
213
        }
214
215 8
        echo "ERROR: Missing parameters!\n";
216 8
        $this->usageLint();
217
218 8
        return 1;
219
    }
220
221
    /**
222
     * @return void
223
     */
224 16
    public function usageTokenize()
225
    {
226 16
        echo "Usage: tokenize-query --query SQL [--ansi]\n";
227 16
        echo "       cat file.sql | tokenize-query\n";
228 16
    }
229
230
    /**
231
     * @return mixed
232
     */
233 36
    public function parseTokenize()
234
    {
235 9
        $longopts = [
236 27
            'help',
237
            'query:',
238
            'ansi',
239
        ];
240 36
        $params = $this->getopt('hq:a', $longopts);
241 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

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