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

CLI   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 165
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 5
dl 20
loc 165
ccs 101
cts 101
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A usageHighlight() 0 4 1
A usageLint() 0 4 1
B runLint() 0 29 5
A getopt() 0 4 1
A mergeLongOpts() 0 9 3
A parseHighlight() 0 21 4
B runHighlight() 0 24 4
A parseLint() 10 10 1
A usageTokenize() 0 4 1
A parseTokenize() 10 10 1
B runTokenize() 0 33 5

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\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