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 |
||
16 | class Model extends Eloquent |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * The name of the "updated by" column. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | const CREATED_BY = 'created_by'; |
||
25 | |||
26 | /** |
||
27 | * The name of the "updated by" column. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | const UPDATED_BY = 'updated_by'; |
||
32 | |||
33 | protected $searchableFields = []; |
||
34 | |||
35 | protected $sortableFields = []; |
||
36 | |||
37 | protected $filterableFields = []; |
||
38 | |||
39 | /** |
||
40 | * Get Searchable Fields. |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | public function getSearchableFields() |
||
48 | |||
49 | /** |
||
50 | * Get Sortable Fields. |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | public function getSortableFields() |
||
58 | |||
59 | /** |
||
60 | * Get Sortable Fields. |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | public function getFilterableFields() |
||
68 | |||
69 | /** |
||
70 | * Update the creation and update timestamps. |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | protected function updateTimestamps() |
||
88 | |||
89 | /** |
||
90 | * Set the value of the "updated by" attribute. |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | View Code Duplication | public function setUpdatedBy() |
|
103 | |||
104 | /** |
||
105 | * Set the value of the "created by" attribute. |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | View Code Duplication | public function setCreatedBy() |
|
118 | |||
119 | } |
||
120 |
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.