Complex classes like ProfileProfileHandler 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 ProfileProfileHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class ProfileProfileHandler 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) |
||
| 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) |
||
| 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) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create new {@link ProfileField} object |
||
| 121 | * |
||
| 122 | * @param bool $isNew |
||
| 123 | * |
||
| 124 | * @return ProfileField |
||
| 125 | */ |
||
| 126 | public function createField($isNew = true) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Load field information |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function loadFields() |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get array of standard variable names (user table) |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function getUserVars() |
||
| 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) |
||
| 357 | } |
||
| 358 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.