Completed
Push — master ( 65f66e...428edc )
by Michal
04:14
created

Routine::getParameters()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 34
Code Lines 25

Duplication

Lines 4
Ratio 11.76 %

Code Coverage

Tests 28
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 2
nop 1
dl 4
loc 34
ccs 28
cts 28
cp 1
crap 5
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Routine utilities.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Utils;
8
9
use PhpMyAdmin\SqlParser\Lexer;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Components\DataType;
12
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
13
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
14
15
/**
16
 * Routine utilities.
17
 *
18
 * @category   Routines
19
 *
20
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
21
 */
22
class Routine
23
{
24
    /**
25
     * Parses a parameter of a routine.
26
     *
27
     * @param string $param parameter's definition
28
     *
29
     * @return array
30
     */
31 8
    public static function getReturnType($param)
32
    {
33 8
        $lexer = new Lexer($param);
34
35
        // A dummy parser is used for error reporting.
36 8
        $type = DataType::parse(new Parser(), $lexer->list);
37
38 8
        if ($type === null) {
39 1
            return array('', '', '', '', '');
40
        }
41
42 7
        $options = array();
43 7
        foreach ($type->options->options as $opt) {
44 4
            $options[] = is_string($opt) ? $opt : $opt['value'];
45 7
        }
46
47
        return array(
48 7
            '',
49 7
            '',
50 7
            $type->name,
51 7
            implode(',', $type->parameters),
52 7
            implode(' ', $options),
53 7
        );
54
    }
55
56
    /**
57
     * Parses a parameter of a routine.
58
     *
59
     * @param string $param parameter's definition
60
     *
61
     * @return array
62
     */
63 8
    public static function getParameter($param)
64
    {
65 8
        $lexer = new Lexer('(' . $param . ')');
66
67
        // A dummy parser is used for error reporting.
68 8
        $param = ParameterDefinition::parse(new Parser(), $lexer->list);
69
70 8
        if (empty($param[0])) {
71 1
            return array('', '', '', '', '');
72
        }
73
74 7
        $param = $param[0];
75
76 7
        $options = array();
77 7 View Code Duplication
        foreach ($param->type->options->options as $opt) {
78 4
            $options[] = is_string($opt) ? $opt : $opt['value'];
79 7
        }
80
81
        return array(
82 7
            empty($param->inOut) ? '' : $param->inOut,
83 7
            $param->name,
84 7
            $param->type->name,
85 7
            implode(',', $param->type->parameters),
86 7
            implode(' ', $options),
87 7
        );
88
    }
89
90
    /**
91
     * Gets the parameters of a routine from the parse tree.
92
     *
93
     * @param CreateStatement $statement the statement to be processed
94
     *
95
     * @return array
96
     */
97 4
    public static function getParameters($statement)
98
    {
99
        $retval = array(
100 4
            'num' => 0,
101 4
            'dir' => array(),
102 4
            'name' => array(),
103 4
            'type' => array(),
104 4
            'length' => array(),
105 4
            'length_arr' => array(),
106 4
            'opts' => array(),
107 4
        );
108
109 4
        if (!empty($statement->parameters)) {
110 3
            $idx = 0;
111 3
            foreach ($statement->parameters as $param) {
112 3
                $retval['dir'][$idx] = $param->inOut;
113 3
                $retval['name'][$idx] = $param->name;
114 3
                $retval['type'][$idx] = $param->type->name;
115 3
                $retval['length'][$idx] = implode(',', $param->type->parameters);
116 3
                $retval['length_arr'][$idx] = $param->type->parameters;
117 3
                $retval['opts'][$idx] = array();
118 3 View Code Duplication
                foreach ($param->type->options->options as $opt) {
119 2
                    $retval['opts'][$idx][] = is_string($opt) ?
120 2
                        $opt : $opt['value'];
121 3
                }
122 3
                $retval['opts'][$idx] = implode(' ', $retval['opts'][$idx]);
123 3
                ++$idx;
124 3
            }
125
126 3
            $retval['num'] = $idx;
127 3
        }
128
129 4
        return $retval;
130
    }
131
}
132