Completed
Push — master ( 578d3a...04a929 )
by Maurício
34s queued 14s
created

RenameOperations::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\Expression;
8
use PhpMyAdmin\SqlParser\Components\RenameOperation;
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
 * `RENAME TABLE` keyword parser.
18
 */
19
final class RenameOperations 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 RenameOperation[]
27
     */
28 18
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
29
    {
30 18
        $ret = [];
31
32 18
        $expr = new RenameOperation();
33
34
        /**
35
         * The state of the parser.
36
         *
37
         * Below are the states of the parser.
38
         *
39
         *      0 ---------------------[ old name ]--------------------> 1
40
         *
41
         *      1 ------------------------[ TO ]-----------------------> 2
42
         *
43
         *      2 ---------------------[ new name ]--------------------> 3
44
         *
45
         *      3 ------------------------[ , ]------------------------> 0
46
         *      3 -----------------------[ else ]----------------------> (END)
47
         */
48 18
        $state = 0;
49
50 18
        for (; $list->idx < $list->count; ++$list->idx) {
51
            /**
52
             * Token parsed at this moment.
53
             */
54 18
            $token = $list->tokens[$list->idx];
55
56
            // End of statement.
57 18
            if ($token->type === TokenType::Delimiter) {
58 14
                break;
59
            }
60
61
            // Skipping whitespaces and comments.
62 18
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
63 14
                continue;
64
            }
65
66 18
            if ($state === 0) {
67 18
                $expr->old = Expression::parse(
68 18
                    $parser,
69 18
                    $list,
70 18
                    [
71 18
                        'breakOnAlias' => true,
72 18
                        'parseField' => 'table',
73 18
                    ],
74 18
                );
75 18
                if (empty($expr->old)) {
76 2
                    $parser->error('The old name of the table was expected.', $token);
77
                }
78
79 18
                $state = 1;
80 16
            } elseif ($state === 1) {
81 16
                if ($token->type !== TokenType::Keyword || $token->keyword !== 'TO') {
82 2
                    $parser->error('Keyword "TO" was expected.', $token);
83 2
                    break;
84
                }
85
86 14
                $state = 2;
87 14
            } elseif ($state === 2) {
88 14
                $expr->new = Expression::parse(
89 14
                    $parser,
90 14
                    $list,
91 14
                    [
92 14
                        'breakOnAlias' => true,
93 14
                        'parseField' => 'table',
94 14
                    ],
95 14
                );
96 14
                if (empty($expr->new)) {
97 2
                    $parser->error('The new name of the table was expected.', $token);
98
                }
99
100 14
                $state = 3;
101
            } else {
102 10
                if (($token->type !== TokenType::Operator) || ($token->value !== ',')) {
103 2
                    break;
104
                }
105
106 8
                $ret[] = $expr;
107 8
                $expr = new RenameOperation();
108 8
                $state = 0;
109
            }
110
        }
111
112 18
        if ($state !== 3) {
0 ignored issues
show
introduced by
The condition $state !== 3 is always true.
Loading history...
113 4
            $parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]);
114
        }
115
116
        // Last iteration was not saved.
117 18
        if (! empty($expr->old)) {
118 16
            $ret[] = $expr;
119
        }
120
121 18
        --$list->idx;
122
123 18
        return $ret;
124
    }
125
126
    /** @param RenameOperation[] $component the component to be built */
127 4
    public static function buildAll(array $component): string
128
    {
129 4
        return implode(', ', $component);
130
    }
131
}
132