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