Complex classes like BuildsQueries 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 BuildsQueries, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait BuildsQueries |
||
10 | { |
||
11 | /** |
||
12 | * Set the columns to be selected. |
||
13 | * |
||
14 | * @param array|mixed $columns |
||
15 | * @return $this |
||
16 | */ |
||
17 | public function select($columns = ['*']) |
||
23 | |||
24 | /** |
||
25 | * Add a new "raw" select expression to the query. |
||
26 | * |
||
27 | * @param string $expression |
||
28 | * @param array $bindings |
||
29 | * @return $this |
||
30 | */ |
||
31 | public function selectRaw($expression, array $bindings = []) |
||
37 | |||
38 | /** |
||
39 | * Add a subselect expression to the query. |
||
40 | * |
||
41 | * @param \Closure|\Illuminate\Database\Query\Builder|string $query |
||
42 | * @param string $as |
||
43 | * @return $this |
||
44 | * |
||
45 | * @throws \InvalidArgumentException |
||
46 | */ |
||
47 | public function selectSub($query, $as) |
||
53 | |||
54 | /** |
||
55 | * Add a new select column to the query. |
||
56 | * |
||
57 | * @param array|mixed $column |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function addSelect($column) |
||
66 | |||
67 | /** |
||
68 | * Force the query to only return distinct results. |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function distinct() |
||
78 | |||
79 | /** |
||
80 | * Set the table which the query is targeting. |
||
81 | * |
||
82 | * @param string $table |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function from($table) |
||
91 | |||
92 | /** |
||
93 | * Add a join clause to the query. |
||
94 | * |
||
95 | * @param string $table |
||
96 | * @param string $first |
||
97 | * @param string $operator |
||
98 | * @param string $second |
||
99 | * @param string $type |
||
100 | * @param bool $where |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false) |
||
109 | |||
110 | /** |
||
111 | * Add a "join where" clause to the query. |
||
112 | * |
||
113 | * @param string $table |
||
114 | * @param string $first |
||
115 | * @param string $operator |
||
116 | * @param string $second |
||
117 | * @param string $type |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function joinWhere($table, $first, $operator, $second, $type = 'inner') |
||
126 | |||
127 | /** |
||
128 | * Add a left join to the query. |
||
129 | * |
||
130 | * @param string $table |
||
131 | * @param string $first |
||
132 | * @param string $operator |
||
133 | * @param string $second |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function leftJoin($table, $first, $operator = null, $second = null) |
||
142 | |||
143 | /** |
||
144 | * Add a "join where" clause to the query. |
||
145 | * |
||
146 | * @param string $table |
||
147 | * @param string $first |
||
148 | * @param string $operator |
||
149 | * @param string $second |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function leftJoinWhere($table, $first, $operator, $second) |
||
158 | |||
159 | /** |
||
160 | * Add a right join to the query. |
||
161 | * |
||
162 | * @param string $table |
||
163 | * @param string $first |
||
164 | * @param string $operator |
||
165 | * @param string $second |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function rightJoin($table, $first, $operator = null, $second = null) |
||
174 | |||
175 | /** |
||
176 | * Add a "right join where" clause to the query. |
||
177 | * |
||
178 | * @param string $table |
||
179 | * @param string $first |
||
180 | * @param string $operator |
||
181 | * @param string $second |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function rightJoinWhere($table, $first, $operator, $second) |
||
190 | |||
191 | /** |
||
192 | * Add a "cross join" clause to the query. |
||
193 | * |
||
194 | * @param string $table |
||
195 | * @param string $first |
||
196 | * @param string $operator |
||
197 | * @param string $second |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function crossJoin($table, $first = null, $operator = null, $second = null) |
||
206 | |||
207 | /** |
||
208 | * Merge an array of where clauses and bindings. |
||
209 | * |
||
210 | * @param array $wheres |
||
211 | * @param array $bindings |
||
212 | * @return void |
||
213 | */ |
||
214 | public function mergeWheres($wheres, $bindings) |
||
220 | |||
221 | /** |
||
222 | * Pass the query to a given callback. |
||
223 | * |
||
224 | * @param \Closure $callback |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function tap($callback) |
||
233 | |||
234 | /** |
||
235 | * Add a basic where clause to the query. |
||
236 | * |
||
237 | * @param string|array|\Closure $column |
||
238 | * @param string $operator |
||
239 | * @param mixed $value |
||
240 | * @param string $boolean |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function where($column, $operator = null, $value = null, $boolean = 'and') |
||
249 | |||
250 | /** |
||
251 | * Add an "or where" clause to the query. |
||
252 | * |
||
253 | * @param \Closure|string $column |
||
254 | * @param string $operator |
||
255 | * @param mixed $value |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function orWhere($column, $operator = null, $value = null) |
||
264 | |||
265 | /** |
||
266 | * Add a "where" clause comparing two columns to the query. |
||
267 | * |
||
268 | * @param string|array $first |
||
269 | * @param string|null $operator |
||
270 | * @param string|null $second |
||
271 | * @param string|null $boolean |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') |
||
280 | |||
281 | /** |
||
282 | * Add an "or where" clause comparing two columns to the query. |
||
283 | * |
||
284 | * @param string|array $first |
||
285 | * @param string|null $operator |
||
286 | * @param string|null $second |
||
287 | * @return $this |
||
288 | */ |
||
289 | public function orWhereColumn($first, $operator = null, $second = null) |
||
295 | |||
296 | /** |
||
297 | * Add a raw where clause to the query. |
||
298 | * |
||
299 | * @param string $sql |
||
300 | * @param mixed $bindings |
||
301 | * @param string $boolean |
||
302 | * @return $this |
||
303 | */ |
||
304 | public function whereRaw($sql, $bindings = [], $boolean = 'and') |
||
310 | |||
311 | /** |
||
312 | * Add a raw or where clause to the query. |
||
313 | * |
||
314 | * @param string $sql |
||
315 | * @param array $bindings |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function orWhereRaw($sql, array $bindings = []) |
||
324 | |||
325 | /** |
||
326 | * Add a "where in" clause to the query. |
||
327 | * |
||
328 | * @param string $column |
||
329 | * @param mixed $values |
||
330 | * @param string $boolean |
||
331 | * @param bool $not |
||
332 | * @return $this |
||
333 | */ |
||
334 | public function whereIn($column, $values, $boolean = 'and', $not = false) |
||
340 | |||
341 | /** |
||
342 | * Add an "or where in" clause to the query. |
||
343 | * |
||
344 | * @param string $column |
||
345 | * @param mixed $values |
||
346 | * @return $this |
||
347 | */ |
||
348 | public function orWhereIn($column, $values) |
||
354 | |||
355 | /** |
||
356 | * Add a "where not in" clause to the query. |
||
357 | * |
||
358 | * @param string $column |
||
359 | * @param mixed $values |
||
360 | * @param string $boolean |
||
361 | * @return $this |
||
362 | */ |
||
363 | public function whereNotIn($column, $values, $boolean = 'and') |
||
369 | |||
370 | /** |
||
371 | * Add an "or where not in" clause to the query. |
||
372 | * |
||
373 | * @param string $column |
||
374 | * @param mixed $values |
||
375 | * @return $this |
||
376 | */ |
||
377 | public function orWhereNotIn($column, $values) |
||
383 | |||
384 | /** |
||
385 | * Add a "where null" clause to the query. |
||
386 | * |
||
387 | * @param string $column |
||
388 | * @param string $boolean |
||
389 | * @param bool $not |
||
390 | * @return $this |
||
391 | */ |
||
392 | public function whereNull($column, $boolean = 'and', $not = false) |
||
398 | |||
399 | /** |
||
400 | * Add an "or where null" clause to the query. |
||
401 | * |
||
402 | * @param string $column |
||
403 | * @return $this |
||
404 | */ |
||
405 | public function orWhereNull($column) |
||
411 | |||
412 | /** |
||
413 | * Add a "where not null" clause to the query. |
||
414 | * |
||
415 | * @param string $column |
||
416 | * @param string $boolean |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function whereNotNull($column, $boolean = 'and') |
||
425 | |||
426 | /** |
||
427 | * Add a where between statement to the query. |
||
428 | * |
||
429 | * @param string $column |
||
430 | * @param array $values |
||
431 | * @param string $boolean |
||
432 | * @param bool $not |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function whereBetween($column, array $values, $boolean = 'and', $not = false) |
||
441 | |||
442 | /** |
||
443 | * Add an or where between statement to the query. |
||
444 | * |
||
445 | * @param string $column |
||
446 | * @param array $values |
||
447 | * @return $this |
||
448 | */ |
||
449 | public function orWhereBetween($column, array $values) |
||
455 | |||
456 | /** |
||
457 | * Add a where not between statement to the query. |
||
458 | * |
||
459 | * @param string $column |
||
460 | * @param array $values |
||
461 | * @param string $boolean |
||
462 | * @return $this |
||
463 | */ |
||
464 | public function whereNotBetween($column, array $values, $boolean = 'and') |
||
470 | |||
471 | /** |
||
472 | * Add an or where not between statement to the query. |
||
473 | * |
||
474 | * @param string $column |
||
475 | * @param array $values |
||
476 | * @return $this |
||
477 | */ |
||
478 | public function orWhereNotBetween($column, array $values) |
||
484 | |||
485 | /** |
||
486 | * Add an "or where not null" clause to the query. |
||
487 | * |
||
488 | * @param string $column |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function orWhereNotNull($column) |
||
497 | |||
498 | /** |
||
499 | * Add a "where date" statement to the query. |
||
500 | * |
||
501 | * @param string $column |
||
502 | * @param string $operator |
||
503 | * @param mixed $value |
||
504 | * @param string $boolean |
||
505 | * @return $this |
||
506 | */ |
||
507 | public function whereDate($column, $operator, $value = null, $boolean = 'and') |
||
513 | |||
514 | /** |
||
515 | * Add an "or where date" statement to the query. |
||
516 | * |
||
517 | * @param string $column |
||
518 | * @param string $operator |
||
519 | * @param string $value |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function orWhereDate($column, $operator, $value) |
||
528 | |||
529 | /** |
||
530 | * Add a "where time" statement to the query. |
||
531 | * |
||
532 | * @param string $column |
||
533 | * @param string $operator |
||
534 | * @param int $value |
||
535 | * @param string $boolean |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function whereTime($column, $operator, $value, $boolean = 'and') |
||
544 | |||
545 | /** |
||
546 | * Add an "or where time" statement to the query. |
||
547 | * |
||
548 | * @param string $column |
||
549 | * @param string $operator |
||
550 | * @param int $value |
||
551 | * @return $this |
||
552 | */ |
||
553 | public function orWhereTime($column, $operator, $value) |
||
559 | |||
560 | /** |
||
561 | * Add a "where day" statement to the query. |
||
562 | * |
||
563 | * @param string $column |
||
564 | * @param string $operator |
||
565 | * @param mixed $value |
||
566 | * @param string $boolean |
||
567 | * @return $this |
||
568 | */ |
||
569 | public function whereDay($column, $operator, $value = null, $boolean = 'and') |
||
575 | |||
576 | /** |
||
577 | * Add a "where month" statement to the query. |
||
578 | * |
||
579 | * @param string $column |
||
580 | * @param string $operator |
||
581 | * @param mixed $value |
||
582 | * @param string $boolean |
||
583 | * @return $this |
||
584 | */ |
||
585 | public function whereMonth($column, $operator, $value = null, $boolean = 'and') |
||
591 | |||
592 | /** |
||
593 | * Add a "where year" statement to the query. |
||
594 | * |
||
595 | * @param string $column |
||
596 | * @param string $operator |
||
597 | * @param mixed $value |
||
598 | * @param string $boolean |
||
599 | * @return $this |
||
600 | */ |
||
601 | public function whereYear($column, $operator, $value = null, $boolean = 'and') |
||
607 | |||
608 | /** |
||
609 | * Add a nested where statement to the query. |
||
610 | * |
||
611 | * @param \Closure $callback |
||
612 | * @param string $boolean |
||
613 | * @return $this |
||
614 | */ |
||
615 | public function whereNested(Closure $callback, $boolean = 'and') |
||
621 | |||
622 | /** |
||
623 | * Add another query builder as a nested where to the query builder. |
||
624 | * |
||
625 | * @param $this $query |
||
626 | * @param string $boolean |
||
627 | * @return $this |
||
628 | */ |
||
629 | public function addNestedWhereQuery($query, $boolean = 'and') |
||
635 | |||
636 | /** |
||
637 | * Add an exists clause to the query. |
||
638 | * |
||
639 | * @param \Closure $callback |
||
640 | * @param string $boolean |
||
641 | * @param bool $not |
||
642 | * @return $this |
||
643 | */ |
||
644 | public function whereExists(Closure $callback, $boolean = 'and', $not = false) |
||
650 | |||
651 | /** |
||
652 | * Add an or exists clause to the query. |
||
653 | * |
||
654 | * @param \Closure $callback |
||
655 | * @param bool $not |
||
656 | * @return $this |
||
657 | */ |
||
658 | public function orWhereExists(Closure $callback, $not = false) |
||
664 | |||
665 | /** |
||
666 | * Add a where not exists clause to the query. |
||
667 | * |
||
668 | * @param \Closure $callback |
||
669 | * @param string $boolean |
||
670 | * @return $this |
||
671 | */ |
||
672 | public function whereNotExists(Closure $callback, $boolean = 'and') |
||
678 | |||
679 | /** |
||
680 | * Add a where not exists clause to the query. |
||
681 | * |
||
682 | * @param \Closure $callback |
||
683 | * @return $this |
||
684 | */ |
||
685 | public function orWhereNotExists(Closure $callback) |
||
691 | |||
692 | /** |
||
693 | * Add an exists clause to the query. |
||
694 | * |
||
695 | * @param \Illuminate\Database\Query\Builder $query |
||
696 | * @param string $boolean |
||
697 | * @param bool $not |
||
698 | * @return $this |
||
699 | */ |
||
700 | public function addWhereExistsQuery(Builder $query, $boolean = 'and', $not = false) |
||
706 | |||
707 | /** |
||
708 | * Handles dynamic "where" clauses to the query. |
||
709 | * |
||
710 | * @param string $method |
||
711 | * @param string $parameters |
||
712 | * @return $this |
||
713 | */ |
||
714 | public function dynamicWhere($method, $parameters) |
||
720 | |||
721 | /** |
||
722 | * Add a "group by" clause to the query. |
||
723 | * |
||
724 | * @param array ...$groups |
||
725 | * @return $this |
||
726 | */ |
||
727 | public function groupBy() |
||
733 | |||
734 | /** |
||
735 | * Add a "having" clause to the query. |
||
736 | * |
||
737 | * @param string $column |
||
738 | * @param string $operator |
||
739 | * @param string $value |
||
740 | * @param string $boolean |
||
741 | * @return $this |
||
742 | */ |
||
743 | public function having($column, $operator = null, $value = null, $boolean = 'and') |
||
749 | |||
750 | /** |
||
751 | * Add a "or having" clause to the query. |
||
752 | * |
||
753 | * @param string $column |
||
754 | * @param string $operator |
||
755 | * @param string $value |
||
756 | * @return $this |
||
757 | */ |
||
758 | public function orHaving($column, $operator = null, $value = null) |
||
764 | |||
765 | /** |
||
766 | * Add a raw having clause to the query. |
||
767 | * |
||
768 | * @param string $sql |
||
769 | * @param array $bindings |
||
770 | * @param string $boolean |
||
771 | * @return $this |
||
772 | */ |
||
773 | public function havingRaw($sql, array $bindings = [], $boolean = 'and') |
||
779 | |||
780 | /** |
||
781 | * Add a raw or having clause to the query. |
||
782 | * |
||
783 | * @param string $sql |
||
784 | * @param array $bindings |
||
785 | * @return $this |
||
786 | */ |
||
787 | public function orHavingRaw($sql, array $bindings = []) |
||
793 | |||
794 | /** |
||
795 | * Add an "order by" clause to the query. |
||
796 | * |
||
797 | * @param string $column |
||
798 | * @param string $direction |
||
799 | * @return $this |
||
800 | */ |
||
801 | public function orderBy($column, $direction = 'asc') |
||
807 | |||
808 | /** |
||
809 | * Add a descending "order by" clause to the query. |
||
810 | * |
||
811 | * @param string $column |
||
812 | * @return $this |
||
813 | */ |
||
814 | public function orderByDesc($column) |
||
820 | |||
821 | /** |
||
822 | * Add an "order by" clause for a timestamp to the query. |
||
823 | * |
||
824 | * @param string $column |
||
825 | * @return $this |
||
826 | */ |
||
827 | public function latest($column = 'created_at') |
||
833 | |||
834 | /** |
||
835 | * Add an "order by" clause for a timestamp to the query. |
||
836 | * |
||
837 | * @param string $column |
||
838 | * @return $this |
||
839 | */ |
||
840 | public function oldest($column = 'created_at') |
||
846 | |||
847 | /** |
||
848 | * Put the query's results in random order. |
||
849 | * |
||
850 | * @param string $seed |
||
851 | * @return $this |
||
852 | */ |
||
853 | public function inRandomOrder($seed = '') |
||
859 | |||
860 | /** |
||
861 | * Add a raw "order by" clause to the query. |
||
862 | * |
||
863 | * @param string $sql |
||
864 | * @param array $bindings |
||
865 | * @return $this |
||
866 | */ |
||
867 | public function orderByRaw($sql, $bindings = []) |
||
873 | |||
874 | /** |
||
875 | * Alias to set the "offset" value of the query. |
||
876 | * |
||
877 | * @param int $value |
||
878 | * @return $this |
||
879 | */ |
||
880 | public function skip($value) |
||
886 | |||
887 | /** |
||
888 | * Set the "offset" value of the query. |
||
889 | * |
||
890 | * @param int $value |
||
891 | * @return $this |
||
892 | */ |
||
893 | public function offset($value) |
||
899 | |||
900 | /** |
||
901 | * Alias to set the "limit" value of the query. |
||
902 | * |
||
903 | * @param int $value |
||
904 | * @return $this |
||
905 | */ |
||
906 | public function take($value) |
||
912 | |||
913 | /** |
||
914 | * Set the "limit" value of the query. |
||
915 | * |
||
916 | * @param int $value |
||
917 | * @return $this |
||
918 | */ |
||
919 | public function limit($value) |
||
925 | |||
926 | /** |
||
927 | * Set the limit and offset for a given page. |
||
928 | * |
||
929 | * @param int $page |
||
930 | * @param int $perPage |
||
931 | * @return $this |
||
932 | */ |
||
933 | public function forPage($page, $perPage = 15) |
||
939 | |||
940 | /** |
||
941 | * Constrain the query to the next "page" of results after a given ID. |
||
942 | * |
||
943 | * @param int $perPage |
||
944 | * @param int $lastId |
||
945 | * @param string $column |
||
946 | * @return $this |
||
947 | */ |
||
948 | public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') |
||
954 | |||
955 | /** |
||
956 | * Add a union statement to the query. |
||
957 | * |
||
958 | * @param \Illuminate\Database\Query\Builder|\Closure $query |
||
959 | * @param bool $all |
||
960 | * @return $this |
||
961 | */ |
||
962 | public function union($query, $all = false) |
||
968 | |||
969 | /** |
||
970 | * Add a union all statement to the query. |
||
971 | * |
||
972 | * @param \Illuminate\Database\Query\Builder|\Closure $query |
||
973 | * @return $this |
||
974 | */ |
||
975 | public function unionAll($query) |
||
981 | |||
982 | /** |
||
983 | * Lock the selected rows in the table. |
||
984 | * |
||
985 | * @param string|bool $value |
||
986 | * @return $this |
||
987 | */ |
||
988 | public function lock($value = true) |
||
994 | |||
995 | /** |
||
996 | * Lock the selected rows in the table for updating. |
||
997 | * |
||
998 | * @return $this |
||
999 | */ |
||
1000 | public function lockForUpdate() |
||
1006 | |||
1007 | /** |
||
1008 | * Share lock the selected rows in the table. |
||
1009 | * |
||
1010 | * @return $this |
||
1011 | */ |
||
1012 | public function sharedLock() |
||
1018 | |||
1019 | /** |
||
1020 | * Apply the callback's query changes if the given "value" is true. |
||
1021 | * |
||
1022 | * @param mixed $value |
||
1023 | * @param callable $callback |
||
1024 | * @param callable $default |
||
1025 | * @return $this |
||
1026 | */ |
||
1027 | public function when($value, $callback, $default = null) |
||
1033 | |||
1034 | /** |
||
1035 | * Apply the callback's query changes if the given "value" is false. |
||
1036 | * |
||
1037 | * @param mixed $value |
||
1038 | * @param callable $callback |
||
1039 | * @param callable $default |
||
1040 | * @return $this |
||
1041 | */ |
||
1042 | public function unless($value, $callback, $default = null) |
||
1048 | } |
||
1049 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: