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 |
||
22 | View Code Duplication | class SmallWorldForm |
|
|
|||
23 | { |
||
24 | /** |
||
25 | * @create a dropdown select |
||
26 | * @param string $name |
||
27 | * @param array $options |
||
28 | * @param string $selected (optional) |
||
29 | * @return string |
||
30 | */ |
||
31 | public function dropdown($name, array $options, $selected = null) |
||
42 | |||
43 | /** |
||
44 | * @create a radio select |
||
45 | * @param string $name |
||
46 | * @param array $options |
||
47 | * @param string $selected (optional) |
||
48 | * @return string |
||
49 | */ |
||
50 | public function radio($name, array $options, $selected = null) |
||
60 | |||
61 | /** |
||
62 | * @param string $name |
||
63 | * @param array $options |
||
64 | * @param array $valuearray |
||
65 | * @param string $selected (optional) |
||
66 | * @return string |
||
67 | */ |
||
68 | public function RetrieveRadio($name, $options, $valuearray, $selected = null) |
||
81 | |||
82 | /** |
||
83 | * @param string $name |
||
84 | * @param string $id |
||
85 | * @param string $class |
||
86 | * @param int $size (optional) |
||
87 | * @param string $preset (optional) |
||
88 | * @return string |
||
89 | */ |
||
90 | public function input($name, $id, $class, $size = null, $preset = null) |
||
96 | |||
97 | /** |
||
98 | * @param string $name |
||
99 | * @param string $id |
||
100 | * @param string $preset (optional) |
||
101 | * @return string |
||
102 | */ |
||
103 | public function hidden($name, $id, $preset = null) |
||
108 | |||
109 | /** |
||
110 | * @param string $value |
||
111 | * @return string |
||
112 | */ |
||
113 | public function simpleText($value) |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * @param string $class |
||
122 | * @param string $name |
||
123 | * @param $name2 |
||
124 | * @param string $rel |
||
125 | * @param array $options |
||
126 | * @param string $textmore |
||
127 | * @param string $selected (optional) |
||
128 | * @param string $preset (optional) |
||
129 | * @return as|string |
||
130 | */ |
||
131 | public function dropdown_add($class, $name, $name2, $rel, array $options, $textmore, $selected = null, $preset = null) |
||
142 | |||
143 | /** |
||
144 | * |
||
145 | * @param string $class |
||
146 | * @param string $name |
||
147 | * @param $name2 |
||
148 | * @param string $rel |
||
149 | * @param int $size |
||
150 | * @param string $textmore |
||
151 | * @param string $preset (optional) |
||
152 | * @param string $id (optional) |
||
153 | * @return dynamic|string |
||
154 | */ |
||
155 | public function input_add($class, $name, $name2, $rel, $size, $textmore, $preset = null, $id = null) |
||
162 | |||
163 | /** |
||
164 | * @return string|textarea |
||
165 | * @param string $name |
||
166 | * @param string $id |
||
167 | * @param string $title |
||
168 | * @param string $rows |
||
169 | * @param string $cols |
||
170 | * @param string $class |
||
171 | * @param string $preset (optional) |
||
172 | * @return string |
||
173 | */ |
||
174 | public function textarea($name, $id, $title, $rows, $cols, $class, $preset = null) |
||
178 | |||
179 | /** |
||
180 | * @return dynamic|string |
||
181 | * @param string $class |
||
182 | * @param string $name |
||
183 | * @param string $name2 |
||
184 | * @param string $rel |
||
185 | * @param array $options |
||
186 | * @param string $textmore |
||
187 | * @param string $selected (optional) |
||
188 | * @param string $preset (optional) |
||
189 | * @param string $selectedstart (optional) |
||
190 | * @param string $selectedstop (optional) |
||
191 | * @return string |
||
192 | */ |
||
193 | public function school_add( |
||
230 | |||
231 | /** |
||
232 | * |
||
233 | * @param string $class |
||
234 | * @param string $name |
||
235 | * @param $name2 |
||
236 | * @param string $rel |
||
237 | * @param string $textmore |
||
238 | * @param string $employer (optional) |
||
239 | * @param string $position (optional) |
||
240 | * @param string $selectedstart (optional) |
||
241 | * @param string $selectedstop (optional) |
||
242 | * @param string $description (optional) |
||
243 | * @return dynamic|string |
||
244 | */ |
||
245 | public function job( |
||
265 | |||
266 | /** |
||
267 | * @return file|string |
||
268 | * @param int $userID |
||
269 | * @return string |
||
270 | */ |
||
271 | public function uploadform($userID) |
||
280 | |||
281 | /** |
||
282 | * @return dynamic|string |
||
283 | * @param int $userID |
||
284 | * @param string $imgurl |
||
285 | * @param string $imgdesc |
||
286 | * @param string $id |
||
287 | * @return string |
||
288 | */ |
||
289 | public function edit_images($userID, $imgurl, $imgdesc, $id) |
||
301 | |||
302 | /** |
||
303 | * @create a radio select |
||
304 | * @param $userid |
||
305 | * @return string |
||
306 | */ |
||
307 | public function usersettings($userid) |
||
339 | } |
||
340 |
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.