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() |
||
| 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) |
||
| 96 | { |
||
| 97 | $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e'); |
||
| 98 | |||
| 99 | $caption = $this->getVar('field_title'); |
||
| 100 | $caption = defined($caption) ? constant($caption) : $caption; |
||
| 101 | $name = $this->getVar('field_name', 'e'); |
||
| 102 | $options = $this->getVar('field_options'); |
||
| 103 | if (is_array($options)) { |
||
| 104 | //asort($options); |
||
| 105 | |||
| 106 | foreach (array_keys($options) as $key) { |
||
| 107 | $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key]; |
||
| 108 | $optkey = defined($key) ? constant($key) : $key; |
||
| 109 | unset($options[$key]); |
||
| 110 | $options[$optkey] = $optval; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | include_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
| 114 | switch ($this->getVar('field_type')) { |
||
| 115 | default: |
||
| 116 | case 'autotext': |
||
| 117 | //autotext is not for editing |
||
| 118 | $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile)); |
||
| 119 | break; |
||
| 120 | |||
| 121 | case 'textbox': |
||
| 122 | $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
| 123 | break; |
||
| 124 | |||
| 125 | case 'textarea': |
||
| 126 | $element = new XoopsFormTextArea($caption, $name, $value, 4, 30); |
||
| 127 | break; |
||
| 128 | |||
| 129 | case 'dhtml': |
||
| 130 | $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30); |
||
| 131 | break; |
||
| 132 | |||
| 133 | case 'select': |
||
| 134 | $element = new XoopsFormSelect($caption, $name, $value); |
||
| 135 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
| 136 | // if (!in_array('', array_keys($options))) { |
||
| 137 | if (!array_key_exists('', $options)) { |
||
| 138 | $element->addOption('', _NONE); |
||
| 139 | |||
| 140 | $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf(_FORM_ENTER, $caption); |
||
| 141 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
||
| 142 | $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" . "for (i = 0; i < selectBox.options.length; i++) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
||
| 143 | } |
||
| 144 | $element->addOptionArray($options); |
||
| 145 | break; |
||
| 146 | |||
| 147 | case 'select_multi': |
||
| 148 | $element = new XoopsFormSelect($caption, $name, $value, 5, true); |
||
| 149 | $element->addOptionArray($options); |
||
| 150 | break; |
||
| 151 | |||
| 152 | case 'radio': |
||
| 153 | $element = new XoopsFormRadio($caption, $name, $value); |
||
| 154 | $element->addOptionArray($options); |
||
| 155 | break; |
||
| 156 | |||
| 157 | case 'checkbox': |
||
| 158 | $element = new XoopsFormCheckBox($caption, $name, $value); |
||
| 159 | $element->addOptionArray($options); |
||
| 160 | break; |
||
| 161 | |||
| 162 | case 'yesno': |
||
| 163 | $element = new XoopsFormRadioYN($caption, $name, $value); |
||
| 164 | break; |
||
| 165 | |||
| 166 | case 'group': |
||
| 167 | $element = new XoopsFormSelectGroup($caption, $name, true, $value); |
||
| 168 | break; |
||
| 169 | |||
| 170 | case 'group_multi': |
||
| 171 | $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true); |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 'language': |
||
| 175 | $element = new XoopsFormSelectLang($caption, $name, $value); |
||
| 176 | break; |
||
| 177 | |||
| 178 | case 'date': |
||
| 179 | $element = new XoopsFormTextDateSelect($caption, $name, 15, $value); |
||
| 180 | break; |
||
| 181 | |||
| 182 | case 'longdate': |
||
| 183 | $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace('-', '/', $value)); |
||
| 184 | break; |
||
| 185 | |||
| 186 | case 'datetime': |
||
| 187 | $element = new XoopsFormDatetime($caption, $name, 15, $value); |
||
| 188 | break; |
||
| 189 | |||
| 190 | case 'list': |
||
| 191 | $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]); |
||
| 192 | break; |
||
| 193 | |||
| 194 | case 'timezone': |
||
| 195 | $element = new XoopsFormSelectTimezone($caption, $name, $value); |
||
| 196 | $element->setExtra("style='width: 280px;'"); |
||
| 197 | break; |
||
| 198 | |||
| 199 | case 'rank': |
||
| 200 | $element = new XoopsFormSelect($caption, $name, $value); |
||
| 201 | |||
| 202 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
| 203 | $ranks = XoopsLists::getUserRankList(); |
||
| 204 | $element->addOption(0, '--------------'); |
||
| 205 | $element->addOptionArray($ranks); |
||
| 206 | break; |
||
| 207 | |||
| 208 | case 'theme': |
||
| 209 | $element = new XoopsFormSelectTheme($caption, $name, $value, 1, true); |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | if ($this->getVar('field_description') != '') { |
||
| 213 | $element->setDescription($this->getVar('field_description')); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $element; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns a value for output of this field |
||
| 221 | * |
||
| 222 | * @param XoopsUser $user {@link XoopsUser} object to get the value of |
||
| 223 | * @param profileProfile $profile object to get the value of |
||
| 224 | * |
||
| 225 | * @return mixed |
||
| 226 | **/ |
||
| 227 | public function getOutputValue(&$user, $profile) |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Returns a value ready to be saved in the database |
||
| 358 | * |
||
| 359 | * @param mixed $value Value to format |
||
| 360 | * |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | public function getValueForSave($value) |
||
| 364 | { |
||
| 365 | switch ($this->getVar('field_type')) { |
||
| 366 | default: |
||
| 367 | case 'textbox': |
||
| 368 | case 'textarea': |
||
| 369 | case 'dhtml': |
||
| 370 | case 'yesno': |
||
| 371 | case 'timezone': |
||
| 372 | case 'theme': |
||
| 373 | case 'language': |
||
| 374 | case 'list': |
||
| 375 | case 'select': |
||
| 376 | case 'radio': |
||
| 377 | case 'select_multi': |
||
| 378 | case 'group': |
||
| 379 | case 'group_multi': |
||
| 380 | case 'longdate': |
||
| 381 | return $value; |
||
| 382 | |||
| 383 | case 'checkbox': |
||
| 384 | return (array)$value; |
||
| 385 | |||
| 386 | case 'date': |
||
| 387 | if ($value !== '') { |
||
| 388 | return strtotime($value); |
||
| 389 | } |
||
| 390 | |||
| 391 | return $value; |
||
| 392 | break; |
||
| 393 | |||
| 394 | case 'datetime': |
||
| 395 | if (!empty($value)) { |
||
| 396 | return strtotime($value['date']) + (int)$value['time']; |
||
| 397 | } |
||
| 398 | |||
| 399 | return $value; |
||
| 400 | break; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get names of user variables |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function getUserVars() |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 681 |