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 |
||
34 | class Content extends ActiveModel |
||
35 | { |
||
36 | use SoftDeletes, SearchableTrait; |
||
37 | |||
38 | /** @var string $title */ |
||
39 | |||
40 | protected $searchable = [ |
||
41 | 'columns' => [ |
||
42 | 'title' => 3, |
||
43 | 'text' => 1 |
||
44 | ] |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Get category relation of this content id |
||
49 | * @return \Apps\ActiveRecord\ContentCategory|null |
||
50 | */ |
||
51 | public function getCategory() |
||
55 | |||
56 | /** |
||
57 | * Get content_rating relation one-to-many |
||
58 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
59 | */ |
||
60 | public function getRating() |
||
64 | |||
65 | /** |
||
66 | * Get item path URI - category/item |
||
67 | * @return null|string |
||
68 | */ |
||
69 | public function getPath() |
||
85 | |||
86 | /** |
||
87 | * Get poster URI like /upload/gallery/1/orig/9ds2jd1.png |
||
88 | * @return null|string |
||
89 | */ |
||
90 | View Code Duplication | public function getPosterUri() |
|
106 | |||
107 | /** |
||
108 | * Get poster thumbnail uri |
||
109 | * @return null|string |
||
110 | */ |
||
111 | View Code Duplication | public function getPosterThumbUri() |
|
128 | } |
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.