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 |
||
28 | class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface { |
||
29 | |||
30 | /** |
||
31 | * String class values to be reset |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $string_vars = [ |
||
36 | 'select_string', |
||
37 | 'from_string', |
||
38 | 'set_string', |
||
39 | 'order_string', |
||
40 | 'group_string', |
||
41 | 'limit', |
||
42 | 'offset', |
||
43 | 'explain', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Array class variables to be reset |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $array_vars = [ |
||
52 | 'set_array_keys', |
||
53 | 'order_array', |
||
54 | 'group_array', |
||
55 | 'values', |
||
56 | 'where_values', |
||
57 | 'query_map', |
||
58 | 'having_map' |
||
59 | ]; |
||
60 | |||
61 | // -------------------------------------------------------------------------- |
||
62 | // ! Methods |
||
63 | // -------------------------------------------------------------------------- |
||
64 | |||
65 | /** |
||
66 | * Constructor |
||
67 | * |
||
68 | * @param DriverInterface $db |
||
69 | * @param QueryParser $parser |
||
70 | */ |
||
71 | public function __construct(DriverInterface $db, QueryParser $parser) |
||
83 | |||
84 | // -------------------------------------------------------------------------- |
||
85 | |||
86 | /** |
||
87 | * Destructor |
||
88 | * @codeCoverageIgnore |
||
89 | */ |
||
90 | public function __destruct() |
||
94 | |||
95 | // -------------------------------------------------------------------------- |
||
96 | |||
97 | /** |
||
98 | * Calls a function further down the inheritence chain |
||
99 | * |
||
100 | * @param string $name |
||
101 | * @param array $params |
||
102 | * @return mixed |
||
103 | * @throws BadMethodCallException |
||
104 | */ |
||
105 | public function __call($name, $params) |
||
124 | |||
125 | // -------------------------------------------------------------------------- |
||
126 | // ! Select Queries |
||
127 | // -------------------------------------------------------------------------- |
||
128 | |||
129 | /** |
||
130 | * Specifies rows to select in a query |
||
131 | * |
||
132 | * @param string $fields |
||
133 | * @return QueryBuilderInterface |
||
134 | */ |
||
135 | public function select($fields) |
||
171 | |||
172 | // -------------------------------------------------------------------------- |
||
173 | |||
174 | /** |
||
175 | * Selects the maximum value of a field from a query |
||
176 | * |
||
177 | * @param string $field |
||
178 | * @param string|bool $as |
||
179 | * @return QueryBuilderInterface |
||
180 | */ |
||
181 | public function select_max($field, $as=FALSE) |
||
187 | |||
188 | // -------------------------------------------------------------------------- |
||
189 | |||
190 | /** |
||
191 | * Selects the minimum value of a field from a query |
||
192 | * |
||
193 | * @param string $field |
||
194 | * @param string|bool $as |
||
195 | * @return QueryBuilderInterface |
||
196 | */ |
||
197 | public function select_min($field, $as=FALSE) |
||
203 | |||
204 | // -------------------------------------------------------------------------- |
||
205 | |||
206 | /** |
||
207 | * Selects the average value of a field from a query |
||
208 | * |
||
209 | * @param string $field |
||
210 | * @param string|bool $as |
||
211 | * @return QueryBuilderInterface |
||
212 | */ |
||
213 | public function select_avg($field, $as=FALSE) |
||
219 | |||
220 | // -------------------------------------------------------------------------- |
||
221 | |||
222 | /** |
||
223 | * Selects the sum of a field from a query |
||
224 | * |
||
225 | * @param string $field |
||
226 | * @param string|bool $as |
||
227 | * @return QueryBuilderInterface |
||
228 | */ |
||
229 | public function select_sum($field, $as=FALSE) |
||
235 | |||
236 | // -------------------------------------------------------------------------- |
||
237 | |||
238 | /** |
||
239 | * Adds the 'distinct' keyword to a query |
||
240 | * |
||
241 | * @return QueryBuilderInterface |
||
242 | */ |
||
243 | public function distinct() |
||
249 | |||
250 | // -------------------------------------------------------------------------- |
||
251 | |||
252 | /** |
||
253 | * Tell the database to give you the query plan instead of result set |
||
254 | * |
||
255 | * @return QueryBuilderInterface |
||
256 | */ |
||
257 | public function explain() |
||
262 | |||
263 | // -------------------------------------------------------------------------- |
||
264 | |||
265 | /** |
||
266 | * Specify the database table to select from |
||
267 | * |
||
268 | * @param string $tblname |
||
269 | * @return QueryBuilderInterface |
||
270 | */ |
||
271 | public function from($tblname) |
||
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') |
||
303 | |||
304 | // -------------------------------------------------------------------------- |
||
305 | |||
306 | /** |
||
307 | * Generates an OR Like clause |
||
308 | * |
||
309 | * @param string $field |
||
310 | * @param mixed $val |
||
311 | * @param string $pos |
||
312 | * @return QueryBuilderInterface |
||
313 | */ |
||
314 | public function or_like($field, $val, $pos='both') |
||
318 | |||
319 | // -------------------------------------------------------------------------- |
||
320 | |||
321 | /** |
||
322 | * Generates a NOT LIKE clause |
||
323 | * |
||
324 | * @param string $field |
||
325 | * @param mixed $val |
||
326 | * @param string $pos |
||
327 | * @return QueryBuilderInterface |
||
328 | */ |
||
329 | public function not_like($field, $val, $pos='both') |
||
333 | |||
334 | // -------------------------------------------------------------------------- |
||
335 | |||
336 | /** |
||
337 | * Generates a OR NOT LIKE clause |
||
338 | * |
||
339 | * @param string $field |
||
340 | * @param mixed $val |
||
341 | * @param string $pos |
||
342 | * @return QueryBuilderInterface |
||
343 | */ |
||
344 | public function or_not_like($field, $val, $pos='both') |
||
348 | |||
349 | // -------------------------------------------------------------------------- |
||
350 | // ! Having methods |
||
351 | // -------------------------------------------------------------------------- |
||
352 | |||
353 | /** |
||
354 | * Generates a 'Having' clause |
||
355 | * |
||
356 | * @param mixed $key |
||
357 | * @param mixed $val |
||
358 | * @return QueryBuilderInterface |
||
359 | */ |
||
360 | public function having($key, $val=[]) |
||
364 | |||
365 | // -------------------------------------------------------------------------- |
||
366 | |||
367 | /** |
||
368 | * Generates a 'Having' clause prefixed with 'OR' |
||
369 | * |
||
370 | * @param mixed $key |
||
371 | * @param mixed $val |
||
372 | * @return QueryBuilderInterface |
||
373 | */ |
||
374 | public function or_having($key, $val=[]) |
||
378 | |||
379 | // -------------------------------------------------------------------------- |
||
380 | // ! 'Where' methods |
||
381 | // -------------------------------------------------------------------------- |
||
382 | |||
383 | /** |
||
384 | * Specify condition(s) in the where clause of a query |
||
385 | * Note: this function works with key / value, or a |
||
386 | * passed array with key / value pairs |
||
387 | * |
||
388 | * @param mixed $key |
||
389 | * @param mixed $val |
||
390 | * @param mixed $escape |
||
391 | * @return QueryBuilderInterface |
||
392 | */ |
||
393 | public function where($key, $val=[], $escape=NULL) |
||
397 | |||
398 | // -------------------------------------------------------------------------- |
||
399 | |||
400 | /** |
||
401 | * Where clause prefixed with "OR" |
||
402 | * |
||
403 | * @param string $key |
||
404 | * @param mixed $val |
||
405 | * @return QueryBuilderInterface |
||
406 | */ |
||
407 | public function or_where($key, $val=[]) |
||
411 | |||
412 | // -------------------------------------------------------------------------- |
||
413 | |||
414 | /** |
||
415 | * Where clause with 'IN' statement |
||
416 | * |
||
417 | * @param mixed $field |
||
418 | * @param mixed $val |
||
419 | * @return QueryBuilderInterface |
||
420 | */ |
||
421 | public function where_in($field, $val=[]) |
||
425 | |||
426 | // -------------------------------------------------------------------------- |
||
427 | |||
428 | /** |
||
429 | * Where in statement prefixed with "or" |
||
430 | * |
||
431 | * @param string $field |
||
432 | * @param mixed $val |
||
433 | * @return QueryBuilderInterface |
||
434 | */ |
||
435 | public function or_where_in($field, $val=[]) |
||
439 | |||
440 | // -------------------------------------------------------------------------- |
||
441 | |||
442 | /** |
||
443 | * WHERE NOT IN (FOO) clause |
||
444 | * |
||
445 | * @param string $field |
||
446 | * @param mixed $val |
||
447 | * @return QueryBuilderInterface |
||
448 | */ |
||
449 | public function where_not_in($field, $val=[]) |
||
453 | |||
454 | // -------------------------------------------------------------------------- |
||
455 | |||
456 | /** |
||
457 | * OR WHERE NOT IN (FOO) clause |
||
458 | * |
||
459 | * @param string $field |
||
460 | * @param mixed $val |
||
461 | * @return QueryBuilderInterface |
||
462 | */ |
||
463 | public function or_where_not_in($field, $val=[]) |
||
467 | |||
468 | // -------------------------------------------------------------------------- |
||
469 | // ! Other Query Modifier methods |
||
470 | // -------------------------------------------------------------------------- |
||
471 | |||
472 | /** |
||
473 | * Sets values for inserts / updates / deletes |
||
474 | * |
||
475 | * @param mixed $key |
||
476 | * @param mixed $val |
||
477 | * @return QueryBuilderInterface |
||
478 | */ |
||
479 | public function set($key, $val = NULL) |
||
494 | |||
495 | // -------------------------------------------------------------------------- |
||
496 | |||
497 | /** |
||
498 | * Creates a join phrase in a compiled query |
||
499 | * |
||
500 | * @param string $table |
||
501 | * @param string $condition |
||
502 | * @param string $type |
||
503 | * @return QueryBuilderInterface |
||
504 | */ |
||
505 | public function join($table, $condition, $type='') |
||
521 | |||
522 | // -------------------------------------------------------------------------- |
||
523 | |||
524 | /** |
||
525 | * Group the results by the selected field(s) |
||
526 | * |
||
527 | * @param mixed $field |
||
528 | * @return QueryBuilderInterface |
||
529 | */ |
||
530 | public function group_by($field) |
||
546 | |||
547 | // -------------------------------------------------------------------------- |
||
548 | |||
549 | /** |
||
550 | * Order the results by the selected field(s) |
||
551 | * |
||
552 | * @param string $field |
||
553 | * @param string $type |
||
554 | * @return QueryBuilderInterface |
||
555 | */ |
||
556 | public function order_by($field, $type="") |
||
585 | |||
586 | // -------------------------------------------------------------------------- |
||
587 | |||
588 | /** |
||
589 | * Set a limit on the current sql statement |
||
590 | * |
||
591 | * @param int $limit |
||
592 | * @param int|bool $offset |
||
593 | * @return QueryBuilderInterface |
||
594 | */ |
||
595 | public function limit($limit, $offset=FALSE) |
||
602 | |||
603 | // -------------------------------------------------------------------------- |
||
604 | // ! Query Grouping Methods |
||
605 | // -------------------------------------------------------------------------- |
||
606 | |||
607 | /** |
||
608 | * Adds a paren to the current query for query grouping |
||
609 | * |
||
610 | * @return QueryBuilderInterface |
||
611 | */ |
||
612 | public function group_start() |
||
620 | |||
621 | // -------------------------------------------------------------------------- |
||
622 | |||
623 | /** |
||
624 | * Adds a paren to the current query for query grouping, |
||
625 | * prefixed with 'NOT' |
||
626 | * |
||
627 | * @return QueryBuilderInterface |
||
628 | */ |
||
629 | public function not_group_start() |
||
637 | |||
638 | // -------------------------------------------------------------------------- |
||
639 | |||
640 | /** |
||
641 | * Adds a paren to the current query for query grouping, |
||
642 | * prefixed with 'OR' |
||
643 | * |
||
644 | * @return QueryBuilderInterface |
||
645 | */ |
||
646 | public function or_group_start() |
||
652 | |||
653 | // -------------------------------------------------------------------------- |
||
654 | |||
655 | /** |
||
656 | * Adds a paren to the current query for query grouping, |
||
657 | * prefixed with 'OR NOT' |
||
658 | * |
||
659 | * @return QueryBuilderInterface |
||
660 | */ |
||
661 | public function or_not_group_start() |
||
667 | |||
668 | // -------------------------------------------------------------------------- |
||
669 | |||
670 | /** |
||
671 | * Ends a query group |
||
672 | * |
||
673 | * @return QueryBuilderInterface |
||
674 | */ |
||
675 | public function group_end() |
||
681 | |||
682 | // -------------------------------------------------------------------------- |
||
683 | // ! Query execution methods |
||
684 | // -------------------------------------------------------------------------- |
||
685 | |||
686 | /** |
||
687 | * Select and retrieve all records from the current table, and/or |
||
688 | * execute current compiled query |
||
689 | * |
||
690 | * @param string $table |
||
691 | * @param int|bool $limit |
||
692 | * @param int|bool $offset |
||
693 | * @return \PDOStatement |
||
694 | */ |
||
695 | public function get($table='', $limit=FALSE, $offset=FALSE) |
||
711 | |||
712 | // -------------------------------------------------------------------------- |
||
713 | |||
714 | /** |
||
715 | * Convenience method for get() with a where clause |
||
716 | * |
||
717 | * @param string $table |
||
718 | * @param array $where |
||
719 | * @param int|bool $limit |
||
720 | * @param int|bool $offset |
||
721 | * @return \PDOStatement |
||
722 | */ |
||
723 | public function get_where($table, $where=[], $limit=FALSE, $offset=FALSE) |
||
731 | |||
732 | // -------------------------------------------------------------------------- |
||
733 | |||
734 | /** |
||
735 | * Retrieve the number of rows in the selected table |
||
736 | * |
||
737 | * @param string $table |
||
738 | * @return int |
||
739 | */ |
||
740 | public function count_all($table) |
||
746 | |||
747 | // -------------------------------------------------------------------------- |
||
748 | |||
749 | /** |
||
750 | * Retrieve the number of results for the generated query - used |
||
751 | * in place of the get() method |
||
752 | * |
||
753 | * @param string $table |
||
754 | * @param boolean $reset |
||
755 | * @return int |
||
756 | */ |
||
757 | public function count_all_results($table='', $reset = TRUE) |
||
770 | |||
771 | // -------------------------------------------------------------------------- |
||
772 | |||
773 | /** |
||
774 | * Creates an insert clause, and executes it |
||
775 | * |
||
776 | * @param string $table |
||
777 | * @param mixed $data |
||
778 | * @return \PDOStatement |
||
779 | */ |
||
780 | public function insert($table, $data=[]) |
||
789 | |||
790 | // -------------------------------------------------------------------------- |
||
791 | |||
792 | /** |
||
793 | * Creates and executes a batch insertion query |
||
794 | * |
||
795 | * @param string $table |
||
796 | * @param array $data |
||
797 | * @return \PDOStatement |
||
798 | */ |
||
799 | View Code Duplication | public function insert_batch($table, $data=[]) |
|
808 | |||
809 | // -------------------------------------------------------------------------- |
||
810 | |||
811 | /** |
||
812 | * Creates an update clause, and executes it |
||
813 | * |
||
814 | * @param string $table |
||
815 | * @param mixed $data |
||
816 | * @return \PDOStatement |
||
817 | */ |
||
818 | public function update($table, $data=[]) |
||
827 | |||
828 | // -------------------------------------------------------------------------- |
||
829 | |||
830 | /** |
||
831 | * Creates a batch update, and executes it. |
||
832 | * Returns the number of affected rows |
||
833 | * |
||
834 | * @param string $table |
||
835 | * @param array|object $data |
||
836 | * @param string $where |
||
837 | * @return int|null |
||
838 | */ |
||
839 | View Code Duplication | public function update_batch($table, $data, $where) |
|
848 | |||
849 | // -------------------------------------------------------------------------- |
||
850 | |||
851 | /** |
||
852 | * Insertion with automatic overwrite, rather than attempted duplication |
||
853 | * |
||
854 | * @param string $table |
||
855 | * @param array $data |
||
856 | * @return \PDOStatement|null |
||
857 | */ |
||
858 | public function replace($table, $data=[]) |
||
867 | |||
868 | // -------------------------------------------------------------------------- |
||
869 | |||
870 | /** |
||
871 | * Deletes data from a table |
||
872 | * |
||
873 | * @param string $table |
||
874 | * @param mixed $where |
||
875 | * @return \PDOStatement |
||
876 | */ |
||
877 | public function delete($table, $where='') |
||
887 | |||
888 | // -------------------------------------------------------------------------- |
||
889 | // ! SQL Returning Methods |
||
890 | // -------------------------------------------------------------------------- |
||
891 | |||
892 | |||
893 | |||
894 | /** |
||
895 | * Returns the generated 'select' sql query |
||
896 | * |
||
897 | * @param string $table |
||
898 | * @param bool $reset |
||
899 | * @return string |
||
900 | */ |
||
901 | public function get_compiled_select($table='', $reset=TRUE) |
||
911 | |||
912 | // -------------------------------------------------------------------------- |
||
913 | |||
914 | /** |
||
915 | * Returns the generated 'insert' sql query |
||
916 | * |
||
917 | * @param string $table |
||
918 | * @param bool $reset |
||
919 | * @return string |
||
920 | */ |
||
921 | public function get_compiled_insert($table, $reset=TRUE) |
||
925 | |||
926 | // -------------------------------------------------------------------------- |
||
927 | |||
928 | /** |
||
929 | * Returns the generated 'update' sql query |
||
930 | * |
||
931 | * @param string $table |
||
932 | * @param bool $reset |
||
933 | * @return string |
||
934 | */ |
||
935 | public function get_compiled_update($table='', $reset=TRUE) |
||
939 | |||
940 | // -------------------------------------------------------------------------- |
||
941 | |||
942 | /** |
||
943 | * Returns the generated 'delete' sql query |
||
944 | * |
||
945 | * @param string $table |
||
946 | * @param bool $reset |
||
947 | * @return string |
||
948 | */ |
||
949 | public function get_compiled_delete($table="", $reset=TRUE) |
||
953 | |||
954 | |||
955 | // -------------------------------------------------------------------------- |
||
956 | // ! Miscellaneous Methods |
||
957 | // -------------------------------------------------------------------------- |
||
958 | |||
959 | /** |
||
960 | * Clear out the class variables, so the next query can be run |
||
961 | * |
||
962 | * @return void |
||
963 | */ |
||
964 | public function reset_query() |
||
978 | } |
||
979 | // End of query_builder.php |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.