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