Complex classes like Grammar 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 Grammar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class Grammar |
||
16 | { |
||
17 | /** |
||
18 | * The grammar table prefix. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $tablePrefix = ''; |
||
23 | |||
24 | /** |
||
25 | * The components that make up a select clause. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $selectComponents = [ |
||
30 | 'aggregate', |
||
31 | 'columns', |
||
32 | 'from', |
||
33 | 'joins', |
||
34 | 'wheres', |
||
35 | 'groups', |
||
36 | 'havings', |
||
37 | 'orders', |
||
38 | 'limit', |
||
39 | 'offset', |
||
40 | 'unions', |
||
41 | 'lock', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Wrap an array of values. |
||
46 | * |
||
47 | * @param array $values |
||
48 | * @return array |
||
49 | */ |
||
50 | public function wrapArray(array $values) |
||
54 | |||
55 | /** |
||
56 | * Wrap a table in keyword identifiers. |
||
57 | * |
||
58 | * @param string $table |
||
59 | * @return string |
||
60 | */ |
||
61 | public function wrapTable($table) |
||
65 | |||
66 | /** |
||
67 | * Wrap a value in keyword identifiers. |
||
68 | * |
||
69 | * @param string $value |
||
70 | * @param bool $prefixAlias |
||
71 | * @return string |
||
72 | */ |
||
73 | public function wrap($value, $prefixAlias = false) |
||
84 | |||
85 | /** |
||
86 | * Wrap a value that has an alias. |
||
87 | * |
||
88 | * @param string $value |
||
89 | * @param bool $prefixAlias |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function wrapAliasedValue($value, $prefixAlias = false) |
||
107 | |||
108 | /** |
||
109 | * Wrap the given value segments. |
||
110 | * |
||
111 | * @param array $segments |
||
112 | * @return string |
||
113 | */ |
||
114 | protected function wrapSegments($segments) |
||
122 | |||
123 | /** |
||
124 | * Wrap a single string in keyword identifiers. |
||
125 | * |
||
126 | * @param string $value |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function wrapValue($value) |
||
137 | |||
138 | /** |
||
139 | * Convert an array of column names into a delimited string. |
||
140 | * |
||
141 | * @param array $columns |
||
142 | * @return string |
||
143 | */ |
||
144 | public function columnize(array $columns) |
||
148 | |||
149 | /** |
||
150 | * Create query parameter place-holders for an array. |
||
151 | * |
||
152 | * @param array $values |
||
153 | * @return string |
||
154 | */ |
||
155 | public function parameterize(array $values) |
||
159 | |||
160 | /** |
||
161 | * Get the format for database stored dates. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getDateFormat() |
||
169 | |||
170 | /** |
||
171 | * Get the grammar's table prefix. |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getTablePrefix() |
||
179 | |||
180 | /** |
||
181 | * Set the grammar's table prefix. |
||
182 | * |
||
183 | * @param string $prefix |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function setTablePrefix($prefix) |
||
192 | |||
193 | /** |
||
194 | * Compile a select query into SQL. |
||
195 | * |
||
196 | * @param \Childish\query\Builder $query |
||
197 | * @return string |
||
198 | */ |
||
199 | public function compileSelect(Builder $query) |
||
221 | |||
222 | /** |
||
223 | * Compile the components necessary for a select clause. |
||
224 | * |
||
225 | * @param \Childish\query\Builder $query |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function compileComponents(Builder $query) |
||
245 | |||
246 | /** |
||
247 | * Compile an aggregated select clause. |
||
248 | * |
||
249 | * @param \Childish\query\Builder $query |
||
250 | * @param array $aggregate |
||
251 | * @return string |
||
252 | */ |
||
253 | protected function compileAggregate(Builder $query, $aggregate) |
||
266 | |||
267 | /** |
||
268 | * Compile the "select *" portion of the query. |
||
269 | * |
||
270 | * @param \Childish\query\Builder $query |
||
271 | * @param array $columns |
||
272 | * @return string|null |
||
273 | */ |
||
274 | protected function compileColumns(Builder $query, $columns) |
||
287 | |||
288 | /** |
||
289 | * Compile the "from" portion of the query. |
||
290 | * |
||
291 | * @param \Childish\query\Builder $query |
||
292 | * @param string $table |
||
293 | * @return string |
||
294 | */ |
||
295 | protected function compileFrom(Builder $query, $table) |
||
299 | |||
300 | /** |
||
301 | * Compile the "join" portions of the query. |
||
302 | * |
||
303 | * @param \Childish\query\Builder $query |
||
304 | * @param array $joins |
||
305 | * @return string |
||
306 | */ |
||
307 | protected function compileJoins(Builder $query, $joins) |
||
315 | |||
316 | /** |
||
317 | * Compile the "where" portions of the query. |
||
318 | * |
||
319 | * @param \Childish\query\Builder $query |
||
320 | * @return string |
||
321 | */ |
||
322 | protected function compileWheres(Builder $query) |
||
340 | |||
341 | /** |
||
342 | * Get an array of all the where clauses for the query. |
||
343 | * |
||
344 | * @param \Childish\query\Builder $query |
||
345 | * @return array |
||
346 | */ |
||
347 | protected function compileWheresToArray($query) |
||
353 | |||
354 | /** |
||
355 | * Format the where clause statements into one string. |
||
356 | * |
||
357 | * @param \Childish\query\Builder $query |
||
358 | * @param array $sql |
||
359 | * @return string |
||
360 | */ |
||
361 | protected function concatenateWhereClauses($query, $sql) |
||
367 | |||
368 | /** |
||
369 | * Compile a raw where clause. |
||
370 | * |
||
371 | * @param \Childish\query\Builder $query |
||
372 | * @param array $where |
||
373 | * @return string |
||
374 | */ |
||
375 | protected function whereRaw(Builder $query, $where) |
||
379 | |||
380 | /** |
||
381 | * Compile a basic where clause. |
||
382 | * |
||
383 | * @param \Childish\query\Builder $query |
||
384 | * @param array $where |
||
385 | * @return string |
||
386 | */ |
||
387 | protected function whereBasic(Builder $query, $where) |
||
391 | |||
392 | /** |
||
393 | * Compile a "where in" clause. |
||
394 | * |
||
395 | * @param \Childish\query\Builder $query |
||
396 | * @param array $where |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function whereIn(Builder $query, $where) |
||
407 | |||
408 | /** |
||
409 | * Compile a "where not in" clause. |
||
410 | * |
||
411 | * @param \Childish\query\Builder $query |
||
412 | * @param array $where |
||
413 | * @return string |
||
414 | */ |
||
415 | protected function whereNotIn(Builder $query, $where) |
||
423 | |||
424 | /** |
||
425 | * Compile a where in sub-select clause. |
||
426 | * |
||
427 | * @param \Childish\query\Builder $query |
||
428 | * @param array $where |
||
429 | * @return string |
||
430 | */ |
||
431 | protected function whereInSub(Builder $query, $where) |
||
435 | |||
436 | /** |
||
437 | * Compile a where not in sub-select clause. |
||
438 | * |
||
439 | * @param \Childish\query\Builder $query |
||
440 | * @param array $where |
||
441 | * @return string |
||
442 | */ |
||
443 | protected function whereNotInSub(Builder $query, $where) |
||
447 | |||
448 | /** |
||
449 | * Compile a "where null" clause. |
||
450 | * |
||
451 | * @param \Childish\query\Builder $query |
||
452 | * @param array $where |
||
453 | * @return string |
||
454 | */ |
||
455 | protected function whereNull(Builder $query, $where) |
||
459 | |||
460 | /** |
||
461 | * Compile a "where not null" clause. |
||
462 | * |
||
463 | * @param \Childish\query\Builder $query |
||
464 | * @param array $where |
||
465 | * @return string |
||
466 | */ |
||
467 | protected function whereNotNull(Builder $query, $where) |
||
471 | |||
472 | /** |
||
473 | * Compile a "between" where clause. |
||
474 | * |
||
475 | * @param \Childish\query\Builder $query |
||
476 | * @param array $where |
||
477 | * @return string |
||
478 | */ |
||
479 | protected function whereBetween(Builder $query, $where) |
||
485 | |||
486 | /** |
||
487 | * Compile a "where date" clause. |
||
488 | * |
||
489 | * @param \Childish\query\Builder $query |
||
490 | * @param array $where |
||
491 | * @return string |
||
492 | */ |
||
493 | protected function whereDate(Builder $query, $where) |
||
497 | |||
498 | /** |
||
499 | * Compile a "where time" clause. |
||
500 | * |
||
501 | * @param \Childish\query\Builder $query |
||
502 | * @param array $where |
||
503 | * @return string |
||
504 | */ |
||
505 | protected function whereTime(Builder $query, $where) |
||
509 | |||
510 | /** |
||
511 | * Compile a "where day" clause. |
||
512 | * |
||
513 | * @param \Childish\query\Builder $query |
||
514 | * @param array $where |
||
515 | * @return string |
||
516 | */ |
||
517 | protected function whereDay(Builder $query, $where) |
||
521 | |||
522 | /** |
||
523 | * Compile a "where month" clause. |
||
524 | * |
||
525 | * @param \Childish\query\Builder $query |
||
526 | * @param array $where |
||
527 | * @return string |
||
528 | */ |
||
529 | protected function whereMonth(Builder $query, $where) |
||
533 | |||
534 | /** |
||
535 | * Compile a "where year" clause. |
||
536 | * |
||
537 | * @param \Childish\query\Builder $query |
||
538 | * @param array $where |
||
539 | * @return string |
||
540 | */ |
||
541 | protected function whereYear(Builder $query, $where) |
||
545 | |||
546 | /** |
||
547 | * Compile a date based where clause. |
||
548 | * |
||
549 | * @param string $type |
||
550 | * @param \Childish\query\Builder $query |
||
551 | * @param array $where |
||
552 | * @return string |
||
553 | */ |
||
554 | protected function dateBasedWhere($type, Builder $query, $where) |
||
558 | |||
559 | /** |
||
560 | * Compile a where clause comparing two columns.. |
||
561 | * |
||
562 | * @param \Childish\query\Builder $query |
||
563 | * @param array $where |
||
564 | * @return string |
||
565 | */ |
||
566 | protected function whereColumn(Builder $query, $where) |
||
570 | |||
571 | /** |
||
572 | * Compile a nested where clause. |
||
573 | * |
||
574 | * @param \Childish\query\Builder $query |
||
575 | * @param array $where |
||
576 | * @return string |
||
577 | */ |
||
578 | protected function whereNested(Builder $query, $where) |
||
587 | |||
588 | /** |
||
589 | * Compile a where condition with a sub-select. |
||
590 | * |
||
591 | * @param \Childish\query\Builder $query |
||
592 | * @param array $where |
||
593 | * @return string |
||
594 | */ |
||
595 | protected function whereSub(Builder $query, $where) |
||
601 | |||
602 | /** |
||
603 | * Compile a where exists clause. |
||
604 | * |
||
605 | * @param \Childish\query\Builder $query |
||
606 | * @param array $where |
||
607 | * @return string |
||
608 | */ |
||
609 | protected function whereExists(Builder $query, $where) |
||
613 | |||
614 | /** |
||
615 | * Compile a where exists clause. |
||
616 | * |
||
617 | * @param \Childish\query\Builder $query |
||
618 | * @param array $where |
||
619 | * @return string |
||
620 | */ |
||
621 | protected function whereNotExists(Builder $query, $where) |
||
625 | |||
626 | /** |
||
627 | * Compile the "group by" portions of the query. |
||
628 | * |
||
629 | * @param \Childish\query\Builder $query |
||
630 | * @param array $groups |
||
631 | * @return string |
||
632 | */ |
||
633 | protected function compileGroups(Builder $query, $groups) |
||
637 | |||
638 | /** |
||
639 | * Compile the "having" portions of the query. |
||
640 | * |
||
641 | * @param \Childish\query\Builder $query |
||
642 | * @param array $havings |
||
643 | * @return string |
||
644 | */ |
||
645 | protected function compileHavings(Builder $query, $havings) |
||
651 | |||
652 | /** |
||
653 | * Compile a single having clause. |
||
654 | * |
||
655 | * @param array $having |
||
656 | * @return string |
||
657 | */ |
||
658 | protected function compileHaving(array $having) |
||
669 | |||
670 | /** |
||
671 | * Compile a basic having clause. |
||
672 | * |
||
673 | * @param array $having |
||
674 | * @return string |
||
675 | */ |
||
676 | protected function compileBasicHaving($having) |
||
683 | |||
684 | /** |
||
685 | * Compile the "order by" portions of the query. |
||
686 | * |
||
687 | * @param \Childish\query\Builder $query |
||
688 | * @param array $orders |
||
689 | * @return string |
||
690 | */ |
||
691 | protected function compileOrders(Builder $query, $orders) |
||
699 | |||
700 | /** |
||
701 | * Compile the query orders to an array. |
||
702 | * |
||
703 | * @param \Childish\query\Builder |
||
704 | * @param array $orders |
||
705 | * @return array |
||
706 | */ |
||
707 | protected function compileOrdersToArray(Builder $query, $orders) |
||
715 | |||
716 | /** |
||
717 | * Compile the random statement into SQL. |
||
718 | * |
||
719 | * @param string $seed |
||
720 | * @return string |
||
721 | */ |
||
722 | public function compileRandom($seed) |
||
726 | |||
727 | /** |
||
728 | * Compile the "limit" portions of the query. |
||
729 | * |
||
730 | * @param \Childish\query\Builder $query |
||
731 | * @param int $limit |
||
732 | * @return string |
||
733 | */ |
||
734 | protected function compileLimit(Builder $query, $limit) |
||
738 | |||
739 | /** |
||
740 | * Compile the "offset" portions of the query. |
||
741 | * |
||
742 | * @param \Childish\query\Builder $query |
||
743 | * @param int $offset |
||
744 | * @return string |
||
745 | */ |
||
746 | protected function compileOffset(Builder $query, $offset) |
||
750 | |||
751 | /** |
||
752 | * Compile the "union" queries attached to the main query. |
||
753 | * |
||
754 | * @param \Childish\query\Builder $query |
||
755 | * @return string |
||
756 | */ |
||
757 | protected function compileUnions(Builder $query) |
||
779 | |||
780 | /** |
||
781 | * Compile a single union statement. |
||
782 | * |
||
783 | * @param array $union |
||
784 | * @return string |
||
785 | */ |
||
786 | protected function compileUnion(array $union) |
||
792 | |||
793 | /** |
||
794 | * Compile an exists statement into SQL. |
||
795 | * |
||
796 | * @param \Childish\query\Builder $query |
||
797 | * @return string |
||
798 | */ |
||
799 | public function compileExists(Builder $query) |
||
805 | |||
806 | /** |
||
807 | * Compile an insert statement into SQL. |
||
808 | * |
||
809 | * @param \Childish\query\Builder $query |
||
810 | * @param array $values |
||
811 | * @return string |
||
812 | */ |
||
813 | public function compileInsert(Builder $query, array $values) |
||
836 | |||
837 | /** |
||
838 | * Compile an insert and get ID statement into SQL. |
||
839 | * |
||
840 | * @param \Childish\query\Builder $query |
||
841 | * @param array $values |
||
842 | * @param string $sequence |
||
843 | * @return string |
||
844 | */ |
||
845 | public function compileInsertGetId(Builder $query, $values, $sequence) |
||
849 | |||
850 | /** |
||
851 | * Compile an update statement into SQL. |
||
852 | * |
||
853 | * @param \Childish\query\Builder $query |
||
854 | * @param array $values |
||
855 | * @return string |
||
856 | */ |
||
857 | public function compileUpdate(Builder $query, $values) |
||
884 | |||
885 | /** |
||
886 | * Prepare the bindings for an update statement. |
||
887 | * |
||
888 | * @param array $bindings |
||
889 | * @param array $values |
||
890 | * @return array |
||
891 | */ |
||
892 | public function prepareBindingsForUpdate(array $bindings, array $values) |
||
900 | |||
901 | /** |
||
902 | * Compile a delete statement into SQL. |
||
903 | * |
||
904 | * @param \Childish\query\Builder $query |
||
905 | * @return string |
||
906 | */ |
||
907 | public function compileDelete(Builder $query) |
||
913 | |||
914 | /** |
||
915 | * Compile a truncate table statement into SQL. |
||
916 | * |
||
917 | * @param \Childish\query\Builder $query |
||
918 | * @return array |
||
919 | */ |
||
920 | public function compileTruncate(Builder $query) |
||
924 | |||
925 | /** |
||
926 | * Compile the lock into SQL. |
||
927 | * |
||
928 | * @param \Childish\query\Builder $query |
||
929 | * @param bool|string $value |
||
930 | * @return string |
||
931 | */ |
||
932 | protected function compileLock(Builder $query, $value) |
||
936 | |||
937 | /** |
||
938 | * Determine if the grammar supports savepoints. |
||
939 | * |
||
940 | * @return bool |
||
941 | */ |
||
942 | public function supportsSavepoints() |
||
946 | |||
947 | /** |
||
948 | * Compile the SQL statement to define a savepoint. |
||
949 | * |
||
950 | * @param string $name |
||
951 | * @return string |
||
952 | */ |
||
953 | public function compileSavepoint($name) |
||
957 | |||
958 | /** |
||
959 | * Compile the SQL statement to execute a savepoint rollback. |
||
960 | * |
||
961 | * @param string $name |
||
962 | * @return string |
||
963 | */ |
||
964 | public function compileSavepointRollBack($name) |
||
968 | |||
969 | /** |
||
970 | * Concatenate an array of segments, removing empties. |
||
971 | * |
||
972 | * @param array $segments |
||
973 | * @return string |
||
974 | */ |
||
975 | protected function concatenate($segments) |
||
981 | |||
982 | /** |
||
983 | * Remove the leading boolean from a statement. |
||
984 | * |
||
985 | * @param string $value |
||
986 | * @return string |
||
987 | */ |
||
988 | protected function removeLeadingBoolean($value) |
||
992 | |||
993 | /** |
||
994 | * Get the grammar specific operators. |
||
995 | * |
||
996 | * @return array |
||
997 | */ |
||
998 | public function getOperators() |
||
1002 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.