Completed
Push — master ( d70e65...52ffc4 )
by Maurício
21s queued 15s
created

RenameOperation   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 152
ccs 60
cts 60
cp 1
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 3 1
A buildAll() 0 3 1
C parse() 0 98 17
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Parser;
9
use PhpMyAdmin\SqlParser\Token;
10
use PhpMyAdmin\SqlParser\TokensList;
11
12
use function implode;
13
14
/**
15
 * `RENAME TABLE` keyword parser.
16
 */
17
final class RenameOperation implements Component
18
{
19
    /**
20
     * The old table name.
21
     *
22
     * @var Expression
23
     */
24
    public $old;
25
26
    /**
27
     * The new table name.
28
     *
29
     * @var Expression
30
     */
31
    public $new;
32
33
    /**
34
     * @param Expression $old old expression
35
     * @param Expression $new new expression containing new name
36
     */
37 18
    public function __construct($old = null, $new = null)
38
    {
39 18
        $this->old = $old;
40 18
        $this->new = $new;
41
    }
42
43
    /**
44
     * @param Parser               $parser  the parser that serves as context
45
     * @param TokensList           $list    the list of tokens that are being parsed
46
     * @param array<string, mixed> $options parameters for parsing
47
     *
48
     * @return RenameOperation[]
49
     */
50 18
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
51
    {
52 18
        $ret = [];
53
54 18
        $expr = new static();
55
56
        /**
57
         * The state of the parser.
58
         *
59
         * Below are the states of the parser.
60
         *
61
         *      0 ---------------------[ old name ]--------------------> 1
62
         *
63
         *      1 ------------------------[ TO ]-----------------------> 2
64
         *
65
         *      2 ---------------------[ new name ]--------------------> 3
66
         *
67
         *      3 ------------------------[ , ]------------------------> 0
68
         *      3 -----------------------[ else ]----------------------> (END)
69
         *
70
         * @var int
71
         */
72 18
        $state = 0;
73
74 18
        for (; $list->idx < $list->count; ++$list->idx) {
75
            /**
76
             * Token parsed at this moment.
77
             */
78 18
            $token = $list->tokens[$list->idx];
79
80
            // End of statement.
81 18
            if ($token->type === Token::TYPE_DELIMITER) {
82 14
                break;
83
            }
84
85
            // Skipping whitespaces and comments.
86 18
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
87 14
                continue;
88
            }
89
90 18
            if ($state === 0) {
91 18
                $expr->old = Expression::parse(
92 18
                    $parser,
93 18
                    $list,
94 18
                    [
95 18
                        'breakOnAlias' => true,
96 18
                        'parseField' => 'table',
97 18
                    ]
98 18
                );
99 18
                if (empty($expr->old)) {
100 2
                    $parser->error('The old name of the table was expected.', $token);
101
                }
102
103 18
                $state = 1;
104 16
            } elseif ($state === 1) {
105 16
                if ($token->type !== Token::TYPE_KEYWORD || $token->keyword !== 'TO') {
106 2
                    $parser->error('Keyword "TO" was expected.', $token);
107 2
                    break;
108
                }
109
110 14
                $state = 2;
111 14
            } elseif ($state === 2) {
112 14
                $expr->new = Expression::parse(
113 14
                    $parser,
114 14
                    $list,
115 14
                    [
116 14
                        'breakOnAlias' => true,
117 14
                        'parseField' => 'table',
118 14
                    ]
119 14
                );
120 14
                if (empty($expr->new)) {
121 2
                    $parser->error('The new name of the table was expected.', $token);
122
                }
123
124 14
                $state = 3;
125 10
            } elseif ($state === 3) {
126 10
                if (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== ',')) {
127 2
                    break;
128
                }
129
130 8
                $ret[] = $expr;
131 8
                $expr = new static();
132 8
                $state = 0;
133
            }
134
        }
135
136 18
        if ($state !== 3) {
0 ignored issues
show
introduced by
The condition $state !== 3 is always true.
Loading history...
137 4
            $parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]);
138
        }
139
140
        // Last iteration was not saved.
141 18
        if (! empty($expr->old)) {
142 16
            $ret[] = $expr;
143
        }
144
145 18
        --$list->idx;
146
147 18
        return $ret;
148
    }
149
150
    /**
151
     * @param RenameOperation $component the component to be built
152
     */
153 4
    public static function build($component): string
154
    {
155 4
        return $component->old . ' TO ' . $component->new;
156
    }
157
158
    /**
159
     * @param RenameOperation[] $component the component to be built
160
     */
161 4
    public static function buildAll(array $component): string
162
    {
163 4
        return implode(', ', $component);
164
    }
165
166 4
    public function __toString(): string
167
    {
168 4
        return static::build($this);
169
    }
170
}
171