Passed
Push — master ( ee792a...6ee6fa )
by Michal
03:51
created

CLI   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 99.03%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
lcom 1
cbo 6
dl 0
loc 168
c 1
b 0
f 0
ccs 102
cts 103
cp 0.9903
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeLongOpts() 0 9 3
A usageHighlight() 0 4 1
A getopt() 0 4 1
A parseHighlight() 0 21 4
B runHighlight() 0 24 4
A usageLint() 0 4 1
A parseLint() 0 10 1
B runLint() 0 32 6
A usageTokenize() 0 4 1
A parseTokenize() 0 10 1
B runTokenize() 0 33 5
1
<?php
2
3
/**
4
 * CLI interface.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Utils;
8
9
use PhpMyAdmin\SqlParser\Context;
10
use PhpMyAdmin\SqlParser\Lexer;
11
use PhpMyAdmin\SqlParser\Parser;
12
13
/**
14
 * CLI interface.
15
 *
16
 * @category   Exceptions
17
 *
18
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
19
 */
20
class CLI
21
{
22 18
    public function mergeLongOpts(&$params, &$longopts)
23
    {
24 18
        foreach ($longopts as $value) {
25 18
            $value = rtrim($value, ':');
26 18
            if (isset($params[$value])) {
27 3
                $params[$value[0]] = $params[$value];
28
            }
29
        }
30 18
    }
31
32 2
    public function usageHighlight()
33
    {
34 2
        echo "Usage: highlight-query --query SQL [--format html|cli|text]\n";
35 2
    }
36
37 1
    public function getopt($opt, $long)
38
    {
39 1
        return getopt($opt, $long);
40
    }
41
42 8
    public function parseHighlight()
43
    {
44 8
        $longopts = array('help', 'query:', 'format:');
45 8
        $params = $this->getopt(
46 8
            'hq:f:', $longopts
47
        );
48 8
        if ($params === false) {
49 1
            return false;
50
        }
51 7
        $this->mergeLongOpts($params, $longopts);
52 7
        if (!isset($params['f'])) {
53 4
            $params['f'] = 'cli';
54
        }
55 7
        if (!in_array($params['f'], array('html', 'cli', 'text'))) {
56 1
            echo "ERROR: Invalid value for format!\n";
57
58 1
            return false;
59
        }
60
61 6
        return $params;
62
    }
63
64 8
    public function runHighlight()
65
    {
66 8
        $params = $this->parseHighlight();
67 8
        if ($params === false) {
68 2
            return 1;
69
        }
70 6
        if (isset($params['h'])) {
71 1
            $this->usageHighlight();
72
73 1
            return 0;
74
        }
75 5
        if (isset($params['q'])) {
76 4
            echo Formatter::format(
77 4
                $params['q'], array('type' => $params['f'])
78
            );
79 4
            echo "\n";
80
81 4
            return 0;
82
        }
83 1
        echo "ERROR: Missing parameters!\n";
84 1
        $this->usageHighlight();
85
86 1
        return 1;
87
    }
88
89 2
    public function usageLint()
90
    {
91 2
        echo "Usage: lint-query --query SQL\n";
92 2
    }
93
94 6
    public function parseLint()
95
    {
96 6
        $longopts = array('help', 'query:', 'context:');
97 6
        $params = $this->getopt(
98 6
            'hq:c:', $longopts
99
        );
100 6
        $this->mergeLongOpts($params, $longopts);
101
102 6
        return $params;
103
    }
104
105 6
    public function runLint()
106
    {
107 6
        $params = $this->parseLint();
108 6
        if ($params === false) {
109 1
            return 1;
110
        }
111 5
        if (isset($params['h'])) {
112 1
            $this->usageLint();
113
114 1
            return 0;
115
        }
116 4
        if (isset($params['c'])) {
117
            Context::load($params['c']);
118
        }
119 4
        if (isset($params['q'])) {
120 3
            $lexer = new Lexer($params['q'], false);
121 3
            $parser = new Parser($lexer->list);
122 3
            $errors = Error::get(array($lexer, $parser));
123 3
            if (count($errors) == 0) {
124 2
                return 0;
125
            }
126 1
            $output = Error::format($errors);
127 1
            echo implode("\n", $output);
128 1
            echo "\n";
129
130 1
            return 10;
131
        }
132 1
        echo "ERROR: Missing parameters!\n";
133 1
        $this->usageLint();
134
135 1
        return 1;
136
    }
137
138 2
    public function usageTokenize()
139
    {
140 2
        echo "Usage: tokenize-query --query SQL\n";
141 2
    }
142
143 5
    public function parseTokenize()
144
    {
145 5
        $longopts = array('help', 'query:');
146 5
        $params = $this->getopt(
147 5
            'hq:', $longopts
148
        );
149 5
        $this->mergeLongOpts($params, $longopts);
150
151 5
        return $params;
152
    }
153
154 5
    public function runTokenize()
155
    {
156 5
        $params = $this->parseTokenize();
157 5
        if ($params === false) {
158 1
            return 1;
159
        }
160 4
        if (isset($params['h'])) {
161 1
            $this->usageTokenize();
162
163 1
            return 0;
164
        }
165 3
        if (isset($params['q'])) {
166 2
            $lexer = new Lexer($params['q'], false);
167 2
            foreach ($lexer->list->tokens as $idx => $token) {
168 2
                echo '[TOKEN ', $idx, "]\n";
169 2
                echo 'Type = ', $token->type, "\n";
170 2
                echo 'Flags = ', $token->flags, "\n";
171 2
                echo 'Value = ';
172 2
                var_export($token->value);
173 2
                echo "\n";
174 2
                echo 'Token = ';
175 2
                var_export($token->token);
176 2
                echo "\n";
177 2
                echo "\n";
178
            }
179
180 2
            return 0;
181
        }
182 1
        echo "ERROR: Missing parameters!\n";
183 1
        $this->usageTokenize();
184
185 1
        return 1;
186
    }
187
}
188