These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Parses a data type. |
||
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 | * Parses a data type. |
||
16 | * |
||
17 | * @category Components |
||
18 | * |
||
19 | * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
||
20 | */ |
||
21 | class DataType extends Component |
||
22 | { |
||
23 | /** |
||
24 | * All data type options. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | public static $DATA_TYPE_OPTIONS = array( |
||
29 | 'BINARY' => 1, |
||
30 | 'CHARACTER SET' => array(2, 'var'), |
||
31 | 'CHARSET' => array(2, 'var'), |
||
32 | 'COLLATE' => array(3, 'var'), |
||
33 | 'UNSIGNED' => 4, |
||
34 | 'ZEROFILL' => 5, |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * The name of the data type. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public $name; |
||
43 | |||
44 | /** |
||
45 | * The parameters of this data type. |
||
46 | * |
||
47 | * Some data types have no parameters. |
||
48 | * Numeric types might have parameters for the maximum number of digits, |
||
49 | * precision, etc. |
||
50 | * String types might have parameters for the maximum length stored. |
||
51 | * `ENUM` and `SET` have parameters for possible values. |
||
52 | * |
||
53 | * For more information, check the MySQL manual. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | public $parameters = array(); |
||
58 | |||
59 | /** |
||
60 | * The options of this data type. |
||
61 | * |
||
62 | * @var OptionsArray |
||
63 | */ |
||
64 | public $options; |
||
65 | |||
66 | /** |
||
67 | * Constructor. |
||
68 | * |
||
69 | * @param string $name the name of this data type |
||
0 ignored issues
–
show
|
|||
70 | * @param array $parameters the parameters (size or possible values) |
||
71 | * @param OptionsArray $options the options of this data type |
||
0 ignored issues
–
show
Should the type for parameter
$options not be OptionsArray|null ?
This check looks for 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...
|
|||
72 | */ |
||
73 | 50 | public function __construct( |
|
74 | $name = null, |
||
75 | array $parameters = array(), |
||
76 | $options = null |
||
77 | ) { |
||
78 | 50 | $this->name = $name; |
|
79 | 50 | $this->parameters = $parameters; |
|
80 | 50 | $this->options = $options; |
|
81 | 50 | } |
|
82 | |||
83 | /** |
||
84 | * @param Parser $parser the parser that serves as context |
||
85 | * @param TokensList $list the list of tokens that are being parsed |
||
86 | * @param array $options parameters for parsing |
||
87 | * |
||
88 | * @return DataType |
||
0 ignored issues
–
show
|
|||
89 | */ |
||
90 | 50 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
91 | { |
||
92 | 50 | $ret = new self(); |
|
93 | |||
94 | /** |
||
95 | * The state of the parser. |
||
96 | * |
||
97 | * Below are the states of the parser. |
||
98 | * |
||
99 | * 0 -------------------[ data type ]--------------------> 1 |
||
100 | * |
||
101 | * 1 ----------------[ size and options ]----------------> 2 |
||
102 | * |
||
103 | * @var int |
||
104 | */ |
||
105 | 50 | $state = 0; |
|
106 | |||
107 | 50 | for (; $list->idx < $list->count; ++$list->idx) { |
|
108 | /** |
||
109 | * Token parsed at this moment. |
||
110 | * |
||
111 | * @var Token |
||
112 | */ |
||
113 | 49 | $token = $list->tokens[$list->idx]; |
|
114 | |||
115 | // Skipping whitespaces and comments. |
||
116 | 49 | if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
|
117 | 16 | continue; |
|
118 | } |
||
119 | |||
120 | 49 | if ($state === 0) { |
|
121 | 49 | $ret->name = strtoupper($token->value); |
|
122 | 49 | if (($token->type !== Token::TYPE_KEYWORD) || (!($token->flags & Token::FLAG_KEYWORD_DATA_TYPE))) { |
|
123 | 2 | $parser->error('Unrecognized data type.', $token); |
|
124 | } |
||
125 | 49 | $state = 1; |
|
126 | 48 | } elseif ($state === 1) { |
|
127 | 48 | if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) { |
|
128 | 30 | $parameters = ArrayObj::parse($parser, $list); |
|
129 | 30 | ++$list->idx; |
|
130 | 30 | $ret->parameters = (($ret->name === 'ENUM') || ($ret->name === 'SET')) ? |
|
131 | 30 | $parameters->raw : $parameters->values; |
|
132 | } |
||
133 | 48 | $ret->options = OptionsArray::parse($parser, $list, static::$DATA_TYPE_OPTIONS); |
|
134 | 48 | ++$list->idx; |
|
135 | 48 | break; |
|
136 | } |
||
137 | } |
||
138 | |||
139 | 50 | if (empty($ret->name)) { |
|
140 | 2 | return null; |
|
141 | } |
||
142 | |||
143 | 48 | --$list->idx; |
|
144 | |||
145 | 48 | return $ret; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param DataType $component the component to be built |
||
150 | * @param array $options parameters for building |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 5 | public static function build($component, array $options = array()) |
|
155 | { |
||
156 | 5 | $name = (empty($options['lowercase'])) ? |
|
157 | 5 | $component->name : strtolower($component->name); |
|
158 | |||
159 | 5 | $parameters = ''; |
|
160 | 5 | if (!empty($component->parameters)) { |
|
161 | 4 | $parameters = '(' . implode(',', $component->parameters) . ')'; |
|
162 | } |
||
163 | |||
164 | 5 | return trim($name . $parameters . ' ' . $component->options); |
|
165 | } |
||
166 | } |
||
167 |
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.