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