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 | /** |
||
| 24 | * |
||
| 25 | * An array of union SELECT statements. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | * |
||
| 29 | */ |
||
| 30 | protected $union = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * |
||
| 34 | * Is this a SELECT FOR UPDATE? |
||
| 35 | * |
||
| 36 | * @var |
||
| 37 | * |
||
| 38 | */ |
||
| 39 | protected $for_update = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * |
||
| 43 | * The columns to be selected. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | protected $cols = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | * Select from these tables; includes JOIN clauses. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | * |
||
| 56 | */ |
||
| 57 | protected $from = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * |
||
| 61 | * The current key in the `$from` array. |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | * |
||
| 65 | */ |
||
| 66 | protected $from_key = -1; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * |
||
| 70 | * GROUP BY these columns. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | * |
||
| 74 | */ |
||
| 75 | protected $group_by = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | * The list of HAVING conditions. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | * |
||
| 83 | */ |
||
| 84 | protected $having = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * |
||
| 88 | * The page number to select. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | * |
||
| 92 | */ |
||
| 93 | protected $page = 0; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * |
||
| 97 | * The number of rows per page. |
||
| 98 | * |
||
| 99 | * @var int |
||
| 100 | * |
||
| 101 | */ |
||
| 102 | protected $paging = 10; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * |
||
| 106 | * Tracks table references to avoid duplicate identifiers. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | * |
||
| 110 | */ |
||
| 111 | protected $table_refs = array(); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | * Returns this query object as an SQL statement string. |
||
| 116 | * |
||
| 117 | * @return string An SQL statement string. |
||
| 118 | * |
||
| 119 | */ |
||
| 120 | 174 | public function getStatement() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * |
||
| 131 | * Sets the number of rows per page. |
||
| 132 | * |
||
| 133 | * @param int $paging The number of rows to page at. |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | * |
||
| 137 | */ |
||
| 138 | 10 | public function setPaging($paging) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * |
||
| 149 | * Gets the number of rows per page. |
||
| 150 | * |
||
| 151 | * @return int The number of rows per page. |
||
| 152 | * |
||
| 153 | */ |
||
| 154 | 10 | public function getPaging() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * |
||
| 161 | * Makes the select FOR UPDATE (or not). |
||
| 162 | * |
||
| 163 | * @param bool $enable Whether or not the SELECT is FOR UPDATE (default |
||
| 164 | * true). |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | * |
||
| 168 | */ |
||
| 169 | 5 | public function forUpdate($enable = true) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * |
||
| 177 | * Makes the select DISTINCT (or not). |
||
| 178 | * |
||
| 179 | * @param bool $enable Whether or not the SELECT is DISTINCT (default |
||
| 180 | * true). |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | * |
||
| 184 | */ |
||
| 185 | 16 | public function distinct($enable = true) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * |
||
| 193 | * Adds columns to the query. |
||
| 194 | * |
||
| 195 | * Multiple calls to cols() will append to the list of columns, not |
||
| 196 | * overwrite the previous columns. |
||
| 197 | * |
||
| 198 | * @param array $cols The column(s) to add to the query. The elements can be |
||
| 199 | * any mix of these: `array("col", "col AS alias", "col" => "alias")` |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | * |
||
| 203 | */ |
||
| 204 | 214 | public function cols(array $cols) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * |
||
| 214 | * Adds a column and alias to the columns to be selected. |
||
| 215 | * |
||
| 216 | * @param mixed $key If an integer, ignored. Otherwise, the column to be |
||
| 217 | * added. |
||
| 218 | * |
||
| 219 | * @param mixed $val If $key was an integer, the column to be added; |
||
| 220 | * otherwise, the column alias. |
||
| 221 | * |
||
| 222 | * @return null |
||
| 223 | * |
||
| 224 | */ |
||
| 225 | 214 | protected function addCol($key, $val) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * |
||
| 237 | * Adds a column with an alias to the columns to be selected. |
||
| 238 | * |
||
| 239 | * @param string $spec The column specification: "col alias", |
||
| 240 | * "col AS alias", or something else entirely. |
||
| 241 | * |
||
| 242 | * @return null |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | 204 | protected function addColWithAlias($spec) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * |
||
| 263 | * Remove a column via its alias. |
||
| 264 | * |
||
| 265 | * @param string $alias The column to remove |
||
| 266 | * |
||
| 267 | * @return null |
||
| 268 | * |
||
| 269 | */ |
||
| 270 | 15 | public function removeCol($alias) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * |
||
| 289 | * Does the query have any columns in it? |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | * |
||
| 293 | */ |
||
| 294 | 5 | public function hasCols() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * |
||
| 301 | * Returns a list of columns. |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | * |
||
| 305 | */ |
||
| 306 | 15 | public function getCols() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * |
||
| 313 | * Tracks table references. |
||
| 314 | * |
||
| 315 | * @param string $type FROM, JOIN, etc. |
||
| 316 | * |
||
| 317 | * @param string $spec The table and alias name. |
||
| 318 | * |
||
| 319 | * @return null |
||
| 320 | * |
||
| 321 | * @throws Exception when the reference has already been used. |
||
| 322 | * |
||
| 323 | */ |
||
| 324 | 139 | protected function addTableRef($type, $spec) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * |
||
| 343 | * Adds a FROM element to the query; quotes the table name automatically. |
||
| 344 | * |
||
| 345 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | * |
||
| 349 | */ |
||
| 350 | 129 | public function from($spec) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * |
||
| 358 | * Adds a raw unquoted FROM element to the query; useful for adding FROM |
||
| 359 | * elements that are functions. |
||
| 360 | * |
||
| 361 | * @param string $spec The table specification, e.g. "function_name()". |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | * |
||
| 365 | */ |
||
| 366 | 5 | public function fromRaw($spec) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * |
||
| 374 | * Adds to the $from property and increments the key count. |
||
| 375 | * |
||
| 376 | * @param string $spec The table specification. |
||
| 377 | * |
||
| 378 | * @return $this |
||
| 379 | * |
||
| 380 | */ |
||
| 381 | 134 | protected function addFrom($spec) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * |
||
| 390 | * Adds an aliased sub-select to the query. |
||
| 391 | * |
||
| 392 | * @param string|Select $spec If a Select object, use as the sub-select; |
||
| 393 | * if a string, the sub-select string. |
||
| 394 | * |
||
| 395 | * @param string $name The alias name for the sub-select. |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | * |
||
| 399 | */ |
||
| 400 | 15 | public function fromSubSelect($spec, $name) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * |
||
| 412 | * Formats a sub-SELECT statement, binding values from a Select object as |
||
| 413 | * needed. |
||
| 414 | * |
||
| 415 | * @param string|SelectInterface $spec A sub-SELECT specification. |
||
| 416 | * |
||
| 417 | * @param string $indent Indent each line with this string. |
||
| 418 | * |
||
| 419 | * @return string The sub-SELECT string. |
||
| 420 | * |
||
| 421 | */ |
||
| 422 | 20 | protected function subSelect($spec, $indent) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * |
||
| 435 | * Adds a JOIN table and columns to the query. |
||
| 436 | * |
||
| 437 | * @param string $join The join type: inner, left, natural, etc. |
||
| 438 | * |
||
| 439 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 440 | * |
||
| 441 | * @param string $cond Join on this condition. |
||
| 442 | * |
||
| 443 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | * |
||
| 447 | * @throws Exception |
||
| 448 | * |
||
| 449 | */ |
||
| 450 | 40 | public function join($join, $spec, $cond = null, array $bind = array()) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * |
||
| 467 | * Fixes a JOIN condition to quote names in the condition and prefix it |
||
| 468 | * with a condition type ('ON' is the default and 'USING' is recognized). |
||
| 469 | * |
||
| 470 | * @param string $cond Join on this condition. |
||
| 471 | * |
||
| 472 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | * |
||
| 476 | */ |
||
| 477 | 45 | protected function fixJoinCondition($cond, array $bind) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * |
||
| 499 | * Adds a INNER JOIN table and columns to the query. |
||
| 500 | * |
||
| 501 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 502 | * |
||
| 503 | * @param string $cond Join on this condition. |
||
| 504 | * |
||
| 505 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 506 | * |
||
| 507 | * @return $this |
||
| 508 | * |
||
| 509 | * @throws Exception |
||
| 510 | * |
||
| 511 | */ |
||
| 512 | 10 | public function innerJoin($spec, $cond = null, array $bind = array()) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * |
||
| 519 | * Adds a LEFT JOIN table and columns to the query. |
||
| 520 | * |
||
| 521 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
| 522 | * |
||
| 523 | * @param string $cond Join on this condition. |
||
| 524 | * |
||
| 525 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 526 | * |
||
| 527 | * @return $this |
||
| 528 | * |
||
| 529 | * @throws Exception |
||
| 530 | * |
||
| 531 | */ |
||
| 532 | 10 | public function leftJoin($spec, $cond = null, array $bind = array()) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * |
||
| 539 | * Adds a JOIN to an aliased subselect and columns to the query. |
||
| 540 | * |
||
| 541 | * @param string $join The join type: inner, left, natural, etc. |
||
| 542 | * |
||
| 543 | * @param string|Select $spec If a Select |
||
| 544 | * object, use as the sub-select; if a string, the sub-select |
||
| 545 | * command string. |
||
| 546 | * |
||
| 547 | * @param string $name The alias name for the sub-select. |
||
| 548 | * |
||
| 549 | * @param string $cond Join on this condition. |
||
| 550 | * |
||
| 551 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
| 552 | * |
||
| 553 | * @return $this |
||
| 554 | * |
||
| 555 | * @throws Exception |
||
| 556 | * |
||
| 557 | */ |
||
| 558 | 15 | public function joinSubSelect($join, $spec, $name, $cond = null, array $bind = array()) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * |
||
| 578 | * Adds grouping to the query. |
||
| 579 | * |
||
| 580 | * @param array $spec The column(s) to group by. |
||
| 581 | * |
||
| 582 | * @return $this |
||
| 583 | * |
||
| 584 | */ |
||
| 585 | 5 | public function groupBy(array $spec) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * |
||
| 595 | * Adds a HAVING condition to the query by AND. If the condition has |
||
| 596 | * ?-placeholders, additional arguments to the method will be bound to |
||
| 597 | * those placeholders sequentially. |
||
| 598 | * |
||
| 599 | * @param string $cond The HAVING condition. |
||
| 600 | * |
||
| 601 | * @return $this |
||
| 602 | * |
||
| 603 | */ |
||
| 604 | 10 | public function having($cond) |
|
| 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 | * @return $this |
||
| 619 | * |
||
| 620 | * @see having() |
||
| 621 | * |
||
| 622 | */ |
||
| 623 | 5 | public function orHaving($cond) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * |
||
| 631 | * Sets the limit and count by page number. |
||
| 632 | * |
||
| 633 | * @param int $page Limit results to this page number. |
||
| 634 | * |
||
| 635 | * @return $this |
||
| 636 | * |
||
| 637 | */ |
||
| 638 | 10 | public function page($page) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * |
||
| 647 | * Updates the limit and offset values when changing pagination. |
||
| 648 | * |
||
| 649 | * @return null |
||
| 650 | * |
||
| 651 | */ |
||
| 652 | 10 | protected function setPagingLimitOffset() |
|
| 661 | |||
| 662 | /** |
||
| 663 | * |
||
| 664 | * Returns the page number being selected. |
||
| 665 | * |
||
| 666 | * @return int |
||
| 667 | * |
||
| 668 | */ |
||
| 669 | 5 | public function getPage() |
|
| 673 | |||
| 674 | /** |
||
| 675 | * |
||
| 676 | * Takes the current select properties and retains them, then sets |
||
| 677 | * UNION for the next set of properties. |
||
| 678 | * |
||
| 679 | * @return $this |
||
| 680 | * |
||
| 681 | */ |
||
| 682 | 10 | public function union() |
|
| 688 | |||
| 689 | /** |
||
| 690 | * |
||
| 691 | * Takes the current select properties and retains them, then sets |
||
| 692 | * UNION ALL for the next set of properties. |
||
| 693 | * |
||
| 694 | * @return $this |
||
| 695 | * |
||
| 696 | */ |
||
| 697 | 5 | public function unionAll() |
|
| 703 | |||
| 704 | /** |
||
| 705 | * |
||
| 706 | * Returns the LIMIT value. |
||
| 707 | * |
||
| 708 | * @return int |
||
| 709 | * |
||
| 710 | */ |
||
| 711 | 10 | public function getLimit() |
|
| 715 | |||
| 716 | /** |
||
| 717 | * |
||
| 718 | * Returns the OFFSET value. |
||
| 719 | * |
||
| 720 | * @return int |
||
| 721 | * |
||
| 722 | */ |
||
| 723 | 10 | public function getOffset() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * |
||
| 730 | * Clears the current select properties; generally used after adding a |
||
| 731 | * union. |
||
| 732 | * |
||
| 733 | * @return null |
||
| 734 | * |
||
| 735 | */ |
||
| 736 | 15 | protected function reset() |
|
| 752 | |||
| 753 | /** |
||
| 754 | * if user needs to reset |
||
| 755 | */ |
||
| 756 | public function resetCols() |
||
| 760 | |||
| 761 | public function resetOrderBy() |
||
| 765 | |||
| 766 | public function resetLimit() |
||
| 770 | |||
| 771 | public function resetOffset() |
||
| 775 | |||
| 776 | public function resetPage() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * |
||
| 783 | * Builds this query object into a string. |
||
| 784 | * |
||
| 785 | * @return string |
||
| 786 | * |
||
| 787 | */ |
||
| 788 | 174 | protected function build() |
|
| 801 | |||
| 802 | /** |
||
| 803 | * |
||
| 804 | * Builds the columns clause. |
||
| 805 | * |
||
| 806 | * @return string |
||
| 807 | * |
||
| 808 | * @throws Exception when there are no columns in the SELECT. |
||
| 809 | * |
||
| 810 | */ |
||
| 811 | 174 | protected function buildCols() |
|
| 828 | |||
| 829 | /** |
||
| 830 | * |
||
| 831 | * Builds the FROM clause. |
||
| 832 | * |
||
| 833 | * @return string |
||
| 834 | * |
||
| 835 | */ |
||
| 836 | 169 | protected function buildFrom() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * |
||
| 851 | * Builds the GROUP BY clause. |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | * |
||
| 855 | */ |
||
| 856 | 169 | protected function buildGroupBy() |
|
| 864 | |||
| 865 | /** |
||
| 866 | * |
||
| 867 | * Builds the HAVING clause. |
||
| 868 | * |
||
| 869 | * @return string |
||
| 870 | * |
||
| 871 | */ |
||
| 872 | 169 | protected function buildHaving() |
|
| 880 | |||
| 881 | /** |
||
| 882 | * |
||
| 883 | * Builds the FOR UPDATE clause. |
||
| 884 | * |
||
| 885 | * @return string |
||
| 886 | * |
||
| 887 | */ |
||
| 888 | 169 | protected function buildForUpdate() |
|
| 896 | |||
| 897 | /** |
||
| 898 | * |
||
| 899 | * Adds a WHERE condition to the query by AND. If the condition has |
||
| 900 | * ?-placeholders, additional arguments to the method will be bound to |
||
| 901 | * those placeholders sequentially. |
||
| 902 | * |
||
| 903 | * @param string $cond The WHERE condition. |
||
| 904 | * @param mixed ...$bind arguments to bind to placeholders |
||
| 905 | * |
||
| 906 | * @return $this |
||
| 907 | * |
||
| 908 | */ |
||
| 909 | 30 | public function where($cond) |
|
| 914 | |||
| 915 | /** |
||
| 916 | * |
||
| 917 | * Adds a WHERE condition to the query by OR. If the condition has |
||
| 918 | * ?-placeholders, additional arguments to the method will be bound to |
||
| 919 | * those placeholders sequentially. |
||
| 920 | * |
||
| 921 | * @param string $cond The WHERE condition. |
||
| 922 | * @param mixed ...$bind arguments to bind to placeholders |
||
| 923 | * |
||
| 924 | * @return $this |
||
| 925 | * |
||
| 926 | * @see where() |
||
| 927 | * |
||
| 928 | */ |
||
| 929 | 5 | public function orWhere($cond) |
|
| 934 | |||
| 935 | /** |
||
| 936 | * |
||
| 937 | * Sets a limit count on the query. |
||
| 938 | * |
||
| 939 | * @param int $limit The number of rows to select. |
||
| 940 | * |
||
| 941 | * @return $this |
||
| 942 | * |
||
| 943 | */ |
||
| 944 | 15 | public function limit($limit) |
|
| 953 | |||
| 954 | /** |
||
| 955 | * |
||
| 956 | * Sets a limit offset on the query. |
||
| 957 | * |
||
| 958 | * @param int $offset Start returning after this many rows. |
||
| 959 | * |
||
| 960 | * @return $this |
||
| 961 | * |
||
| 962 | */ |
||
| 963 | 15 | public function offset($offset) |
|
| 972 | |||
| 973 | /** |
||
| 974 | * |
||
| 975 | * Adds a column order to the query. |
||
| 976 | * |
||
| 977 | * @param array $spec The columns and direction to order by. |
||
| 978 | * |
||
| 979 | * @return $this |
||
| 980 | * |
||
| 981 | */ |
||
| 982 | 5 | public function orderBy(array $spec) |
|
| 986 | } |
||
| 987 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.