| Total Complexity | 102 |
| Total Lines | 389 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 |
||
| 28 | class ProfileField extends XoopsObject |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Constructor |
||
| 32 | */ |
||
| 33 | public function __construct() |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Extra treatment dealing with non latin encoding |
||
| 56 | * Tricky solution |
||
| 57 | * |
||
| 58 | * @param string $key |
||
| 59 | * @param mixed $value |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | * |
||
| 63 | * @todo evaluate removing this. New considerations: full UTF-8 system, new Dtype::TYPE_JSON |
||
| 64 | */ |
||
| 65 | public function setVar($key, $value) |
||
| 66 | { |
||
| 67 | if ($key === 'field_options' && is_array($value)) { |
||
| 68 | foreach (array_keys($value) as $idx) { |
||
| 69 | $value[$idx] = base64_encode($value[$idx]); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | parent::setVar($key, $value); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $key |
||
| 77 | * @param string $format |
||
| 78 | * @return array|mixed |
||
| 79 | */ |
||
| 80 | public function getVar($key, $format = 's') |
||
| 81 | { |
||
| 82 | $value = parent::getVar($key, $format); |
||
| 83 | if ($key === 'field_options' && !empty($value)) { |
||
| 84 | foreach (array_keys($value) as $idx) { |
||
| 85 | $value[$idx] = base64_decode($value[$idx]); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | return $value; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns a {@link Xoops\Form\Element} for editing the value of this field |
||
| 93 | * |
||
| 94 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
| 95 | * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of |
||
| 96 | * |
||
| 97 | * @return Xoops\Form\Element |
||
| 98 | **/ |
||
| 99 | public function getEditElement(XoopsUser $user, ProfileProfile $profile) |
||
| 100 | { |
||
| 101 | $xoops = Xoops::getInstance(); |
||
| 102 | $value = in_array($this->getVar('field_name'), $this->getUserVars()) |
||
| 103 | ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e'); |
||
| 104 | |||
| 105 | $caption = $this->getVar('field_title'); |
||
| 106 | $caption = defined($caption) ? constant($caption) : $caption; |
||
| 107 | $name = $this->getVar('field_name', 'e'); |
||
| 108 | $options = $this->getVar('field_options'); |
||
| 109 | if (is_array($options)) { |
||
| 110 | //asort($options); |
||
| 111 | |||
| 112 | foreach (array_keys($options) as $key) { |
||
| 113 | $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key]; |
||
| 114 | $optkey = defined($key) ? constant($key) : $key; |
||
| 115 | unset($options[$key]); |
||
| 116 | $options[$optkey] = $optval; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | switch ($this->getVar('field_type')) { |
||
| 120 | default: |
||
| 121 | case "autotext": |
||
| 122 | //autotext is not for editing |
||
| 123 | $element = new Xoops\Form\Label($caption, $this->getOutputValue($user, $profile)); |
||
| 124 | break; |
||
| 125 | |||
| 126 | case "textbox": |
||
| 127 | $element = new Xoops\Form\Text($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
| 128 | break; |
||
| 129 | |||
| 130 | case "textarea": |
||
| 131 | $element = new Xoops\Form\TextArea($caption, $name, $value, 4, 30); |
||
| 132 | break; |
||
| 133 | |||
| 134 | case "dhtml": |
||
| 135 | $element = new Xoops\Form\DhtmlTextArea($caption, $name, $value, 10, 30); |
||
| 136 | break; |
||
| 137 | |||
| 138 | case "select": |
||
| 139 | $element = new Xoops\Form\Select($caption, $name, $value); |
||
| 140 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
| 141 | if (!in_array('', array_keys($options))) { |
||
| 142 | $element->addOption('', XoopsLocale::NONE); |
||
| 143 | |||
| 144 | $eltmsg = empty($caption) ? sprintf(XoopsLocale::F_ENTER, $name) : sprintf(XoopsLocale::F_ENTER, $caption); |
||
| 145 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
||
| 146 | $element->addCustomValidationCode("\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; }"); |
||
| 147 | } |
||
| 148 | $element->addOptionArray($options); |
||
| 149 | break; |
||
| 150 | |||
| 151 | case "select_multi": |
||
| 152 | $element = new Xoops\Form\Select($caption, $name, $value, 5, true); |
||
| 153 | $element->addOptionArray($options); |
||
| 154 | break; |
||
| 155 | |||
| 156 | case "radio": |
||
| 157 | $element = new Xoops\Form\Radio($caption, $name, $value); |
||
| 158 | $element->addOptionArray($options); |
||
| 159 | break; |
||
| 160 | |||
| 161 | case "checkbox": |
||
| 162 | $element = new Xoops\Form\Checkbox($caption, $name, $value); |
||
| 163 | $element->addOptionArray($options); |
||
| 164 | break; |
||
| 165 | |||
| 166 | case "yesno": |
||
| 167 | $element = new Xoops\Form\RadioYesNo($caption, $name, $value); |
||
| 168 | break; |
||
| 169 | |||
| 170 | case "group": |
||
| 171 | $element = new Xoops\Form\SelectGroup($caption, $name, true, $value); |
||
| 172 | break; |
||
| 173 | |||
| 174 | case "group_multi": |
||
| 175 | $element = new Xoops\Form\SelectGroup($caption, $name, true, $value, 5, true); |
||
| 176 | break; |
||
| 177 | |||
| 178 | case "language": |
||
| 179 | $element = new Xoops\Form\SelectLanguage($caption, $name, $value); |
||
| 180 | break; |
||
| 181 | |||
| 182 | case "date": |
||
| 183 | $element = new Xoops\Form\DateSelect($caption, $name, $value); |
||
| 184 | break; |
||
| 185 | |||
| 186 | case "longdate": |
||
| 187 | $element = new Xoops\Form\DateSelect($caption, $name, str_replace("-", "/", $value)); |
||
| 188 | break; |
||
| 189 | |||
| 190 | case "datetime": |
||
| 191 | $element = new Xoops\Form\DateTimeSelect($caption, $name, $value); |
||
| 192 | break; |
||
| 193 | |||
| 194 | case "timezone": |
||
| 195 | $element = new Xoops\Form\SelectTimeZone($caption, $name, $value); |
||
| 196 | break; |
||
| 197 | |||
| 198 | case "rank": |
||
| 199 | $ranklist = $xoops->service('userrank')->getAssignableUserRankList()->getValue(); |
||
| 200 | if ($ranklist !== null) { |
||
| 201 | $element = new Xoops\Form\Select($caption, $name, $value); |
||
| 202 | $element->addOption(0, "--------------"); |
||
| 203 | $element->addOptionArray($ranklist); |
||
| 204 | } else { |
||
| 205 | $element = new Xoops\Form\Hidden($name, $value); |
||
| 206 | } |
||
| 207 | break; |
||
| 208 | |||
| 209 | case 'theme': |
||
| 210 | $element = new Xoops\Form\Select($caption, $name, $value); |
||
| 211 | $element->addOption("0", _PROFILE_MA_SITEDEFAULT); |
||
| 212 | $handle = opendir(\XoopsBaseConfig::get('themes-path') . '/'); |
||
| 213 | $dirlist = array(); |
||
| 214 | while (false !== ($file = readdir($handle))) { |
||
| 215 | if (is_dir(\XoopsBaseConfig::get('themes-path') . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) !== 'cvs') { |
||
| 216 | if (XoopsLoad::fileExists(\XoopsBaseConfig::get('themes-path') . "/" . $file . "/theme.tpl") && in_array($file, $xoops->getConfig('theme_set_allowed'))) { |
||
| 217 | $dirlist[$file] = $file; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | closedir($handle); |
||
| 222 | if (!empty($dirlist)) { |
||
| 223 | asort($dirlist); |
||
| 224 | $element->addOptionArray($dirlist); |
||
| 225 | } |
||
| 226 | break; |
||
| 227 | } |
||
| 228 | if ($this->getVar('field_description') != "") { |
||
| 229 | $element->setDescription($this->getVar('field_description')); |
||
| 230 | } |
||
| 231 | return $element; |
||
| 232 | } |
||
| 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 string |
||
| 241 | **/ |
||
| 242 | public function getOutputValue(XoopsUser $user, ProfileProfile $profile) |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns a value ready to be saved in the database |
||
| 361 | * |
||
| 362 | * @param mixed $value Value to format |
||
| 363 | * |
||
| 364 | * @return mixed |
||
| 365 | */ |
||
| 366 | public function getValueForSave($value) |
||
| 367 | { |
||
| 368 | switch ($this->getVar('field_type')) { |
||
| 369 | default: |
||
| 370 | case "textbox": |
||
| 371 | case "textarea": |
||
| 372 | case "dhtml": |
||
| 373 | case "yesno": |
||
| 374 | case 'theme': |
||
| 375 | case "language": |
||
| 376 | case "list": |
||
| 377 | case "select": |
||
| 378 | case "radio": |
||
| 379 | case "select_multi": |
||
| 380 | case "group": |
||
| 381 | case "group_multi": |
||
| 382 | case "longdate": |
||
| 383 | return $value; |
||
| 384 | |||
| 385 | case "timezone": |
||
| 386 | return $value; |
||
| 387 | |||
| 388 | case "checkbox": |
||
| 389 | return (array)$value; |
||
| 390 | |||
| 391 | case "date": |
||
| 392 | if ($value != "") { |
||
| 393 | return strtotime($value); |
||
| 394 | } |
||
| 395 | return $value; |
||
| 396 | break; |
||
| 397 | |||
| 398 | case "datetime": |
||
| 399 | if (!empty($value)) { |
||
| 400 | return strtotime($value['date']) + (int)($value['time']); |
||
| 401 | } |
||
| 402 | return $value; |
||
| 403 | break; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get names of user variables |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | public function getUserVars() |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 631 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: