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