Test Failed
Pull Request — master (#291)
by William
12:25
created

RenameOperation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * `RENAME TABLE` keyword parser.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Components;
8
9
use PhpMyAdmin\SqlParser\Component;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
14
/**
15
 * `RENAME TABLE` keyword parser.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class RenameOperation extends Component
22
{
23
    /**
24
     * The old table name.
25
     *
26
     * @var Expression
27
     */
28
    public $old;
29
30
    /**
31
     * The new table name.
32
     *
33
     * @var Expression
34
     */
35
    public $new;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param Expression $old old expression
0 ignored issues
show
Documentation introduced by
Should the type for parameter $old not be Expression|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     * @param Expression $new new expression containing new name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $new not be Expression|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
     */
43 8
    public function __construct($old = null, $new = null)
44
    {
45 8
        $this->old = $old;
46 8
        $this->new = $new;
47 8
    }
48
49
    /**
50
     * @param Parser     $parser  the parser that serves as context
51
     * @param TokensList $list    the list of tokens that are being parsed
52
     * @param array      $options parameters for parsing
53
     *
54
     * @return RenameOperation[]
55
     */
56 8
    public static function parse(Parser $parser, TokensList $list, array $options = array())
57
    {
58 8
        $ret = array();
59
60 8
        $expr = new self();
61
62
        /**
63
         * The state of the parser.
64
         *
65
         * Below are the states of the parser.
66
         *
67
         *      0 ---------------------[ old name ]--------------------> 1
68
         *
69
         *      1 ------------------------[ TO ]-----------------------> 2
70
         *
71
         *      2 ---------------------[ new name ]--------------------> 3
72
         *
73
         *      3 ------------------------[ , ]------------------------> 0
74
         *      3 -----------------------[ else ]----------------------> (END)
75
         *
76
         * @var int
77
         */
78 8
        $state = 0;
79
80 8
        for (; $list->idx < $list->count; ++$list->idx) {
81
            /**
82
             * Token parsed at this moment.
83
             *
84
             * @var Token
85
             */
86 8
            $token = $list->tokens[$list->idx];
87
88
            // End of statement.
89 8
            if ($token->type === Token::TYPE_DELIMITER) {
90 7
                break;
91
            }
92
93
            // Skipping whitespaces and comments.
94 8
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
95 7
                continue;
96
            }
97
98 8
            if ($state === 0) {
99 8
                $expr->old = Expression::parse(
100 8
                    $parser,
101 8
                    $list,
102
                    array(
103 8
                        'breakOnAlias' => true,
104
                        'parseField' => 'table'
105
                    )
106
                );
107 8
                if (empty($expr->old)) {
108 1
                    $parser->error(
109 1
                        'The old name of the table was expected.',
110
                        $token
111
                    );
112
                }
113 8
                $state = 1;
114 7
            } elseif ($state === 1) {
115 7
                if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'TO') {
116 7
                    $state = 2;
117
                } else {
118
                    $parser->error(
119
                        'Keyword "TO" was expected.',
120
                        $token
121
                    );
122 7
                    break;
123
                }
124 7
            } elseif ($state === 2) {
125 7
                $expr->new = Expression::parse(
126 7
                    $parser,
127 7
                    $list,
128
                    array(
129 7
                        'breakOnAlias' => true,
130
                        'parseField' => 'table'
131
                    )
132
                );
133 7
                if (empty($expr->new)) {
134 1
                    $parser->error(
135 1
                        'The new name of the table was expected.',
136
                        $token
137
                    );
138
                }
139 7
                $state = 3;
140 5
            } elseif ($state === 3) {
141 5
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
142 4
                    $ret[] = $expr;
143 4
                    $expr = new self();
144 4
                    $state = 0;
145
                } else {
146 1
                    break;
147
                }
148
            }
149
        }
150
151 8
        if ($state !== 3) {
152 1
            $parser->error(
153 1
                'A rename operation was expected.',
154 1
                $list->tokens[$list->idx - 1]
155
            );
156
        }
157
158
        // Last iteration was not saved.
159 8
        if (! empty($expr->old)) {
160 7
            $ret[] = $expr;
161
        }
162
163 8
        --$list->idx;
164
165 8
        return $ret;
166
    }
167
168
    /**
169
     * @param RenameOperation $component the component to be built
170
     * @param array           $options   parameters for building
171
     *
172
     * @return string
173
     */
174 2 View Code Duplication
    public static function build($component, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176 2
        if (is_array($component)) {
177 2
            return implode(', ', $component);
178
        }
179
180 2
        return $component->old . ' TO ' . $component->new;
181
    }
182
}
183