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 | |||
25 | /** |
||
26 | * |
||
27 | * An array of union SELECT statements. |
||
28 | * |
||
29 | * @var array |
||
30 | * |
||
31 | */ |
||
32 | protected $union = array(); |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * Is this a SELECT FOR UPDATE? |
||
37 | * |
||
38 | * @var |
||
39 | * |
||
40 | */ |
||
41 | protected $for_update = false; |
||
42 | |||
43 | /** |
||
44 | * |
||
45 | * The columns to be selected. |
||
46 | * |
||
47 | * @var array |
||
48 | * |
||
49 | */ |
||
50 | protected $cols = array(); |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * Select from these tables; includes JOIN clauses. |
||
55 | * |
||
56 | * @var array |
||
57 | * |
||
58 | */ |
||
59 | protected $from = array(); |
||
60 | |||
61 | /** |
||
62 | * |
||
63 | * The current key in the `$from` array. |
||
64 | * |
||
65 | * @var int |
||
66 | * |
||
67 | */ |
||
68 | protected $from_key = -1; |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * Tracks which JOIN clauses are attached to which FROM tables. |
||
73 | * |
||
74 | * @var array |
||
75 | * |
||
76 | */ |
||
77 | protected $join = array(); |
||
78 | |||
79 | /** |
||
80 | * |
||
81 | * GROUP BY these columns. |
||
82 | * |
||
83 | * @var array |
||
84 | * |
||
85 | */ |
||
86 | protected $group_by = array(); |
||
87 | |||
88 | /** |
||
89 | * |
||
90 | * The list of HAVING conditions. |
||
91 | * |
||
92 | * @var array |
||
93 | * |
||
94 | */ |
||
95 | protected $having = array(); |
||
96 | |||
97 | /** |
||
98 | * |
||
99 | * The page number to select. |
||
100 | * |
||
101 | * @var int |
||
102 | * |
||
103 | */ |
||
104 | protected $page = 0; |
||
105 | |||
106 | /** |
||
107 | * |
||
108 | * The number of rows per page. |
||
109 | * |
||
110 | * @var int |
||
111 | * |
||
112 | */ |
||
113 | protected $paging = 10; |
||
114 | |||
115 | /** |
||
116 | * |
||
117 | * Tracks table references to avoid duplicate identifiers. |
||
118 | * |
||
119 | * @var array |
||
120 | * |
||
121 | */ |
||
122 | protected $table_refs = array(); |
||
123 | |||
124 | /** |
||
125 | * |
||
126 | * Returns this query object as an SQL statement string. |
||
127 | * |
||
128 | * @return string An SQL statement string. |
||
129 | * |
||
130 | */ |
||
131 | 184 | public function getStatement() |
|
139 | |||
140 | /** |
||
141 | * |
||
142 | * Sets the number of rows per page. |
||
143 | * |
||
144 | * @param int $paging The number of rows to page at. |
||
145 | * |
||
146 | * @return $this |
||
147 | * |
||
148 | */ |
||
149 | 10 | public function setPaging($paging) |
|
157 | |||
158 | /** |
||
159 | * |
||
160 | * Gets the number of rows per page. |
||
161 | * |
||
162 | * @return int The number of rows per page. |
||
163 | * |
||
164 | */ |
||
165 | 10 | public function getPaging() |
|
169 | |||
170 | /** |
||
171 | * |
||
172 | * Makes the select FOR UPDATE (or not). |
||
173 | * |
||
174 | * @param bool $enable Whether or not the SELECT is FOR UPDATE (default |
||
175 | * true). |
||
176 | * |
||
177 | * @return $this |
||
178 | * |
||
179 | */ |
||
180 | 20 | public function forUpdate($enable = true) |
|
185 | |||
186 | /** |
||
187 | * |
||
188 | * Makes the select DISTINCT (or not). |
||
189 | * |
||
190 | * @param bool $enable Whether or not the SELECT is DISTINCT (default |
||
191 | * true). |
||
192 | * |
||
193 | * @return $this |
||
194 | * |
||
195 | */ |
||
196 | 16 | public function distinct($enable = true) |
|
201 | |||
202 | /** |
||
203 | * |
||
204 | * Adds columns to the query. |
||
205 | * |
||
206 | * Multiple calls to cols() will append to the list of columns, not |
||
207 | * overwrite the previous columns. |
||
208 | * |
||
209 | * @param array $cols The column(s) to add to the query. The elements can be |
||
210 | * any mix of these: `array("col", "col AS alias", "col" => "alias")` |
||
211 | * |
||
212 | * @return $this |
||
213 | * |
||
214 | */ |
||
215 | 224 | public function cols(array $cols) |
|
222 | |||
223 | /** |
||
224 | * |
||
225 | * Adds a column and alias to the columns to be selected. |
||
226 | * |
||
227 | * @param mixed $key If an integer, ignored. Otherwise, the column to be |
||
228 | * added. |
||
229 | * |
||
230 | * @param mixed $val If $key was an integer, the column to be added; |
||
231 | * otherwise, the column alias. |
||
232 | * |
||
233 | * @return null |
||
234 | * |
||
235 | */ |
||
236 | 224 | protected function addCol($key, $val) |
|
245 | |||
246 | /** |
||
247 | * |
||
248 | * Adds a column with an alias to the columns to be selected. |
||
249 | * |
||
250 | * @param string $spec The column specification: "col alias", |
||
251 | * "col AS alias", or something else entirely. |
||
252 | * |
||
253 | * @return null |
||
254 | * |
||
255 | */ |
||
256 | 214 | protected function addColWithAlias($spec) |
|
271 | |||
272 | /** |
||
273 | * |
||
274 | * Remove a column via its alias. |
||
275 | * |
||
276 | * @param string $alias The column to remove |
||
277 | * |
||
278 | * @return bool |
||
279 | * |
||
280 | */ |
||
281 | 15 | public function removeCol($alias) |
|
297 | |||
298 | /** |
||
299 | * |
||
300 | * Does the query have any columns in it? |
||
301 | * |
||
302 | * @return bool |
||
303 | * |
||
304 | */ |
||
305 | 5 | public function hasCols() |
|
309 | |||
310 | /** |
||
311 | * |
||
312 | * Returns a list of columns. |
||
313 | * |
||
314 | * @return array |
||
315 | * |
||
316 | */ |
||
317 | 15 | public function getCols() |
|
321 | |||
322 | /** |
||
323 | * |
||
324 | * Tracks table references. |
||
325 | * |
||
326 | * @param string $type FROM, JOIN, etc. |
||
327 | * |
||
328 | * @param string $spec The table and alias name. |
||
329 | * |
||
330 | * @return null |
||
331 | * |
||
332 | * @throws Exception when the reference has already been used. |
||
333 | * |
||
334 | */ |
||
335 | 149 | protected function addTableRef($type, $spec) |
|
351 | |||
352 | /** |
||
353 | * |
||
354 | * Adds a FROM element to the query; quotes the table name automatically. |
||
355 | * |
||
356 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
357 | * |
||
358 | * @return $this |
||
359 | * |
||
360 | */ |
||
361 | 139 | public function from($spec) |
|
366 | |||
367 | /** |
||
368 | * |
||
369 | * Adds a raw unquoted FROM element to the query; useful for adding FROM |
||
370 | * elements that are functions. |
||
371 | * |
||
372 | * @param string $spec The table specification, e.g. "function_name()". |
||
373 | * |
||
374 | * @return $this |
||
375 | * |
||
376 | */ |
||
377 | 5 | public function fromRaw($spec) |
|
382 | |||
383 | /** |
||
384 | * |
||
385 | * Adds to the $from property and increments the key count. |
||
386 | * |
||
387 | * @param string $spec The table specification. |
||
388 | * |
||
389 | * @return $this |
||
390 | * |
||
391 | */ |
||
392 | 149 | protected function addFrom($spec) |
|
398 | |||
399 | /** |
||
400 | * |
||
401 | * Adds an aliased sub-select to the query. |
||
402 | * |
||
403 | * @param string|Select $spec If a Select object, use as the sub-select; |
||
404 | * if a string, the sub-select string. |
||
405 | * |
||
406 | * @param string $name The alias name for the sub-select. |
||
407 | * |
||
408 | * @return $this |
||
409 | * |
||
410 | */ |
||
411 | 15 | public function fromSubSelect($spec, $name) |
|
418 | |||
419 | /** |
||
420 | * |
||
421 | * Formats a sub-SELECT statement, binding values from a Select object as |
||
422 | * needed. |
||
423 | * |
||
424 | * @param string|SelectInterface $spec A sub-SELECT specification. |
||
425 | * |
||
426 | * @param string $indent Indent each line with this string. |
||
427 | * |
||
428 | * @return string The sub-SELECT string. |
||
429 | * |
||
430 | */ |
||
431 | 25 | protected function subSelect($spec, $indent) |
|
441 | |||
442 | /** |
||
443 | * |
||
444 | * Adds a JOIN table and columns to the query. |
||
445 | * |
||
446 | * @param string $join The join type: inner, left, natural, etc. |
||
447 | * |
||
448 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
449 | * |
||
450 | * @param string $cond Join on this condition. |
||
451 | * |
||
452 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
453 | * |
||
454 | * @return $this |
||
455 | * |
||
456 | * @throws Exception |
||
457 | * |
||
458 | */ |
||
459 | 45 | public function join($join, $spec, $cond = null, array $bind = array()) |
|
468 | |||
469 | /** |
||
470 | * |
||
471 | * Fixes a JOIN condition to quote names in the condition and prefix it |
||
472 | * with a condition type ('ON' is the default and 'USING' is recognized). |
||
473 | * |
||
474 | * @param string $cond Join on this condition. |
||
475 | * |
||
476 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
477 | * |
||
478 | * @return string |
||
479 | * |
||
480 | */ |
||
481 | 55 | protected function fixJoinCondition($cond, array $bind) |
|
500 | |||
501 | /** |
||
502 | * |
||
503 | * Adds a INNER JOIN table and columns to the query. |
||
504 | * |
||
505 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
506 | * |
||
507 | * @param string $cond Join on this condition. |
||
508 | * |
||
509 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
510 | * |
||
511 | * @return $this |
||
512 | * |
||
513 | * @throws Exception |
||
514 | * |
||
515 | */ |
||
516 | 10 | public function innerJoin($spec, $cond = null, array $bind = array()) |
|
520 | |||
521 | /** |
||
522 | * |
||
523 | * Adds a LEFT JOIN table and columns to the query. |
||
524 | * |
||
525 | * @param string $spec The table specification; "foo" or "foo AS bar". |
||
526 | * |
||
527 | * @param string $cond Join on this condition. |
||
528 | * |
||
529 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
530 | * |
||
531 | * @return $this |
||
532 | * |
||
533 | * @throws Exception |
||
534 | * |
||
535 | */ |
||
536 | 10 | public function leftJoin($spec, $cond = null, array $bind = array()) |
|
540 | |||
541 | /** |
||
542 | * |
||
543 | * Adds a JOIN to an aliased subselect and columns to the query. |
||
544 | * |
||
545 | * @param string $join The join type: inner, left, natural, etc. |
||
546 | * |
||
547 | * @param string|Select $spec If a Select |
||
548 | * object, use as the sub-select; if a string, the sub-select |
||
549 | * command string. |
||
550 | * |
||
551 | * @param string $name The alias name for the sub-select. |
||
552 | * |
||
553 | * @param string $cond Join on this condition. |
||
554 | * |
||
555 | * @param array $bind Values to bind to ?-placeholders in the condition. |
||
556 | * |
||
557 | * @return $this |
||
558 | * |
||
559 | * @throws Exception |
||
560 | * |
||
561 | */ |
||
562 | 20 | public function joinSubSelect($join, $spec, $name, $cond = null, array $bind = array()) |
|
574 | |||
575 | /** |
||
576 | * |
||
577 | * Adds the JOIN to the right place, given whether or not a FROM has been |
||
578 | * specified yet. |
||
579 | * |
||
580 | * @param string $spec The JOIN clause. |
||
581 | * |
||
582 | * @return $this |
||
583 | * |
||
584 | */ |
||
585 | 55 | protected function addJoin($spec) |
|
591 | |||
592 | /** |
||
593 | * |
||
594 | * Adds grouping to the query. |
||
595 | * |
||
596 | * @param array $spec The column(s) to group by. |
||
597 | * |
||
598 | * @return $this |
||
599 | * |
||
600 | */ |
||
601 | 5 | public function groupBy(array $spec) |
|
608 | |||
609 | /** |
||
610 | * |
||
611 | * Adds a HAVING condition to the query by AND. If the condition has |
||
612 | * ?-placeholders, additional arguments to the method will be bound to |
||
613 | * those placeholders sequentially. |
||
614 | * |
||
615 | * @param string $cond The HAVING condition. |
||
616 | * |
||
617 | * @param array ...$bind arguments to bind to placeholders |
||
618 | * |
||
619 | * @return $this |
||
620 | * |
||
621 | */ |
||
622 | 10 | public function having($cond, ...$bind) |
|
627 | |||
628 | /** |
||
629 | * |
||
630 | * Adds a HAVING condition to the query by AND. If the condition has |
||
631 | * ?-placeholders, additional arguments to the method will be bound to |
||
632 | * those placeholders sequentially. |
||
633 | * |
||
634 | * @param string $cond The HAVING condition. |
||
635 | * |
||
636 | * @param array ...$bind arguments to bind to placeholders |
||
637 | * |
||
638 | * @return $this |
||
639 | * |
||
640 | * @see having() |
||
641 | * |
||
642 | */ |
||
643 | 5 | public function orHaving($cond, ...$bind) |
|
648 | |||
649 | /** |
||
650 | * |
||
651 | * Sets the limit and count by page number. |
||
652 | * |
||
653 | * @param int $page Limit results to this page number. |
||
654 | * |
||
655 | * @return $this |
||
656 | * |
||
657 | */ |
||
658 | 25 | public function page($page) |
|
664 | |||
665 | /** |
||
666 | * |
||
667 | * Updates the limit and offset values when changing pagination. |
||
668 | * |
||
669 | * @return null |
||
670 | * |
||
671 | */ |
||
672 | 25 | protected function setPagingLimitOffset() |
|
681 | |||
682 | /** |
||
683 | * |
||
684 | * Returns the page number being selected. |
||
685 | * |
||
686 | * @return int |
||
687 | * |
||
688 | */ |
||
689 | 5 | public function getPage() |
|
693 | |||
694 | /** |
||
695 | * |
||
696 | * Takes the current select properties and retains them, then sets |
||
697 | * UNION for the next set of properties. |
||
698 | * |
||
699 | * @return $this |
||
700 | * |
||
701 | */ |
||
702 | 10 | public function union() |
|
708 | |||
709 | /** |
||
710 | * |
||
711 | * Takes the current select properties and retains them, then sets |
||
712 | * UNION ALL for the next set of properties. |
||
713 | * |
||
714 | * @return $this |
||
715 | * |
||
716 | */ |
||
717 | 5 | public function unionAll() |
|
723 | |||
724 | /** |
||
725 | * |
||
726 | * Returns the LIMIT value. |
||
727 | * |
||
728 | * @return int |
||
729 | * |
||
730 | */ |
||
731 | 10 | public function getLimit() |
|
735 | |||
736 | /** |
||
737 | * |
||
738 | * Returns the OFFSET value. |
||
739 | * |
||
740 | * @return int |
||
741 | * |
||
742 | */ |
||
743 | 10 | public function getOffset() |
|
747 | |||
748 | /** |
||
749 | * |
||
750 | * Clears the current select properties; generally used after adding a |
||
751 | * union. |
||
752 | * |
||
753 | * @return null |
||
754 | * |
||
755 | */ |
||
756 | 15 | protected function reset() |
|
770 | |||
771 | /** |
||
772 | * |
||
773 | * Resets the columns on the SELECT. |
||
774 | * |
||
775 | * @return $this |
||
776 | * |
||
777 | */ |
||
778 | 15 | public function resetCols() |
|
783 | |||
784 | /** |
||
785 | * |
||
786 | * Resets the FROM and JOIN clauses on the SELECT. |
||
787 | * |
||
788 | * @return $this |
||
789 | * |
||
790 | */ |
||
791 | 15 | public function resetTables() |
|
799 | |||
800 | /** |
||
801 | * |
||
802 | * Resets the WHERE clause on the SELECT. |
||
803 | * |
||
804 | * @return $this |
||
805 | * |
||
806 | */ |
||
807 | 15 | public function resetWhere() |
|
812 | |||
813 | /** |
||
814 | * |
||
815 | * Resets the GROUP BY clause on the SELECT. |
||
816 | * |
||
817 | * @return $this |
||
818 | * |
||
819 | */ |
||
820 | 15 | public function resetGroupBy() |
|
825 | |||
826 | /** |
||
827 | * |
||
828 | * Resets the HAVING clause on the SELECT. |
||
829 | * |
||
830 | * @return $this |
||
831 | * |
||
832 | */ |
||
833 | 15 | public function resetHaving() |
|
838 | |||
839 | /** |
||
840 | * |
||
841 | * Resets the ORDER BY clause on the SELECT. |
||
842 | * |
||
843 | * @return $this |
||
844 | * |
||
845 | */ |
||
846 | 15 | public function resetOrderBy() |
|
851 | |||
852 | /** |
||
853 | * |
||
854 | * Resets the UNION and UNION ALL clauses on the SELECT. |
||
855 | * |
||
856 | * @return $this |
||
857 | * |
||
858 | */ |
||
859 | public function resetUnions() |
||
864 | |||
865 | /** |
||
866 | * |
||
867 | * Builds this query object into a string. |
||
868 | * |
||
869 | * @return string |
||
870 | * |
||
871 | */ |
||
872 | 184 | protected function build() |
|
885 | |||
886 | /** |
||
887 | * |
||
888 | * Builds the columns clause. |
||
889 | * |
||
890 | * @return string |
||
891 | * |
||
892 | * @throws Exception when there are no columns in the SELECT. |
||
893 | * |
||
894 | */ |
||
895 | 184 | protected function buildCols() |
|
912 | |||
913 | /** |
||
914 | * |
||
915 | * Builds the FROM clause. |
||
916 | * |
||
917 | * @return string |
||
918 | * |
||
919 | */ |
||
920 | 179 | protected function buildFrom() |
|
935 | |||
936 | /** |
||
937 | * |
||
938 | * Builds the GROUP BY clause. |
||
939 | * |
||
940 | * @return string |
||
941 | * |
||
942 | */ |
||
943 | 179 | protected function buildGroupBy() |
|
951 | |||
952 | /** |
||
953 | * |
||
954 | * Builds the HAVING clause. |
||
955 | * |
||
956 | * @return string |
||
957 | * |
||
958 | */ |
||
959 | 179 | protected function buildHaving() |
|
967 | |||
968 | /** |
||
969 | * |
||
970 | * Builds the FOR UPDATE clause. |
||
971 | * |
||
972 | * @return string |
||
973 | * |
||
974 | */ |
||
975 | 179 | protected function buildForUpdate() |
|
983 | |||
984 | /** |
||
985 | * |
||
986 | * Sets a limit count on the query. |
||
987 | * |
||
988 | * @param int $limit The number of rows to select. |
||
989 | * |
||
990 | * @return $this |
||
991 | * |
||
992 | */ |
||
993 | 30 | public function limit($limit) |
|
1002 | |||
1003 | /** |
||
1004 | * |
||
1005 | * Sets a limit offset on the query. |
||
1006 | * |
||
1007 | * @param int $offset Start returning after this many rows. |
||
1008 | * |
||
1009 | * @return $this |
||
1010 | * |
||
1011 | */ |
||
1012 | 30 | public function offset($offset) |
|
1021 | |||
1022 | /** |
||
1023 | * |
||
1024 | * Adds a column order to the query. |
||
1025 | * |
||
1026 | * @param array $spec The columns and direction to order by. |
||
1027 | * |
||
1028 | * @return $this |
||
1029 | * |
||
1030 | */ |
||
1031 | 5 | public function orderBy(array $spec) |
|
1035 | } |
||
1036 |