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 |
||
42 | class Query |
||
43 | { |
||
44 | /** |
||
45 | * Functions that set the flag `is_func`. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | public static $FUNCTIONS = array( |
||
50 | 'SUM', 'AVG', 'STD', 'STDDEV', 'MIN', 'MAX', 'BIT_OR', 'BIT_AND', |
||
51 | ); |
||
52 | |||
53 | public static $ALLFLAGS = array( |
||
54 | /* |
||
55 | * select ... DISTINCT ... |
||
56 | */ |
||
57 | 'distinct' => false, |
||
58 | |||
59 | /* |
||
60 | * drop ... DATABASE ... |
||
61 | */ |
||
62 | 'drop_database' => false, |
||
63 | |||
64 | /* |
||
65 | * ... GROUP BY ... |
||
66 | */ |
||
67 | 'group' => false, |
||
68 | |||
69 | /* |
||
70 | * ... HAVING ... |
||
71 | */ |
||
72 | 'having' => false, |
||
73 | |||
74 | /* |
||
75 | * INSERT ... |
||
76 | * or |
||
77 | * REPLACE ... |
||
78 | * or |
||
79 | * DELETE ... |
||
80 | */ |
||
81 | 'is_affected' => false, |
||
82 | |||
83 | /* |
||
84 | * select ... PROCEDURE ANALYSE( ... ) ... |
||
85 | */ |
||
86 | 'is_analyse' => false, |
||
87 | |||
88 | /* |
||
89 | * select COUNT( ... ) ... |
||
90 | */ |
||
91 | 'is_count' => false, |
||
92 | |||
93 | /* |
||
94 | * DELETE ... |
||
95 | */ |
||
96 | 'is_delete' => false, // @deprecated; use `querytype` |
||
97 | |||
98 | /* |
||
99 | * EXPLAIN ... |
||
100 | */ |
||
101 | 'is_explain' => false, // @deprecated; use `querytype` |
||
102 | |||
103 | /* |
||
104 | * select ... INTO OUTFILE ... |
||
105 | */ |
||
106 | 'is_export' => false, |
||
107 | |||
108 | /* |
||
109 | * select FUNC( ... ) ... |
||
110 | */ |
||
111 | 'is_func' => false, |
||
112 | |||
113 | /* |
||
114 | * select ... GROUP BY ... |
||
115 | * or |
||
116 | * select ... HAVING ... |
||
117 | */ |
||
118 | 'is_group' => false, |
||
119 | |||
120 | /* |
||
121 | * INSERT ... |
||
122 | * or |
||
123 | * REPLACE ... |
||
124 | * or |
||
125 | * TODO: LOAD DATA ... |
||
|
|||
126 | */ |
||
127 | 'is_insert' => false, |
||
128 | |||
129 | /* |
||
130 | * ANALYZE ... |
||
131 | * or |
||
132 | * CHECK ... |
||
133 | * or |
||
134 | * CHECKSUM ... |
||
135 | * or |
||
136 | * OPTIMIZE ... |
||
137 | * or |
||
138 | * REPAIR ... |
||
139 | */ |
||
140 | 'is_maint' => false, |
||
141 | |||
142 | /* |
||
143 | * CALL ... |
||
144 | */ |
||
145 | 'is_procedure' => false, |
||
146 | |||
147 | /* |
||
148 | * REPLACE ... |
||
149 | */ |
||
150 | 'is_replace' => false, // @deprecated; use `querytype` |
||
151 | |||
152 | /* |
||
153 | * SELECT ... |
||
154 | */ |
||
155 | 'is_select' => false, // @deprecated; use `querytype` |
||
156 | |||
157 | /* |
||
158 | * SHOW ... |
||
159 | */ |
||
160 | 'is_show' => false, // @deprecated; use `querytype` |
||
161 | |||
162 | /* |
||
163 | * Contains a subquery. |
||
164 | */ |
||
165 | 'is_subquery' => false, |
||
166 | |||
167 | /* |
||
168 | * ... JOIN ... |
||
169 | */ |
||
170 | 'join' => false, |
||
171 | |||
172 | /* |
||
173 | * ... LIMIT ... |
||
174 | */ |
||
175 | 'limit' => false, |
||
176 | |||
177 | /* |
||
178 | * TODO |
||
179 | */ |
||
180 | 'offset' => false, |
||
181 | |||
182 | /* |
||
183 | * ... ORDER ... |
||
184 | */ |
||
185 | 'order' => false, |
||
186 | |||
187 | /* |
||
188 | * The type of the query (which is usually the first keyword of |
||
189 | * the statement). |
||
190 | */ |
||
191 | 'querytype' => false, |
||
192 | |||
193 | /* |
||
194 | * Whether a page reload is required. |
||
195 | */ |
||
196 | 'reload' => false, |
||
197 | |||
198 | /* |
||
199 | * SELECT ... FROM ... |
||
200 | */ |
||
201 | 'select_from' => false, |
||
202 | |||
203 | /* |
||
204 | * ... UNION ... |
||
205 | */ |
||
206 | 'union' => false, |
||
207 | ); |
||
208 | |||
209 | /** |
||
210 | * Gets an array with flags select statement has. |
||
211 | * |
||
212 | * @param SelectStatement $statement the statement to be processed |
||
213 | * @param array $flags flags set so far |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | 13 | private static function _getFlagsSelect($statement, $flags) |
|
284 | |||
285 | /** |
||
286 | * Gets an array with flags this statement has. |
||
287 | * |
||
288 | * @param Statement|null $statement the statement to be processed |
||
289 | * @param bool $all if `false`, false values will not be included |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | 29 | public static function getFlags($statement, $all = false) |
|
377 | |||
378 | /** |
||
379 | * Parses a query and gets all information about it. |
||
380 | * |
||
381 | * @param string $query the query to be parsed |
||
382 | * |
||
383 | * @return array The array returned is the one returned by |
||
384 | * `static::getFlags()`, with the following keys added: |
||
385 | * - parser - the parser used to analyze the query; |
||
386 | * - statement - the first statement resulted from parsing; |
||
387 | * - select_tables - the real name of the tables selected; |
||
388 | * if there are no table names in the `SELECT` |
||
389 | * expressions, the table names are fetched from the |
||
390 | * `FROM` expressions |
||
391 | * - select_expr - selected expressions |
||
392 | */ |
||
393 | 1 | public static function getAll($query) |
|
468 | |||
469 | /** |
||
470 | * Gets a list of all tables used in this statement. |
||
471 | * |
||
472 | * @param Statement $statement statement to be scanned |
||
473 | * |
||
474 | * @return array |
||
475 | */ |
||
476 | 7 | public static function getTables($statement) |
|
517 | |||
518 | /** |
||
519 | * Gets a specific clause. |
||
520 | * |
||
521 | * @param Statement $statement the parsed query that has to be modified |
||
522 | * @param TokensList $list the list of tokens |
||
523 | * @param string $clause the clause to be returned |
||
524 | * @param int|string $type The type of the search. |
||
525 | * If int, |
||
526 | * -1 for everything that was before |
||
527 | * 0 only for the clause |
||
528 | * 1 for everything after |
||
529 | * If string, the name of the first clause that |
||
530 | * should not be included. |
||
531 | * @param bool $skipFirst whether to skip the first keyword in clause |
||
532 | * |
||
533 | * @return string |
||
534 | */ |
||
535 | 4 | public static function getClause($statement, $list, $clause, $type = 0, $skipFirst = true) |
|
649 | |||
650 | /** |
||
651 | * Builds a query by rebuilding the statement from the tokens list supplied |
||
652 | * and replaces a clause. |
||
653 | * |
||
654 | * It is a very basic version of a query builder. |
||
655 | * |
||
656 | * @param Statement $statement the parsed query that has to be modified |
||
657 | * @param TokensList $list the list of tokens |
||
658 | * @param string $old The type of the clause that should be |
||
659 | * replaced. This can be an entire clause. |
||
660 | * @param string $new The new clause. If this parameter is omitted |
||
661 | * it is considered to be equal with `$old`. |
||
662 | * @param bool $onlyType whether only the type of the clause should |
||
663 | * be replaced or the entire clause |
||
664 | * |
||
665 | * @return string |
||
666 | */ |
||
667 | 3 | public static function replaceClause($statement, $list, $old, $new = null, $onlyType = false) |
|
684 | |||
685 | /** |
||
686 | * Builds a query by rebuilding the statement from the tokens list supplied |
||
687 | * and replaces multiple clauses. |
||
688 | * |
||
689 | * @param Statement $statement the parsed query that has to be modified |
||
690 | * @param TokensList $list the list of tokens |
||
691 | * @param array $ops Clauses to be replaced. Contains multiple |
||
692 | * arrays having two values: array($old, $new). |
||
693 | * Clauses must be sorted. |
||
694 | * |
||
695 | * @return string |
||
696 | */ |
||
697 | 1 | public static function replaceClauses($statement, $list, array $ops) |
|
741 | |||
742 | /** |
||
743 | * Gets the first full statement in the query. |
||
744 | * |
||
745 | * @param string $query the query to be analyzed |
||
746 | * @param string $delimiter the delimiter to be used |
||
747 | * |
||
748 | * @return array array containing the first full query, the |
||
749 | * remaining part of the query and the last |
||
750 | * delimiter |
||
751 | */ |
||
752 | 1 | public static function getFirstStatement($query, $delimiter = null) |
|
802 | |||
803 | /** |
||
804 | * Gets a starting offset of a specific clause. |
||
805 | * |
||
806 | * @param Statement $statement the parsed query that has to be modified |
||
807 | * @param TokensList $list the list of tokens |
||
808 | * @param string $clause the clause to be returned |
||
809 | * |
||
810 | * @return int |
||
811 | */ |
||
812 | 136 | public static function getClauseStartOffset($statement, $list, $clause) |
|
858 | } |
||
859 |