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 | 30 | public static function getFlags($statement, $all = false) |
|
| 294 | { |
||
| 295 | 30 | $flags = array(); |
|
| 296 | 30 | if ($all) { |
|
| 297 | 1 | $flags = self::$ALLFLAGS; |
|
| 298 | } |
||
| 299 | |||
| 300 | 30 | if ($statement instanceof AlterStatement) { |
|
| 301 | 1 | $flags['querytype'] = 'ALTER'; |
|
| 302 | 1 | $flags['reload'] = true; |
|
| 303 | } elseif ($statement instanceof CreateStatement) { |
||
| 304 | 1 | $flags['querytype'] = 'CREATE'; |
|
| 305 | 1 | $flags['reload'] = true; |
|
| 306 | 21 | } elseif ($statement instanceof AnalyzeStatement) { |
|
| 307 | 1 | $flags['querytype'] = 'ANALYZE'; |
|
| 308 | 1 | $flags['is_maint'] = true; |
|
| 309 | 1 | } elseif ($statement instanceof CheckStatement) { |
|
| 310 | 1 | $flags['querytype'] = 'CHECK'; |
|
| 311 | 1 | $flags['is_maint'] = true; |
|
| 312 | 20 | } elseif ($statement instanceof ChecksumStatement) { |
|
| 313 | 1 | $flags['querytype'] = 'CHECKSUM'; |
|
| 314 | 1 | $flags['is_maint'] = true; |
|
| 315 | 20 | } elseif ($statement instanceof OptimizeStatement) { |
|
| 316 | 1 | $flags['querytype'] = 'OPTIMIZE'; |
|
| 317 | 1 | $flags['is_maint'] = true; |
|
| 318 | 20 | } elseif ($statement instanceof RepairStatement) { |
|
| 319 | 1 | $flags['querytype'] = 'REPAIR'; |
|
| 320 | 1 | $flags['is_maint'] = true; |
|
| 321 | } elseif ($statement instanceof CallStatement) { |
||
| 322 | 1 | $flags['querytype'] = 'CALL'; |
|
| 323 | 1 | $flags['is_procedure'] = true; |
|
| 324 | View Code Duplication | } elseif ($statement instanceof DeleteStatement) { |
|
| 325 | 1 | $flags['querytype'] = 'DELETE'; |
|
| 326 | 1 | $flags['is_delete'] = true; |
|
| 327 | 1 | $flags['is_affected'] = true; |
|
| 328 | } elseif ($statement instanceof DropStatement) { |
||
| 329 | 2 | $flags['querytype'] = 'DROP'; |
|
| 330 | 2 | $flags['reload'] = true; |
|
| 331 | |||
| 332 | 2 | if (($statement->options->has('DATABASE') |
|
| 333 | 2 | || ($statement->options->has('SCHEMA'))) |
|
| 334 | ) { |
||
| 335 | 2 | $flags['drop_database'] = true; |
|
| 336 | } |
||
| 337 | } elseif ($statement instanceof ExplainStatement) { |
||
| 338 | 1 | $flags['querytype'] = 'EXPLAIN'; |
|
| 339 | 1 | $flags['is_explain'] = true; |
|
| 340 | } elseif ($statement instanceof InsertStatement) { |
||
| 341 | 1 | $flags['querytype'] = 'INSERT'; |
|
| 342 | 1 | $flags['is_affected'] = true; |
|
| 343 | 1 | $flags['is_insert'] = true; |
|
| 344 | View Code Duplication | } elseif ($statement instanceof LoadStatement) { |
|
| 345 | 1 | $flags['querytype'] = 'LOAD'; |
|
| 346 | 1 | $flags['is_affected'] = true; |
|
| 347 | 1 | $flags['is_insert'] = true; |
|
| 348 | } elseif ($statement instanceof ReplaceStatement) { |
||
| 349 | 1 | $flags['querytype'] = 'REPLACE'; |
|
| 350 | 1 | $flags['is_affected'] = true; |
|
| 351 | 1 | $flags['is_replace'] = true; |
|
| 352 | 1 | $flags['is_insert'] = true; |
|
| 353 | } elseif ($statement instanceof SelectStatement) { |
||
| 354 | 13 | $flags = self::_getFlagsSelect($statement, $flags); |
|
| 355 | } elseif ($statement instanceof ShowStatement) { |
||
| 356 | 1 | $flags['querytype'] = 'SHOW'; |
|
| 357 | 1 | $flags['is_show'] = true; |
|
| 358 | } elseif ($statement instanceof UpdateStatement) { |
||
| 359 | 1 | $flags['querytype'] = 'UPDATE'; |
|
| 360 | 1 | $flags['is_affected'] = true; |
|
| 361 | } |
||
| 362 | |||
| 363 | 30 | if (($statement instanceof SelectStatement) |
|
| 364 | 18 | || ($statement instanceof UpdateStatement) |
|
| 365 | 30 | || ($statement instanceof DeleteStatement) |
|
| 366 | ) { |
||
| 367 | 15 | if (!empty($statement->limit)) { |
|
| 368 | 2 | $flags['limit'] = true; |
|
| 369 | } |
||
| 370 | 15 | if (!empty($statement->order)) { |
|
| 371 | 2 | $flags['order'] = true; |
|
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | 30 | return $flags; |
|
| 376 | } |
||
| 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 |