Passed
Pull Request — master (#535)
by
unknown
02:55
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
         * @var int
49
         */
50 18
        $state = 0;
51
52 18
        for (; $list->idx < $list->count; ++$list->idx) {
53
            /**
54
             * Token parsed at this moment.
55
             */
56 18
            $token = $list->tokens[$list->idx];
57
58
            // End of statement.
59 18
            if ($token->type === TokenType::Delimiter) {
60 14
                break;
61
            }
62
63
            // Skipping whitespaces and comments.
64 18
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
65 14
                continue;
66
            }
67
68 18
            if ($state === 0) {
69 18
                $expr->old = Expression::parse(
70 18
                    $parser,
71 18
                    $list,
72 18
                    [
73 18
                        'breakOnAlias' => true,
74 18
                        'parseField' => 'table',
75 18
                    ],
76 18
                );
77 18
                if (empty($expr->old)) {
78 2
                    $parser->error('The old name of the table was expected.', $token);
79
                }
80
81 18
                $state = 1;
82 16
            } elseif ($state === 1) {
83 16
                if ($token->type !== TokenType::Keyword || $token->keyword !== 'TO') {
84 2
                    $parser->error('Keyword "TO" was expected.', $token);
85 2
                    break;
86
                }
87
88 14
                $state = 2;
89 14
            } elseif ($state === 2) {
90 14
                $expr->new = Expression::parse(
91 14
                    $parser,
92 14
                    $list,
93 14
                    [
94 14
                        'breakOnAlias' => true,
95 14
                        'parseField' => 'table',
96 14
                    ],
97 14
                );
98 14
                if (empty($expr->new)) {
99 2
                    $parser->error('The new name of the table was expected.', $token);
100
                }
101
102 14
                $state = 3;
103 10
            } elseif ($state === 3) {
104 10
                if (($token->type !== TokenType::Operator) || ($token->value !== ',')) {
105 2
                    break;
106
                }
107
108 8
                $ret[] = $expr;
109 8
                $expr = new RenameOperation();
110 8
                $state = 0;
111
            }
112
        }
113
114 18
        if ($state !== 3) {
0 ignored issues
show
introduced by
The condition $state !== 3 is always true.
Loading history...
115 4
            $parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]);
116
        }
117
118
        // Last iteration was not saved.
119 18
        if (! empty($expr->old)) {
120 16
            $ret[] = $expr;
121
        }
122
123 18
        --$list->idx;
124
125 18
        return $ret;
126
    }
127
128
    /** @param RenameOperation[] $component the component to be built */
129 4
    public static function buildAll(array $component): string
130
    {
131 4
        return implode(', ', $component);
132
    }
133
}
134