Complex classes like Select 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 Select, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Select extends AbstractQuery implements SelectInterface, SubselectInterface |
||
| 22 | { |
||
| 23 | use WhereTrait; |
||
| 24 | use LimitOffsetTrait { limit as setLimit; offset as setOffset; } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * |
||
| 28 | * An array of union SELECT statements. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | protected $union = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * |
||
| 37 | * Is this a SELECT FOR UPDATE? |
||
| 38 | * |
||
| 39 | * @var |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | protected $for_update = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * |
||
| 46 | * The columns to be selected. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | * |
||
| 50 | */ |
||
| 51 | protected $cols = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * Select from these tables; includes JOIN clauses. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | * |
||
| 59 | */ |
||
| 60 | protected $from = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * |
||
| 64 | * The current key in the `$from` array. |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | * |
||
| 68 | */ |
||
| 69 | protected $from_key = -1; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * |
||
| 73 | * Tracks which JOIN clauses are attached to which FROM tables. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | * |
||
| 77 | */ |
||
| 78 | protected $join = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * |
||
| 82 | * GROUP BY these columns. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | * |
||
| 86 | */ |
||
| 87 | protected $group_by = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * |
||
| 91 | * The list of HAVING conditions. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | * |
||
| 95 | */ |
||
| 96 | protected $having = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * |
||
| 100 | * The page number to select. |
||
| 101 | * |
||
| 102 | * @var int |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | protected $page = 0; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * |
||
| 109 | * The number of rows per page. |
||
| 110 | * |
||
| 111 | * @var int |
||
| 112 | * |
||
| 113 | */ |
||
| 114 | protected $paging = 10; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * |
||
| 118 | * Tracks table references to avoid duplicate identifiers. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | * |
||
| 122 | */ |
||
| 123 | protected $table_refs = array(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * |
||
| 127 | * Returns this query object as an SQL statement string. |
||
| 128 | * |
||
| 129 | * @return string An SQL statement string. |
||
| 130 | * |
||
| 131 | */ |
||
| 132 | 184 | public function getStatement() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | * Sets the number of rows per page. |
||
| 144 | * |
||
| 145 | * @param int $paging The number of rows to page at. |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | * |
||
| 149 | */ |
||
| 150 | 10 | public function setPaging($paging) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * |
||
| 161 | * Gets the number of rows per page. |
||
| 162 | * |
||
| 163 | * @return int The number of rows per page. |
||
| 164 | * |
||
| 165 | */ |
||
| 166 | 10 | public function getPaging() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * |
||
| 173 | * Makes the select FOR UPDATE (or not). |
||
| 174 | * |
||
| 175 | * @param bool $enable Whether or not the SELECT is FOR UPDATE (default |
||
| 176 | * true). |
||
| 177 | * |
||
| 178 | * @return $this |
||
| 179 | * |
||
| 180 | */ |
||
| 181 | 20 | public function forUpdate($enable = true) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * |
||
| 189 | * Makes the select DISTINCT (or not). |
||
| 190 | * |
||
| 191 | * @param bool $enable Whether or not the SELECT is DISTINCT (default |
||
| 192 | * true). |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | * |
||
| 196 | */ |
||
| 197 | 16 | public function distinct($enable = true) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * |
||
| 205 | * Adds columns to the query. |
||
| 206 | * |
||
| 207 | * Multiple calls to cols() will append to the list of columns, not |
||
| 208 | * overwrite the previous columns. |
||
| 209 | * |
||
| 210 | * @param array $cols The column(s) to add to the query. The elements can be |
||
| 211 | * any mix of these: `array("col", "col AS alias", "col" => "alias")` |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | * |
||
| 215 | */ |
||
| 216 | 224 | public function cols(array $cols) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * |
||
| 226 | * Adds a column and alias to the columns to be selected. |
||
| 227 | * |
||
| 228 | * @param mixed $key If an integer, ignored. Otherwise, the column to be |
||
| 229 | * added. |
||
| 230 | * |
||
| 231 | * @param mixed $val If $key was an integer, the column to be added; |
||
| 232 | * otherwise, the column alias. |
||
| 233 | * |
||
| 234 | * @return null |
||
| 235 | * |
||
| 236 | */ |
||
| 237 | 224 | protected function addCol($key, $val) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * |
||
| 249 | * Adds a column with an alias to the columns to be selected. |
||
| 250 | * |
||
| 251 | * @param string $spec The column specification: "col alias", |
||
| 252 | * "col AS alias", or something else entirely. |
||
| 253 | * |
||
| 254 | * @return null |
||
| 255 | * |
||
| 256 | */ |
||
| 257 | 214 | protected function addColWithAlias($spec) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * |
||
| 275 | * Remove a column via its alias. |
||
| 276 | * |
||
| 277 | * @param string $alias The column to remove |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | * |
||
| 281 | */ |
||
| 282 | 15 | public function removeCol($alias) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * |
||
| 301 | * Does the query have any columns in it? |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | * |
||
| 305 | */ |
||
| 306 | 5 | public function hasCols() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * |
||
| 313 | * Returns a list of columns. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | * |
||
| 317 | */ |
||
| 318 | 15 | public function getCols() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * |
||
| 325 | * Tracks table references. |
||
| 326 | * |
||
| 327 | * @param string $type FROM, JOIN, etc. |
||
| 328 | * |
||
| 329 | * @param string $spec The table and alias name. |
||
| 330 | * |
||
| 331 | * @return null |
||
| 332 | * |
||
| 333 | * @throws Exception when the reference has already been used. |
||
| 334 | * |
||
| 335 | */ |
||
| 336 | 149 | protected function addTableRef($type, $spec) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * |
||
| 355 | * Adds a FROM element to the query; quotes the table name automatically. |
||
| 356 | * |
||
| 357 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 358 | * |
||
| 359 | * @return $this |
||
| 360 | * |
||
| 361 | */ |
||
| 362 | 139 | public function from($spec) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * |
||
| 370 | * Adds a raw unquoted FROM element to the query; useful for adding FROM |
||
| 371 | * elements that are functions. |
||
| 372 | * |
||
| 373 | * @param string $spec The table specification, e.g. "function_name()". |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | * |
||
| 377 | */ |
||
| 378 | 5 | public function fromRaw($spec) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * |
||
| 386 | * Adds to the $from property and increments the key count. |
||
| 387 | * |
||
| 388 | * @param string $spec The table specification. |
||
| 389 | * |
||
| 390 | * @return $this |
||
| 391 | * |
||
| 392 | */ |
||
| 393 | 149 | protected function addFrom($spec) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * |
||
| 402 | * Adds an aliased sub-select to the query. |
||
| 403 | * |
||
| 404 | * @param string|Select $spec If a Select object, use as the sub-select; |
||
| 405 | * if a string, the sub-select string. |
||
| 406 | * |
||
| 407 | * @param string $name The alias name for the sub-select. |
||
| 408 | * |
||
| 409 | * @return $this |
||
| 410 | * |
||
| 411 | */ |
||
| 412 | 15 | public function fromSubSelect($spec, $name) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * |
||
| 422 | * Formats a sub-SELECT statement, binding values from a Select object as |
||
| 423 | * needed. |
||
| 424 | * |
||
| 425 | * @param string|SelectInterface $spec A sub-SELECT specification. |
||
| 426 | * |
||
| 427 | * @param string $indent Indent each line with this string. |
||
| 428 | * |
||
| 429 | * @return string The sub-SELECT string. |
||
| 430 | * |
||
| 431 | */ |
||
| 432 | 25 | protected function subSelect($spec, $indent) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * |
||
| 445 | * Adds a JOIN table and columns to the query. |
||
| 446 | * |
||
| 447 | * @param string $join The join type: inner, left, natural, etc. |
||
| 448 | * |
||
| 449 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 450 | * |
||
| 451 | * @param string $cond Join on this condition. |
||
| 452 | * |
||
| 453 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 454 | * |
||
| 455 | * @return $this |
||
| 456 | * |
||
| 457 | * @throws Exception |
||
| 458 | * |
||
| 459 | */ |
||
| 460 | 45 | public function join($join, $spec, $cond = null, array $bind = array()) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * |
||
| 472 | * Fixes a JOIN condition to quote names in the condition and prefix it |
||
| 473 | * with a condition type ('ON' is the default and 'USING' is recognized). |
||
| 474 | * |
||
| 475 | * @param string $cond Join on this condition. |
||
| 476 | * |
||
| 477 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | * |
||
| 481 | */ |
||
| 482 | 55 | protected function fixJoinCondition($cond, array $bind) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * |
||
| 504 | * Adds a INNER JOIN table and columns to the query. |
||
| 505 | * |
||
| 506 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 507 | * |
||
| 508 | * @param string $cond Join on this condition. |
||
| 509 | * |
||
| 510 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 511 | * |
||
| 512 | * @return $this |
||
| 513 | * |
||
| 514 | * @throws Exception |
||
| 515 | * |
||
| 516 | */ |
||
| 517 | 10 | public function innerJoin($spec, $cond = null, array $bind = array()) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * |
||
| 524 | * Adds a LEFT JOIN table and columns to the query. |
||
| 525 | * |
||
| 526 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 527 | * |
||
| 528 | * @param string $cond Join on this condition. |
||
| 529 | * |
||
| 530 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 531 | * |
||
| 532 | * @return $this |
||
| 533 | * |
||
| 534 | * @throws Exception |
||
| 535 | * |
||
| 536 | */ |
||
| 537 | 10 | public function leftJoin($spec, $cond = null, array $bind = array()) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * |
||
| 544 | * Adds a JOIN to an aliased subselect and columns to the query. |
||
| 545 | * |
||
| 546 | * @param string $join The join type: inner, left, natural, etc. |
||
| 547 | * |
||
| 548 | * @param string|Select $spec If a Select |
||
| 549 | * object, use as the sub-select; if a string, the sub-select |
||
| 550 | * command string. |
||
| 551 | * |
||
| 552 | * @param string $name The alias name for the sub-select. |
||
| 553 | * |
||
| 554 | * @param string $cond Join on this condition. |
||
| 555 | * |
||
| 556 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 557 | * |
||
| 558 | * @return $this |
||
| 559 | * |
||
| 560 | * @throws Exception |
||
| 561 | * |
||
| 562 | */ |
||
| 563 | 20 | public function joinSubSelect($join, $spec, $name, $cond = null, array $bind = array()) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * |
||
| 578 | * Adds the JOIN to the right place, given whether or not a FROM has been |
||
| 579 | * specified yet. |
||
| 580 | * |
||
| 581 | * @param string $spec The JOIN clause. |
||
| 582 | * |
||
| 583 | * @return $this |
||
| 584 | * |
||
| 585 | */ |
||
| 586 | 55 | protected function addJoin($spec) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * |
||
| 595 | * Adds grouping to the query. |
||
| 596 | * |
||
| 597 | * @param array $spec The column(s) to group by. |
||
| 598 | * |
||
| 599 | * @return $this |
||
| 600 | * |
||
| 601 | */ |
||
| 602 | 5 | public function groupBy(array $spec) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * |
||
| 612 | * Adds a HAVING condition to the query by AND. If the condition has |
||
| 613 | * ?-placeholders, additional arguments to the method will be bound to |
||
| 614 | * those placeholders sequentially. |
||
| 615 | * |
||
| 616 | * @param string $cond The HAVING condition. |
||
| 617 | * |
||
| 618 | * @param array ...$bind arguments to bind to placeholders |
||
| 619 | * |
||
| 620 | * @return $this |
||
| 621 | * |
||
| 622 | */ |
||
| 623 | 10 | public function having($cond, ...$bind) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * |
||
| 631 | * Adds a HAVING condition to the query by AND. If the condition has |
||
| 632 | * ?-placeholders, additional arguments to the method will be bound to |
||
| 633 | * those placeholders sequentially. |
||
| 634 | * |
||
| 635 | * @param string $cond The HAVING condition. |
||
| 636 | * |
||
| 637 | * @param array ...$bind arguments to bind to placeholders |
||
| 638 | * |
||
| 639 | * @return $this |
||
| 640 | * |
||
| 641 | * @see having() |
||
| 642 | * |
||
| 643 | */ |
||
| 644 | 5 | public function orHaving($cond, ...$bind) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * |
||
| 652 | * Sets the limit and count by page number. |
||
| 653 | * |
||
| 654 | * @param int $page Limit results to this page number. |
||
| 655 | * |
||
| 656 | * @return $this |
||
| 657 | * |
||
| 658 | */ |
||
| 659 | 25 | public function page($page) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * |
||
| 668 | * Updates the limit and offset values when changing pagination. |
||
| 669 | * |
||
| 670 | * @return null |
||
| 671 | * |
||
| 672 | */ |
||
| 673 | 25 | protected function setPagingLimitOffset() |
|
| 682 | |||
| 683 | /** |
||
| 684 | * |
||
| 685 | * Returns the page number being selected. |
||
| 686 | * |
||
| 687 | * @return int |
||
| 688 | * |
||
| 689 | */ |
||
| 690 | 5 | public function getPage() |
|
| 694 | |||
| 695 | /** |
||
| 696 | * |
||
| 697 | * Takes the current select properties and retains them, then sets |
||
| 698 | * UNION for the next set of properties. |
||
| 699 | * |
||
| 700 | * @return $this |
||
| 701 | * |
||
| 702 | */ |
||
| 703 | 10 | public function union() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * |
||
| 712 | * Takes the current select properties and retains them, then sets |
||
| 713 | * UNION ALL for the next set of properties. |
||
| 714 | * |
||
| 715 | * @return $this |
||
| 716 | * |
||
| 717 | */ |
||
| 718 | 5 | public function unionAll() |
|
| 724 | |||
| 725 | /** |
||
| 726 | * |
||
| 727 | * Clears the current select properties; generally used after adding a |
||
| 728 | * union. |
||
| 729 | * |
||
| 730 | * @return null |
||
| 731 | * |
||
| 732 | */ |
||
| 733 | 15 | protected function reset() |
|
| 747 | |||
| 748 | /** |
||
| 749 | * |
||
| 750 | * Resets the columns on the SELECT. |
||
| 751 | * |
||
| 752 | * @return $this |
||
| 753 | * |
||
| 754 | */ |
||
| 755 | 15 | public function resetCols() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * |
||
| 763 | * Resets the FROM and JOIN clauses on the SELECT. |
||
| 764 | * |
||
| 765 | * @return $this |
||
| 766 | * |
||
| 767 | */ |
||
| 768 | 15 | public function resetTables() |
|
| 776 | |||
| 777 | /** |
||
| 778 | * |
||
| 779 | * Resets the WHERE clause on the SELECT. |
||
| 780 | * |
||
| 781 | * @return $this |
||
| 782 | * |
||
| 783 | */ |
||
| 784 | 15 | public function resetWhere() |
|
| 789 | |||
| 790 | /** |
||
| 791 | * |
||
| 792 | * Resets the GROUP BY clause on the SELECT. |
||
| 793 | * |
||
| 794 | * @return $this |
||
| 795 | * |
||
| 796 | */ |
||
| 797 | 15 | public function resetGroupBy() |
|
| 802 | |||
| 803 | /** |
||
| 804 | * |
||
| 805 | * Resets the HAVING clause on the SELECT. |
||
| 806 | * |
||
| 807 | * @return $this |
||
| 808 | * |
||
| 809 | */ |
||
| 810 | 15 | public function resetHaving() |
|
| 815 | |||
| 816 | /** |
||
| 817 | * |
||
| 818 | * Resets the ORDER BY clause on the SELECT. |
||
| 819 | * |
||
| 820 | * @return $this |
||
| 821 | * |
||
| 822 | */ |
||
| 823 | 15 | public function resetOrderBy() |
|
| 828 | |||
| 829 | /** |
||
| 830 | * |
||
| 831 | * Resets the UNION and UNION ALL clauses on the SELECT. |
||
| 832 | * |
||
| 833 | * @return $this |
||
| 834 | * |
||
| 835 | */ |
||
| 836 | public function resetUnions() |
||
| 841 | |||
| 842 | /** |
||
| 843 | * |
||
| 844 | * Builds this query object into a string. |
||
| 845 | * |
||
| 846 | * @return string |
||
| 847 | * |
||
| 848 | */ |
||
| 849 | 184 | protected function build() |
|
| 862 | |||
| 863 | /** |
||
| 864 | * |
||
| 865 | * Builds the columns clause. |
||
| 866 | * |
||
| 867 | * @return string |
||
| 868 | * |
||
| 869 | * @throws Exception when there are no columns in the SELECT. |
||
| 870 | * |
||
| 871 | */ |
||
| 872 | 184 | protected function buildCols() |
|
| 889 | |||
| 890 | /** |
||
| 891 | * |
||
| 892 | * Builds the FROM clause. |
||
| 893 | * |
||
| 894 | * @return string |
||
| 895 | * |
||
| 896 | */ |
||
| 897 | 179 | protected function buildFrom() |
|
| 912 | |||
| 913 | /** |
||
| 914 | * |
||
| 915 | * Builds the GROUP BY clause. |
||
| 916 | * |
||
| 917 | * @return string |
||
| 918 | * |
||
| 919 | */ |
||
| 920 | 179 | protected function buildGroupBy() |
|
| 928 | |||
| 929 | /** |
||
| 930 | * |
||
| 931 | * Builds the HAVING clause. |
||
| 932 | * |
||
| 933 | * @return string |
||
| 934 | * |
||
| 935 | */ |
||
| 936 | 179 | protected function buildHaving() |
|
| 944 | |||
| 945 | /** |
||
| 946 | * |
||
| 947 | * Builds the FOR UPDATE clause. |
||
| 948 | * |
||
| 949 | * @return string |
||
| 950 | * |
||
| 951 | */ |
||
| 952 | 179 | protected function buildForUpdate() |
|
| 960 | |||
| 961 | /** |
||
| 962 | * |
||
| 963 | * Sets a limit count on the query. |
||
| 964 | * |
||
| 965 | * @param int $limit The number of rows to select. |
||
| 966 | * |
||
| 967 | * @return $this |
||
| 968 | * |
||
| 969 | */ |
||
| 970 | 30 | public function limit($limit) |
|
| 979 | |||
| 980 | /** |
||
| 981 | * |
||
| 982 | * Sets a limit offset on the query. |
||
| 983 | * |
||
| 984 | * @param int $offset Start returning after this many rows. |
||
| 985 | * |
||
| 986 | * @return $this |
||
| 987 | * |
||
| 988 | */ |
||
| 989 | 30 | public function offset($offset) |
|
| 998 | |||
| 999 | /** |
||
| 1000 | * |
||
| 1001 | * Adds a column order to the query. |
||
| 1002 | * |
||
| 1003 | * @param array $spec The columns and direction to order by. |
||
| 1004 | * |
||
| 1005 | * @return $this |
||
| 1006 | * |
||
| 1007 | */ |
||
| 1008 | 5 | public function orderBy(array $spec) |
|
| 1012 | } |
||
| 1013 |