Passed
Push — master ( c70841...3cb6c0 )
by William
02:43 queued 11s
created

AlterOperation::parse()   D

Complexity

Conditions 33
Paths 20

Size

Total Lines 157
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 33.0034

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 33
eloc 71
c 2
b 0
f 0
nc 20
nop 3
dl 0
loc 157
ccs 67
cts 68
cp 0.9853
crap 33.0034
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
introduced by
An error occurred during processing; checking has been aborted. The error message was: "else" without curly braces is not supported.
Loading history...
2
/**
3
 * Parses an alter operation.
4
 */
5
6
declare(strict_types=1);
7
8
namespace PhpMyAdmin\SqlParser\Components;
9
10
use PhpMyAdmin\SqlParser\Component;
11
use PhpMyAdmin\SqlParser\Parser;
12
use PhpMyAdmin\SqlParser\Token;
13
use PhpMyAdmin\SqlParser\TokensList;
14
15
use function array_key_exists;
16
use function in_array;
17
use function is_numeric;
18
use function is_string;
19
20
/**
21
 * Parses an alter operation.
22
 */
23
class AlterOperation extends Component
24
{
25
    /**
26
     * All database options.
27
     *
28
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \PhpMyAdmin\SqlParser\Components\AlterOperation::$DB_OPTIONS does not specify type hint for its items.
Loading history...
29
     */
30
    public static $DB_OPTIONS = [
0 ignored issues
show
Coding Style introduced by
Member variable "DB_OPTIONS" is not in valid camel caps format
Loading history...
31
        'CHARACTER SET' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
32
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 28 spaces, but found 12.
Loading history...
33
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 28 spaces, but found 12.
Loading history...
34
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 27 space(s), but found 8.
Loading history...
35
        'CHARSET' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 15 space(s) but found 1
Loading history...
36
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
37
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
38
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 21 space(s), but found 8.
Loading history...
39
        'DEFAULT CHARACTER SET' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
40
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 36 spaces, but found 12.
Loading history...
41
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 36 spaces, but found 12.
Loading history...
42
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 35 space(s), but found 8.
Loading history...
43
        'DEFAULT CHARSET' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 1
Loading history...
44
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 30 spaces, but found 12.
Loading history...
45
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 30 spaces, but found 12.
Loading history...
46
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 29 space(s), but found 8.
Loading history...
47
        'UPGRADE' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 15 space(s) but found 1
Loading history...
48
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
49
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
50
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 21 space(s), but found 8.
Loading history...
51
        'COLLATE' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 15 space(s) but found 1
Loading history...
52
            2,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
53
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
54
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 21 space(s), but found 8.
Loading history...
55
        'DEFAULT COLLATE' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 1
Loading history...
56
            2,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 30 spaces, but found 12.
Loading history...
57
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 30 spaces, but found 12.
Loading history...
58
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 29 space(s), but found 8.
Loading history...
59
    ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 32 space(s), but found 4.
Loading history...
60
61
    /**
62
     * All table options.
63
     *
64
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \PhpMyAdmin\SqlParser\Components\AlterOperation::$TABLE_OPTIONS does not specify type hint for its items.
Loading history...
65
     */
66
    public static $TABLE_OPTIONS = [
0 ignored issues
show
Coding Style introduced by
Member variable "TABLE_OPTIONS" is not in valid camel caps format
Loading history...
67
        'ENGINE' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
68
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 21 spaces, but found 12.
Loading history...
69
            'var=',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 21 spaces, but found 12.
Loading history...
70
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 20 space(s), but found 8.
Loading history...
71
        'AUTO_INCREMENT' => [
0 ignored issues
show
Coding Style introduced by
Array double arrow not aligned correctly; expected 2 space(s) but found 1
Loading history...
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
72
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 29 spaces, but found 12.
Loading history...
73
            'var=',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 29 spaces, but found 12.
Loading history...
74
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 28 space(s), but found 8.
Loading history...
75
        'AVG_ROW_LENGTH' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 2 space(s) but found 1
Loading history...
76
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 29 spaces, but found 12.
Loading history...
77
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 29 spaces, but found 12.
Loading history...
78
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 28 space(s), but found 8.
Loading history...
79
        'MAX_ROWS' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 8 space(s) but found 1
Loading history...
80
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 23 spaces, but found 12.
Loading history...
81
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 23 spaces, but found 12.
Loading history...
82
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
83
        'ROW_FORMAT' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 6 space(s) but found 1
Loading history...
84
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 25 spaces, but found 12.
Loading history...
85
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 25 spaces, but found 12.
Loading history...
86
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 24 space(s), but found 8.
Loading history...
87
        'COMMENT' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
88
            1,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
89
            'var',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 22 spaces, but found 12.
Loading history...
90
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 21 space(s), but found 8.
Loading history...
91
        'ADD' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 13 space(s) but found 1
Loading history...
92
        'ALTER' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 11 space(s) but found 1
Loading history...
93
        'ANALYZE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
94
        'CHANGE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
95
        'CHARSET' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
96
        'CHECK' => 1,
0 ignored issues
show
Coding Style introduced by
Array double arrow not aligned correctly; expected 11 space(s) but found 1
Loading history...
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
97
        'COALESCE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 8 space(s) but found 1
Loading history...
98
        'CONVERT' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
99
        'DEFAULT CHARSET' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
100
        'DISABLE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
101
        'DISCARD' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
102
        'DROP' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 12 space(s) but found 1
Loading history...
103
        'ENABLE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
104
        'IMPORT' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
105
        'MODIFY' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
106
        'OPTIMIZE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 8 space(s) but found 1
Loading history...
107
        'ORDER' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 11 space(s) but found 1
Loading history...
108
        'PARTITION' => 1,
0 ignored issues
show
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 1
Loading history...
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
109
        'REBUILD' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
110
        'REMOVE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
111
        'RENAME' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
112
        'REORGANIZE' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 6 space(s) but found 1
Loading history...
113
        'REPAIR' => 1,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
114
        'UPGRADE' => 1,
0 ignored issues
show
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
115
116
        'COLUMN' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 10 space(s) but found 1
Loading history...
117
        'CONSTRAINT' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 6 space(s) but found 1
Loading history...
118
        'DEFAULT' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
119
        'TO' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 14 space(s) but found 1
Loading history...
120
        'BY' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 14 space(s) but found 1
Loading history...
121
        'FOREIGN' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
122
        'FULLTEXT' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 8 space(s) but found 1
Loading history...
123
        'KEY' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 13 space(s) but found 1
Loading history...
124
        'KEYS' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 12 space(s) but found 1
Loading history...
125
        'PARTITIONING' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 4 space(s) but found 1
Loading history...
126
        'PRIMARY KEY' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 5 space(s) but found 1
Loading history...
127
        'SPATIAL' => 2,
0 ignored issues
show
Coding Style introduced by
Array double arrow not aligned correctly; expected 9 space(s) but found 1
Loading history...
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
128
        'TABLESPACE' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 6 space(s) but found 1
Loading history...
129
        'INDEX' => 2,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 11 space(s) but found 1
Loading history...
130
131
        'CHARACTER SET' => 3,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 36 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 3 space(s) but found 1
Loading history...
132
    ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 35 space(s), but found 4.
Loading history...
133
134
    /**
135
     * All view options.
136
     *
137
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \PhpMyAdmin\SqlParser\Components\AlterOperation::$VIEW_OPTIONS does not specify type hint for its items.
Loading history...
138
     */
139
    public static $VIEW_OPTIONS = ['AS' => 1];
0 ignored issues
show
Coding Style introduced by
Member variable "VIEW_OPTIONS" is not in valid camel caps format
Loading history...
140
141
    /**
142
     * Options of this operation.
143
     *
144
     * @var OptionsArray
145
     */
146
    public $options;
147
148
    /**
149
     * The altered field.
150
     *
151
     * @var Expression
152
     */
153
    public $field;
154
155
    /**
156
     * Unparsed tokens.
157
     *
158
     * @var Token[]|string
159
     */
160
    public $unknown = [];
161
162
    /**
163
     * @param OptionsArray $options options of alter operation
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
164
     * @param Expression   $field   altered field
0 ignored issues
show
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
165
     * @param array        $unknown unparsed tokens found at the end of operation
0 ignored issues
show
introduced by
@param annotation of method \PhpMyAdmin\SqlParser\Components\AlterOperation::__construct() does not specify type hint for items of its traversable parameter $unknown.
Loading history...
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
166
     */
167 128
    public function __construct(
0 ignored issues
show
introduced by
Method \PhpMyAdmin\SqlParser\Components\AlterOperation::__construct() does not have native type hint for its parameter $options but it should be possible to add it based on @param annotation "OptionsArray".
Loading history...
introduced by
Method \PhpMyAdmin\SqlParser\Components\AlterOperation::__construct() does not have native type hint for its parameter $field but it should be possible to add it based on @param annotation "Expression".
Loading history...
introduced by
Method \PhpMyAdmin\SqlParser\Components\AlterOperation::__construct() does not have native type hint for its parameter $unknown but it should be possible to add it based on @param annotation "array".
Loading history...
Coding Style introduced by
Type hint "OptionsArray" missing for $options
Loading history...
Coding Style introduced by
Type hint "Expression" missing for $field
Loading history...
Coding Style introduced by
Type hint "array" missing for $unknown
Loading history...
168
        $options = null,
169
        $field = null,
170
        $unknown = []
171
    ) {
172 128
        $this->options = $options;
173 128
        $this->field = $field;
174 128
        $this->unknown = $unknown;
175 128
    }
176
177
    /**
178
     * @param Parser     $parser  the parser that serves as context
0 ignored issues
show
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
179
     * @param TokensList $list    the list of tokens that are being parsed
0 ignored issues
show
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
180
     * @param array      $options parameters for parsing
0 ignored issues
show
introduced by
@param annotation of method \PhpMyAdmin\SqlParser\Components\AlterOperation::parse() does not specify type hint for items of its traversable parameter $options.
Loading history...
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
181
     *
182
     * @return AlterOperation
183
     */
184 128
    public static function parse(Parser $parser, TokensList $list, array $options = [])
0 ignored issues
show
introduced by
Method \PhpMyAdmin\SqlParser\Components\AlterOperation::parse() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "AlterOperation".
Loading history...
185
    {
186 128
        $ret = new static();
187
188
        /**
189
         * Counts brackets.
190
         *
191
         * @var int
192
         */
193 128
        $brackets = 0;
194
195
        /**
196
         * The state of the parser.
197
         *
198
         * Below are the states of the parser.
199
         *
200
         *      0 ---------------------[ options ]---------------------> 1
201
         *
202
         *      1 ----------------------[ field ]----------------------> 2
203
         *
204
         *      2 -------------------------[ , ]-----------------------> 0
205
         *
206
         * @var int
207
         */
208 128
        $state = 0;
209
210 128
        for (; $list->idx < $list->count; ++$list->idx) {
211
            /**
212
             * Token parsed at this moment.
213
             *
214
             * @var Token
215
             */
216 128
            $token = $list->tokens[$list->idx];
217
218
            // End of statement.
219 128
            if ($token->type === Token::TYPE_DELIMITER) {
220 104
                break;
221
            }
222
223
            // Skipping comments.
224 128
            if ($token->type === Token::TYPE_COMMENT) {
225 4
                continue;
226
            }
227
228
            // Skipping whitespaces.
229 128
            if ($token->type === Token::TYPE_WHITESPACE) {
230 68
                if ($state === 2) {
231
                    // When parsing the unknown part, the whitespaces are
232
                    // included to not break anything.
233 68
                    $ret->unknown[] = $token;
234
                }
235
236 68
                continue;
237
            }
238
239 128
            if ($state === 0) {
240 128
                $ret->options = OptionsArray::parse($parser, $list, $options);
241
242 128
                if ($ret->options->has('AS')) {
243 4
                    for (; $list->idx < $list->count; ++$list->idx) {
244 4
                        if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
245 4
                            break;
246
                        }
247
248 4
                        $ret->unknown[] = $list->tokens[$list->idx];
249
                    }
250
251 4
                    break;
252
                }
253
254 124
                $state = 1;
255 112
            } elseif ($state === 1) {
256 112
                $ret->field = Expression::parse(
257 112
                    $parser,
258 112
                    $list,
259
                    [
260 112
                        'breakOnAlias' => true,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 24.
Loading history...
261
                        'parseField' => 'column',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 21 spaces, but found 24.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 3 space(s) but found 1
Loading history...
262
                    ]
263
                );
264 112
                if ($ret->field === null) {
0 ignored issues
show
introduced by
Expected 1 line before "if", found 0.
Loading history...
265
                    // No field was read. We go back one token so the next
266
                    // iteration will parse the same token, but in state 2.
267 24
                    --$list->idx;
268
                }
269
270 112
                $state = 2;
271 96
            } elseif ($state === 2) {
272 96
                $arrayKey = '';
273 96
                if (is_string($token->value) || is_numeric($token->value)) {
0 ignored issues
show
introduced by
Expected 1 line before "if", found 0.
Loading history...
274 96
                    $arrayKey = $token->value;
275
                } else {
276 4
                    $arrayKey = $token->token;
277
                }
278
279 96
                if ($token->type === Token::TYPE_OPERATOR) {
280 60
                    if ($token->value === '(') {
281 44
                        ++$brackets;
282 60
                    } elseif ($token->value === ')') {
283 44
                        --$brackets;
284 32
                    } elseif (($token->value === ',') && ($brackets === 0)) {
285 60
                        break;
286
                    }
287 96
                } elseif (! self::checkIfTokenQuotedSymbol($token)) {
288 84
                    if (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
0 ignored issues
show
Coding Style introduced by
Member variable "$STATEMENT_PARSERS" is not in valid camel caps format
Loading history...
289
                        // We want to get the next non-comment and non-space token after $token
290
                        // therefore, the first getNext call will start with the current $idx which's $token,
291
                        // will return it and increase $idx by 1, which's not guaranteed to be non-comment
292
                        // and non-space, that's why we're calling getNext again.
293
                        // In order to get back to the original value of idx, we kept it in $currentID
294
295 24
                        $currentTokenID = $list->idx;
296 24
                        $list->getNext();
297 24
                        $nextToken = $list->getNext();
298
299 24
                        if ($token->value === 'CHARACTER SET') {
300
                            // Reverting the changes we made in the beginning
301
                            $list->idx = $currentTokenID;
302 24
                        } else if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === '(') {
0 ignored issues
show
introduced by
Expected 1 line before "if", found -1.
Loading history...
303
                            // To avoid adding the tokens between the SET() parentheses to the unknown tokens
304 8
                            $list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
305 16
                        } else if ($token->value === 'SET' && $nextToken !== null && $nextToken->value === 'DEFAULT') {
0 ignored issues
show
introduced by
Expected 1 line before "if", found -1.
Loading history...
introduced by
Expected 0 lines after "if", found 1.
Loading history...
306
                            // to avoid adding the `DEFAULT` token to the unknown tokens.
307 8
                            ++$list->idx;
308
                        } else {
0 ignored issues
show
introduced by
Use early exit instead of "else".
Loading history...
309
                            // We have reached the end of ALTER operation and suddenly found
310
                            // a start to new statement, but have not find a delimiter between them
311 8
                            $parser->error(
312 8
                                'A new statement was found, but no delimiter between it and the previous one.',
313 2
                                $token
314
                            );
315 24
                            break;
316
                        }
0 ignored issues
show
Coding Style introduced by
Blank line found after control structure
Loading history...
317
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
318
                    } elseif (
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; newline found
Loading history...
319 38
                        (array_key_exists($arrayKey, self::$DB_OPTIONS)
320 76
                        || array_key_exists($arrayKey, self::$TABLE_OPTIONS))
321 76
                        && ! self::checkIfColumnDefinitionKeyword($arrayKey)
322
                    ) {
323
                        // This alter operation has finished, which means a comma
324
                        // was missing before start of new alter operation
325 8
                        $parser->error('Missing comma before start of a new alter operation.', $token);
326 8
                        break;
327
                    }
328
                }
329
330 96
                $ret->unknown[] = $token;
331
            }
332
        }
333
334 128
        if ($ret->options->isEmpty()) {
335 4
            $parser->error('Unrecognized alter operation.', $list->tokens[$list->idx]);
336
        }
337
338 128
        --$list->idx;
339
340 128
        return $ret;
341
    }
342
343
    /**
344
     * @param AlterOperation $component the component to be built
345
     * @param array          $options   parameters for building
346
     *
347
     * @return string
348
     */
349 4
    public static function build($component, array $options = [])
350
    {
351 4
        $ret = $component->options . ' ';
352 4
        if (isset($component->field) && ($component->field !== '')) {
353 4
            $ret .= $component->field . ' ';
354
        }
355
356 4
        $ret .= TokensList::build($component->unknown);
357
358 4
        return $ret;
359
    }
360
361
    /**
362
     * Check if token's value is one of the common keywords
363
     * between column and table alteration
364
     *
365
     * @param string $tokenValue Value of current token
366
     *
367
     * @return bool
368
     */
369 56
    private static function checkIfColumnDefinitionKeyword($tokenValue)
370
    {
371 14
        $commonOptions = [
372 42
            'AUTO_INCREMENT',
373
            'COMMENT',
374
            'DEFAULT',
375
            'CHARACTER SET',
376
            'COLLATE',
377
            'PRIMARY',
378
            'UNIQUE',
379
            'PRIMARY KEY',
380
            'UNIQUE KEY',
381
        ];
382
383
        // Since these options can be used for
384
        // both table as well as a specific column in the table
385 56
        return in_array($tokenValue, $commonOptions);
386
    }
387
388
    /**
389
     * Check if token is symbol and quoted with backtick
390
     *
391
     * @param Token $token token to check
392
     *
393
     * @return bool
394
     */
395 96
    private static function checkIfTokenQuotedSymbol($token)
396
    {
397 96
        return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
398
    }
399
}
400