Passed
Pull Request — master (#521)
by
unknown
08:41 queued 05:54
created

CLI::usageHighlight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
crap 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 getopt;
12
use function implode;
13
use function in_array;
14
use function rtrim;
15
use function stream_get_contents;
16
use function stream_select;
17
use function var_export;
18
19
use const STDIN;
20
21
/**
22
 * CLI interface.
23
 */
24
class CLI
25
{
26 2
    public function __construct()
27
    {
28 2
        Context::load();
29
    }
30
31
    /**
32
     * @param string[]|false[] $params
33
     * @param string[]         $longopts
34
     */
35 62
    public function mergeLongOpts(array &$params, array &$longopts): void
36
    {
37 62
        foreach ($longopts as $value) {
38 62
            $value = rtrim($value, ':');
39 62
            if (! isset($params[$value])) {
40 62
                continue;
41
            }
42
43 6
            $params[$value[0]] = $params[$value];
44
        }
45
    }
46
47 8
    public function usageHighlight(): void
48
    {
49 8
        echo "Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]\n";
50 8
        echo "       cat file.sql | highlight-query\n";
51
    }
52
53
    /**
54
     * @param string[] $long
55
     *
56
     * @return string[]|false[]|false
57
     */
58 2
    public function getopt(string $opt, array $long): array|false
59
    {
60 2
        return getopt($opt, $long);
61
    }
62
63
    /**
64
     * @return string[]|false[]|false
65
     */
66 30
    public function parseHighlight(): array|false
67
    {
68 30
        $longopts = [
69 30
            'help',
70 30
            'query:',
71 30
            'format:',
72 30
            'ansi',
73 30
        ];
74 30
        $params = $this->getopt('hq:f:a', $longopts);
75 30
        if ($params === false) {
76 4
            return false;
77
        }
78
79 26
        $this->mergeLongOpts($params, $longopts);
80 26
        if (! isset($params['f'])) {
81 14
            $params['f'] = 'cli';
82
        }
83
84 26
        if (! in_array($params['f'], ['html', 'cli', 'text'])) {
85 4
            echo "ERROR: Invalid value for format!\n";
86
87 4
            return false;
88
        }
89
90 22
        return $params;
91
    }
92
93 30
    public function runHighlight(): int
94
    {
95 30
        $params = $this->parseHighlight();
96 30
        if ($params === false) {
0 ignored issues
show
introduced by
The condition $params === false is always true.
Loading history...
97 8
            return 1;
98
        }
99
100 22
        if (isset($params['h'])) {
101 4
            $this->usageHighlight();
102
103 4
            return 0;
104
        }
105
106 18
        if (! isset($params['q'])) {
107 10
            $stdIn = $this->readStdin();
108
109 10
            if ($stdIn) {
110 6
                $params['q'] = $stdIn;
111
            }
112
        }
113
114 18
        if (isset($params['a'])) {
115
            Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
116
        }
117
118 18
        if (isset($params['q'])) {
119 14
            echo Formatter::format(
120 14
                $params['q'],
121 14
                ['type' => $params['f']]
122 14
            );
123 14
            echo "\n";
124
125 14
            return 0;
126
        }
127
128 4
        echo "ERROR: Missing parameters!\n";
129 4
        $this->usageHighlight();
130
131 4
        return 1;
132
    }
133
134 8
    public function usageLint(): void
135
    {
136 8
        echo "Usage: lint-query --query SQL [--ansi]\n";
137 8
        echo "       cat file.sql | lint-query\n";
138
    }
139
140
    /**
141
     * @return string[]|false[]|false
142
     */
143 26
    public function parseLint(): array|false
144
    {
145 26
        $longopts = [
146 26
            'help',
147 26
            'query:',
148 26
            'context:',
149 26
            'ansi',
150 26
        ];
151 26
        $params = $this->getopt('hq:c:a', $longopts);
152 26
        if ($params === false) {
153 4
            return false;
154
        }
155
156 22
        $this->mergeLongOpts($params, $longopts);
157
158 22
        return $params;
159
    }
160
161 26
    public function runLint(): int
162
    {
163 26
        $params = $this->parseLint();
164 26
        if ($params === false) {
0 ignored issues
show
introduced by
The condition $params === false is always true.
Loading history...
165 4
            return 1;
166
        }
167
168 22
        if (isset($params['h'])) {
169 4
            $this->usageLint();
170
171 4
            return 0;
172
        }
173
174 18
        if (isset($params['c'])) {
175 4
            Context::load($params['c']);
176
        }
177
178 18
        if (! isset($params['q'])) {
179 10
            $stdIn = $this->readStdin();
180
181 10
            if ($stdIn) {
182 6
                $params['q'] = $stdIn;
183
            }
184
        }
185
186 18
        if (isset($params['a'])) {
187
            Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
188
        }
189
190 18
        if (isset($params['q'])) {
191 14
            $lexer = new Lexer($params['q'], false);
192 14
            $parser = new Parser($lexer->list);
193 14
            $errors = Error::get([$lexer, $parser]);
194 14
            if ($errors === []) {
195 6
                return 0;
196
            }
197
198 8
            $output = Error::format($errors);
199 8
            echo implode("\n", $output);
200 8
            echo "\n";
201
202 8
            return 10;
203
        }
204
205 4
        echo "ERROR: Missing parameters!\n";
206 4
        $this->usageLint();
207
208 4
        return 1;
209
    }
210
211 8
    public function usageTokenize(): void
212
    {
213 8
        echo "Usage: tokenize-query --query SQL [--ansi]\n";
214 8
        echo "       cat file.sql | tokenize-query\n";
215
    }
216
217
    /**
218
     * @return string[]|false[]|false
219
     */
220 18
    public function parseTokenize(): array|false
221
    {
222 18
        $longopts = [
223 18
            'help',
224 18
            'query:',
225 18
            'ansi',
226 18
        ];
227 18
        $params = $this->getopt('hq:a', $longopts);
228 18
        if ($params === false) {
229 4
            return false;
230
        }
231
232 14
        $this->mergeLongOpts($params, $longopts);
233
234 14
        return $params;
235
    }
236
237 18
    public function runTokenize(): int
238
    {
239 18
        $params = $this->parseTokenize();
240 18
        if ($params === false) {
0 ignored issues
show
introduced by
The condition $params === false is always true.
Loading history...
241 4
            return 1;
242
        }
243
244 14
        if (isset($params['h'])) {
245 4
            $this->usageTokenize();
246
247 4
            return 0;
248
        }
249
250 10
        if (! isset($params['q'])) {
251 6
            $stdIn = $this->readStdin();
252
253 6
            if ($stdIn) {
254 2
                $params['q'] = $stdIn;
255
            }
256
        }
257
258 10
        if (isset($params['a'])) {
259
            Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
260
        }
261
262 10
        if (isset($params['q'])) {
263 6
            $lexer = new Lexer($params['q'], false);
264 6
            foreach ($lexer->list->tokens as $idx => $token) {
265 6
                echo '[TOKEN ', $idx, "]\n";
266 6
                echo 'Type = ', $token->type->value, "\n";
267 6
                echo 'Flags = ', $token->flags, "\n";
268 6
                echo 'Value = ';
269 6
                var_export($token->value);
270 6
                echo "\n";
271 6
                echo 'Token = ';
272 6
                var_export($token->token);
273 6
                echo "\n";
274 6
                echo "\n";
275
            }
276
277 6
            return 0;
278
        }
279
280 4
        echo "ERROR: Missing parameters!\n";
281 4
        $this->usageTokenize();
282
283 4
        return 1;
284
    }
285
286 6
    public function readStdin(): string|false|null
287
    {
288 6
        $read = [STDIN];
289 6
        $write = [];
290 6
        $except = [];
291
292
        // Assume there's nothing to be read from STDIN.
293 6
        $stdin = null;
294
295
        // Try to read from STDIN.  Wait 0.2 second before timing out.
296 6
        $result = stream_select($read, $write, $except, 0, 2000);
297
298 6
        if ($result > 0) {
299 6
            $stdin = stream_get_contents(STDIN);
300
        }
301
302 6
        return $stdin;
303
    }
304
}
305