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 TNTSearchEngine 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 TNTSearchEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class TNTSearchEngine extends Engine |
||
18 | { |
||
19 | |||
20 | private $filters; |
||
21 | /** |
||
22 | * @var TNTSearch |
||
23 | */ |
||
24 | protected $tnt; |
||
25 | |||
26 | /** |
||
27 | * @var Builder |
||
28 | */ |
||
29 | protected $builder; |
||
30 | |||
31 | /** |
||
32 | * Create a new engine instance. |
||
33 | * |
||
34 | * @param TNTSearch $tnt |
||
35 | */ |
||
36 | public function __construct(TNTSearch $tnt) |
||
40 | |||
41 | public function getTNT() |
||
45 | |||
46 | /** |
||
47 | * Update the given model in the index. |
||
48 | * |
||
49 | * @param Collection $models |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function update($models) |
||
76 | |||
77 | /** |
||
78 | * Remove the given model from the index. |
||
79 | * |
||
80 | * @param Collection $models |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | public function delete($models) |
||
94 | |||
95 | /** |
||
96 | * Perform the given search on the engine. |
||
97 | * |
||
98 | * @param Builder $builder |
||
99 | * |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function search(Builder $builder) |
||
110 | |||
111 | /** |
||
112 | * Perform the given search on the engine. |
||
113 | * |
||
114 | * @param Builder $builder |
||
115 | * @param int $perPage |
||
116 | * @param int $page |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function paginate(Builder $builder, $perPage, $page) |
||
146 | |||
147 | /** |
||
148 | * Perform the given search on the engine. |
||
149 | * |
||
150 | * @param Builder $builder |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | protected function performSearch(Builder $builder, array $options = []) |
||
188 | |||
189 | /** |
||
190 | * Map the given results to instances of the given model. |
||
191 | * |
||
192 | * @param mixed $results |
||
193 | * @param \Illuminate\Database\Eloquent\Model $model |
||
194 | * |
||
195 | * @return Collection |
||
196 | */ |
||
197 | View Code Duplication | public function map(Builder $builder, $results, $model) |
|
227 | |||
228 | /** |
||
229 | * Map the given results to instances of the given model via a lazy collection. |
||
230 | * |
||
231 | * @param mixed $results |
||
232 | * @param \Illuminate\Database\Eloquent\Model $model |
||
233 | * |
||
234 | * @return LazyCollection |
||
235 | */ |
||
236 | View Code Duplication | public function lazyMap(Builder $builder, $results, $model) |
|
266 | |||
267 | /** |
||
268 | * Return query builder either from given constraints, or as |
||
269 | * new query. Add where statements to builder when given. |
||
270 | * |
||
271 | * @param \Illuminate\Database\Eloquent\Model $model |
||
272 | * |
||
273 | * @return Builder |
||
274 | */ |
||
275 | public function getBuilder($model) |
||
288 | |||
289 | /** |
||
290 | * Pluck and return the primary keys of the given results. |
||
291 | * |
||
292 | * @param mixed $results |
||
293 | * @return \Illuminate\Support\Collection |
||
294 | */ |
||
295 | public function mapIds($results) |
||
303 | |||
304 | /** |
||
305 | * Get the total count from a raw result returned by the engine. |
||
306 | * |
||
307 | * @param mixed $results |
||
308 | * |
||
309 | * @return int |
||
310 | */ |
||
311 | public function getTotalCount($results) |
||
315 | |||
316 | public function initIndex($model) |
||
326 | |||
327 | /** |
||
328 | * The search index results ($results['ids']) need to be compared against our query |
||
329 | * that contains the constraints. |
||
330 | * |
||
331 | * To get the correct results and counts for the pagination, we remove those ids |
||
332 | * from the search index results that were found by the search but are not part of |
||
333 | * the query ($sub) that is constrained. |
||
334 | * |
||
335 | * This is achieved with self joining the constrained query as subquery and selecting |
||
336 | * the ids which are not matching to anything (i.e., is null). |
||
337 | * |
||
338 | * The constraints usually remove only a small amount of results, which is why the non |
||
339 | * matching results are looked up and removed, instead of returning a collection with |
||
340 | * all the valid results. |
||
341 | */ |
||
342 | private function discardIdsFromResultSetByConstraints($builder, $searchResults) |
||
362 | |||
363 | /** |
||
364 | * Determine if the given model uses soft deletes. |
||
365 | * |
||
366 | * @param \Illuminate\Database\Eloquent\Model $model |
||
367 | * @return bool |
||
368 | */ |
||
369 | protected function usesSoftDelete($model) |
||
373 | |||
374 | /** |
||
375 | * Determine if soft delete is active and depending on state return the |
||
376 | * appropriate builder. |
||
377 | * |
||
378 | * @param Builder $builder |
||
379 | * @param \Illuminate\Database\Eloquent\Model $model |
||
380 | * @return Builder |
||
381 | */ |
||
382 | private function handleSoftDeletes($builder, $model) |
||
414 | |||
415 | /** |
||
416 | * Apply where statements as constraints to the query builder. |
||
417 | * |
||
418 | * @param Builder $builder |
||
419 | * @return \Illuminate\Support\Collection |
||
420 | */ |
||
421 | private function applyWheres($builder) |
||
433 | |||
434 | /** |
||
435 | * Apply order by statements as constraints to the query builder. |
||
436 | * |
||
437 | * @param Builder $builder |
||
438 | * @return \Illuminate\Support\Collection |
||
439 | */ |
||
440 | private function applyOrders($builder) |
||
452 | |||
453 | /** |
||
454 | * Flush all of the model's records from the engine. |
||
455 | * |
||
456 | * @param \Illuminate\Database\Eloquent\Model $model |
||
457 | * @return void |
||
458 | */ |
||
459 | public function flush($model) |
||
467 | |||
468 | |||
469 | /** |
||
470 | * Create a search index. |
||
471 | * |
||
472 | * @param string $name |
||
473 | * @param array $options |
||
474 | * @return mixed |
||
475 | * |
||
476 | * @throws \Exception |
||
477 | */ |
||
478 | public function createIndex($name, array $options = []) |
||
482 | |||
483 | /** |
||
484 | * Delete a search index. |
||
485 | * |
||
486 | * @param string $name |
||
487 | * @return mixed |
||
488 | */ |
||
489 | public function deleteIndex($name) |
||
493 | |||
494 | /** |
||
495 | * Adds a filter |
||
496 | * |
||
497 | * @param string |
||
498 | * @param callback |
||
499 | * @return void |
||
500 | */ |
||
501 | public function addFilter($name, $callback) |
||
508 | |||
509 | /** |
||
510 | * Returns an array of filters |
||
511 | * |
||
512 | * @param string |
||
513 | * @return array |
||
514 | */ |
||
515 | public function getFilters($name) |
||
519 | |||
520 | /** |
||
521 | * Returns a string on which a filter is applied |
||
522 | * |
||
523 | * @param string |
||
524 | * @param string |
||
525 | * @return string |
||
526 | */ |
||
527 | public function applyFilters($name, $result, $model) |
||
540 | } |
||
541 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.