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 |
||
| 20 | class Builder extends HookableBuilder |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Parser factory instance. |
||
| 24 | * |
||
| 25 | * @var ParserFactory |
||
| 26 | */ |
||
| 27 | protected static $parser; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Joiner factory instance. |
||
| 31 | * |
||
| 32 | * @var JoinerFactory |
||
| 33 | */ |
||
| 34 | protected static $joinerFactory; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Relations joiner instance. |
||
| 38 | * |
||
| 39 | * @var Joiner |
||
| 40 | */ |
||
| 41 | protected $joiner; |
||
| 42 | |||
| 43 | /* |
||
| 44 | |-------------------------------------------------------------------------- |
||
| 45 | | Additional features |
||
| 46 | |-------------------------------------------------------------------------- |
||
| 47 | */ |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Execute the query as a "select" statement. |
||
| 51 | * |
||
| 52 | * @param array $columns |
||
| 53 | * @return Collection |
||
| 54 | */ |
||
| 55 | public function get($columns = ['*']) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Search through any columns on current table or any defined relations |
||
| 66 | * and return results ordered by search relevance. |
||
| 67 | * |
||
| 68 | * @param array|string $query |
||
| 69 | * @param array $columns |
||
| 70 | * @param bool $fulltext |
||
| 71 | * @param float $threshold |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function search($query, $columns = null, $fulltext = true, $threshold = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Build the search subquery. |
||
| 95 | * |
||
| 96 | * @param array $words |
||
| 97 | * @param array $mappings |
||
| 98 | * @param float $threshold |
||
| 99 | * @return SearchableSubquery |
||
| 100 | */ |
||
| 101 | protected function buildSubquery(array $words, array $mappings, $threshold) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Add select and where clauses on the subquery. |
||
| 122 | * |
||
| 123 | * @param SearchableSubquery $subquery |
||
| 124 | * @param ColumnCollection $columns |
||
| 125 | * @param array $words |
||
| 126 | * @param float $threshold |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | protected function addSearchClauses( |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Apply relevance select on the subquery. |
||
| 161 | * |
||
| 162 | * @param SearchableSubquery $subquery |
||
| 163 | * @param ColumnCollection $columns |
||
| 164 | * @param array $words |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function searchSelect(SearchableSubquery $subquery, ColumnCollection $columns, array $words) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Apply where clauses on the subquery. |
||
| 188 | * |
||
| 189 | * @param SearchableSubquery $subquery |
||
| 190 | * @param ColumnCollection $columns |
||
| 191 | * @param array $words |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | protected function searchWhere( |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Move where clauses to subquery to improve performance. |
||
| 220 | * |
||
| 221 | * @param SearchableSubquery $subquery |
||
| 222 | * @return void |
||
| 223 | */ |
||
| 224 | protected function wheresToSubquery(SearchableSubquery $subquery) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get number of bindings provided for a where clause. |
||
| 267 | * |
||
| 268 | * @param array $where |
||
| 269 | * @param string $type |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | protected function countBindings(array $where, $type) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Determine whether where clause is eloquent has subquery. |
||
| 295 | * |
||
| 296 | * @param array $where |
||
| 297 | * @param string $type |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | protected function isHasWhere($where, $type) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Build case clause from all words for a single column. |
||
| 309 | * |
||
| 310 | * @param Column $column |
||
| 311 | * @param array $words |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | protected function buildCase(Column $column, array $words) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Replace '?' with single character SQL wildcards. |
||
| 364 | * |
||
| 365 | * @param string $word |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | protected function caseBinding($word) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Build basic search case for 'equals' comparison. |
||
| 377 | * |
||
| 378 | * @param Column $column |
||
| 379 | * @param array $words |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | protected function buildEqualsCase(Column $column, array $words) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Determine whether word ends with wildcard. |
||
| 393 | * |
||
| 394 | * @param string $word |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | protected function isLeftMatching($word) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Determine whether word starts and ends with wildcards. |
||
| 404 | * |
||
| 405 | * @param string $word |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | protected function isWildcard($word) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get driver-specific case insensitive like operator. |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getLikeOperator() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Join related tables on the search subquery. |
||
| 431 | * |
||
| 432 | * @param array $mappings |
||
| 433 | * @param SearchableSubquery $subquery |
||
| 434 | * @return ColumnCollection |
||
| 435 | */ |
||
| 436 | protected function joinForSearch($mappings, $subquery) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Prefix selected columns with table name in order to avoid collisions. |
||
| 470 | * |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | public function prefixColumnsForJoin() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Join related tables. |
||
| 492 | * |
||
| 493 | * @param array|string $relations |
||
| 494 | * @param string $type |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | public function joinRelations($relations, $type = 'inner') |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Left join related tables. |
||
| 516 | * |
||
| 517 | * @param array|string $relations |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | public function leftJoinRelations($relations) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Right join related tables. |
||
| 529 | * |
||
| 530 | * @param array|string $relations |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | public function rightJoinRelations($relations) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Set search query parser factory instance. |
||
| 542 | * |
||
| 543 | * @param ParserFactory $factory |
||
| 544 | */ |
||
| 545 | public static function setParserFactory(ParserFactory $factory) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Set the relations joiner factory instance. |
||
| 552 | * |
||
| 553 | * @param JoinerFactory $factory |
||
| 554 | */ |
||
| 555 | public static function setJoinerFactory(JoinerFactory $factory) |
||
| 559 | } |
||
| 560 |