Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
28 | class QueryBuilder implements QueryBuilderInterface { |
||
29 | |||
30 | /** |
||
31 | * Convenience property for connection management |
||
32 | * @var string |
||
33 | */ |
||
34 | public $connName = ''; |
||
35 | |||
36 | /** |
||
37 | * List of queries executed |
||
38 | * @var array |
||
39 | */ |
||
40 | public $queries; |
||
41 | |||
42 | /** |
||
43 | * Whether to do only an explain on the query |
||
44 | * @var boolean |
||
45 | */ |
||
46 | protected $explain = FALSE; |
||
47 | |||
48 | /** |
||
49 | * The current database driver |
||
50 | * @var DriverInterface |
||
51 | */ |
||
52 | public $driver; |
||
53 | |||
54 | /** |
||
55 | * Query parser class instance |
||
56 | * @var QueryParser |
||
57 | */ |
||
58 | protected $parser; |
||
59 | |||
60 | /** |
||
61 | * Alias to driver util class |
||
62 | * @var AbstractUtil |
||
63 | */ |
||
64 | protected $util; |
||
65 | |||
66 | /** |
||
67 | * Alias to driver sql class |
||
68 | * @var SQLInterface |
||
69 | */ |
||
70 | protected $sql; |
||
71 | |||
72 | /** |
||
73 | * Query Builder state |
||
74 | * @var State |
||
75 | */ |
||
76 | protected $state; |
||
77 | |||
78 | // -------------------------------------------------------------------------- |
||
79 | // ! Methods |
||
80 | // -------------------------------------------------------------------------- |
||
81 | |||
82 | /** |
||
83 | * Constructor |
||
84 | * |
||
85 | * @param DriverInterface $driver |
||
86 | * @param QueryParser $parser |
||
87 | */ |
||
88 | public function __construct(DriverInterface $driver, QueryParser $parser) |
||
103 | |||
104 | /** |
||
105 | * Destructor |
||
106 | * @codeCoverageIgnore |
||
107 | */ |
||
108 | public function __destruct() |
||
112 | |||
113 | /** |
||
114 | * Calls a function further down the inheritance chain |
||
115 | * |
||
116 | * @param string $name |
||
117 | * @param array $params |
||
118 | * @return mixed |
||
119 | * @throws BadMethodCallException |
||
120 | */ |
||
121 | public function __call(string $name, array $params) |
||
140 | |||
141 | // -------------------------------------------------------------------------- |
||
142 | // ! Select Queries |
||
143 | // -------------------------------------------------------------------------- |
||
144 | |||
145 | /** |
||
146 | * Specifies rows to select in a query |
||
147 | * |
||
148 | * @param string $fields |
||
149 | * @return QueryBuilderInterface |
||
150 | */ |
||
151 | public function select(string $fields): QueryBuilderInterface |
||
185 | |||
186 | /** |
||
187 | * Selects the maximum value of a field from a query |
||
188 | * |
||
189 | * @param string $field |
||
190 | * @param string|bool $as |
||
191 | * @return QueryBuilderInterface |
||
192 | */ |
||
193 | public function selectMax(string $field, $as=FALSE): QueryBuilderInterface |
||
199 | |||
200 | /** |
||
201 | * Selects the minimum value of a field from a query |
||
202 | * |
||
203 | * @param string $field |
||
204 | * @param string|bool $as |
||
205 | * @return QueryBuilderInterface |
||
206 | */ |
||
207 | public function selectMin(string $field, $as=FALSE): QueryBuilderInterface |
||
213 | |||
214 | /** |
||
215 | * Selects the average value of a field from a query |
||
216 | * |
||
217 | * @param string $field |
||
218 | * @param string|bool $as |
||
219 | * @return QueryBuilderInterface |
||
220 | */ |
||
221 | public function selectAvg(string $field, $as=FALSE): QueryBuilderInterface |
||
227 | |||
228 | /** |
||
229 | * Selects the sum of a field from a query |
||
230 | * |
||
231 | * @param string $field |
||
232 | * @param string|bool $as |
||
233 | * @return QueryBuilderInterface |
||
234 | */ |
||
235 | public function selectSum(string $field, $as=FALSE): QueryBuilderInterface |
||
241 | |||
242 | /** |
||
243 | * Adds the 'distinct' keyword to a query |
||
244 | * |
||
245 | * @return QueryBuilderInterface |
||
246 | */ |
||
247 | public function distinct(): QueryBuilderInterface |
||
253 | |||
254 | /** |
||
255 | * Tell the database to give you the query plan instead of result set |
||
256 | * |
||
257 | * @return QueryBuilderInterface |
||
258 | */ |
||
259 | public function explain(): QueryBuilderInterface |
||
264 | |||
265 | /** |
||
266 | * Specify the database table to select from |
||
267 | * |
||
268 | * @param string $tblname |
||
269 | * @return QueryBuilderInterface |
||
270 | */ |
||
271 | public function from($tblname): QueryBuilderInterface |
||
286 | |||
287 | // -------------------------------------------------------------------------- |
||
288 | // ! 'Like' methods |
||
289 | // -------------------------------------------------------------------------- |
||
290 | |||
291 | /** |
||
292 | * Creates a Like clause in the sql statement |
||
293 | * |
||
294 | * @param string $field |
||
295 | * @param mixed $val |
||
296 | * @param string $pos |
||
297 | * @return QueryBuilderInterface |
||
298 | */ |
||
299 | public function like($field, $val, $pos='both'): QueryBuilderInterface |
||
303 | |||
304 | /** |
||
305 | * Generates an OR Like clause |
||
306 | * |
||
307 | * @param string $field |
||
308 | * @param mixed $val |
||
309 | * @param string $pos |
||
310 | * @return QueryBuilderInterface |
||
311 | */ |
||
312 | public function orLike($field, $val, $pos='both'): QueryBuilderInterface |
||
316 | |||
317 | /** |
||
318 | * Generates a NOT LIKE clause |
||
319 | * |
||
320 | * @param string $field |
||
321 | * @param mixed $val |
||
322 | * @param string $pos |
||
323 | * @return QueryBuilderInterface |
||
324 | */ |
||
325 | public function notLike($field, $val, $pos='both'): QueryBuilderInterface |
||
329 | |||
330 | /** |
||
331 | * Generates a OR NOT LIKE clause |
||
332 | * |
||
333 | * @param string $field |
||
334 | * @param mixed $val |
||
335 | * @param string $pos |
||
336 | * @return QueryBuilderInterface |
||
337 | */ |
||
338 | public function orNotLike($field, $val, $pos='both'): QueryBuilderInterface |
||
342 | |||
343 | // -------------------------------------------------------------------------- |
||
344 | // ! Having methods |
||
345 | // -------------------------------------------------------------------------- |
||
346 | |||
347 | /** |
||
348 | * Generates a 'Having' clause |
||
349 | * |
||
350 | * @param mixed $key |
||
351 | * @param mixed $val |
||
352 | * @return QueryBuilderInterface |
||
353 | */ |
||
354 | public function having($key, $val=[]): QueryBuilderInterface |
||
358 | |||
359 | /** |
||
360 | * Generates a 'Having' clause prefixed with 'OR' |
||
361 | * |
||
362 | * @param mixed $key |
||
363 | * @param mixed $val |
||
364 | * @return QueryBuilderInterface |
||
365 | */ |
||
366 | public function orHaving($key, $val=[]): QueryBuilderInterface |
||
370 | |||
371 | // -------------------------------------------------------------------------- |
||
372 | // ! 'Where' methods |
||
373 | // -------------------------------------------------------------------------- |
||
374 | |||
375 | /** |
||
376 | * Specify condition(s) in the where clause of a query |
||
377 | * Note: this function works with key / value, or a |
||
378 | * passed array with key / value pairs |
||
379 | * |
||
380 | * @param mixed $key |
||
381 | * @param mixed $val |
||
382 | * @param mixed $escape |
||
383 | * @return QueryBuilderInterface |
||
384 | */ |
||
385 | public function where($key, $val=[], $escape=NULL): QueryBuilderInterface |
||
389 | |||
390 | /** |
||
391 | * Where clause prefixed with "OR" |
||
392 | * |
||
393 | * @param string $key |
||
394 | * @param mixed $val |
||
395 | * @return QueryBuilderInterface |
||
396 | */ |
||
397 | public function orWhere($key, $val=[]): QueryBuilderInterface |
||
401 | |||
402 | /** |
||
403 | * Where clause with 'IN' statement |
||
404 | * |
||
405 | * @param mixed $field |
||
406 | * @param mixed $val |
||
407 | * @return QueryBuilderInterface |
||
408 | */ |
||
409 | public function whereIn($field, $val=[]): QueryBuilderInterface |
||
413 | |||
414 | /** |
||
415 | * Where in statement prefixed with "or" |
||
416 | * |
||
417 | * @param string $field |
||
418 | * @param mixed $val |
||
419 | * @return QueryBuilderInterface |
||
420 | */ |
||
421 | public function orWhereIn($field, $val=[]): QueryBuilderInterface |
||
425 | |||
426 | /** |
||
427 | * WHERE NOT IN (FOO) clause |
||
428 | * |
||
429 | * @param string $field |
||
430 | * @param mixed $val |
||
431 | * @return QueryBuilderInterface |
||
432 | */ |
||
433 | public function whereNotIn($field, $val=[]): QueryBuilderInterface |
||
437 | |||
438 | /** |
||
439 | * OR WHERE NOT IN (FOO) clause |
||
440 | * |
||
441 | * @param string $field |
||
442 | * @param mixed $val |
||
443 | * @return QueryBuilderInterface |
||
444 | */ |
||
445 | public function orWhereNotIn($field, $val=[]): QueryBuilderInterface |
||
449 | |||
450 | // -------------------------------------------------------------------------- |
||
451 | // ! Other Query Modifier methods |
||
452 | // -------------------------------------------------------------------------- |
||
453 | |||
454 | /** |
||
455 | * Sets values for inserts / updates / deletes |
||
456 | * |
||
457 | * @param mixed $key |
||
458 | * @param mixed $val |
||
459 | * @return QueryBuilderInterface |
||
460 | */ |
||
461 | public function set($key, $val = NULL): QueryBuilderInterface |
||
492 | |||
493 | /** |
||
494 | * Creates a join phrase in a compiled query |
||
495 | * |
||
496 | * @param string $table |
||
497 | * @param string $condition |
||
498 | * @param string $type |
||
499 | * @return QueryBuilderInterface |
||
500 | */ |
||
501 | public function join($table, $condition, $type=''): QueryBuilderInterface |
||
517 | |||
518 | /** |
||
519 | * Group the results by the selected field(s) |
||
520 | * |
||
521 | * @param mixed $field |
||
522 | * @return QueryBuilderInterface |
||
523 | */ |
||
524 | public function groupBy($field): QueryBuilderInterface |
||
542 | |||
543 | /** |
||
544 | * Order the results by the selected field(s) |
||
545 | * |
||
546 | * @param string $field |
||
547 | * @param string $type |
||
548 | * @return QueryBuilderInterface |
||
549 | */ |
||
550 | public function orderBy($field, $type=''): QueryBuilderInterface |
||
581 | |||
582 | /** |
||
583 | * Set a limit on the current sql statement |
||
584 | * |
||
585 | * @param int $limit |
||
586 | * @param int|bool $offset |
||
587 | * @return QueryBuilderInterface |
||
588 | */ |
||
589 | public function limit($limit, $offset=FALSE): QueryBuilderInterface |
||
596 | |||
597 | // -------------------------------------------------------------------------- |
||
598 | // ! Query Grouping Methods |
||
599 | // -------------------------------------------------------------------------- |
||
600 | |||
601 | /** |
||
602 | * Adds a paren to the current query for query grouping |
||
603 | * |
||
604 | * @return QueryBuilderInterface |
||
605 | */ |
||
606 | View Code Duplication | public function groupStart(): QueryBuilderInterface |
|
614 | |||
615 | /** |
||
616 | * Adds a paren to the current query for query grouping, |
||
617 | * prefixed with 'NOT' |
||
618 | * |
||
619 | * @return QueryBuilderInterface |
||
620 | */ |
||
621 | View Code Duplication | public function notGroupStart(): QueryBuilderInterface |
|
629 | |||
630 | /** |
||
631 | * Adds a paren to the current query for query grouping, |
||
632 | * prefixed with 'OR' |
||
633 | * |
||
634 | * @return QueryBuilderInterface |
||
635 | */ |
||
636 | public function orGroupStart(): QueryBuilderInterface |
||
642 | |||
643 | /** |
||
644 | * Adds a paren to the current query for query grouping, |
||
645 | * prefixed with 'OR NOT' |
||
646 | * |
||
647 | * @return QueryBuilderInterface |
||
648 | */ |
||
649 | public function orNotGroupStart(): QueryBuilderInterface |
||
655 | |||
656 | /** |
||
657 | * Ends a query group |
||
658 | * |
||
659 | * @return QueryBuilderInterface |
||
660 | */ |
||
661 | public function groupEnd(): QueryBuilderInterface |
||
667 | |||
668 | // -------------------------------------------------------------------------- |
||
669 | // ! Query execution methods |
||
670 | // -------------------------------------------------------------------------- |
||
671 | |||
672 | /** |
||
673 | * Select and retrieve all records from the current table, and/or |
||
674 | * execute current compiled query |
||
675 | * |
||
676 | * @param string $table |
||
677 | * @param int|bool $limit |
||
678 | * @param int|bool $offset |
||
679 | * @return PDOStatement |
||
680 | */ |
||
681 | public function get($table='', $limit=FALSE, $offset=FALSE): PDOStatement |
||
697 | |||
698 | /** |
||
699 | * Convenience method for get() with a where clause |
||
700 | * |
||
701 | * @param string $table |
||
702 | * @param array $where |
||
703 | * @param int|bool $limit |
||
704 | * @param int|bool $offset |
||
705 | * @return PDOStatement |
||
706 | */ |
||
707 | public function getWhere($table, $where=[], $limit=FALSE, $offset=FALSE): PDOStatement |
||
715 | |||
716 | /** |
||
717 | * Retrieve the number of rows in the selected table |
||
718 | * |
||
719 | * @param string $table |
||
720 | * @return int |
||
721 | */ |
||
722 | public function countAll($table): int |
||
728 | |||
729 | /** |
||
730 | * Retrieve the number of results for the generated query - used |
||
731 | * in place of the get() method |
||
732 | * |
||
733 | * @param string $table |
||
734 | * @param boolean $reset |
||
735 | * @return int |
||
736 | */ |
||
737 | public function countAllResults(string $table='', bool $reset = TRUE): int |
||
750 | |||
751 | /** |
||
752 | * Creates an insert clause, and executes it |
||
753 | * |
||
754 | * @param string $table |
||
755 | * @param mixed $data |
||
756 | * @return PDOStatement |
||
757 | */ |
||
758 | public function insert($table, $data=[]): PDOStatement |
||
767 | |||
768 | /** |
||
769 | * Creates and executes a batch insertion query |
||
770 | * |
||
771 | * @param string $table |
||
772 | * @param array $data |
||
773 | * @return PDOStatement |
||
774 | */ |
||
775 | public function insertBatch($table, $data=[]): PDOStatement |
||
784 | |||
785 | /** |
||
786 | * Creates an update clause, and executes it |
||
787 | * |
||
788 | * @param string $table |
||
789 | * @param mixed $data |
||
790 | * @return PDOStatement |
||
791 | */ |
||
792 | public function update($table, $data=[]): PDOStatement |
||
801 | |||
802 | /** |
||
803 | * Creates a batch update, and executes it. |
||
804 | * Returns the number of affected rows |
||
805 | * |
||
806 | * @param string $table |
||
807 | * @param array|object $data |
||
808 | * @param string $where |
||
809 | * @return int|null |
||
810 | */ |
||
811 | public function updateBatch($table, $data, $where) |
||
820 | |||
821 | /** |
||
822 | * Insertion with automatic overwrite, rather than attempted duplication |
||
823 | * |
||
824 | * @param string $table |
||
825 | * @param array $data |
||
826 | * @return \PDOStatement|null |
||
827 | */ |
||
828 | public function replace($table, $data=[]) |
||
837 | |||
838 | /** |
||
839 | * Deletes data from a table |
||
840 | * |
||
841 | * @param string $table |
||
842 | * @param mixed $where |
||
843 | * @return PDOStatement |
||
844 | */ |
||
845 | public function delete($table, $where=''): PDOStatement |
||
855 | |||
856 | // -------------------------------------------------------------------------- |
||
857 | // ! SQL Returning Methods |
||
858 | // -------------------------------------------------------------------------- |
||
859 | |||
860 | /** |
||
861 | * Returns the generated 'select' sql query |
||
862 | * |
||
863 | * @param string $table |
||
864 | * @param bool $reset |
||
865 | * @return string |
||
866 | */ |
||
867 | public function getCompiledSelect(string $table='', bool $reset=TRUE): string |
||
877 | |||
878 | /** |
||
879 | * Returns the generated 'insert' sql query |
||
880 | * |
||
881 | * @param string $table |
||
882 | * @param bool $reset |
||
883 | * @return string |
||
884 | */ |
||
885 | public function getCompiledInsert(string $table, bool $reset=TRUE): string |
||
889 | |||
890 | /** |
||
891 | * Returns the generated 'update' sql query |
||
892 | * |
||
893 | * @param string $table |
||
894 | * @param bool $reset |
||
895 | * @return string |
||
896 | */ |
||
897 | public function getCompiledUpdate(string $table='', bool $reset=TRUE): string |
||
901 | |||
902 | /** |
||
903 | * Returns the generated 'delete' sql query |
||
904 | * |
||
905 | * @param string $table |
||
906 | * @param bool $reset |
||
907 | * @return string |
||
908 | */ |
||
909 | public function getCompiledDelete(string $table='', bool $reset=TRUE): string |
||
913 | |||
914 | // -------------------------------------------------------------------------- |
||
915 | // ! Miscellaneous Methods |
||
916 | // -------------------------------------------------------------------------- |
||
917 | |||
918 | /** |
||
919 | * Clear out the class variables, so the next query can be run |
||
920 | * |
||
921 | * @return void |
||
922 | */ |
||
923 | public function resetQuery(): void |
||
928 | |||
929 | |||
930 | |||
931 | /** |
||
932 | * Method to simplify select_ methods |
||
933 | * |
||
934 | * @param string $field |
||
935 | * @param string|bool $as |
||
936 | * @return string |
||
937 | */ |
||
938 | protected function _select(string $field, $as = FALSE): string |
||
951 | |||
952 | /** |
||
953 | * Helper function for returning sql strings |
||
954 | * |
||
955 | * @param string $type |
||
956 | * @param string $table |
||
957 | * @param bool $reset |
||
958 | * @return string |
||
959 | */ |
||
960 | protected function _getCompile(string $type, string $table, bool $reset): string |
||
972 | |||
973 | /** |
||
974 | * Simplify 'like' methods |
||
975 | * |
||
976 | * @param string $field |
||
977 | * @param mixed $val |
||
978 | * @param string $pos |
||
979 | * @param string $like |
||
980 | * @param string $conj |
||
981 | * @return self |
||
982 | */ |
||
983 | protected function _like(string $field, $val, string $pos, string $like='LIKE', string $conj='AND'): self |
||
1011 | |||
1012 | /** |
||
1013 | * Simplify building having clauses |
||
1014 | * |
||
1015 | * @param mixed $key |
||
1016 | * @param mixed $values |
||
1017 | * @param string $conj |
||
1018 | * @return self |
||
1019 | */ |
||
1020 | protected function _having($key, $values=[], string $conj='AND'): self |
||
1047 | |||
1048 | /** |
||
1049 | * Do all the redundant stuff for where/having type methods |
||
1050 | * |
||
1051 | * @param mixed $key |
||
1052 | * @param mixed $val |
||
1053 | * @return array |
||
1054 | */ |
||
1055 | protected function _where($key, $val=[]): array |
||
1077 | |||
1078 | /** |
||
1079 | * Simplify generating where string |
||
1080 | * |
||
1081 | * @param mixed $key |
||
1082 | * @param mixed $values |
||
1083 | * @param string $defaultConj |
||
1084 | * @return self |
||
1085 | */ |
||
1086 | protected function _whereString($key, $values=[], string $defaultConj='AND'): self |
||
1123 | |||
1124 | /** |
||
1125 | * Simplify where_in methods |
||
1126 | * |
||
1127 | * @param mixed $key |
||
1128 | * @param mixed $val |
||
1129 | * @param string $in - The (not) in fragment |
||
1130 | * @param string $conj - The where in conjunction |
||
1131 | * @return self |
||
1132 | */ |
||
1133 | protected function _whereIn($key, $val=[], string $in='IN', string $conj='AND'): self |
||
1146 | |||
1147 | /** |
||
1148 | * Executes the compiled query |
||
1149 | * |
||
1150 | * @param string $type |
||
1151 | * @param string $table |
||
1152 | * @param string $sql |
||
1153 | * @param array|null $vals |
||
1154 | * @param boolean $reset |
||
1155 | * @return PDOStatement |
||
1156 | */ |
||
1157 | protected function _run(string $type, string $table, $sql=NULL, $vals=NULL, bool $reset=TRUE): PDOStatement |
||
1189 | |||
1190 | /** |
||
1191 | * Convert the prepared statement into readable sql |
||
1192 | * |
||
1193 | * @param array $vals |
||
1194 | * @param string $sql |
||
1195 | * @param int $totalTime |
||
1196 | * @return void |
||
1197 | */ |
||
1198 | protected function _appendQuery($vals, string $sql, int $totalTime) |
||
1226 | |||
1227 | /** |
||
1228 | * Sub-method for generating sql strings |
||
1229 | * |
||
1230 | * @param string $type |
||
1231 | * @param string $table |
||
1232 | * @return string |
||
1233 | */ |
||
1234 | protected function _compileType(string $type='', string $table=''): string |
||
1279 | |||
1280 | /** |
||
1281 | * String together the sql statements for sending to the db |
||
1282 | * |
||
1283 | * @param string $type |
||
1284 | * @param string $table |
||
1285 | * @return string |
||
1286 | */ |
||
1287 | protected function _compile(string $type='', string $table=''): string |
||
1333 | } |
||
1334 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..