| Total Complexity | 44 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like YogurtProfileHandler 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 YogurtProfileHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class YogurtProfileHandler extends XoopsPersistableObjectHandler |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * holds reference to {@link profileFieldHandler} object |
||
| 59 | */ |
||
| 60 | public $_fHandler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Array of {@link XoopsProfileField} objects |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | public $_fields = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param null|XoopsDatabase $db |
||
| 70 | */ |
||
| 71 | public function __construct(XoopsDatabase $db) |
||
| 72 | { |
||
| 73 | parent::__construct($db, 'yogurt_profile', 'yogurtprofile', 'profile_id'); |
||
| 74 | $this->_fHandler = xoops_getModuleHandler('field', 'yogurt'); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * create a new {@link ProfileProfile} |
||
| 79 | * |
||
| 80 | * @param bool $isNew Flag the new objects as "new"? |
||
| 81 | * |
||
| 82 | * @return object {@link ProfileProfile} |
||
| 83 | */ |
||
| 84 | public function create($isNew = true) |
||
| 85 | { |
||
| 86 | $obj = new $this->className($this->loadFields()); |
||
| 87 | $obj->handler = $this; |
||
| 88 | if ($isNew === true) { |
||
| 89 | $obj->setNew(); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $obj; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a ProfileProfile object for a user id. |
||
| 97 | * |
||
| 98 | * We will create an empty profile if none exists. This behavior allows user objects |
||
| 99 | * created outside of profile to be edited correctly in the profile module. |
||
| 100 | * |
||
| 101 | * @param integer|null $uid |
||
| 102 | * @param string[]|null $fields array of field names to fetch, null for all |
||
| 103 | * |
||
| 104 | * @return object {@link ProfileProfile} |
||
| 105 | * |
||
| 106 | * @internal This was get($uid, $createOnFailure = true). No callers found using the extra parameter. |
||
| 107 | * @internal Modified to match parent signature. |
||
| 108 | */ |
||
| 109 | public function get($uid = null, $fields = null) |
||
| 110 | { |
||
| 111 | $obj = parent::get($uid, $fields); |
||
| 112 | if (!is_object($obj)) { |
||
| 113 | $obj = $this->create(); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $obj; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create new {@link ProfileField} object |
||
| 121 | * |
||
| 122 | * @param bool $isNew |
||
| 123 | * |
||
| 124 | * @return ProfileField |
||
| 125 | */ |
||
| 126 | public function createField($isNew = true) |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Load field information |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function loadFields() |
||
| 139 | { |
||
| 140 | if (count($this->_fields) == 0) { |
||
| 141 | $this->_fields = $this->_fHandler->loadFields(); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $this->_fields; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Fetch fields |
||
| 149 | * |
||
| 150 | * @param CriteriaElement $criteria {@link CriteriaElement} object |
||
| 151 | * @param bool $id_as_key return array with field IDs as key? |
||
| 152 | * @param bool $as_object return array of objects? |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | **/ |
||
| 156 | public function getFields(CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
||
| 157 | { |
||
| 158 | return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Insert a field in the database |
||
| 163 | * |
||
| 164 | * @param ProfileField $field |
||
| 165 | * @param bool $force |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function insertField(ProfileField $field, $force = false) |
||
| 170 | { |
||
| 171 | return $this->_fHandler->insert($field, $force); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Delete a field from the database |
||
| 176 | * |
||
| 177 | * @param ProfileField $field |
||
| 178 | * @param bool $force |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function deleteField(ProfileField $field, $force = false) |
||
| 183 | { |
||
| 184 | return $this->_fHandler->delete($field, $force); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Save a new field in the database |
||
| 189 | * |
||
| 190 | * @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
||
| 191 | * @param int $weight |
||
| 192 | * |
||
| 193 | * @internal param int $categoryid ID of the category to add it to |
||
| 194 | * @internal param int $type valuetype of the field |
||
| 195 | * @internal param int $moduleid ID of the module, this field belongs to |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function saveField($vars, $weight = 0) |
||
| 199 | { |
||
| 200 | $field = $this->createField(); |
||
| 201 | $field->setVar('field_name', $vars['name']); |
||
| 202 | $field->setVar('field_valuetype', $vars['valuetype']); |
||
| 203 | $field->setVar('field_type', $vars['type']); |
||
| 204 | $field->setVar('field_weight', $weight); |
||
| 205 | if (isset($vars['title'])) { |
||
| 206 | $field->setVar('field_title', $vars['title']); |
||
| 207 | } |
||
| 208 | if (isset($vars['description'])) { |
||
| 209 | $field->setVar('field_description', $vars['description']); |
||
| 210 | } |
||
| 211 | if (isset($vars['required'])) { |
||
| 212 | $field->setVar('field_required', $vars['required']); //0 = no, 1 = yes |
||
| 213 | } |
||
| 214 | if (isset($vars['maxlength'])) { |
||
| 215 | $field->setVar('field_maxlength', $vars['maxlength']); |
||
| 216 | } |
||
| 217 | if (isset($vars['default'])) { |
||
| 218 | $field->setVar('field_default', $vars['default']); |
||
| 219 | } |
||
| 220 | if (isset($vars['notnull'])) { |
||
| 221 | $field->setVar('field_notnull', $vars['notnull']); |
||
| 222 | } |
||
| 223 | if (isset($vars['show'])) { |
||
| 224 | $field->setVar('field_show', $vars['show']); |
||
| 225 | } |
||
| 226 | if (isset($vars['edit'])) { |
||
| 227 | $field->setVar('field_edit', $vars['edit']); |
||
| 228 | } |
||
| 229 | if (isset($vars['config'])) { |
||
| 230 | $field->setVar('field_config', $vars['config']); |
||
| 231 | } |
||
| 232 | if (isset($vars['options'])) { |
||
| 233 | $field->setVar('field_options', $vars['options']); |
||
| 234 | } else { |
||
| 235 | $field->setVar('field_options', array()); |
||
| 236 | } |
||
| 237 | if ($this->insertField($field)) { |
||
| 238 | $msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database'; |
||
| 239 | } else { |
||
| 240 | $msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . implode(' ', $field->getErrors()) . $this->db->error() . '</span>'; |
||
| 241 | } |
||
| 242 | unset($field); |
||
| 243 | |||
| 244 | return $msg; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * insert a new object in the database |
||
| 249 | * |
||
| 250 | * @param XoopsObject|ProfileProfile $obj reference to the object |
||
| 251 | * @param bool $force whether to force the query execution despite security settings |
||
| 252 | * |
||
| 253 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 254 | */ |
||
| 255 | public function insert(XoopsObject $obj, $force = false) |
||
| 256 | { |
||
| 257 | if (!($obj instanceof $this->className)) { |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | $uservars = $this->getUserVars(); |
||
| 261 | foreach ($uservars as $var) { |
||
| 262 | unset($obj->vars[$var]); |
||
| 263 | } |
||
| 264 | if (count($obj->vars) == 0) { |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | |||
| 268 | return parent::insert($obj, $force); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get array of standard variable names (user table) |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function getUserVars() |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Search profiles and users |
||
| 283 | * |
||
| 284 | * @param CriteriaElement $criteria CriteriaElement |
||
| 285 | * @param array $searchvars Fields to be fetched |
||
| 286 | * @param array $groups for Usergroups is selected (only admin!) |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function search(CriteriaElement $criteria, $searchvars = array(), $groups = null) |
||
| 358 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.