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 |
||
10 | class Select extends NamedElement |
||
11 | { |
||
12 | protected $type = 'select'; |
||
13 | |||
14 | protected $size = ''; |
||
15 | |||
16 | protected $options; |
||
17 | |||
18 | protected $extraOptions; |
||
19 | |||
20 | protected $optionsLabelAttribute; |
||
21 | |||
22 | protected $optionsValueAttribute; |
||
23 | |||
24 | public function __construct($name, $title, $options = null) |
||
25 | { |
||
26 | parent::__construct($name, $title); |
||
27 | |||
28 | $this->setOptions($options); |
||
29 | |||
30 | $this->extraOptions = new Collection(); |
||
31 | } |
||
32 | |||
33 | public function getSize() |
||
37 | |||
38 | public function setSize($value) |
||
44 | |||
45 | public function getValue() |
||
49 | |||
50 | public function save() |
||
56 | |||
57 | protected function isOptionsModel() |
||
61 | |||
62 | protected function isRelation() |
||
66 | |||
67 | public function addOptions($options) |
||
75 | |||
76 | public function addOption($key, $value) |
||
82 | |||
83 | public function getOptions() |
||
110 | |||
111 | /** |
||
112 | * @param mixed $options |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function setOptions($options) |
||
122 | |||
123 | /** |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | protected function setOptionsFromModel() |
||
149 | |||
150 | /** |
||
151 | * @return Model |
||
152 | */ |
||
153 | View Code Duplication | public function getOptionsModel() |
|
173 | |||
174 | /** |
||
175 | * 获取 options 标题字段 |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getOptionsLabelAttribute() |
||
183 | |||
184 | /** |
||
185 | * 设置 options 标题字段 |
||
186 | * |
||
187 | * @param string $value |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function setOptionsLabelAttribute($value) |
||
197 | |||
198 | /** |
||
199 | * 获取 options value 字段 |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getOptionsValueAttribute() |
||
207 | |||
208 | /** |
||
209 | * 设置 options value 字段 |
||
210 | * |
||
211 | * @param string $value |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setOptionsValueAttribute($value) |
||
221 | |||
222 | public function toArray() |
||
229 | } |
||
230 |
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.