Completed
Push — master ( f3f36d...511484 )
by Michal
04:00
created

CLI::runHighlight()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 0
crap 4
1
<?php
2
3
/**
4
 * CLI interface.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Utils;
8
9
use PhpMyAdmin\SqlParser\Lexer;
10
use PhpMyAdmin\SqlParser\Parser;
11
12
/**
13
 * CLI interface.
14
 *
15
 * @category   Exceptions
16
 *
17
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
18
 */
19
class CLI
20
{
21 18
    public function mergeLongOpts(&$params, &$longopts)
22
    {
23 18
        foreach ($longopts as $value) {
24 18
            $value = rtrim($value, ':');
25 18
            if (isset($params[$value])) {
26 18
                $params[$value[0]] = $params[$value];
27
            }
28
        }
29 18
    }
30
31 2
    public function usageHighlight()
32
    {
33 2
        echo "Usage: highlight-query --query SQL [--format html|cli|text]\n";
34 2
    }
35
36 1
    public function getopt($opt, $long)
37
    {
38 1
        return getopt($opt, $long);
39
    }
40
41 8
    public function parseHighlight()
42
    {
43 8
        $longopts = array('help', 'query:', 'format:');
44 8
        $params = $this->getopt(
45 8
            'hq:f:', $longopts
46
        );
47 8
        if ($params === false) {
48 1
            return false;
49
        }
50 7
        $this->mergeLongOpts($params, $longopts);
51 7
        if (!isset($params['f'])) {
52 4
            $params['f'] = 'cli';
53
        }
54 7
        if (!in_array($params['f'], array('html', 'cli', 'text'))) {
55 1
            echo "ERROR: Invalid value for format!\n";
56
57 1
            return false;
58
        }
59
60 6
        return $params;
61
    }
62
63 8
    public function runHighlight()
64
    {
65 8
        $params = $this->parseHighlight();
66 8
        if ($params === false) {
67 2
            return 1;
68
        }
69 6
        if (isset($params['h'])) {
70 1
            $this->usageHighlight();
71
72 1
            return 0;
73
        }
74 5
        if (isset($params['q'])) {
75 4
            echo Formatter::format(
76 4
                $params['q'], array('type' => $params['f'])
77
            );
78 4
            echo "\n";
79
80 4
            return 0;
81
        }
82 1
        echo "ERROR: Missing parameters!\n";
83 1
        $this->usageHighlight();
84
85 1
        return 1;
86
    }
87
88 2
    public function usageLint()
89
    {
90 2
        echo "Usage: lint-query --query SQL\n";
91 2
    }
92
93 6 View Code Duplication
    public function parseLint()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95 6
        $longopts = array('help', 'query:');
96 6
        $params = $this->getopt(
97 6
            'hq:', $longopts
98
        );
99 6
        $this->mergeLongOpts($params, $longopts);
100
101 6
        return $params;
102
    }
103
104 6
    public function runLint()
105
    {
106 6
        $params = $this->parseLint();
107 6
        if ($params === false) {
108 1
            return 1;
109
        }
110 5
        if (isset($params['h'])) {
111 1
            $this->usageLint();
112
113 1
            return 0;
114
        }
115 4
        if (isset($params['q'])) {
116 3
            $lexer = new Lexer($params['q'], false);
117 3
            $parser = new Parser($lexer->list);
118 3
            $errors = Error::get(array($lexer, $parser));
119 3
            if (count($errors) == 0) {
120 2
                return 0;
121
            }
122 1
            $output = Error::format($errors);
123 1
            echo implode("\n", $output);
124 1
            echo "\n";
125
126 1
            return 10;
127
        }
128 1
        echo "ERROR: Missing parameters!\n";
129 1
        $this->usageLint();
130
131 1
        return 1;
132
    }
133
134 2
    public function usageTokenize()
135
    {
136 2
        echo "Usage: tokenize-query --query SQL\n";
137 2
    }
138
139 5 View Code Duplication
    public function parseTokenize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141 5
        $longopts = array('help', 'query:');
142 5
        $params = $this->getopt(
143 5
            'hq:', $longopts
144
        );
145 5
        $this->mergeLongOpts($params, $longopts);
146
147 5
        return $params;
148
    }
149
150 5
    public function runTokenize()
151
    {
152 5
        $params = $this->parseTokenize();
153 5
        if ($params === false) {
154 1
            return 1;
155
        }
156 4
        if (isset($params['h'])) {
157 1
            $this->usageTokenize();
158
159 1
            return 0;
160
        }
161 3
        if (isset($params['q'])) {
162 2
            $lexer = new Lexer($params['q'], false);
163 2
            foreach ($lexer->list->tokens as $idx => $token) {
164 2
                echo '[TOKEN ', $idx, "]\n";
165 2
                echo 'Type = ', $token->type, "\n";
166 2
                echo 'Flags = ', $token->flags, "\n";
167 2
                echo 'Value = ';
168 2
                var_export($token->value);
169 2
                echo "\n";
170 2
                echo 'Token = ';
171 2
                var_export($token->token);
172 2
                echo "\n";
173 2
                echo "\n";
174
            }
175
176 2
            return 0;
177
        }
178 1
        echo "ERROR: Missing parameters!\n";
179 1
        $this->usageTokenize();
180
181 1
        return 1;
182
    }
183
}
184