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:
Complex classes like CreateStatement 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CreateStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class CreateStatement extends Statement |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Options for `CREATE` statements. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | public static $OPTIONS = array( |
||
43 | |||
44 | // CREATE TABLE |
||
45 | 'TEMPORARY' => 1, |
||
46 | |||
47 | // CREATE VIEW |
||
48 | 'OR REPLACE' => array(2, 'var='), |
||
49 | 'ALGORITHM' => array(3, 'var='), |
||
50 | // `DEFINER` is also used for `CREATE FUNCTION / PROCEDURE` |
||
51 | 'DEFINER' => array(4, 'var='), |
||
52 | 'SQL SECURITY' => array(5, 'var'), |
||
53 | |||
54 | 'DATABASE' => 6, |
||
55 | 'EVENT' => 6, |
||
56 | 'FUNCTION' => 6, |
||
57 | 'INDEX' => 6, |
||
58 | 'UNIQUE INDEX' => 6, |
||
59 | 'FULLTEXT INDEX' => 6, |
||
60 | 'SPATIAL INDEX' => 6, |
||
61 | 'PROCEDURE' => 6, |
||
62 | 'SERVER' => 6, |
||
63 | 'TABLE' => 6, |
||
64 | 'TABLESPACE' => 6, |
||
65 | 'TRIGGER' => 6, |
||
66 | 'USER' => 6, |
||
67 | 'VIEW' => 6, |
||
68 | |||
69 | // CREATE TABLE |
||
70 | 'IF NOT EXISTS' => 7, |
||
71 | ); |
||
72 | |||
73 | /** |
||
74 | * All database options. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | public static $DB_OPTIONS = array( |
||
79 | 'CHARACTER SET' => array(1, 'var='), |
||
80 | 'CHARSET' => array(1, 'var='), |
||
81 | 'DEFAULT CHARACTER SET' => array(1, 'var='), |
||
82 | 'DEFAULT CHARSET' => array(1, 'var='), |
||
83 | 'DEFAULT COLLATE' => array(2, 'var='), |
||
84 | 'COLLATE' => array(2, 'var='), |
||
85 | ); |
||
86 | |||
87 | /** |
||
88 | * All table options. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | public static $TABLE_OPTIONS = array( |
||
93 | 'ENGINE' => array(1, 'var='), |
||
94 | 'AUTO_INCREMENT' => array(2, 'var='), |
||
95 | 'AVG_ROW_LENGTH' => array(3, 'var'), |
||
96 | 'CHARACTER SET' => array(4, 'var='), |
||
97 | 'CHARSET' => array(4, 'var='), |
||
98 | 'DEFAULT CHARACTER SET' => array(4, 'var='), |
||
99 | 'DEFAULT CHARSET' => array(4, 'var='), |
||
100 | 'CHECKSUM' => array(5, 'var'), |
||
101 | 'DEFAULT COLLATE' => array(6, 'var='), |
||
102 | 'COLLATE' => array(6, 'var='), |
||
103 | 'COMMENT' => array(7, 'var='), |
||
104 | 'CONNECTION' => array(8, 'var'), |
||
105 | 'DATA DIRECTORY' => array(9, 'var'), |
||
106 | 'DELAY_KEY_WRITE' => array(10, 'var'), |
||
107 | 'INDEX DIRECTORY' => array(11, 'var'), |
||
108 | 'INSERT_METHOD' => array(12, 'var'), |
||
109 | 'KEY_BLOCK_SIZE' => array(13, 'var'), |
||
110 | 'MAX_ROWS' => array(14, 'var'), |
||
111 | 'MIN_ROWS' => array(15, 'var'), |
||
112 | 'PACK_KEYS' => array(16, 'var'), |
||
113 | 'PASSWORD' => array(17, 'var'), |
||
114 | 'ROW_FORMAT' => array(18, 'var'), |
||
115 | 'TABLESPACE' => array(19, 'var'), |
||
116 | 'STORAGE' => array(20, 'var'), |
||
117 | 'UNION' => array(21, 'var'), |
||
118 | ); |
||
119 | |||
120 | /** |
||
121 | * All function options. |
||
122 | * |
||
123 | * @var array |
||
124 | */ |
||
125 | public static $FUNC_OPTIONS = array( |
||
126 | 'COMMENT' => array(1, 'var='), |
||
127 | 'LANGUAGE SQL' => 2, |
||
128 | 'DETERMINISTIC' => 3, |
||
129 | 'NOT DETERMINISTIC' => 3, |
||
130 | 'CONTAINS SQL' => 4, |
||
131 | 'NO SQL' => 4, |
||
132 | 'READS SQL DATA' => 4, |
||
133 | 'MODIFIES SQL DATA' => 4, |
||
134 | 'SQL SECURITY DEFINER' => array(5, 'var'), |
||
135 | ); |
||
136 | |||
137 | /** |
||
138 | * All trigger options. |
||
139 | * |
||
140 | * @var array |
||
141 | */ |
||
142 | public static $TRIGGER_OPTIONS = array( |
||
143 | 'BEFORE' => 1, |
||
144 | 'AFTER' => 1, |
||
145 | 'INSERT' => 2, |
||
146 | 'UPDATE' => 2, |
||
147 | 'DELETE' => 2, |
||
148 | ); |
||
149 | |||
150 | /** |
||
151 | * The name of the entity that is created. |
||
152 | * |
||
153 | * Used by all `CREATE` statements. |
||
154 | * |
||
155 | * @var Expression |
||
156 | */ |
||
157 | public $name; |
||
158 | |||
159 | /** |
||
160 | * The options of the entity (table, procedure, function, etc.). |
||
161 | * |
||
162 | * Used by `CREATE TABLE`, `CREATE FUNCTION` and `CREATE PROCEDURE`. |
||
163 | * |
||
164 | * @var OptionsArray |
||
165 | * |
||
166 | * @see static::$TABLE_OPTIONS |
||
167 | * @see static::$FUNC_OPTIONS |
||
168 | * @see static::$TRIGGER_OPTIONS |
||
169 | */ |
||
170 | public $entityOptions; |
||
171 | |||
172 | /** |
||
173 | * If `CREATE TABLE`, a list of columns and keys. |
||
174 | * If `CREATE VIEW`, a list of columns. |
||
175 | * |
||
176 | * Used by `CREATE TABLE` and `CREATE VIEW`. |
||
177 | * |
||
178 | * @var CreateDefinition[]|ArrayObj |
||
179 | */ |
||
180 | public $fields; |
||
181 | |||
182 | /** |
||
183 | * If `CREATE TABLE ... SELECT` |
||
184 | * |
||
185 | * Used by `CREATE TABLE` |
||
186 | * |
||
187 | * @var SelectStatement |
||
188 | */ |
||
189 | public $select; |
||
190 | |||
191 | /** |
||
192 | * If `CREATE TABLE ... LIKE` |
||
193 | * |
||
194 | * Used by `CREATE TABLE` |
||
195 | * |
||
196 | * @var Expression |
||
197 | */ |
||
198 | public $like; |
||
199 | |||
200 | /** |
||
201 | * Expression used for partitioning. |
||
202 | * |
||
203 | * @var string |
||
204 | */ |
||
205 | public $partitionBy; |
||
206 | |||
207 | /** |
||
208 | * The number of partitions. |
||
209 | * |
||
210 | * @var int |
||
211 | */ |
||
212 | public $partitionsNum; |
||
213 | |||
214 | /** |
||
215 | * Expression used for subpartitioning. |
||
216 | * |
||
217 | * @var string |
||
218 | */ |
||
219 | public $subpartitionBy; |
||
220 | |||
221 | /** |
||
222 | * The number of subpartitions. |
||
223 | * |
||
224 | * @var int |
||
225 | */ |
||
226 | public $subpartitionsNum; |
||
227 | |||
228 | /** |
||
229 | * The partition of the new table. |
||
230 | * |
||
231 | * @var PartitionDefinition[] |
||
232 | */ |
||
233 | public $partitions; |
||
234 | |||
235 | /** |
||
236 | * If `CREATE TRIGGER` the name of the table. |
||
237 | * |
||
238 | * Used by `CREATE TRIGGER`. |
||
239 | * |
||
240 | * @var Expression |
||
241 | */ |
||
242 | public $table; |
||
243 | |||
244 | /** |
||
245 | * The return data type of this routine. |
||
246 | * |
||
247 | * Used by `CREATE FUNCTION`. |
||
248 | * |
||
249 | * @var DataType |
||
250 | */ |
||
251 | public $return; |
||
252 | |||
253 | /** |
||
254 | * The parameters of this routine. |
||
255 | * |
||
256 | * Used by `CREATE FUNCTION` and `CREATE PROCEDURE`. |
||
257 | * |
||
258 | * @var ParameterDefinition[] |
||
259 | */ |
||
260 | public $parameters; |
||
261 | |||
262 | /** |
||
263 | * The body of this function or procedure. For views, it is the select |
||
264 | * statement that gets the |
||
265 | 10 | * |
|
266 | * Used by `CREATE FUNCTION`, `CREATE PROCEDURE` and `CREATE VIEW`. |
||
267 | 10 | * |
|
268 | 10 | * @var Token[]|string |
|
269 | 5 | */ |
|
270 | 4 | public $body = array(); |
|
271 | 5 | ||
272 | 1 | /** |
|
273 | 1 | * @return string |
|
274 | 5 | */ |
|
275 | 10 | public function build() |
|
356 | |||
357 | 42 | /** |
|
358 | 42 | * @param Parser $parser The instance that requests parsing. |
|
359 | 42 | * @param TokensList $list The list of tokens to be parsed. |
|
360 | * |
||
361 | 42 | * @return void |
|
362 | 42 | */ |
|
363 | public function parse(Parser $parser, TokensList $list) |
||
640 | } |
||
641 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.