Passed
Pull Request — master (#535)
by
unknown
02:55
created

ParameterDefinitions::buildAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\DataType;
8
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
9
use PhpMyAdmin\SqlParser\Parseable;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\TokensList;
12
use PhpMyAdmin\SqlParser\TokenType;
13
14
use function implode;
15
16
/**
17
 * The definition of a parameter of a function or procedure.
18
 */
19
final class ParameterDefinitions implements Parseable
20
{
21
    /**
22
     * @param Parser               $parser  the parser that serves as context
23
     * @param TokensList           $list    the list of tokens that are being parsed
24
     * @param array<string, mixed> $options parameters for parsing
25
     *
26
     * @return ParameterDefinition[]
27
     */
28 52
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
29
    {
30 52
        $ret = [];
31
32 52
        $expr = new ParameterDefinition();
33
34
        /**
35
         * The state of the parser.
36
         *
37
         * Below are the states of the parser.
38
         *
39
         *      0 -----------------------[ ( ]------------------------> 1
40
         *
41
         *      1 ----------------[ IN / OUT / INOUT ]----------------> 1
42
         *      1 ----------------------[ name ]----------------------> 2
43
         *
44
         *      2 -------------------[ data type ]--------------------> 3
45
         *
46
         *      3 ------------------------[ , ]-----------------------> 1
47
         *      3 ------------------------[ ) ]-----------------------> (END)
48
         *
49
         * @var int
50
         */
51 52
        $state = 0;
52
53 52
        for (; $list->idx < $list->count; ++$list->idx) {
54
            /**
55
             * Token parsed at this moment.
56
             */
57 52
            $token = $list->tokens[$list->idx];
58
59
            // End of statement.
60 52
            if ($token->type === TokenType::Delimiter) {
61 4
                break;
62
            }
63
64
            // Skipping whitespaces and comments.
65 52
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
66 44
                continue;
67
            }
68
69 52
            if ($state === 0) {
70 52
                if (($token->type === TokenType::Operator) && ($token->value === '(')) {
71 51
                    $state = 1;
72
                }
73 50
            } elseif ($state === 1) {
74 50
                if (($token->value === 'IN') || ($token->value === 'OUT') || ($token->value === 'INOUT')) {
75 22
                    $expr->inOut = $token->value;
76 22
                    ++$list->idx;
77 50
                } elseif ($token->value === ')') {
78 6
                    ++$list->idx;
79 6
                    break;
80
                } else {
81 44
                    $expr->name = $token->value;
82 44
                    $state = 2;
83
                }
84 44
            } elseif ($state === 2) {
85 44
                $expr->type = DataType::parse($parser, $list);
86 44
                $state = 3;
87 44
            } elseif ($state === 3) {
88 44
                $ret[] = $expr;
89 44
                $expr = new ParameterDefinition();
90 44
                if ($token->value === ',') {
91 10
                    $state = 1;
92 42
                } elseif ($token->value === ')') {
93 42
                    ++$list->idx;
94 42
                    break;
95
                }
96
            }
97
        }
98
99
        // Last iteration was not saved.
100 52
        if (isset($expr->name) && ($expr->name !== '')) {
101 2
            $ret[] = $expr;
102
        }
103
104 52
        --$list->idx;
105
106 52
        return $ret;
107
    }
108
109
    /** @param ParameterDefinition[] $component the component to be built */
110 6
    public static function buildAll(array $component): string
111
    {
112 6
        return '(' . implode(', ', $component) . ')';
113
    }
114
}
115