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:
1 | <?php namespace Modules\Page\Repositories\Eloquent; |
||
10 | class EloquentPageRepository extends EloquentBaseRepository implements PageRepository |
||
11 | { |
||
12 | /** |
||
13 | * Find the page set as homepage |
||
14 | * @return object |
||
15 | */ |
||
16 | public function findHomepage() |
||
20 | |||
21 | /** |
||
22 | * Count all records |
||
23 | * @return int |
||
24 | */ |
||
25 | public function countAll() |
||
29 | |||
30 | /** |
||
31 | * @param mixed $data |
||
32 | * @return object |
||
33 | */ |
||
34 | View Code Duplication | public function create($data) |
|
45 | |||
46 | /** |
||
47 | * @param $model |
||
48 | * @param array $data |
||
49 | * @return object |
||
50 | */ |
||
51 | View Code Duplication | public function update($model, $data) |
|
62 | |||
63 | public function destroy($model) |
||
69 | |||
70 | /** |
||
71 | * @param $slug |
||
72 | * @param $locale |
||
73 | * @return object |
||
74 | */ |
||
75 | public function findBySlugInLocale($slug, $locale) |
||
86 | |||
87 | /** |
||
88 | * Set the current page set as homepage to 0 |
||
89 | */ |
||
90 | private function removeOtherHomepage() |
||
99 | } |
||
100 |
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.