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) |
||
| 49 | { |
||
| 50 | $this->query = $builder; |
||
| 51 | $this->request = $request; |
||
| 52 | $this->columns = $builder->columns; |
||
| 53 | $this->connection = $builder->getConnection(); |
||
| 54 | if ($this->isDebugging()) { |
||
| 55 | $this->connection->enableQueryLog(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set auto filter off and run your own filter. |
||
| 61 | * Overrides global search. |
||
| 62 | * |
||
| 63 | * @param callable $callback |
||
| 64 | * @param bool $globalSearch |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | public function filter(callable $callback, $globalSearch = false) |
||
| 68 | { |
||
| 69 | $this->overrideGlobalSearch($callback, $this->query, $globalSearch); |
||
| 70 | |||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Perform global search. |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function filtering() |
||
| 80 | { |
||
| 81 | $keyword = $this->request->keyword(); |
||
| 82 | |||
| 83 | if ($this->isSmartSearch()) { |
||
| 84 | $this->smartGlobalSearch($keyword); |
||
|
|
|||
| 85 | |||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->globalSearch($keyword); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Perform multi-term search by splitting keyword into |
||
| 94 | * individual words and searches for each of them. |
||
| 95 | * |
||
| 96 | * @param string $keyword |
||
| 97 | */ |
||
| 98 | private function smartGlobalSearch($keyword) |
||
| 99 | { |
||
| 100 | $keywords = array_filter(explode(' ', $keyword)); |
||
| 101 | |||
| 102 | foreach ($keywords as $keyword) { |
||
| 103 | $this->globalSearch($keyword); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Perform global search for the given keyword. |
||
| 109 | * |
||
| 110 | * @param string $keyword |
||
| 111 | */ |
||
| 112 | private function globalSearch($keyword) |
||
| 113 | { |
||
| 114 | $this->query->where(function ($query) use ($keyword) { |
||
| 115 | $query = $this->getBaseQueryBuilder($query); |
||
| 116 | |||
| 117 | foreach ($this->request->searchableColumnIndex() as $index) { |
||
| 118 | $columnName = $this->getColumnName($index); |
||
| 119 | if ($this->isBlacklisted($columnName) && !$this->hasCustomFilter($columnName)) { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($this->hasCustomFilter($columnName)) { |
||
| 124 | $callback = $this->columnDef['filter'][$columnName]['method']; |
||
| 125 | $builder = $query->newQuery(); |
||
| 126 | $callback($builder, $keyword); |
||
| 127 | $query->addNestedWhereQuery($builder, 'or'); |
||
| 128 | } else { |
||
| 129 | View Code Duplication | if (count(explode('.', $columnName)) > 1) { |
|
| 130 | $eagerLoads = $this->getEagerLoads(); |
||
| 131 | $parts = explode('.', $columnName); |
||
| 132 | $relationColumn = array_pop($parts); |
||
| 133 | $relation = implode('.', $parts); |
||
| 134 | if (in_array($relation, $eagerLoads)) { |
||
| 135 | $this->compileRelationSearch( |
||
| 136 | $query, |
||
| 137 | $relation, |
||
| 138 | $relationColumn, |
||
| 139 | $keyword |
||
| 140 | ); |
||
| 141 | } else { |
||
| 142 | $this->compileQuerySearch($query, $columnName, $keyword); |
||
| 143 | } |
||
| 144 | } else { |
||
| 145 | $this->compileQuerySearch($query, $columnName, $keyword); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->isFilterApplied = true; |
||
| 150 | } |
||
| 151 | }); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the base query builder instance. |
||
| 156 | * |
||
| 157 | * @param mixed $instance |
||
| 158 | * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
||
| 159 | */ |
||
| 160 | protected function getBaseQueryBuilder($instance = null) |
||
| 161 | { |
||
| 162 | if (!$instance) { |
||
| 163 | $instance = $this->query; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($instance instanceof EloquentBuilder) { |
||
| 167 | return $instance->getQuery(); |
||
| 168 | } |
||
| 169 | |||
| 170 | return $instance; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Check if column has custom filter handler. |
||
| 175 | * |
||
| 176 | * @param string $columnName |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function hasCustomFilter($columnName) |
||
| 180 | { |
||
| 181 | return isset($this->columnDef['filter'][$columnName]); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get eager loads keys if eloquent. |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | protected function getEagerLoads() |
||
| 190 | { |
||
| 191 | if ($this->query instanceof EloquentBuilder) { |
||
| 192 | return array_keys($this->query->getEagerLoads()); |
||
| 193 | } |
||
| 194 | |||
| 195 | return []; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add relation query on global search. |
||
| 200 | * |
||
| 201 | * @param mixed $query |
||
| 202 | * @param string $relation |
||
| 203 | * @param string $column |
||
| 204 | * @param string $keyword |
||
| 205 | */ |
||
| 206 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 207 | { |
||
| 208 | $myQuery = clone $this->query; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * For compile nested relation, we need store all nested relation as array |
||
| 212 | * and reverse order to apply where query. |
||
| 213 | * With this method we can create nested sub query with properly relation. |
||
| 214 | */ |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Store all relation data that require in next step |
||
| 218 | */ |
||
| 219 | $relationChunk = []; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Store last eloquent query builder for get next relation. |
||
| 223 | */ |
||
| 224 | $lastQuery = $query; |
||
| 225 | |||
| 226 | $relations = explode('.', $relation); |
||
| 227 | $lastRelation = end($relations); |
||
| 228 | foreach ($relations as $relation) { |
||
| 229 | $relationType = $myQuery->getModel()->{$relation}(); |
||
| 230 | $myQuery->orWhereHas($relation, function ($builder) use ( |
||
| 231 | $column, |
||
| 232 | $keyword, |
||
| 233 | $query, |
||
| 234 | $relationType, |
||
| 235 | $relation, |
||
| 236 | $lastRelation, |
||
| 237 | &$relationChunk, |
||
| 238 | &$lastQuery |
||
| 239 | ) { |
||
| 240 | $builder->select($this->connection->raw('count(1)')); |
||
| 241 | |||
| 242 | // We will perform search on last relation only. |
||
| 243 | if ($relation == $lastRelation) { |
||
| 244 | $this->compileQuerySearch($builder, $column, $keyword, ''); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Put require object to next step!! |
||
| 248 | $relationChunk[$relation] = [ |
||
| 249 | 'builder' => $builder, |
||
| 250 | 'relationType' => $relationType, |
||
| 251 | 'query' => $lastQuery, |
||
| 252 | ]; |
||
| 253 | |||
| 254 | // This is trick make sub query. |
||
| 255 | $lastQuery = $builder; |
||
| 256 | }); |
||
| 257 | |||
| 258 | // This is trick to make nested relation by pass previous relation to be next query eloquent builder |
||
| 259 | $myQuery = $relationType; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Reverse them all |
||
| 264 | */ |
||
| 265 | $relationChunk = array_reverse($relationChunk, true); |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Create valuable for use in check last relation |
||
| 269 | */ |
||
| 270 | end($relationChunk); |
||
| 271 | $lastRelation = key($relationChunk); |
||
| 272 | reset($relationChunk); |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Walking ... |
||
| 276 | */ |
||
| 277 | foreach ($relationChunk as $relation => $chunk) { |
||
| 278 | // Prepare variables |
||
| 279 | $builder = $chunk['builder']; |
||
| 280 | $query = $chunk['query']; |
||
| 281 | $bindings = $builder->getBindings(); |
||
| 282 | $builder = "({$builder->toSql()}) >= 1"; |
||
| 283 | |||
| 284 | // Check if it last relation we will use orWhereRaw |
||
| 285 | if ($lastRelation == $relation) { |
||
| 286 | $relationMethod = "orWhereRaw"; |
||
| 287 | } else { |
||
| 288 | // For case parent relation of nested relation. |
||
| 289 | // We must use and for properly query and get correct result |
||
| 290 | $relationMethod = "whereRaw"; |
||
| 291 | } |
||
| 292 | |||
| 293 | $query->{$relationMethod}($builder, $bindings); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Compile query builder where clause depending on configurations. |
||
| 299 | * |
||
| 300 | * @param mixed $query |
||
| 301 | * @param string $column |
||
| 302 | * @param string $keyword |
||
| 303 | * @param string $relation |
||
| 304 | */ |
||
| 305 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 306 | { |
||
| 307 | $column = $this->addTablePrefix($query, $column); |
||
| 308 | $column = $this->castColumn($column); |
||
| 309 | $sql = $column . ' LIKE ?'; |
||
| 310 | |||
| 311 | if ($this->isCaseInsensitive()) { |
||
| 312 | $sql = 'LOWER(' . $column . ') LIKE ?'; |
||
| 313 | } |
||
| 314 | |||
| 315 | $query->{$relation . 'WhereRaw'}($sql, [$this->prepareKeyword($keyword)]); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Patch for fix about ambiguous field. |
||
| 320 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 321 | * |
||
| 322 | * @param mixed $query |
||
| 323 | * @param string $column |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | protected function addTablePrefix($query, $column) |
||
| 327 | { |
||
| 328 | // Check if field does not have a table prefix |
||
| 329 | if (strpos($column, '.') === false) { |
||
| 330 | // Alternative method to check instanceof \Illuminate\Database\Eloquent\Builder |
||
| 331 | if (method_exists($query, 'getQuery')) { |
||
| 332 | $q = $query->getQuery(); |
||
| 333 | } else { |
||
| 334 | $q = $query; |
||
| 335 | } |
||
| 336 | |||
| 337 | if (!$q->from instanceof Expression) { |
||
| 338 | // Get table from query and add it. |
||
| 339 | $column = $q->from . '.' . $column; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | return $this->wrap($column); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Wrap column with DB grammar. |
||
| 348 | * |
||
| 349 | * @param string $column |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function wrap($column) |
||
| 353 | { |
||
| 354 | return $this->connection->getQueryGrammar()->wrap($column); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Wrap a column and cast based on database driver. |
||
| 359 | * |
||
| 360 | * @param string $column |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected function castColumn($column) |
||
| 364 | { |
||
| 365 | $driver = $this->connection->getDriverName(); |
||
| 366 | |||
| 367 | if ($driver === 'pgsql') { |
||
| 368 | $column = 'CAST(' . $column . ' as TEXT)'; |
||
| 369 | } elseif ($driver === 'firebird') { |
||
| 370 | $column = 'CAST(' . $column . ' as VARCHAR(255))'; |
||
| 371 | } |
||
| 372 | |||
| 373 | return $column; |
||
| 374 | } |
||
| 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() |
||
| 441 | { |
||
| 442 | $builder = $this->prepareCountQuery(); |
||
| 443 | |||
| 444 | return $this->connection->table($this->connection->raw('(' . $builder->toSql() . ') count_row_table')) |
||
| 445 | ->setBindings($builder->getBindings())->count(); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Prepare count query builder. |
||
| 450 | * |
||
| 451 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 452 | */ |
||
| 453 | protected function prepareCountQuery() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Check if builder query uses complex sql. |
||
| 467 | * |
||
| 468 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | protected function isComplexQuery($builder) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Perform sorting of columns. |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | */ |
||
| 481 | public function ordering() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Check if column has custom sort handler. |
||
| 545 | * |
||
| 546 | * @param string $column |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | protected function hasCustomOrder($column) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Join eager loaded relation and get the related column name. |
||
| 556 | * |
||
| 557 | * @param string $relation |
||
| 558 | * @param string $relationColumn |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Perform join query. |
||
| 610 | * |
||
| 611 | * @param string $table |
||
| 612 | * @param string $foreign |
||
| 613 | * @param string $other |
||
| 614 | */ |
||
| 615 | protected function performJoin($table, $foreign, $other) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get NULLS LAST SQL. |
||
| 629 | * |
||
| 630 | * @param string $column |
||
| 631 | * @param string $direction |
||
| 632 | * @return string |
||
| 633 | */ |
||
| 634 | protected function getNullsLastSql($column, $direction) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Perform column search. |
||
| 643 | * |
||
| 644 | * @return void |
||
| 645 | */ |
||
| 646 | public function columnSearch() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Get column keyword to use for search. |
||
| 684 | * |
||
| 685 | * @param int $i |
||
| 686 | * @param bool $raw |
||
| 687 | * @return string |
||
| 688 | */ |
||
| 689 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Compile queries for column search. |
||
| 701 | * |
||
| 702 | * @param int $i |
||
| 703 | * @param string $column |
||
| 704 | * @param string $keyword |
||
| 705 | */ |
||
| 706 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Compile regex query column search. |
||
| 718 | * |
||
| 719 | * @param mixed $column |
||
| 720 | * @param string $keyword |
||
| 721 | */ |
||
| 722 | protected function regexColumnSearch($column, $keyword) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Perform pagination. |
||
| 743 | * |
||
| 744 | * @return void |
||
| 745 | */ |
||
| 746 | public function paging() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Get paginated results. |
||
| 754 | * |
||
| 755 | * @return \Illuminate\Support\Collection |
||
| 756 | */ |
||
| 757 | public function results() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Add column in collection. |
||
| 764 | * |
||
| 765 | * @param string $name |
||
| 766 | * @param string|callable $content |
||
| 767 | * @param bool|int $order |
||
| 768 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 769 | */ |
||
| 770 | public function addColumn($name, $content, $order = false) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Get query builder instance. |
||
| 779 | * |
||
| 780 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 781 | */ |
||
| 782 | public function getQuery() |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Append debug parameters on output. |
||
| 789 | * |
||
| 790 | * @param array $output |
||
| 791 | * @return array |
||
| 792 | */ |
||
| 793 | protected function showDebugger(array $output) |
||
| 800 | } |
||
| 801 |
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.