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 QueryBuilderEngine 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 QueryBuilderEngine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class QueryBuilderEngine extends BaseEngine |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Builder object. |
||
| 25 | * |
||
| 26 | * @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 27 | */ |
||
| 28 | protected $query; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Query builder object. |
||
| 32 | * |
||
| 33 | * @var \Illuminate\Database\Query\Builder |
||
| 34 | */ |
||
| 35 | protected $builder; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Database connection used. |
||
| 39 | * |
||
| 40 | * @var \Illuminate\Database\Connection |
||
| 41 | */ |
||
| 42 | protected $connection; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 46 | * @param \Yajra\Datatables\Request $request |
||
| 47 | */ |
||
| 48 | public function __construct(Builder $builder, Request $request) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set auto filter off and run your own filter. |
||
| 63 | * Overrides global search. |
||
| 64 | * |
||
| 65 | * @param callable $callback |
||
| 66 | * @param bool $globalSearch |
||
| 67 | * @return $this |
||
| 68 | */ |
||
| 69 | public function filter(callable $callback, $globalSearch = false) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Perform global search. |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function filtering() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Perform multi-term search by splitting keyword into |
||
| 96 | * individual words and searches for each of them. |
||
| 97 | * |
||
| 98 | * @param string $keyword |
||
| 99 | */ |
||
| 100 | private function smartGlobalSearch($keyword) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Perform global search for the given keyword. |
||
| 111 | * |
||
| 112 | * @param string $keyword |
||
| 113 | */ |
||
| 114 | private function globalSearch($keyword) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the base query builder instance. |
||
| 158 | * |
||
| 159 | * @param mixed $instance |
||
| 160 | * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 161 | */ |
||
| 162 | protected function getBaseQueryBuilder($instance = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Check if column has custom filter handler. |
||
| 177 | * |
||
| 178 | * @param string $columnName |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function hasCustomFilter($columnName) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get eager loads keys if eloquent. |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | protected function getEagerLoads() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add relation query on global search. |
||
| 202 | * |
||
| 203 | * @param mixed $query |
||
| 204 | * @param string $relation |
||
| 205 | * @param string $column |
||
| 206 | * @param string $keyword |
||
| 207 | */ |
||
| 208 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Compile query builder where clause depending on configurations. |
||
| 301 | * |
||
| 302 | * @param mixed $query |
||
| 303 | * @param string $column |
||
| 304 | * @param string $keyword |
||
| 305 | * @param string $relation |
||
| 306 | */ |
||
| 307 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Patch for fix about ambiguous field. |
||
| 322 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 323 | * |
||
| 324 | * @param mixed $query |
||
| 325 | * @param string $column |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | protected function addTablePrefix($query, $column) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Wrap column with DB grammar. |
||
| 350 | * |
||
| 351 | * @param string $column |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | protected function wrap($column) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Wrap a column and cast based on database driver. |
||
| 361 | * |
||
| 362 | * @param string $column |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | protected function castColumn($column) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Prepare search keyword based on configurations. |
||
| 378 | * |
||
| 379 | * @param string $keyword |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | protected function prepareKeyword($keyword) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Organizes works. |
||
| 401 | * |
||
| 402 | * @param bool $mDataSupport |
||
| 403 | * @return \Illuminate\Http\JsonResponse |
||
| 404 | * @throws \Exception |
||
| 405 | */ |
||
| 406 | public function make($mDataSupport = false) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Count total items. |
||
| 427 | * |
||
| 428 | * @return integer |
||
| 429 | */ |
||
| 430 | public function totalCount() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Counts current query. |
||
| 437 | * |
||
| 438 | * @return int |
||
| 439 | */ |
||
| 440 | public function count() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Perform sorting of columns. |
||
| 456 | * |
||
| 457 | * @return void |
||
| 458 | */ |
||
| 459 | public function ordering() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Check if column has custom sort handler. |
||
| 523 | * |
||
| 524 | * @param string $column |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | protected function hasCustomOrder($column) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Join eager loaded relation and get the related column name. |
||
| 534 | * |
||
| 535 | * @param string $relation |
||
| 536 | * @param string $relationColumn |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Perform join query. |
||
| 593 | * |
||
| 594 | * @param string $table |
||
| 595 | * @param string $foreign |
||
| 596 | * @param string $other |
||
| 597 | */ |
||
| 598 | protected function performJoin($table, $foreign, $other) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get NULLS LAST SQL. |
||
| 612 | * |
||
| 613 | * @param string $column |
||
| 614 | * @param string $direction |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | protected function getNullsLastSql($column, $direction) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Perform column search. |
||
| 626 | * |
||
| 627 | * @return void |
||
| 628 | */ |
||
| 629 | public function columnSearch() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get column keyword to use for search. |
||
| 668 | * |
||
| 669 | * @param int $i |
||
| 670 | * @param bool $raw |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Compile queries for column search. |
||
| 685 | * |
||
| 686 | * @param int $i |
||
| 687 | * @param string $column |
||
| 688 | * @param string $keyword |
||
| 689 | */ |
||
| 690 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Compile regex query column search. |
||
| 702 | * |
||
| 703 | * @param mixed $column |
||
| 704 | * @param string $keyword |
||
| 705 | */ |
||
| 706 | protected function regexColumnSearch($column, $keyword) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Perform pagination. |
||
| 722 | * |
||
| 723 | * @return void |
||
| 724 | */ |
||
| 725 | public function paging() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get paginated results. |
||
| 733 | * |
||
| 734 | * @return \Illuminate\Support\Collection |
||
| 735 | */ |
||
| 736 | public function results() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Add column in collection. |
||
| 743 | * |
||
| 744 | * @param string $name |
||
| 745 | * @param string|callable $content |
||
| 746 | * @param bool|int $order |
||
| 747 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 748 | */ |
||
| 749 | public function addColumn($name, $content, $order = false) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Get query builder instance. |
||
| 758 | * |
||
| 759 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 760 | */ |
||
| 761 | public function getQuery() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Append debug parameters on output. |
||
| 768 | * |
||
| 769 | * @param array $output |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | protected function showDebugger(array $output) |
||
| 779 | } |
||
| 780 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.