Completed
Push — master ( 082a1f...e8595e )
by Michal
10:33 queued 10:18
created

CLI::parseHighlight()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 5
nop 0
dl 0
loc 21
ccs 0
cts 16
cp 0
crap 20
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CLI interface.
5
 */
6
7
namespace SqlParser\Utils;
8
9
use SqlParser\Parser;
10
use SqlParser\Lexer;
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
    public function mergeLongOpts(&$params, &$longopts)
22
    {
23
        foreach ($longopts as $value) {
24
            $value = rtrim($value, ':');
25
            if (isset($params[$value])) {
26
                $params[$value[0]] = $params[$value];
27
            }
28
        }
29
    }
30
31
    public function usageHighlight()
32
    {
33
        echo "Usage: highlight-query --query SQL [--format html|cli|text]\n";
34
    }
35
36
    public function getopt($opt, $long)
37
    {
38
        return getopt($opt, $long);
39
    }
40
41
    public function parseHighlight()
42
    {
43
        $longopts = array('help', 'query:', 'format:');
44
        $params = $this->getopt(
45
            'hq:f:', $longopts
46
        );
47
        if ($params === false) {
48
            return false;
49
        }
50
        $this->mergeLongOpts($params, $longopts);
51
        if (!isset($params['f'])) {
52
            $params['f'] = 'cli';
53
        }
54
        if (!in_array($params['f'], array('html', 'cli', 'text'))) {
55
            echo "ERROR: Invalid value for format!\n";
56
57
            return false;
58
        }
59
60
        return $params;
61
    }
62
63
    public function runHighlight()
64
    {
65
        $params = $this->parseHighlight();
66
        if ($params === false) {
67
            return 1;
68
        }
69
        if (isset($params['h'])) {
70
            $this->usageHighlight();
71
72
            return 0;
73
        }
74
        if (isset($params['q'])) {
75
            echo Formatter::format(
76
                $params['q'], array('type' => $params['f'])
77
            );
78
            echo "\n";
79
80
            return 0;
81
        }
82
        echo "ERROR: Missing parameters!\n";
83
        $this->usageHighlight();
84
85
        return 1;
86
    }
87
88
    public function usageLint()
89
    {
90
        echo "Usage: lint-query --query SQL\n";
91
    }
92
93
    public function parseLint()
94
    {
95
        $longopts = array('help', 'query:');
96
        $params = $this->getopt(
97
            'hq:', $longopts
98
        );
99
        $this->mergeLongOpts($params, $longopts);
100
101
        return $params;
102
    }
103
104
    public function runLint()
105
    {
106
        $params = $this->parseLint();
107
        if ($params === false) {
108
            return 1;
109
        }
110
        if (isset($params['h'])) {
111
            $this->usageLint();
112
113
            return 0;
114
        }
115
        if (isset($params['q'])) {
116
            $lexer = new Lexer($params['q'], false);
117
            $parser = new Parser($lexer->list);
118
            $errors = Error::get(array($lexer, $parser));
119
            if (count($errors) == 0) {
120
                return 0;
121
            }
122
            $output = Error::format($errors);
123
            echo implode("\n", $output);
124
            echo "\n";
125
126
            return 10;
127
        }
128
        echo "ERROR: Missing parameters!\n";
129
        $this->usageLint();
130
131
        return 1;
132
    }
133
}
134