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 Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Query extends BaseObject |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Query id, defined in query xml file |
||
14 | * @var string |
||
15 | */ |
||
16 | var $queryID; |
||
17 | |||
18 | /** |
||
19 | * DML type, ex) INSERT, DELETE, UPDATE, SELECT |
||
20 | * @var string |
||
21 | */ |
||
22 | var $action; |
||
23 | |||
24 | /** |
||
25 | * priority level ex)LOW_PRIORITY, HIGHT_PRIORITY |
||
26 | * @var string |
||
27 | */ |
||
28 | var $priority; |
||
29 | |||
30 | /** |
||
31 | * column list |
||
32 | * @var string|array |
||
33 | */ |
||
34 | var $columns; |
||
35 | |||
36 | /** |
||
37 | * table list |
||
38 | * @var string|array |
||
39 | */ |
||
40 | var $tables; |
||
41 | |||
42 | /** |
||
43 | * condition list |
||
44 | * @var string|array |
||
45 | */ |
||
46 | var $conditions; |
||
47 | |||
48 | /** |
||
49 | * group list |
||
50 | * @var string|array |
||
51 | */ |
||
52 | var $groups; |
||
53 | |||
54 | /** |
||
55 | * order list |
||
56 | * @var array |
||
57 | */ |
||
58 | var $orderby; |
||
59 | |||
60 | /** |
||
61 | * limit count |
||
62 | * @var int |
||
63 | */ |
||
64 | var $limit; |
||
65 | |||
66 | /** |
||
67 | * argument list |
||
68 | * @var array |
||
69 | */ |
||
70 | var $arguments = NULL; |
||
71 | |||
72 | /** |
||
73 | * column list |
||
74 | * @var array |
||
75 | */ |
||
76 | var $columnList = NULL; |
||
77 | |||
78 | /** |
||
79 | * order by text |
||
80 | * @var string |
||
81 | */ |
||
82 | var $_orderByString; |
||
83 | |||
84 | /** |
||
85 | * constructor |
||
86 | * @param string $queryID |
||
87 | * @param string $action |
||
88 | * @param string|array $columns |
||
89 | * @param string|array $tables |
||
90 | * @param string|array $conditions |
||
91 | * @param string|array $groups |
||
92 | * @param string|array $orderby |
||
93 | * @param int $limit |
||
94 | * @param string $priority |
||
95 | * @return void |
||
|
|||
96 | */ |
||
97 | function __construct($queryID = NULL |
||
98 | , $action = NULL |
||
99 | , $columns = NULL |
||
100 | , $tables = NULL |
||
101 | , $conditions = NULL |
||
102 | , $groups = NULL |
||
103 | , $orderby = NULL |
||
104 | , $limit = NULL |
||
105 | , $priority = NULL) |
||
106 | { |
||
107 | $this->queryID = $queryID; |
||
108 | $this->action = $action; |
||
109 | $this->priority = $priority; |
||
110 | |||
111 | if(!isset($tables)) |
||
112 | { |
||
113 | return; |
||
114 | } |
||
115 | |||
116 | $this->columns = $this->setColumns($columns); |
||
117 | $this->tables = $this->setTables($tables); |
||
118 | $this->conditions = $this->setConditions($conditions); |
||
119 | $this->groups = $this->setGroups($groups); |
||
120 | $this->orderby = $this->setOrder($orderby); |
||
121 | $this->limit = $this->setLimit($limit); |
||
122 | } |
||
123 | |||
124 | function show() |
||
128 | |||
129 | function setQueryId($queryID) |
||
133 | |||
134 | function setAction($action) |
||
138 | |||
139 | function setPriority($priority) |
||
143 | |||
144 | function setColumnList($columnList) |
||
145 | { |
||
146 | $this->columnList = $columnList; |
||
147 | if(is_array($this->columnList) && count($this->columnList) > 0) |
||
148 | { |
||
149 | $selectColumns = array(); |
||
150 | $dbParser = DB::getParser(); |
||
151 | |||
152 | foreach($this->columnList as $columnName) |
||
153 | { |
||
154 | $columnName = $dbParser->escapeColumn($columnName); |
||
155 | $selectColumns[] = new SelectExpression($columnName); |
||
156 | } |
||
157 | unset($this->columns); |
||
158 | $this->columns = $selectColumns; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | function setColumns($columns) |
||
163 | { |
||
164 | if(!isset($columns) || (is_array($columns) && count($columns) === 0)) |
||
165 | { |
||
166 | $this->columns = array(new StarExpression()); |
||
167 | return; |
||
168 | } |
||
169 | |||
170 | if(!is_array($columns)) |
||
171 | { |
||
172 | $columns = array($columns); |
||
173 | } |
||
174 | |||
175 | $this->columns = $columns; |
||
176 | } |
||
177 | |||
178 | function setTables($tables) |
||
179 | { |
||
180 | if(!isset($tables) || (is_array($tables) && count($tables) === 0)) |
||
181 | { |
||
182 | $this->setError(TRUE); |
||
183 | $this->setMessage("You must provide at least one table for the query."); |
||
184 | return; |
||
185 | } |
||
186 | |||
187 | if(!is_array($tables)) |
||
188 | { |
||
189 | $tables = array($tables); |
||
190 | } |
||
191 | |||
192 | $this->tables = $tables; |
||
193 | } |
||
194 | |||
195 | function setSubquery($subquery) |
||
199 | |||
200 | function setConditions($conditions) |
||
201 | { |
||
202 | $this->conditions = array(); |
||
203 | if(!isset($conditions) || (is_array($conditions) && count($conditions) === 0)) |
||
204 | { |
||
205 | return; |
||
206 | } |
||
207 | if(!is_array($conditions)) |
||
208 | { |
||
209 | $conditions = array($conditions); |
||
210 | } |
||
211 | |||
212 | foreach($conditions as $conditionGroup) |
||
213 | { |
||
214 | if($conditionGroup->show()) |
||
215 | { |
||
216 | $this->conditions[] = $conditionGroup; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | View Code Duplication | function setGroups($groups) |
|
222 | { |
||
223 | if(!isset($groups) || (is_array($groups) && count($groups) === 0)) |
||
224 | { |
||
225 | return; |
||
226 | } |
||
227 | if(!is_array($groups)) |
||
228 | { |
||
229 | $groups = array($groups); |
||
230 | } |
||
231 | |||
232 | $this->groups = $groups; |
||
233 | } |
||
234 | |||
235 | View Code Duplication | function setOrder($order) |
|
236 | { |
||
237 | if(!isset($order) || (is_array($order) && count($order) === 0)) |
||
238 | { |
||
239 | return; |
||
240 | } |
||
241 | if(!is_array($order)) |
||
242 | { |
||
243 | $order = array($order); |
||
244 | } |
||
245 | |||
246 | $this->orderby = $order; |
||
247 | } |
||
248 | |||
249 | function getOrder() |
||
253 | |||
254 | function setLimit($limit = NULL) |
||
262 | |||
263 | // START Fluent interface |
||
264 | /** |
||
265 | * seleect set |
||
266 | * @param string|array $columns |
||
267 | * @return Query return Query instance |
||
268 | */ |
||
269 | function select($columns = NULL) |
||
275 | |||
276 | /** |
||
277 | * from set |
||
278 | * @param string|array $tables |
||
279 | * @return Query return Query instance |
||
280 | */ |
||
281 | function from($tables) |
||
286 | |||
287 | /** |
||
288 | * where set |
||
289 | * @param string|array $conditions |
||
290 | * @return Query return Query instance |
||
291 | */ |
||
292 | function where($conditions) |
||
297 | |||
298 | /** |
||
299 | * groupBy set |
||
300 | * @param string|array $groups |
||
301 | * @return Query return Query instance |
||
302 | */ |
||
303 | function groupBy($groups) |
||
308 | |||
309 | /** |
||
310 | * orderBy set |
||
311 | * @param string|array $order |
||
312 | * @return Query return Query instance |
||
313 | */ |
||
314 | function orderBy($order) |
||
319 | |||
320 | /** |
||
321 | * limit set |
||
322 | * @param int $limit |
||
323 | * @return Query return Query instance |
||
324 | */ |
||
325 | function limit($limit) |
||
330 | |||
331 | // END Fluent interface |
||
332 | |||
333 | function getAction() |
||
337 | |||
338 | function getPriority() |
||
342 | |||
343 | /** |
||
344 | * Check if current query uses the click count attribute |
||
345 | * For CUBRID, this statement uses the click count feature. |
||
346 | * For the other databases, using this attribute causes a query |
||
347 | * to produce both a select and an update |
||
348 | */ |
||
349 | function usesClickCount() |
||
353 | |||
354 | function getClickCountColumns() |
||
366 | |||
367 | /** |
||
368 | * Return select sql |
||
369 | * @param boolean $with_values |
||
370 | * @return string |
||
371 | */ |
||
372 | function getSelectString($with_values = TRUE) |
||
390 | |||
391 | /** |
||
392 | * Return update sql |
||
393 | * @param boolean $with_values |
||
394 | * @return string |
||
395 | */ |
||
396 | function getUpdateString($with_values = TRUE) |
||
409 | |||
410 | /** |
||
411 | * Return insert sql |
||
412 | * @param boolean $with_values |
||
413 | * @return string |
||
414 | */ |
||
415 | function getInsertString($with_values = TRUE) |
||
445 | |||
446 | function getTables() |
||
450 | |||
451 | /** |
||
452 | * from table_a |
||
453 | * from table_a inner join table_b on x=y |
||
454 | * from (select * from table a) as x |
||
455 | * from (select * from table t) as x inner join table y on y.x |
||
456 | * @param boolean $with_values |
||
457 | * @return string |
||
458 | */ |
||
459 | function getFromString($with_values = TRUE) |
||
487 | |||
488 | /** |
||
489 | * Return where sql |
||
490 | * @param boolean $with_values |
||
491 | * @param boolean $with_optimization |
||
492 | * @return string |
||
493 | */ |
||
494 | function getWhereString($with_values = TRUE, $with_optimization = TRUE) |
||
537 | |||
538 | /** |
||
539 | * Return groupby sql |
||
540 | * @return string |
||
541 | */ |
||
542 | function getGroupByString() |
||
554 | |||
555 | /** |
||
556 | * Return orderby sql |
||
557 | * @return string |
||
558 | */ |
||
559 | function getOrderByString() |
||
560 | { |
||
561 | if(!$this->_orderByString) |
||
562 | { |
||
563 | if(!is_array($this->orderby) || count($this->orderby) === 0) |
||
564 | { |
||
565 | return ''; |
||
566 | } |
||
567 | $orderBy = ''; |
||
568 | foreach($this->orderby as $order) |
||
569 | { |
||
570 | $orderBy .= $order->toString() . ', '; |
||
571 | } |
||
572 | $orderBy = substr($orderBy, 0, -2); |
||
573 | $this->_orderByString = $orderBy; |
||
574 | } |
||
575 | return $this->_orderByString; |
||
576 | } |
||
577 | |||
578 | function getLimit() |
||
582 | |||
583 | /** |
||
584 | * Return limit sql |
||
585 | * @return string |
||
586 | */ |
||
587 | function getLimitString() |
||
597 | |||
598 | function getFirstTableName() |
||
602 | |||
603 | /** |
||
604 | * Return argument list |
||
605 | * @return array |
||
606 | */ |
||
607 | function getArguments() |
||
608 | { |
||
609 | if(!isset($this->arguments)) |
||
610 | { |
||
674 | |||
675 | } |
||
676 | /* End of file Query.class.php */ |
||
678 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.