Passed
Pull Request — master (#543)
by
unknown
02:57
created

References   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 81
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 56 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\Reference;
8
use PhpMyAdmin\SqlParser\Parseable;
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\TokensList;
11
use PhpMyAdmin\SqlParser\TokenType;
12
13
/**
14
 * `REFERENCES` keyword parser.
15
 */
16
final class References implements Parseable
17
{
18
    /**
19
     * All references options.
20
     */
21
    private const REFERENCES_OPTIONS = [
22
        'MATCH' => [
23
            1,
24
            'var',
25
        ],
26
        'ON DELETE' => [
27
            2,
28
            'var',
29
        ],
30
        'ON UPDATE' => [
31
            3,
32
            'var',
33
        ],
34
    ];
35
36
    /**
37
     * @param Parser               $parser  the parser that serves as context
38
     * @param TokensList           $list    the list of tokens that are being parsed
39
     * @param array<string, mixed> $options parameters for parsing
40
     */
41 18
    public static function parse(Parser $parser, TokensList $list, array $options = []): Reference
42
    {
43 18
        $ret = new Reference();
44
45
        /**
46
         * The state of the parser.
47
         *
48
         * Below are the states of the parser.
49
         *
50
         *      0 ----------------------[ table ]---------------------> 1
51
         *
52
         *      1 ---------------------[ columns ]--------------------> 2
53
         *
54
         *      2 ---------------------[ options ]--------------------> (END)
55
         */
56 18
        $state = 0;
57
58 18
        for (; $list->idx < $list->count; ++$list->idx) {
59
            /**
60
             * Token parsed at this moment.
61
             */
62 18
            $token = $list->tokens[$list->idx];
63
64
            // End of statement.
65 18
            if ($token->type === TokenType::Delimiter) {
66 2
                break;
67
            }
68
69
            // Skipping whitespaces and comments.
70 18
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
71 16
                continue;
72
            }
73
74 18
            if ($state === 0) {
75 18
                $ret->table = Expressions::parse(
76 18
                    $parser,
77 18
                    $list,
78 18
                    [
79 18
                        'parseField' => 'table',
80 18
                        'breakOnAlias' => true,
81 18
                    ],
82 18
                );
83 18
                $state = 1;
84 18
            } elseif ($state === 1) {
85 18
                $ret->columns = ArrayObjs::parse($parser, $list)->values;
86 18
                $state = 2;
87
            } else {
88 16
                $ret->options = OptionsArrays::parse($parser, $list, self::REFERENCES_OPTIONS);
89 16
                ++$list->idx;
90 16
                break;
91
            }
92
        }
93
94 18
        --$list->idx;
95
96 18
        return $ret;
97
    }
98
}
99