Passed
Pull Request — master (#294)
by William
04:00
created

CLI   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 224
Duplicated Lines 20.54 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 97.69%

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 6
dl 46
loc 224
ccs 127
cts 130
cp 0.9769
rs 9.36
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeLongOpts() 0 9 3
A usageHighlight() 0 5 1
A getopt() 0 4 1
A parseHighlight() 0 27 4
B runHighlight() 5 34 7
A usageLint() 0 5 1
A parseLint() 16 16 1
B runLint() 5 41 9
A usageTokenize() 0 5 1
A parseTokenize() 15 15 1
B runTokenize() 5 42 8
A readStdin() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 35
    public function mergeLongOpts(&$params, &$longopts)
23
    {
24 35
        foreach ($longopts as $value) {
25 35
            $value = rtrim($value, ':');
26 35
            if (isset($params[$value])) {
27 3
                $params[$value[0]] = $params[$value];
28
            }
29
        }
30 35
    }
31
32 4
    public function usageHighlight()
33
    {
34 4
        echo "Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]\n";
35 4
        echo "       cat file.sql | highlight-query\n";
36 4
    }
37
38 1
    public function getopt($opt, $long)
39
    {
40 1
        return getopt($opt, $long);
41
    }
42
43 15
    public function parseHighlight()
44
    {
45
        $longopts = array(
46 15
            'help',
47
            'query:',
48
            'format:',
49
            'ansi'
50
        );
51 15
        $params = $this->getopt(
52 15
            'hq:f:a',
53 15
            $longopts
54
        );
55 15
        if ($params === false) {
56 2
            return false;
57
        }
58 13
        $this->mergeLongOpts($params, $longopts);
59 13
        if (! isset($params['f'])) {
60 7
            $params['f'] = 'cli';
61
        }
62 13
        if (! in_array($params['f'], array('html', 'cli', 'text'))) {
63 2
            echo "ERROR: Invalid value for format!\n";
64
65 2
            return false;
66
        }
67
68 11
        return $params;
69
    }
70
71 15
    public function runHighlight()
72
    {
73 15
        $params = $this->parseHighlight();
74 15
        if ($params === false) {
75 4
            return 1;
76
        }
77 11
        if (isset($params['h'])) {
78 2
            $this->usageHighlight();
79
80 2
            return 0;
81
        }
82 9 View Code Duplication
        if (!isset($params['q'])) {
83 5
            if ($stdIn = $this->readStdin()) {
84 3
                $params['q'] = $stdIn;
85
            }
86
        }
87
88 9
        if (isset($params['a'])) {
89
            Context::setMode('ANSI_QUOTES');
90
        }
91 9
        if (isset($params['q'])) {
92 7
            echo Formatter::format(
93 7
                $params['q'],
94 7
                array('type' => $params['f'])
95
            );
96 7
            echo "\n";
97
98 7
            return 0;
99
        }
100 2
        echo "ERROR: Missing parameters!\n";
101 2
        $this->usageHighlight();
102
103 2
        return 1;
104
    }
105
106 4
    public function usageLint()
107
    {
108 4
        echo "Usage: lint-query --query SQL [--ansi]\n";
109 4
        echo "       cat file.sql | lint-query\n";
110 4
    }
111
112 13 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...
113
    {
114
        $longopts = array(
115 13
            'help',
116
            'query:',
117
            'context:',
118
            'ansi'
119
        );
120 13
        $params = $this->getopt(
121 13
            'hq:c:a',
122 13
            $longopts
123
        );
124 13
        $this->mergeLongOpts($params, $longopts);
125
126 13
        return $params;
127
    }
128
129 13
    public function runLint()
130
    {
131 13
        $params = $this->parseLint();
132 13
        if ($params === false) {
133 2
            return 1;
134
        }
135 11
        if (isset($params['h'])) {
136 2
            $this->usageLint();
137
138 2
            return 0;
139
        }
140 9
        if (isset($params['c'])) {
141 2
            Context::load($params['c']);
142
        }
143 9 View Code Duplication
        if (!isset($params['q'])) {
144 5
            if ($stdIn = $this->readStdin()) {
145 3
                $params['q'] = $stdIn;
146
            }
147
        }
148 9
        if (isset($params['a'])) {
149
            Context::setMode('ANSI_QUOTES');
150
        }
151
152 9
        if (isset($params['q'])) {
153 7
            $lexer = new Lexer($params['q'], false);
154 7
            $parser = new Parser($lexer->list);
155 7
            $errors = Error::get(array($lexer, $parser));
156 7
            if (count($errors) === 0) {
157 3
                return 0;
158
            }
159 4
            $output = Error::format($errors);
160 4
            echo implode("\n", $output);
161 4
            echo "\n";
162
163 4
            return 10;
164
        }
165 2
        echo "ERROR: Missing parameters!\n";
166 2
        $this->usageLint();
167
168 2
        return 1;
169
    }
170
171 4
    public function usageTokenize()
172
    {
173 4
        echo "Usage: tokenize-query --query SQL [--ansi]\n";
174 4
        echo "       cat file.sql | tokenize-query\n";
175 4
    }
176
177 9 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...
178
    {
179
        $longopts = array(
180 9
            'help',
181
            'query:',
182
            'ansi'
183
        );
184 9
        $params = $this->getopt(
185 9
            'hq:a',
186 9
            $longopts
187
        );
188 9
        $this->mergeLongOpts($params, $longopts);
189
190 9
        return $params;
191
    }
192
193 9
    public function runTokenize()
194
    {
195 9
        $params = $this->parseTokenize();
196 9
        if ($params === false) {
197 2
            return 1;
198
        }
199 7
        if (isset($params['h'])) {
200 2
            $this->usageTokenize();
201
202 2
            return 0;
203
        }
204 5 View Code Duplication
        if (!isset($params['q'])) {
205 3
            if ($stdIn = $this->readStdin()) {
206 1
                $params['q'] = $stdIn;
207
            }
208
        }
209
210 5
        if (isset($params['a'])) {
211
            Context::setMode('ANSI_QUOTES');
212
        }
213 5
        if (isset($params['q'])) {
214 3
            $lexer = new Lexer($params['q'], false);
215 3
            foreach ($lexer->list->tokens as $idx => $token) {
216 3
                echo '[TOKEN ', $idx, "]\n";
217 3
                echo 'Type = ', $token->type, "\n";
218 3
                echo 'Flags = ', $token->flags, "\n";
219 3
                echo 'Value = ';
220 3
                var_export($token->value);
221 3
                echo "\n";
222 3
                echo 'Token = ';
223 3
                var_export($token->token);
224 3
                echo "\n";
225 3
                echo "\n";
226
            }
227
228 3
            return 0;
229
        }
230 2
        echo "ERROR: Missing parameters!\n";
231 2
        $this->usageTokenize();
232
233 2
        return 1;
234
    }
235
236 3
    public function readStdin() {
237 3
        stream_set_blocking(STDIN, false);
238 3
        $stdin = stream_get_contents(STDIN);
239
        // restore-default block-mode setting
240 3
        stream_set_blocking(STDIN, true);
241 3
        return $stdin;
242
    }
243
}
244