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); |
||
| 24 | class QueryBuilder extends AbstractQueryBuilder implements QueryBuilderInterface { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * String class values to be reset |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $stringVars = [ |
||
| 32 | 'selectString', |
||
| 33 | 'fromString', |
||
| 34 | 'setString', |
||
| 35 | 'orderString', |
||
| 36 | 'groupString', |
||
| 37 | 'limit', |
||
| 38 | 'offset', |
||
| 39 | 'explain', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array class variables to be reset |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private $arrayVars = [ |
||
| 48 | 'setArrayKeys', |
||
| 49 | 'orderArray', |
||
| 50 | 'groupArray', |
||
| 51 | 'values', |
||
| 52 | 'whereValues', |
||
| 53 | 'queryMap', |
||
| 54 | 'havingMap' |
||
| 55 | ]; |
||
| 56 | |||
| 57 | // -------------------------------------------------------------------------- |
||
| 58 | // ! Methods |
||
| 59 | // -------------------------------------------------------------------------- |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param DriverInterface $db |
||
| 65 | * @param QueryParser $parser |
||
| 66 | */ |
||
| 67 | public function __construct(DriverInterface $db, QueryParser $parser) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Destructor |
||
| 82 | * @codeCoverageIgnore |
||
| 83 | */ |
||
| 84 | public function __destruct() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Calls a function further down the inheritance chain |
||
| 91 | * |
||
| 92 | * @param string $name |
||
| 93 | * @param array $params |
||
| 94 | * @return mixed |
||
| 95 | * @throws BadMethodCallException |
||
| 96 | */ |
||
| 97 | public function __call(string $name, array $params) |
||
| 116 | |||
| 117 | // -------------------------------------------------------------------------- |
||
| 118 | // ! Select Queries |
||
| 119 | // -------------------------------------------------------------------------- |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Specifies rows to select in a query |
||
| 123 | * |
||
| 124 | * @param string $fields |
||
| 125 | * @return QueryBuilderInterface |
||
| 126 | */ |
||
| 127 | public function select(string $fields): QueryBuilderInterface |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Selects the maximum value of a field from a query |
||
| 166 | * |
||
| 167 | * @param string $field |
||
| 168 | * @param string|bool $as |
||
| 169 | * @return QueryBuilderInterface |
||
| 170 | */ |
||
| 171 | public function selectMax(string $field, $as=FALSE): QueryBuilderInterface |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Selects the minimum value of a field from a query |
||
| 180 | * |
||
| 181 | * @param string $field |
||
| 182 | * @param string|bool $as |
||
| 183 | * @return QueryBuilderInterface |
||
| 184 | */ |
||
| 185 | public function selectMin(string $field, $as=FALSE): QueryBuilderInterface |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Selects the average value of a field from a query |
||
| 194 | * |
||
| 195 | * @param string $field |
||
| 196 | * @param string|bool $as |
||
| 197 | * @return QueryBuilderInterface |
||
| 198 | */ |
||
| 199 | public function selectAvg(string $field, $as=FALSE): QueryBuilderInterface |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Selects the sum of a field from a query |
||
| 208 | * |
||
| 209 | * @param string $field |
||
| 210 | * @param string|bool $as |
||
| 211 | * @return QueryBuilderInterface |
||
| 212 | */ |
||
| 213 | public function selectSum(string $field, $as=FALSE): QueryBuilderInterface |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Adds the 'distinct' keyword to a query |
||
| 222 | * |
||
| 223 | * @return QueryBuilderInterface |
||
| 224 | */ |
||
| 225 | public function distinct(): QueryBuilderInterface |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Tell the database to give you the query plan instead of result set |
||
| 234 | * |
||
| 235 | * @return QueryBuilderInterface |
||
| 236 | */ |
||
| 237 | public function explain(): QueryBuilderInterface |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Specify the database table to select from |
||
| 245 | * |
||
| 246 | * @param string $tblname |
||
| 247 | * @return QueryBuilderInterface |
||
| 248 | */ |
||
| 249 | public function from($tblname): QueryBuilderInterface |
||
| 264 | |||
| 265 | // -------------------------------------------------------------------------- |
||
| 266 | // ! 'Like' methods |
||
| 267 | // -------------------------------------------------------------------------- |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Creates a Like clause in the sql statement |
||
| 271 | * |
||
| 272 | * @param string $field |
||
| 273 | * @param mixed $val |
||
| 274 | * @param string $pos |
||
| 275 | * @return QueryBuilderInterface |
||
| 276 | */ |
||
| 277 | public function like($field, $val, $pos='both'): QueryBuilderInterface |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Generates an OR Like clause |
||
| 284 | * |
||
| 285 | * @param string $field |
||
| 286 | * @param mixed $val |
||
| 287 | * @param string $pos |
||
| 288 | * @return QueryBuilderInterface |
||
| 289 | */ |
||
| 290 | public function orLike($field, $val, $pos='both'): QueryBuilderInterface |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Generates a NOT LIKE clause |
||
| 297 | * |
||
| 298 | * @param string $field |
||
| 299 | * @param mixed $val |
||
| 300 | * @param string $pos |
||
| 301 | * @return QueryBuilderInterface |
||
| 302 | */ |
||
| 303 | public function notLike($field, $val, $pos='both'): QueryBuilderInterface |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Generates a OR NOT LIKE clause |
||
| 310 | * |
||
| 311 | * @param string $field |
||
| 312 | * @param mixed $val |
||
| 313 | * @param string $pos |
||
| 314 | * @return QueryBuilderInterface |
||
| 315 | */ |
||
| 316 | public function orNotLike($field, $val, $pos='both'): QueryBuilderInterface |
||
| 320 | |||
| 321 | // -------------------------------------------------------------------------- |
||
| 322 | // ! Having methods |
||
| 323 | // -------------------------------------------------------------------------- |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generates a 'Having' clause |
||
| 327 | * |
||
| 328 | * @param mixed $key |
||
| 329 | * @param mixed $val |
||
| 330 | * @return QueryBuilderInterface |
||
| 331 | */ |
||
| 332 | public function having($key, $val=[]): QueryBuilderInterface |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Generates a 'Having' clause prefixed with 'OR' |
||
| 339 | * |
||
| 340 | * @param mixed $key |
||
| 341 | * @param mixed $val |
||
| 342 | * @return QueryBuilderInterface |
||
| 343 | */ |
||
| 344 | public function orHaving($key, $val=[]): QueryBuilderInterface |
||
| 348 | |||
| 349 | // -------------------------------------------------------------------------- |
||
| 350 | // ! 'Where' methods |
||
| 351 | // -------------------------------------------------------------------------- |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Specify condition(s) in the where clause of a query |
||
| 355 | * Note: this function works with key / value, or a |
||
| 356 | * passed array with key / value pairs |
||
| 357 | * |
||
| 358 | * @param mixed $key |
||
| 359 | * @param mixed $val |
||
| 360 | * @param mixed $escape |
||
| 361 | * @return QueryBuilderInterface |
||
| 362 | */ |
||
| 363 | public function where($key, $val=[], $escape=NULL): QueryBuilderInterface |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Where clause prefixed with "OR" |
||
| 370 | * |
||
| 371 | * @param string $key |
||
| 372 | * @param mixed $val |
||
| 373 | * @return QueryBuilderInterface |
||
| 374 | */ |
||
| 375 | public function orWhere($key, $val=[]): QueryBuilderInterface |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Where clause with 'IN' statement |
||
| 382 | * |
||
| 383 | * @param mixed $field |
||
| 384 | * @param mixed $val |
||
| 385 | * @return QueryBuilderInterface |
||
| 386 | */ |
||
| 387 | public function whereIn($field, $val=[]): QueryBuilderInterface |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Where in statement prefixed with "or" |
||
| 394 | * |
||
| 395 | * @param string $field |
||
| 396 | * @param mixed $val |
||
| 397 | * @return QueryBuilderInterface |
||
| 398 | */ |
||
| 399 | public function orWhereIn($field, $val=[]): QueryBuilderInterface |
||
| 403 | |||
| 404 | /** |
||
| 405 | * WHERE NOT IN (FOO) clause |
||
| 406 | * |
||
| 407 | * @param string $field |
||
| 408 | * @param mixed $val |
||
| 409 | * @return QueryBuilderInterface |
||
| 410 | */ |
||
| 411 | public function whereNotIn($field, $val=[]): QueryBuilderInterface |
||
| 415 | |||
| 416 | /** |
||
| 417 | * OR WHERE NOT IN (FOO) clause |
||
| 418 | * |
||
| 419 | * @param string $field |
||
| 420 | * @param mixed $val |
||
| 421 | * @return QueryBuilderInterface |
||
| 422 | */ |
||
| 423 | public function orWhereNotIn($field, $val=[]): QueryBuilderInterface |
||
| 427 | |||
| 428 | // -------------------------------------------------------------------------- |
||
| 429 | // ! Other Query Modifier methods |
||
| 430 | // -------------------------------------------------------------------------- |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Sets values for inserts / updates / deletes |
||
| 434 | * |
||
| 435 | * @param mixed $key |
||
| 436 | * @param mixed $val |
||
| 437 | * @return QueryBuilderInterface |
||
| 438 | */ |
||
| 439 | public function set($key, $val = NULL): QueryBuilderInterface |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Creates a join phrase in a compiled query |
||
| 457 | * |
||
| 458 | * @param string $table |
||
| 459 | * @param string $condition |
||
| 460 | * @param string $type |
||
| 461 | * @return QueryBuilderInterface |
||
| 462 | */ |
||
| 463 | public function join($table, $condition, $type=''): QueryBuilderInterface |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Group the results by the selected field(s) |
||
| 482 | * |
||
| 483 | * @param mixed $field |
||
| 484 | * @return QueryBuilderInterface |
||
| 485 | */ |
||
| 486 | public function groupBy($field): QueryBuilderInterface |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Order the results by the selected field(s) |
||
| 505 | * |
||
| 506 | * @param string $field |
||
| 507 | * @param string $type |
||
| 508 | * @return QueryBuilderInterface |
||
| 509 | */ |
||
| 510 | public function orderBy($field, $type=""): QueryBuilderInterface |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Set a limit on the current sql statement |
||
| 542 | * |
||
| 543 | * @param int $limit |
||
| 544 | * @param int|bool $offset |
||
| 545 | * @return QueryBuilderInterface |
||
| 546 | */ |
||
| 547 | public function limit($limit, $offset=FALSE): QueryBuilderInterface |
||
| 554 | |||
| 555 | // -------------------------------------------------------------------------- |
||
| 556 | // ! Query Grouping Methods |
||
| 557 | // -------------------------------------------------------------------------- |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Adds a paren to the current query for query grouping |
||
| 561 | * |
||
| 562 | * @return QueryBuilderInterface |
||
| 563 | */ |
||
| 564 | View Code Duplication | public function groupStart(): QueryBuilderInterface |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Adds a paren to the current query for query grouping, |
||
| 575 | * prefixed with 'NOT' |
||
| 576 | * |
||
| 577 | * @return QueryBuilderInterface |
||
| 578 | */ |
||
| 579 | View Code Duplication | public function notGroupStart(): QueryBuilderInterface |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Adds a paren to the current query for query grouping, |
||
| 590 | * prefixed with 'OR' |
||
| 591 | * |
||
| 592 | * @return QueryBuilderInterface |
||
| 593 | */ |
||
| 594 | public function orGroupStart(): QueryBuilderInterface |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Adds a paren to the current query for query grouping, |
||
| 603 | * prefixed with 'OR NOT' |
||
| 604 | * |
||
| 605 | * @return QueryBuilderInterface |
||
| 606 | */ |
||
| 607 | public function orNotGroupStart(): QueryBuilderInterface |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Ends a query group |
||
| 616 | * |
||
| 617 | * @return QueryBuilderInterface |
||
| 618 | */ |
||
| 619 | public function groupEnd(): QueryBuilderInterface |
||
| 625 | |||
| 626 | // -------------------------------------------------------------------------- |
||
| 627 | // ! Query execution methods |
||
| 628 | // -------------------------------------------------------------------------- |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Select and retrieve all records from the current table, and/or |
||
| 632 | * execute current compiled query |
||
| 633 | * |
||
| 634 | * @param string $table |
||
| 635 | * @param int|bool $limit |
||
| 636 | * @param int|bool $offset |
||
| 637 | * @return PDOStatement |
||
| 638 | */ |
||
| 639 | public function get($table='', $limit=FALSE, $offset=FALSE): PDOStatement |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Convenience method for get() with a where clause |
||
| 658 | * |
||
| 659 | * @param string $table |
||
| 660 | * @param array $where |
||
| 661 | * @param int|bool $limit |
||
| 662 | * @param int|bool $offset |
||
| 663 | * @return PDOStatement |
||
| 664 | */ |
||
| 665 | public function getWhere($table, $where=[], $limit=FALSE, $offset=FALSE): PDOStatement |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Retrieve the number of rows in the selected table |
||
| 676 | * |
||
| 677 | * @param string $table |
||
| 678 | * @return int |
||
| 679 | */ |
||
| 680 | public function countAll($table): int |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Retrieve the number of results for the generated query - used |
||
| 689 | * in place of the get() method |
||
| 690 | * |
||
| 691 | * @param string $table |
||
| 692 | * @param boolean $reset |
||
| 693 | * @return int |
||
| 694 | */ |
||
| 695 | public function countAllResults(string $table='', bool $reset = TRUE): int |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Creates an insert clause, and executes it |
||
| 711 | * |
||
| 712 | * @param string $table |
||
| 713 | * @param mixed $data |
||
| 714 | * @return PDOStatement |
||
| 715 | */ |
||
| 716 | public function insert($table, $data=[]): PDOStatement |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Creates and executes a batch insertion query |
||
| 728 | * |
||
| 729 | * @param string $table |
||
| 730 | * @param array $data |
||
| 731 | * @return PDOStatement |
||
| 732 | */ |
||
| 733 | View Code Duplication | public function insertBatch($table, $data=[]): PDOStatement |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Creates an update clause, and executes it |
||
| 745 | * |
||
| 746 | * @param string $table |
||
| 747 | * @param mixed $data |
||
| 748 | * @return PDOStatement |
||
| 749 | */ |
||
| 750 | public function update($table, $data=[]): PDOStatement |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Creates a batch update, and executes it. |
||
| 762 | * Returns the number of affected rows |
||
| 763 | * |
||
| 764 | * @param string $table |
||
| 765 | * @param array|object $data |
||
| 766 | * @param string $where |
||
| 767 | * @return int|null |
||
| 768 | */ |
||
| 769 | View Code Duplication | public function updateBatch($table, $data, $where) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Insertion with automatic overwrite, rather than attempted duplication |
||
| 781 | * |
||
| 782 | * @param string $table |
||
| 783 | * @param array $data |
||
| 784 | * @return \PDOStatement|null |
||
| 785 | */ |
||
| 786 | public function replace($table, $data=[]) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Deletes data from a table |
||
| 798 | * |
||
| 799 | * @param string $table |
||
| 800 | * @param mixed $where |
||
| 801 | * @return PDOStatement |
||
| 802 | */ |
||
| 803 | public function delete($table, $where=''): PDOStatement |
||
| 813 | |||
| 814 | // -------------------------------------------------------------------------- |
||
| 815 | // ! SQL Returning Methods |
||
| 816 | // -------------------------------------------------------------------------- |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Returns the generated 'select' sql query |
||
| 820 | * |
||
| 821 | * @param string $table |
||
| 822 | * @param bool $reset |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | public function getCompiledSelect(string $table='', bool $reset=TRUE): string |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Returns the generated 'insert' sql query |
||
| 838 | * |
||
| 839 | * @param string $table |
||
| 840 | * @param bool $reset |
||
| 841 | * @return string |
||
| 842 | */ |
||
| 843 | public function getCompiledInsert(string $table, bool $reset=TRUE): string |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Returns the generated 'update' sql query |
||
| 850 | * |
||
| 851 | * @param string $table |
||
| 852 | * @param bool $reset |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | public function getCompiledUpdate(string $table='', bool $reset=TRUE): string |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Returns the generated 'delete' sql query |
||
| 862 | * |
||
| 863 | * @param string $table |
||
| 864 | * @param bool $reset |
||
| 865 | * @return string |
||
| 866 | */ |
||
| 867 | public function getCompiledDelete(string $table='', bool $reset=TRUE): string |
||
| 871 | |||
| 872 | // -------------------------------------------------------------------------- |
||
| 873 | // ! Miscellaneous Methods |
||
| 874 | // -------------------------------------------------------------------------- |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Clear out the class variables, so the next query can be run |
||
| 878 | * |
||
| 879 | * @return void |
||
| 880 | */ |
||
| 881 | public function resetQuery() |
||
| 895 | } |
||
| 896 | // 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.