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 | protected $optionsLabelAttribute; |
||
11 | |||
12 | protected $optionsValueAttribute; |
||
13 | |||
14 | /** |
||
15 | * 获取 options 标题字段 |
||
16 | * |
||
17 | * @return string |
||
18 | */ |
||
19 | public function getOptionsLabelAttribute() |
||
23 | |||
24 | /** |
||
25 | * 设置 options 标题字段 |
||
26 | * |
||
27 | * @param string $value |
||
28 | * |
||
29 | * @return $this |
||
30 | */ |
||
31 | public function setOptionsLabelAttribute($value) |
||
37 | |||
38 | /** |
||
39 | * 获取 options value 字段 |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | public function getOptionsValueAttribute() |
||
47 | |||
48 | /** |
||
49 | * 设置 options value 字段 |
||
50 | * |
||
51 | * @param string $value |
||
52 | * |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function setOptionsValueAttribute($value) |
||
61 | |||
62 | /** |
||
63 | * @return Model |
||
64 | */ |
||
65 | View Code Duplication | public function getOptionsModel() |
|
84 | |||
85 | /** |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | protected function setOptionsFromModel() |
||
110 | } |
||
111 |
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.