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:
Complex classes like ProfileField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProfileField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ProfileField extends XoopsObject |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | public function __construct() |
||
| 32 | { |
||
| 33 | $this->initVar('field_id', XOBJ_DTYPE_INT, null); |
||
| 34 | $this->initVar('cat_id', XOBJ_DTYPE_INT, null, true); |
||
| 35 | $this->initVar('field_type', XOBJ_DTYPE_TXTBOX); |
||
| 36 | $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true); |
||
| 37 | $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true); |
||
| 38 | $this->initVar('field_title', XOBJ_DTYPE_TXTBOX); |
||
| 39 | $this->initVar('field_description', XOBJ_DTYPE_TXTAREA); |
||
| 40 | $this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes |
||
| 41 | $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0); |
||
| 42 | $this->initVar('field_weight', XOBJ_DTYPE_INT, 0); |
||
| 43 | $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, ''); |
||
| 44 | $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1); |
||
| 45 | $this->initVar('field_edit', XOBJ_DTYPE_INT, 0); |
||
| 46 | $this->initVar('field_show', XOBJ_DTYPE_INT, 0); |
||
| 47 | $this->initVar('field_config', XOBJ_DTYPE_INT, 0); |
||
| 48 | $this->initVar('field_options', XOBJ_DTYPE_ARRAY, array()); |
||
| 49 | $this->initVar('step_id', XOBJ_DTYPE_INT, 0); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Extra treatment dealing with non latin encoding |
||
| 54 | * Tricky solution |
||
| 55 | * @param string $key |
||
| 56 | * @param mixed $value |
||
| 57 | * @param bool $not_gpc |
||
| 58 | */ |
||
| 59 | public function setVar($key, $value, $not_gpc = false) |
||
| 60 | { |
||
| 61 | View Code Duplication | if ($key === 'field_options' && is_array($value)) { |
|
| 62 | foreach (array_keys($value) as $idx) { |
||
| 63 | $value[$idx] = base64_encode($value[$idx]); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | parent::setVar($key, $value, $not_gpc); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $key |
||
| 71 | * @param string $format |
||
| 72 | * |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function getVar($key, $format = 's') |
||
| 76 | { |
||
| 77 | $value = parent::getVar($key, $format); |
||
| 78 | View Code Duplication | if ($key === 'field_options' && !empty($value)) { |
|
| 79 | foreach (array_keys($value) as $idx) { |
||
| 80 | $value[$idx] = base64_decode($value[$idx]); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | return $value; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
| 89 | * |
||
| 90 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
| 91 | * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of |
||
| 92 | * |
||
| 93 | * @return XoopsFormElement |
||
|
|
|||
| 94 | **/ |
||
| 95 | public function getEditElement($user, $profile) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns a value for output of this field |
||
| 236 | * |
||
| 237 | * @param XoopsUser $user {@link XoopsUser} object to get the value of |
||
| 238 | * @param profileProfile $profile object to get the value of |
||
| 239 | * |
||
| 240 | * @return mixed |
||
| 241 | **/ |
||
| 242 | public function getOutputValue(&$user, $profile) |
||
| 243 | { |
||
| 244 | View Code Duplication | if (file_exists($file = $GLOBALS['xoops']->path('modules/profile/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) { |
|
| 245 | include_once $file; |
||
| 246 | } else { |
||
| 247 | include_once $GLOBALS['xoops']->path('modules/profile/language/english/modinfo.php'); |
||
| 248 | } |
||
| 249 | |||
| 250 | $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name')); |
||
| 251 | |||
| 252 | switch ($this->getVar('field_type')) { |
||
| 253 | default: |
||
| 254 | case 'textbox': |
||
| 255 | if ($this->getVar('field_name') === 'url' && $value !== '') { |
||
| 256 | return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>'; |
||
| 257 | } else { |
||
| 258 | return $value; |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | case 'textarea': |
||
| 262 | case 'dhtml': |
||
| 263 | case 'theme': |
||
| 264 | case 'language': |
||
| 265 | case 'list': |
||
| 266 | return $value; |
||
| 267 | break; |
||
| 268 | |||
| 269 | case 'select': |
||
| 270 | case 'radio': |
||
| 271 | $options = $this->getVar('field_options'); |
||
| 272 | View Code Duplication | if (isset($options[$value])) { |
|
| 273 | $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value]); |
||
| 274 | } else { |
||
| 275 | $value = ''; |
||
| 276 | } |
||
| 277 | |||
| 278 | return $value; |
||
| 279 | break; |
||
| 280 | |||
| 281 | case 'select_multi': |
||
| 282 | case 'checkbox': |
||
| 283 | $options = $this->getVar('field_options'); |
||
| 284 | $ret = array(); |
||
| 285 | if (count($options) > 0) { |
||
| 286 | foreach (array_keys($options) as $key) { |
||
| 287 | View Code Duplication | if (in_array($key, $value)) { |
|
| 288 | $ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key]) : $options[$key]); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | return $ret; |
||
| 294 | break; |
||
| 295 | |||
| 296 | case 'group': |
||
| 297 | $member_handler = xoops_getHandler('member'); |
||
| 298 | $options = $member_handler->getGroupList(); |
||
| 299 | $ret = isset($options[$value]) ? $options[$value] : ''; |
||
| 300 | |||
| 301 | return $ret; |
||
| 302 | break; |
||
| 303 | |||
| 304 | case 'group_multi': |
||
| 305 | $member_handler = xoops_getHandler('member'); |
||
| 306 | $options = $member_handler->getGroupList(); |
||
| 307 | $ret = array(); |
||
| 308 | foreach (array_keys($options) as $key) { |
||
| 309 | if (in_array($key, $value)) { |
||
| 310 | $ret[$key] = htmlspecialchars($options[$key]); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | return $ret; |
||
| 315 | break; |
||
| 316 | |||
| 317 | case 'longdate': |
||
| 318 | //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that |
||
| 319 | //when we cannot convert it to a UNIX timestamp? |
||
| 320 | return str_replace('-', '/', $value); |
||
| 321 | |||
| 322 | case 'date': |
||
| 323 | return formatTimestamp($value, 's'); |
||
| 324 | break; |
||
| 325 | |||
| 326 | case 'datetime': |
||
| 327 | if (!empty($value)) { |
||
| 328 | return formatTimestamp($value, 'm'); |
||
| 329 | } else { |
||
| 330 | return $value = _PROFILE_MI_NEVER_LOGGED_IN; |
||
| 331 | } |
||
| 332 | break; |
||
| 333 | |||
| 334 | case 'autotext': |
||
| 335 | $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it |
||
| 336 | $value = str_replace('{X_UID}', $user->getVar('uid'), $value); |
||
| 337 | $value = str_replace('{X_URL}', XOOPS_URL, $value); |
||
| 338 | $value = str_replace('{X_UNAME}', $user->getVar('uname'), $value); |
||
| 339 | |||
| 340 | return $value; |
||
| 341 | break; |
||
| 342 | |||
| 343 | case 'rank': |
||
| 344 | $userrank = $user->rank(); |
||
| 345 | $user_rankimage = ''; |
||
| 346 | if (isset($userrank['image']) && $userrank['image'] !== '') { |
||
| 347 | $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br>'; |
||
| 348 | } |
||
| 349 | |||
| 350 | return $user_rankimage . $userrank['title']; |
||
| 351 | break; |
||
| 352 | |||
| 353 | case 'yesno': |
||
| 354 | return $value ? _YES : _NO; |
||
| 355 | break; |
||
| 356 | |||
| 357 | case 'timezone': |
||
| 358 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
| 359 | $timezones = XoopsLists::getTimeZoneList(); |
||
| 360 | $value = empty($value) ? '0' : (string)$value; |
||
| 361 | |||
| 362 | return $timezones[str_replace('.0', '', $value)]; |
||
| 363 | break; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns a value ready to be saved in the database |
||
| 369 | * |
||
| 370 | * @param mixed $value Value to format |
||
| 371 | * |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | public function getValueForSave($value) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get names of user variables |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | public function getUserVars() |
||
| 426 | } |
||
| 427 | |||
| 685 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.