Passed
Push — master ( 7c26e5...2f0566 )
by William
03:26
created

AlterOperation   F

Complexity

Total Complexity 64

Size/Duplication

Total Lines 548
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 254
c 5
b 1
f 0
dl 0
loc 548
rs 3.28
ccs 143
cts 143
cp 1
wmc 64

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A checkIfColumnDefinitionKeyword() 0 17 1
F parse() 0 214 54
A checkIfTokenQuotedSymbol() 0 3 2
A build() 0 23 6

How to fix   Complexity   

Complex Class

Complex classes like AlterOperation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AlterOperation, and based on these observations, apply Extract Interface, too.

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 array_key_exists;
13
use function in_array;
14
use function is_numeric;
15
use function is_string;
16
use function trim;
17
18
/**
19
 * Parses an alter operation.
20
 *
21
 * @final
22
 */
23
class AlterOperation extends Component
24
{
25
    /**
26
     * All database options.
27
     *
28
     * @var array<string, int|array<int, int|string>>
29
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
30
     */
31
    public static $DB_OPTIONS = [
32
        'CHARACTER SET' => [
33
            1,
34
            'var',
35
        ],
36
        'CHARSET' => [
37
            1,
38
            'var',
39
        ],
40
        'DEFAULT CHARACTER SET' => [
41
            1,
42
            'var',
43
        ],
44
        'DEFAULT CHARSET' => [
45
            1,
46
            'var',
47
        ],
48
        'UPGRADE' => [
49
            1,
50
            'var',
51
        ],
52
        'COLLATE' => [
53
            2,
54
            'var',
55
        ],
56
        'DEFAULT COLLATE' => [
57
            2,
58
            'var',
59
        ],
60
    ];
61
62
    /**
63
     * All table options.
64
     *
65
     * @var array<string, int|array<int, int|string>>
66
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
67
     */
68
    public static $TABLE_OPTIONS = [
69
        'ENGINE' => [
70
            1,
71
            'var=',
72
        ],
73
        'AUTO_INCREMENT' => [
74
            1,
75
            'var=',
76
        ],
77
        'AVG_ROW_LENGTH' => [
78
            1,
79
            'var',
80
        ],
81
        'MAX_ROWS' => [
82
            1,
83
            'var',
84
        ],
85
        'ROW_FORMAT' => [
86
            1,
87
            'var',
88
        ],
89
        'COMMENT' => [
90
            1,
91
            'var',
92
        ],
93
        'ADD' => 1,
94
        'ALTER' => 1,
95
        'ANALYZE' => 1,
96
        'CHANGE' => 1,
97
        'CHARSET' => 1,
98
        'CHECK' => 1,
99
        'COALESCE' => 1,
100
        'CONVERT' => 1,
101
        'DEFAULT CHARSET' => 1,
102
        'DISABLE' => 1,
103
        'DISCARD' => 1,
104
        'DROP' => 1,
105
        'ENABLE' => 1,
106
        'IMPORT' => 1,
107
        'MODIFY' => 1,
108
        'OPTIMIZE' => 1,
109
        'ORDER' => 1,
110
        'REBUILD' => 1,
111
        'REMOVE' => 1,
112
        'RENAME' => 1,
113
        'REORGANIZE' => 1,
114
        'REPAIR' => 1,
115
        'UPGRADE' => 1,
116
117
        'COLUMN' => 2,
118
        'CONSTRAINT' => 2,
119
        'DEFAULT' => 2,
120
        'BY' => 2,
121
        'FOREIGN' => 2,
122
        'FULLTEXT' => 2,
123
        'KEY' => 2,
124
        'KEYS' => 2,
125
        'PARTITION' => 2,
126
        'PARTITION BY' => 2,
127
        'PARTITIONING' => 2,
128
        'PRIMARY KEY' => 2,
129
        'SPATIAL' => 2,
130
        'TABLESPACE' => 2,
131
        'INDEX' => [
132
            2,
133
            'var',
134
        ],
135
136
        'CHARACTER SET' => 3,
137
        'TO' => [
138
            3,
139
            'var',
140
        ],
141
    ];
142
143
    /**
144
     * All user options.
145
     *
146
     * @var array<string, int|array<int, int|string>>
147
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
148
     */
149
    public static $USER_OPTIONS = [
150
        'ATTRIBUTE' => [
151
            1,
152
            'var',
153
        ],
154
        'COMMENT' => [
155
            1,
156
            'var',
157
        ],
158
        'REQUIRE' => [
159
            1,
160
            'var',
161
        ],
162
        'BY' => [
163
            2,
164
            'expr',
165
        ],
166
        'PASSWORD' => [
167
            2,
168
            'var',
169
        ],
170
        'WITH' => [
171
            2,
172
            'var',
173
        ],
174
175
        'ACCOUNT' => 1,
176
        'DEFAULT' => 1,
177
178
        'LOCK' => 2,
179
        'UNLOCK' => 2,
180
181
        'IDENTIFIED' => 3,
182
    ];
183
184
    /**
185
     * All view options.
186
     *
187
     * @var array<string, int|array<int, int|string>>
188
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
189
     */
190
    public static $VIEW_OPTIONS = ['AS' => 1];
191
192
    /**
193
     * All event options.
194
     *
195
     * @var array<string, int|array<int, int|string>>
196
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
197
     */
198
    public static $EVENT_OPTIONS = [
199
        'ON SCHEDULE' => 1,
200
        'EVERY' => [
201
            2,
202
            'expr',
203
        ],
204
        'AT' => [
205
            2,
206
            'expr',
207
        ],
208
        'STARTS' => [
209
            3,
210
            'expr',
211
        ],
212
        'ENDS' => [
213
            4,
214
            'expr',
215
        ],
216
        'ON COMPLETION PRESERVE' => 5,
217
        'ON COMPLETION NOT PRESERVE' => 5,
218
        'RENAME' => 6,
219
        'TO' => [
220
            7,
221
            'expr',
222
            ['parseField' => 'table'],
223
        ],
224
        'ENABLE' => 8,
225
        'DISABLE' => 8,
226
        'DISABLE ON SLAVE' => 8,
227
        'COMMENT' => [
228
            9,
229
            'var',
230
        ],
231
        'DO' => 10,
232
    ];
233
234
    /**
235
     * Options of this operation.
236
     *
237
     * @var OptionsArray
238
     */
239
    public $options;
240
241
    /**
242
     * The altered field.
243
     *
244
     * @var Expression|string|null
245
     */
246
    public $field;
247
248
    /**
249
     * The partitions.
250
     *
251
     * @var Component[]|ArrayObj|null
252
     */
253
    public $partitions;
254
255
    /**
256
     * Unparsed tokens.
257
     *
258
     * @var Token[]|string
259
     */
260
    public $unknown = [];
261
262
    /**
263
     * @param OptionsArray              $options    options of alter operation
264
     * @param Expression|string|null    $field      altered field
265
     * @param Component[]|ArrayObj|null $partitions partitions definition found in the operation
266
     * @param Token[]                   $unknown    unparsed tokens found at the end of operation
267
     */
268 154
    public function __construct(
269
        $options = null,
270
        $field = null,
271
        $partitions = null,
272
        $unknown = []
273
    ) {
274 154
        $this->partitions = $partitions;
275 154
        $this->options = $options;
276 154
        $this->field = $field;
277 154
        $this->unknown = $unknown;
278
    }
279
280
    /**
281
     * @param Parser               $parser  the parser that serves as context
282
     * @param TokensList           $list    the list of tokens that are being parsed
283
     * @param array<string, mixed> $options parameters for parsing
284
     *
285
     * @return AlterOperation
286
     */
287 154
    public static function parse(Parser $parser, TokensList $list, array $options = [])
288
    {
289 154
        $ret = new static();
290
291
        /**
292
         * Counts brackets.
293
         *
294
         * @var int
295
         */
296 154
        $brackets = 0;
297
298
        /**
299
         * The state of the parser.
300
         *
301
         * Below are the states of the parser.
302
         *
303
         *      0 ---------------------[ options ]---------------------> 1
304
         *
305
         *      1 ----------------------[ field ]----------------------> 2
306
         *
307
         *      1 -------------[ PARTITION / PARTITION BY ]------------> 3
308
         *
309
         *      2 -------------------------[ , ]-----------------------> 0
310
         *
311
         * @var int
312
         */
313 154
        $state = 0;
314
315
        /**
316
         * partition state.
317
         *
318
         * @var int
319
         */
320 154
        $partitionState = 0;
321
322 154
        for (; $list->idx < $list->count; ++$list->idx) {
323
            /**
324
             * Token parsed at this moment.
325
             */
326 154
            $token = $list->tokens[$list->idx];
327
328
            // End of statement.
329 154
            if ($token->type === Token::TYPE_DELIMITER) {
330 138
                break;
331
            }
332
333
            // Skipping comments.
334 154
            if ($token->type === Token::TYPE_COMMENT) {
335 2
                continue;
336
            }
337
338
            // Skipping whitespaces.
339 154
            if ($token->type === Token::TYPE_WHITESPACE) {
340 52
                if ($state === 2) {
341
                    // When parsing the unknown part, the whitespaces are
342
                    // included to not break anything.
343 44
                    $ret->unknown[] = $token;
344 44
                    continue;
345
                }
346
            }
347
348 154
            if ($state === 0) {
349 154
                $ret->options = OptionsArray::parse($parser, $list, $options);
350
351
                // Not only when aliasing but also when parsing the body of an event, we just list the tokens of the
352
                // body in the unknown tokens list, as they define their own statements.
353 154
                if ($ret->options->has('AS') || $ret->options->has('DO')) {
354 6
                    for (; $list->idx < $list->count; ++$list->idx) {
355 6
                        if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
356 6
                            break;
357
                        }
358
359 6
                        $ret->unknown[] = $list->tokens[$list->idx];
360
                    }
361
362 6
                    break;
363
                }
364
365 148
                $state = 1;
366 148
                if ($ret->options->has('PARTITION') || $token->value === 'PARTITION BY') {
367 8
                    $state = 3;
368 148
                    $list->getPrevious(); // in order to check whether it's partition or partition by.
369
                }
370 92
            } elseif ($state === 1) {
371 84
                $ret->field = Expression::parse(
372 84
                    $parser,
373 84
                    $list,
374 84
                    [
375 84
                        'breakOnAlias' => true,
376 84
                        'parseField' => 'column',
377 84
                    ]
378 84
                );
379 84
                if ($ret->field === null) {
380
                    // No field was read. We go back one token so the next
381
                    // iteration will parse the same token, but in state 2.
382 28
                    --$list->idx;
383
                }
384
385
                // If the operation is a RENAME COLUMN, now we have detected the field to rename, we need to parse
386
                // again the options to get the new name of the column.
387 84
                if ($ret->options->has('RENAME') && $ret->options->has('COLUMN')) {
388 12
                    $nextOptions = OptionsArray::parse($parser, $list, $options);
389 12
                    $ret->options->merge($nextOptions);
390
                }
391
392 84
                $state = 2;
393 78
            } elseif ($state === 2) {
394 70
                if (is_string($token->value) || is_numeric($token->value)) {
395 70
                    $arrayKey = $token->value;
396
                } else {
397 2
                    $arrayKey = $token->token;
398
                }
399
400 70
                if ($token->type === Token::TYPE_OPERATOR) {
401 44
                    if ($token->value === '(') {
402 28
                        ++$brackets;
403 44
                    } elseif ($token->value === ')') {
404 28
                        --$brackets;
405 30
                    } elseif (($token->value === ',') && ($brackets === 0)) {
406 44
                        break;
407
                    }
408 62
                } elseif (! self::checkIfTokenQuotedSymbol($token)) {
409 54
                    if (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
410 12
                        $list->idx++; // Ignore the current token
411 12
                        $nextToken = $list->getNext();
412
413 12
                        if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') {
414
                            // To avoid adding the tokens between the SET() parentheses to the unknown tokens
415 4
                            $list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
416 8
                        } elseif ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') {
417
                            // to avoid adding the `DEFAULT` token to the unknown tokens.
418 4
                            ++$list->idx;
419
                        } else {
420
                            // We have reached the end of ALTER operation and suddenly found
421
                            // a start to new statement, but have not find a delimiter between them
422 4
                            $parser->error(
423 4
                                'A new statement was found, but no delimiter between it and the previous one.',
424 4
                                $token
425 4
                            );
426 12
                            break;
427
                        }
428
                    } elseif (
429 48
                        (array_key_exists($arrayKey, self::$DB_OPTIONS)
430 48
                        || array_key_exists($arrayKey, self::$TABLE_OPTIONS))
431 48
                        && ! self::checkIfColumnDefinitionKeyword($arrayKey)
432
                    ) {
433
                        // This alter operation has finished, which means a comma
434
                        // was missing before start of new alter operation
435 4
                        $parser->error('Missing comma before start of a new alter operation.', $token);
436 4
                        break;
437
                    }
438
                }
439
440 60
                $ret->unknown[] = $token;
441 8
            } elseif ($state === 3) {
442 8
                if ($partitionState === 0) {
443 8
                    $list->idx++; // Ignore the current token
444 8
                    $nextToken = $list->getNext();
445
                    if (
446 8
                        ($token->type === Token::TYPE_KEYWORD)
447 8
                        && (($token->keyword === 'PARTITION BY')
448 8
                        || ($token->keyword === 'PARTITION' && $nextToken && $nextToken->value !== '('))
449
                    ) {
450 8
                        $partitionState = 1;
451 2
                    } elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION')) {
452 2
                        $partitionState = 2;
453
                    }
454
455 8
                    --$list->idx; // to decrease the idx by one, because the last getNext returned and increased it.
456
457
                    // reverting the effect of the getNext
458 8
                    $list->getPrevious();
459 8
                    $list->getPrevious();
460
461 8
                    ++$list->idx; // to index the idx by one, because the last getPrevious returned and decreased it.
462 8
                } elseif ($partitionState === 1) {
463
                    // Fetch the next token in a way the current index is reset to manage whitespaces in "field".
464 8
                    $currIdx = $list->idx;
465 8
                    ++$list->idx;
466 8
                    $nextToken = $list->getNext();
467 8
                    $list->idx = $currIdx;
468
                    // Building the expression used for partitioning.
469 8
                    if (empty($ret->field)) {
470 8
                        $ret->field = '';
471
                    }
472
473
                    if (
474 8
                        $token->type === Token::TYPE_OPERATOR
475 8
                        && $token->value === '('
476
                        && $nextToken
477 8
                        && $nextToken->keyword === 'PARTITION'
478
                    ) {
479 6
                        $partitionState = 2;
480 6
                        --$list->idx; // Current idx is on "(". We need a step back for ArrayObj::parse incoming.
481
                    } else {
482 8
                        $ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
483
                    }
484 6
                } elseif ($partitionState === 2) {
485 6
                    $ret->partitions = ArrayObj::parse(
486 6
                        $parser,
487 6
                        $list,
488 6
                        ['type' => PartitionDefinition::class]
489 6
                    );
490
                }
491
            }
492
        }
493
494 154
        if ($ret->options->isEmpty()) {
495 2
            $parser->error('Unrecognized alter operation.', $list->tokens[$list->idx]);
496
        }
497
498 154
        --$list->idx;
499
500 154
        return $ret;
501
    }
502
503
    /**
504
     * @param AlterOperation       $component the component to be built
505
     * @param array<string, mixed> $options   parameters for building
506
     *
507
     * @return string
508
     */
509 22
    public static function build($component, array $options = [])
510
    {
511
        // Specific case of RENAME COLUMN that insert the field between 2 options.
512 22
        $afterFieldsOptions = new OptionsArray();
513 22
        if ($component->options->has('RENAME') && $component->options->has('COLUMN')) {
514 8
            $afterFieldsOptions = clone $component->options;
515 8
            $afterFieldsOptions->remove('RENAME');
516 8
            $afterFieldsOptions->remove('COLUMN');
517 8
            $component->options->remove('TO');
518
        }
519
520 22
        $ret = $component->options . ' ';
521 22
        if (isset($component->field) && ($component->field !== '')) {
522 16
            $ret .= $component->field . ' ';
523
        }
524
525 22
        $ret .= $afterFieldsOptions . TokensList::build($component->unknown);
526
527 22
        if (isset($component->partitions)) {
528 2
            $ret .= PartitionDefinition::build($component->partitions);
0 ignored issues
show
Bug introduced by
It seems like $component->partitions can also be of type PhpMyAdmin\SqlParser\Components\ArrayObj; however, parameter $component of PhpMyAdmin\SqlParser\Com...tionDefinition::build() does only seem to accept PhpMyAdmin\SqlParser\Com...nts\PartitionDefinition, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

528
            $ret .= PartitionDefinition::build(/** @scrutinizer ignore-type */ $component->partitions);
Loading history...
529
        }
530
531 22
        return trim($ret);
532
    }
533
534
    /**
535
     * Check if token's value is one of the common keywords
536
     * between column and table alteration
537
     *
538
     * @param string $tokenValue Value of current token
539
     *
540
     * @return bool
541
     */
542 28
    private static function checkIfColumnDefinitionKeyword($tokenValue)
543
    {
544 28
        $commonOptions = [
545 28
            'AUTO_INCREMENT',
546 28
            'COMMENT',
547 28
            'DEFAULT',
548 28
            'CHARACTER SET',
549 28
            'COLLATE',
550 28
            'PRIMARY',
551 28
            'UNIQUE',
552 28
            'PRIMARY KEY',
553 28
            'UNIQUE KEY',
554 28
        ];
555
556
        // Since these options can be used for
557
        // both table as well as a specific column in the table
558 28
        return in_array($tokenValue, $commonOptions);
559
    }
560
561
    /**
562
     * Check if token is symbol and quoted with backtick
563
     *
564
     * @param Token $token token to check
565
     *
566
     * @return bool
567
     */
568 62
    private static function checkIfTokenQuotedSymbol($token)
569
    {
570 62
        return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
571
    }
572
}
573