Completed
Push — master ( 65f66e...428edc )
by Michal
04:14
created

InsertStatement   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 208
Duplicated Lines 24.52 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 51
loc 208
ccs 91
cts 91
cp 1
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 7 19 9
D parse() 44 128 25

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * `INSERT` statement.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Statements;
8
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\Token;
11
use PhpMyAdmin\SqlParser\TokensList;
12
use PhpMyAdmin\SqlParser\Statement;
13
use PhpMyAdmin\SqlParser\Components\IntoKeyword;
14
use PhpMyAdmin\SqlParser\Components\Array2d;
15
use PhpMyAdmin\SqlParser\Components\OptionsArray;
16
use PhpMyAdmin\SqlParser\Components\SetOperation;
17
18
/**
19
 * `INSERT` statement.
20
 *
21
 * INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
22
 *     [INTO] tbl_name
23
 *     [PARTITION (partition_name,...)]
24
 *     [(col_name,...)]
25
 *     {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
26
 *     [ ON DUPLICATE KEY UPDATE
27
 *       col_name=expr
28
 *         [, col_name=expr] ... ]
29
 *
30
 * or
31
 *
32
 * INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
33
 *     [INTO] tbl_name
34
 *     [PARTITION (partition_name,...)]
35
 *     SET col_name={expr | DEFAULT}, ...
36
 *     [ ON DUPLICATE KEY UPDATE
37
 *       col_name=expr
38
 *         [, col_name=expr] ... ]
39
 *
40
 * or
41
 *
42
 * INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
43
 *     [INTO] tbl_name
44
 *     [PARTITION (partition_name,...)]
45
 *     [(col_name,...)]
46
 *     SELECT ...
47
 *     [ ON DUPLICATE KEY UPDATE
48
 *       col_name=expr
49
 *         [, col_name=expr] ... ]
50
 *
51
 * @category   Statements
52
 *
53
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
54
 */
55
class InsertStatement extends Statement
56
{
57
    /**
58
     * Options for `INSERT` statements.
59
     *
60
     * @var array
61
     */
62
    public static $OPTIONS = array(
63
        'LOW_PRIORITY' => 1,
64
        'DELAYED' => 2,
65
        'HIGH_PRIORITY' => 3,
66
        'IGNORE' => 4,
67
    );
68
69
    /**
70
     * Tables used as target for this statement.
71
     *
72
     * @var IntoKeyword
73
     */
74
    public $into;
75
76
    /**
77
     * Values to be inserted.
78
     *
79
     * @var ArrayObj[]|null
80
     */
81
    public $values;
82
83
    /**
84
     * If SET clause is present
85
     * holds the SetOperation.
86
     *
87
     * @var SetOperation[]
88
     */
89
    public $set;
90
91
    /**
92
     * If SELECT clause is present
93
     * holds the SelectStatement.
94
     *
95
     * @var SelectStatement
96
     */
97
    public $select;
98
99
    /**
100
     * If ON DUPLICATE KEY UPDATE clause is present
101
     * holds the SetOperation.
102
     *
103
     * @var SetOperation[]
104
     */
105
    public $onDuplicateSet;
106
107
    /**
108
     * @return string
109
     */
110 1
    public function build()
111
    {
112 1
        $ret = 'INSERT ' . $this->options
113 1
            . ' INTO ' . $this->into;
114
115 1 View Code Duplication
        if ($this->values != null && count($this->values) > 0) {
116 1
            $ret .= ' VALUES ' . Array2d::build($this->values);
117 1
        } elseif ($this->set != null && count($this->set) > 0) {
118 1
            $ret .= ' SET ' . SetOperation::build($this->set);
119 1
        } elseif ($this->select != null && strlen($this->select) > 0) {
120 1
            $ret .= ' ' . $this->select->build();
121 1
        }
122
123 1
        if ($this->onDuplicateSet != null && count($this->onDuplicateSet) > 0) {
124 1
            $ret .= ' ON DUPLICATE KEY UPDATE ' . SetOperation::build($this->onDuplicateSet);
125 1
        }
126
127 1
        return $ret;
128
    }
129
130
    /**
131
     * @param Parser     $parser the instance that requests parsing
132
     * @param TokensList $list   the list of tokens to be parsed
133
     */
134 14
    public function parse(Parser $parser, TokensList $list)
135
    {
136 14
        ++$list->idx; // Skipping `INSERT`.
137
138
        // parse any options if provided
139 14
        $this->options = OptionsArray::parse(
140 14
            $parser,
141 14
            $list,
142
            static::$OPTIONS
143 14
        );
144 14
        ++$list->idx;
145
146
        /**
147
         * The state of the parser.
148
         *
149
         * Below are the states of the parser.
150
         *
151
         *      0 ---------------------------------[ INTO ]----------------------------------> 1
152
         *
153
         *      1 -------------------------[ VALUES/VALUE/SET/SELECT ]-----------------------> 2
154
         *
155
         *      2 -------------------------[ ON DUPLICATE KEY UPDATE ]-----------------------> 3
156
         *
157
         * @var int
158
         */
159 14
        $state = 0;
160
161
        /**
162
         * For keeping track of semi-states on encountering
163
         * ON DUPLICATE KEY UPDATE ...
164
         */
165 14
        $miniState = 0;
166
167 14
        for (; $list->idx < $list->count; ++$list->idx) {
168
            /**
169
             * Token parsed at this moment.
170
             *
171
             * @var Token
172
             */
173 14
            $token = $list->tokens[$list->idx];
174
175
            // End of statement.
176 14
            if ($token->type === Token::TYPE_DELIMITER) {
177 11
                break;
178
            }
179
180
            // Skipping whitespaces and comments.
181 13
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
182 9
                continue;
183
            }
184
185 13
            if ($state === 0) {
186 13 View Code Duplication
                if ($token->type === Token::TYPE_KEYWORD
187 13
                    && $token->value !== 'INTO'
188 13
                ) {
189 1
                    $parser->error(__('Unexpected keyword.'), $token);
190 1
                    break;
191
                } else {
192 12
                    ++$list->idx;
193 12
                    $this->into = IntoKeyword::parse(
194 12
                        $parser,
195 12
                        $list,
196 12
                        array('fromInsert' => true)
197 12
                    );
198
                }
199
200 12
                $state = 1;
201 12 View Code Duplication
            } elseif ($state === 1) {
202 12
                if ($token->type === Token::TYPE_KEYWORD) {
203 11
                    if ($token->value === 'VALUE'
204 11
                        || $token->value === 'VALUES'
205 11
                    ) {
206 7
                        ++$list->idx; // skip VALUES
207
208 7
                        $this->values = Array2d::parse($parser, $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like \PhpMyAdmin\SqlParser\Co...::parse($parser, $list) of type array<integer,object<Php...r\Components\ArrayObj>> is incompatible with the declared type array<integer,object<Php...tements\ArrayObj>>|null of property $values.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
209 11
                    } elseif ($token->value === 'SET') {
210 2
                        ++$list->idx; // skip SET
211
212 2
                        $this->set = SetOperation::parse($parser, $list);
213 5
                    } elseif ($token->value === 'SELECT') {
214 3
                        $this->select = new SelectStatement($parser, $list);
215 3
                    } else {
216 1
                        $parser->error(
217 1
                            __('Unexpected keyword.'),
218
                            $token
219 1
                        );
220 1
                        break;
221
                    }
222 10
                    $state = 2;
223 10
                    $miniState = 1;
224 10
                } else {
225 1
                    $parser->error(
226 1
                        __('Unexpected token.'),
227
                        $token
228 1
                    );
229 1
                    break;
230
                }
231 10
            } elseif ($state == 2) {
232 5
                $lastCount = $miniState;
233
234 5
                if ($miniState === 1 && $token->value === 'ON') {
235 5
                    ++$miniState;
236 5
                } elseif ($miniState === 2 && $token->value === 'DUPLICATE') {
237 5
                    ++$miniState;
238 5
                } elseif ($miniState === 3 && $token->value === 'KEY') {
239 5
                    ++$miniState;
240 5
                } elseif ($miniState === 4 && $token->value === 'UPDATE') {
241 4
                    ++$miniState;
242 4
                }
243
244 5
                if ($lastCount === $miniState) {
245 1
                    $parser->error(
246 1
                        __('Unexpected token.'),
247
                        $token
248 1
                    );
249 1
                    break;
250
                }
251
252 5
                if ($miniState === 5) {
253 4
                    ++$list->idx;
254 4
                    $this->onDuplicateSet = SetOperation::parse($parser, $list);
255 4
                    $state = 3;
256 4
                }
257 5
            }
258 12
        }
259
260 14
        --$list->idx;
261 14
    }
262
}
263