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 Builder 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 Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Builder extends HookableBuilder |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Parser factory instance. |
||
| 21 | * |
||
| 22 | * @var \Sofa\Eloquence\Contracts\Searchable\ParserFactory |
||
| 23 | */ |
||
| 24 | protected static $parser; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Joiner factory instance. |
||
| 28 | * |
||
| 29 | * @var \Sofa\Eloquence\Contracts\Relations\JoinerFactory |
||
| 30 | */ |
||
| 31 | protected static $joinerFactory; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Relations joiner instance. |
||
| 35 | * |
||
| 36 | * @var \Sofa\Eloquence\Contracts\Relations\Joiner |
||
| 37 | */ |
||
| 38 | protected $joiner; |
||
| 39 | |||
| 40 | /* |
||
| 41 | |-------------------------------------------------------------------------- |
||
| 42 | | Additional features |
||
| 43 | |-------------------------------------------------------------------------- |
||
| 44 | */ |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Execute the query as a "select" statement. |
||
| 48 | * |
||
| 49 | * @param array $columns |
||
| 50 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 51 | */ |
||
| 52 | public function get($columns = ['*']) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Search through any columns on current table or any defined relations |
||
| 63 | * and return results ordered by search relevance. |
||
| 64 | * |
||
| 65 | * @param array|string $query |
||
| 66 | * @param array $columns |
||
| 67 | * @param boolean $fulltext |
||
| 68 | * @param float $threshold |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | public function search($query, $columns = null, $fulltext = true, $threshold = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Build the search subquery. |
||
| 92 | * |
||
| 93 | * @param array $words |
||
| 94 | * @param array $mappings |
||
| 95 | * @param float $threshold |
||
| 96 | * @return \Sofa\Eloquence\Searchable\Subquery |
||
| 97 | */ |
||
| 98 | protected function buildSubquery(array $words, array $mappings, $threshold) |
||
| 99 | { |
||
| 100 | $subquery = new SearchableSubquery($this->query->newQuery(), $this->model->getTable()); |
||
| 101 | |||
| 102 | $columns = $this->joinForSearch($mappings, $subquery); |
||
| 103 | |||
| 104 | $threshold = (is_null($threshold)) |
||
| 105 | ? array_sum($columns->getWeights()) / 4 |
||
| 106 | : (float) $threshold; |
||
| 107 | |||
| 108 | $modelTableName = $this->model->getTable(); |
||
| 109 | |||
| 110 | // If we are dealing with a SQL Server database, we need to group by all column names |
||
| 111 | if ($this->model->getConnection()->getDriverName() == 'sqlsrv') { |
||
| 112 | $groupByColumns = $this->model->getConnection()->getSchemaBuilder()->getColumnListing($modelTableName); |
||
| 113 | // Force column names to be fully-qualified |
||
| 114 | foreach ($groupByColumns as &$column) { |
||
| 115 | $column = $modelTableName . '.' . $column; |
||
| 116 | } |
||
| 117 | } else { |
||
| 118 | $groupByColumns = $this->model->getQualifiedKeyName(); |
||
| 119 | } |
||
| 120 | |||
| 121 | $subquery->select($this->model->getTable() . '.*') |
||
| 122 | ->from($this->model->getTable()) |
||
| 123 | ->groupBy($groupByColumns); |
||
| 124 | |||
| 125 | $this->addSearchClauses($subquery, $columns, $words, $threshold); |
||
| 126 | |||
| 127 | return $subquery; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add select and where clauses on the subquery. |
||
| 132 | * |
||
| 133 | * @param \Sofa\Eloquence\Searchable\Subquery $subquery |
||
| 134 | * @param \Sofa\Eloquence\Searchable\ColumnCollection $columns |
||
| 135 | * @param array $words |
||
| 136 | * @param float $threshold |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | protected function addSearchClauses( |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Apply relevance select on the subquery. |
||
| 169 | * |
||
| 170 | * @param \Sofa\Eloquence\Searchable\Subquery $subquery |
||
| 171 | * @param \Sofa\Eloquence\Searchable\ColumnCollection $columns |
||
| 172 | * @param array $words |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | protected function searchSelect(SearchableSubquery $subquery, ColumnCollection $columns, array $words) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Apply where clauses on the subquery. |
||
| 196 | * |
||
| 197 | * @param \Sofa\Eloquence\Searchable\Subquery $subquery |
||
| 198 | * @param \Sofa\Eloquence\Searchable\ColumnCollection $columns |
||
| 199 | * @param array $words |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | protected function searchWhere( |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Move where clauses to subquery to improve performance. |
||
| 228 | * |
||
| 229 | * @param \Sofa\Eloquence\Searchable\Subquery $subquery |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | protected function wheresToSubquery(SearchableSubquery $subquery) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get number of bindings provided for a where clause. |
||
| 275 | * |
||
| 276 | * @param array $where |
||
| 277 | * @param string $type |
||
| 278 | * @return integer |
||
| 279 | */ |
||
| 280 | protected function countBindings(array $where, $type) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Determine whether where clause is eloquent has subquery. |
||
| 303 | * |
||
| 304 | * @param array $where |
||
| 305 | * @param string $type |
||
| 306 | * @return boolean |
||
| 307 | */ |
||
| 308 | protected function isHasWhere($where, $type) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Build case clause from all words for a single column. |
||
| 317 | * |
||
| 318 | * @param \Sofa\Eloquence\Searchable\Column $column |
||
| 319 | * @param array $words |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | protected function buildCase(Column $column, array $words) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Replace '?' with single character SQL wildcards. |
||
| 372 | * |
||
| 373 | * @param string $word |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | protected function caseBinding($word) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Build basic search case for 'equals' comparison. |
||
| 385 | * |
||
| 386 | * @param \Sofa\Eloquence\Searchable\Column $column |
||
| 387 | * @param array $words |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | protected function buildEqualsCase(Column $column, array $words) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Determine whether word ends with wildcard. |
||
| 401 | * |
||
| 402 | * @param string $word |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | protected function isLeftMatching($word) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Determine whether word starts and ends with wildcards. |
||
| 412 | * |
||
| 413 | * @param string $word |
||
| 414 | * @return boolean |
||
| 415 | */ |
||
| 416 | protected function isWildcard($word) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get driver-specific case insensitive like operator. |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getLikeOperator() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Join related tables on the search subquery. |
||
| 439 | * |
||
| 440 | * @param array $mappings |
||
| 441 | * @param \Sofa\Eloquence\Searchable\Subquery $subquery |
||
| 442 | * @return \Sofa\Eloquence\Searchable\ColumnCollection |
||
| 443 | */ |
||
| 444 | protected function joinForSearch($mappings, $subquery) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Prefix selected columns with table name in order to avoid collisions. |
||
| 478 | * |
||
| 479 | * @return $this |
||
| 480 | */ |
||
| 481 | public function prefixColumnsForJoin() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Join related tables. |
||
| 500 | * |
||
| 501 | * @param array|string $relations |
||
| 502 | * @param string $type |
||
| 503 | * @return $this |
||
| 504 | */ |
||
| 505 | public function joinRelations($relations, $type = 'inner') |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Left join related tables. |
||
| 524 | * |
||
| 525 | * @param array|string $relations |
||
| 526 | * @return $this |
||
| 527 | */ |
||
| 528 | public function leftJoinRelations($relations) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Right join related tables. |
||
| 537 | * |
||
| 538 | * @param array|string $relations |
||
| 539 | * @return $this |
||
| 540 | */ |
||
| 541 | public function rightJoinRelations($relations) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Set search query parser factory instance. |
||
| 550 | * |
||
| 551 | * @param \Sofa\Eloquence\Contracts\Searchable\ParserFactory $factory |
||
| 552 | */ |
||
| 553 | public static function setParserFactory(ParserFactory $factory) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Set the relations joiner factory instance. |
||
| 560 | * |
||
| 561 | * @param \Sofa\Eloquence\Contracts\Relations\JoinerFactory $factory |
||
| 562 | */ |
||
| 563 | public static function setJoinerFactory(JoinerFactory $factory) |
||
| 567 | } |
||
| 568 |