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 |
||
| 33 | class Form |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Create a dropdown select |
||
| 37 | * |
||
| 38 | * @param string $name |
||
| 39 | * @param array $options |
||
| 40 | * @param string $selected (optional) |
||
|
|
|||
| 41 | * @param string $sep (optional) |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public function dropdown($name, array $options, $selected = null, $sep = '<br>') |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create a radio select |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | * @param array $options |
||
| 61 | * @param string $selected (optional) |
||
| 62 | * @param string $sep (optional) |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function radio($name, array $options, $selected = null, $sep = '<br>') |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Create a HTML checkbox element |
||
| 84 | * |
||
| 85 | * @param string $name |
||
| 86 | * @param array $options |
||
| 87 | * @param array $valuearray |
||
| 88 | * @param string $selected (optional) |
||
| 89 | * @param string $sep (optional) |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function retrieveRadio($name, $options, $valuearray, $selected = null, $sep = '<br>') |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Create a HTML text input element |
||
| 111 | * |
||
| 112 | * @param string $name |
||
| 113 | * @param string $id |
||
| 114 | * @param string $class |
||
| 115 | * @param int $size (optional) |
||
| 116 | * @param string $preset (optional) |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function input($name, $id, $class, $size = null, $preset = null) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Create a HTML hidden element |
||
| 129 | * |
||
| 130 | * @param string $name |
||
| 131 | * @param string $id |
||
| 132 | * @param string $preset (optional) |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function hidden($name, $id, $preset = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns simple text string |
||
| 144 | * |
||
| 145 | * @param string $value |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function simpleText($value) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Create a HTML select (dropdown) element |
||
| 157 | * |
||
| 158 | * @param string $class |
||
| 159 | * @param string $name |
||
| 160 | * @param $name2 |
||
| 161 | * @param string $rel |
||
| 162 | * @param array $options |
||
| 163 | * @param string $textmore |
||
| 164 | * @param string $selected (optional) |
||
| 165 | * @param string $preset (optional) |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function dropdown_add($class, $name, $name2, $rel, array $options, $textmore, $selected = null, $preset = null) |
||
| 169 | { |
||
| 170 | $dropdown = '<span id="' . $name . '"><input type="text" name="' . $name2 . '[]" value="' . $preset . '">'; |
||
| 171 | $dropdown .= '<select class="smallworld_select" name="' . $name . '[]" id="' . $name . '">' . '<br>'; |
||
| 172 | View Code Duplication | foreach ($options as $key => $option) { |
|
| 173 | $select = $selected == $key ? ' selected' : null; |
||
| 174 | $dropdown .= '<option value="' . $key . '"' . $select . '>' . $option . '</option>' . '<br>'; |
||
| 175 | } |
||
| 176 | $dropdown .= '</select></span>'; |
||
| 177 | |||
| 178 | return $dropdown; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $class |
||
| 183 | * @param string $name |
||
| 184 | * @param $name2 |
||
| 185 | * @param string $rel |
||
| 186 | * @param int $size |
||
| 187 | * @param string $textmore |
||
| 188 | * @param string $preset (optional) |
||
| 189 | * @param string $id (optional) |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function input_add($class, $name, $name2, $rel, $size, $textmore, $preset = null, $id = null) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $name |
||
| 203 | * @param string $id |
||
| 204 | * @param string $title |
||
| 205 | * @param string $rows |
||
| 206 | * @param string $cols |
||
| 207 | * @param string $class |
||
| 208 | * @param string $preset (optional) |
||
| 209 | * @return string |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function textarea($name, $id, $title, $rows, $cols, $class, $preset = null) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $class |
||
| 219 | * @param string $name |
||
| 220 | * @param string $name2 |
||
| 221 | * @param string $rel |
||
| 222 | * @param array $options |
||
| 223 | * @param string $textmore |
||
| 224 | * @param string $selected (optional) |
||
| 225 | * @param string $preset (optional) |
||
| 226 | * @param string $selectedstart (optional) |
||
| 227 | * @param string $selectedstop (optional) |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function school_add( |
||
| 231 | $class, |
||
| 232 | $name, |
||
| 233 | $name2, |
||
| 234 | $rel, |
||
| 235 | array $options, |
||
| 236 | $textmore, |
||
| 237 | $selected = null, |
||
| 238 | $preset = null, |
||
| 239 | $selectedstart = null, |
||
| 240 | $selectedstop = null |
||
| 241 | ) { |
||
| 242 | $dropdown = '<div id="' . $name . '"><p class="smallworld_clonebreaker">' . _SMALLWORLD_SCHOOLNAME . '<input class="school" type="text" value="' . $preset . '" name="' . $name2 . '[]">' |
||
| 243 | . '<br><br>' . _SMALLWORLD_SCHOOLTYPE . '<select class="school" name="' . $name . '[]" id="' . $name . '"">' . '<br>'; |
||
| 244 | View Code Duplication | foreach ($options as $key => $option) { |
|
| 245 | $select = $selected == $key ? ' selected="selected"' : null; |
||
| 246 | $dropdown .= '<option class="school" value="' . $key . '"' . $select . '>' . $option . '</option>' . '<br>'; |
||
| 247 | } |
||
| 248 | $dropdown .= '</select>' |
||
| 249 | . '<br><br>' |
||
| 250 | . _SMALLWORLD_START . '<select class="schooltime" name="schoolstart[]" id="schoolstart">'; |
||
| 251 | $array = SmallworldGetTimestampsToForm(); |
||
| 252 | View Code Duplication | foreach ($array as $key => $option) { |
|
| 253 | $selectstart = $selectedstart == $key ? ' selected="selected"' : null; |
||
| 254 | $dropdown .= '<option value="' . $key . '"' . $selectstart . '>' . $option . '</option>' . '<br>'; |
||
| 255 | } |
||
| 256 | $dropdown .= '</select>' |
||
| 257 | . '<br><br>' |
||
| 258 | . _SMALLWORLD_STOP . '<select class="schooltime" name="schoolstop[]" id="schoolstop">'; |
||
| 259 | $array = SmallworldGetTimestampsToForm(); |
||
| 260 | View Code Duplication | foreach ($array as $key => $option) { |
|
| 261 | $selectstop = $selectedstop == $key ? ' selected="selected"' : null; |
||
| 262 | $dropdown .= '<option value="' . $key . '"' . $selectstop . '>' . $option . '</option>' . '<br>'; |
||
| 263 | } |
||
| 264 | $dropdown .= '</select><br></p></div>'; |
||
| 265 | |||
| 266 | return $dropdown; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Create HTML form elements for Job fields |
||
| 271 | * |
||
| 272 | * @param string $class - not used |
||
| 273 | * @param string $name |
||
| 274 | * @param string $name2 - not used |
||
| 275 | * @param string $rel - not used |
||
| 276 | * @param string $textmore - not used |
||
| 277 | * @param string $employer (optional) |
||
| 278 | * @param string $position (optional) |
||
| 279 | * @param string $selectedstart (optional) |
||
| 280 | * @param string $selectedstop (optional) |
||
| 281 | * @param string $description (optional) |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function job( |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Create a HTML upload form |
||
| 308 | * |
||
| 309 | * @param int $userID - not used |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function uploadform($userID) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param int $userID - not used |
||
| 325 | * @param string $imgurl |
||
| 326 | * @param string $imgdesc |
||
| 327 | * @param string $id |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function edit_images($userID, $imgurl, $imgdesc, $id) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Create HTML radio selects for user settings |
||
| 345 | * |
||
| 346 | * @param int $userid |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function usersettings($userid) |
||
| 380 | } |
||
| 381 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.