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 | * Count total items. |
||
| 78 | * |
||
| 79 | * @return integer |
||
| 80 | */ |
||
| 81 | public function totalCount() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Counts current query. |
||
| 88 | * |
||
| 89 | * @return int |
||
| 90 | */ |
||
| 91 | public function count() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Wrap column with DB grammar. |
||
| 107 | * |
||
| 108 | * @param string $column |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | protected function wrap($column) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Perform global search. |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function filtering() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Perform multi-term search by splitting keyword into |
||
| 136 | * individual words and searches for each of them. |
||
| 137 | * |
||
| 138 | * @param string $keyword |
||
| 139 | */ |
||
| 140 | private function smartGlobalSearch($keyword) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Perform global search for the given keyword. |
||
| 151 | * |
||
| 152 | * @param string $keyword |
||
| 153 | */ |
||
| 154 | private function globalSearch($keyword) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the base query builder instance. |
||
| 198 | * |
||
| 199 | * @param mixed $instance |
||
| 200 | * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 201 | */ |
||
| 202 | protected function getBaseQueryBuilder($instance = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Check if column has custom filter handler. |
||
| 217 | * |
||
| 218 | * @param string $columnName |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function hasCustomFilter($columnName) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get eager loads keys if eloquent. |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | protected function getEagerLoads() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add relation query on global search. |
||
| 242 | * |
||
| 243 | * @param mixed $query |
||
| 244 | * @param string $relation |
||
| 245 | * @param string $column |
||
| 246 | * @param string $keyword |
||
| 247 | */ |
||
| 248 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Compile query builder where clause depending on configurations. |
||
| 341 | * |
||
| 342 | * @param mixed $query |
||
| 343 | * @param string $column |
||
| 344 | * @param string $keyword |
||
| 345 | * @param string $relation |
||
| 346 | */ |
||
| 347 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Patch for fix about ambiguous field. |
||
| 362 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 363 | * |
||
| 364 | * @param mixed $query |
||
| 365 | * @param string $column |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | protected function addTablePrefix($query, $column) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Wrap a column and cast based on database driver. |
||
| 390 | * |
||
| 391 | * @param string $column |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function castColumn($column) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Prepare search keyword based on configurations. |
||
| 407 | * |
||
| 408 | * @param string $keyword |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function prepareKeyword($keyword) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Perform column search. |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | public function columnSearch() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get column keyword to use for search. |
||
| 472 | * |
||
| 473 | * @param int $i |
||
| 474 | * @param bool $raw |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Join eager loaded relation and get the related column name. |
||
| 489 | * |
||
| 490 | * @param string $relation |
||
| 491 | * @param string $relationColumn |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Perform join query. |
||
| 548 | * |
||
| 549 | * @param string $table |
||
| 550 | * @param string $foreign |
||
| 551 | * @param string $other |
||
| 552 | */ |
||
| 553 | protected function performJoin($table, $foreign, $other) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Compile queries for column search. |
||
| 567 | * |
||
| 568 | * @param int $i |
||
| 569 | * @param string $column |
||
| 570 | * @param string $keyword |
||
| 571 | */ |
||
| 572 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Compile regex query column search. |
||
| 584 | * |
||
| 585 | * @param mixed $column |
||
| 586 | * @param string $keyword |
||
| 587 | */ |
||
| 588 | protected function regexColumnSearch($column, $keyword) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Perform sorting of columns. |
||
| 604 | * |
||
| 605 | * @return void |
||
| 606 | */ |
||
| 607 | public function ordering() |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Check if column has custom sort handler. |
||
| 671 | * |
||
| 672 | * @param string $column |
||
| 673 | * @return bool |
||
| 674 | */ |
||
| 675 | protected function hasCustomOrder($column) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get NULLS LAST SQL. |
||
| 682 | * |
||
| 683 | * @param string $column |
||
| 684 | * @param string $direction |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | protected function getNullsLastSql($column, $direction) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Perform pagination. |
||
| 696 | * |
||
| 697 | * @return void |
||
| 698 | */ |
||
| 699 | public function paging() |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Get paginated results. |
||
| 707 | * |
||
| 708 | * @return \Illuminate\Support\Collection |
||
| 709 | */ |
||
| 710 | public function results() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Add column in collection. |
||
| 717 | * |
||
| 718 | * @param string $name |
||
| 719 | * @param string|callable $content |
||
| 720 | * @param bool|int $order |
||
| 721 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 722 | */ |
||
| 723 | public function addColumn($name, $content, $order = false) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Get query builder instance. |
||
| 732 | * |
||
| 733 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 734 | */ |
||
| 735 | public function getQuery() |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Append debug parameters on output. |
||
| 742 | * |
||
| 743 | * @param array $output |
||
| 744 | * @return array |
||
| 745 | */ |
||
| 746 | protected function showDebugger(array $output) |
||
| 753 | } |
||
| 754 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: