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 |
||
8 | trait SelectOptionsFromModel |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $optionsLabelAttribute; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $optionsValueAttribute; |
||
19 | |||
20 | /** |
||
21 | * Get the options label attribute. |
||
22 | * |
||
23 | * @return string |
||
24 | */ |
||
25 | public function getOptionsLabelAttribute() |
||
29 | |||
30 | /** |
||
31 | * Set the options label attribute. |
||
32 | * |
||
33 | * @param string $value |
||
34 | * |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function setOptionsLabelAttribute($value) |
||
43 | |||
44 | /** |
||
45 | * Get the options value attribute. |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getOptionsValueAttribute() |
||
53 | |||
54 | /** |
||
55 | * Set the options value attribute. |
||
56 | * |
||
57 | * @param string $value |
||
58 | * |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function setOptionsValueAttribute($value) |
||
67 | |||
68 | /** |
||
69 | * Get the options model. |
||
70 | * |
||
71 | * @return Model |
||
72 | */ |
||
73 | View Code Duplication | public function getOptionsModel() |
|
94 | |||
95 | protected function isOptionsModel() |
||
99 | |||
100 | /** |
||
101 | * Get the options from model. |
||
102 | * |
||
103 | * @return \Illuminate\Support\Collection |
||
104 | */ |
||
105 | protected function getOptionsFromModel() |
||
128 | } |
||
129 |
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.