Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. 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 ProfileField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ProfileField extends XoopsObject |
||
27 | { |
||
28 | /** |
||
29 | * |
||
30 | */ |
||
31 | public function __construct() |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Extra treatment dealing with non latin encoding |
||
54 | * Tricky solution |
||
55 | * @param string $key |
||
56 | * @param mixed $value |
||
57 | * @param bool $not_gpc |
||
58 | */ |
||
59 | public function setVar($key, $value, $not_gpc = false) |
||
60 | { |
||
61 | if ($key === 'field_options' && is_array($value)) { |
||
62 | foreach (array_keys($value) as $idx) { |
||
63 | $value[$idx] = base64_encode($value[$idx]); |
||
64 | } |
||
65 | } |
||
66 | parent::setVar($key, $value, $not_gpc); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $key |
||
71 | * @param string $format |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getVar($key, $format = 's') |
||
76 | { |
||
77 | $value = parent::getVar($key, $format); |
||
78 | if ($key === 'field_options' && !empty($value)) { |
||
79 | foreach (array_keys($value) as $idx) { |
||
|
|||
80 | $value[$idx] = base64_decode($value[$idx]); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return $value; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
89 | * |
||
90 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
91 | * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of |
||
92 | * |
||
93 | * @return XoopsFormElement |
||
94 | **/ |
||
95 | public function getEditElement($user, $profile) |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns a value for output of this field |
||
221 | * |
||
222 | * @param XoopsUser $user {@link XoopsUser} object to get the value of |
||
223 | * @param profileProfile $profile object to get the value of |
||
224 | * |
||
225 | * @return mixed |
||
226 | **/ |
||
227 | public function getOutputValue(&$user, $profile) |
||
228 | { |
||
229 | if (file_exists($file = $GLOBALS['xoops']->path('modules/profile/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) { |
||
230 | include_once $file; |
||
231 | } else { |
||
232 | include_once $GLOBALS['xoops']->path('modules/profile/language/english/modinfo.php'); |
||
233 | } |
||
234 | |||
235 | $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name')); |
||
236 | |||
237 | switch ($this->getVar('field_type')) { |
||
238 | default: |
||
239 | case 'textbox': |
||
240 | $value = is_array($value) ? $value[0] : $value; |
||
241 | if ($this->getVar('field_name') === 'url' && $value !== '') { |
||
242 | return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>'; |
||
243 | } else { |
||
244 | return $value; |
||
245 | } |
||
246 | break; |
||
247 | case 'textarea': |
||
248 | case 'dhtml': |
||
249 | case 'theme': |
||
250 | case 'language': |
||
251 | case 'list': |
||
252 | return $value; |
||
253 | break; |
||
254 | |||
255 | case 'select': |
||
256 | case 'radio': |
||
257 | $value = is_array($value) ? $value[0] : $value; |
||
258 | $options = $this->getVar('field_options'); |
||
259 | if (isset($options[$value])) { |
||
260 | $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value]); |
||
261 | } else { |
||
262 | $value = ''; |
||
263 | } |
||
264 | |||
265 | return $value; |
||
266 | break; |
||
267 | |||
268 | case 'select_multi': |
||
269 | case 'checkbox': |
||
270 | $options = $this->getVar('field_options'); |
||
271 | $ret = array(); |
||
272 | if (count($options) > 0) { |
||
273 | foreach (array_keys($options) as $key) { |
||
274 | if (in_array($key, $value)) { |
||
275 | $ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key]) : $options[$key]); |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | return $ret; |
||
281 | break; |
||
282 | |||
283 | case 'group': |
||
284 | /* @var $member_handler XoopsMemberHandler */ |
||
285 | $member_handler = xoops_getHandler('member'); |
||
286 | $options = $member_handler->getGroupList(); |
||
287 | $ret = isset($options[$value]) ? $options[$value] : ''; |
||
288 | |||
289 | return $ret; |
||
290 | break; |
||
291 | |||
292 | case 'group_multi': |
||
293 | /* @var $member_handler XoopsMemberHandler */ |
||
294 | $member_handler = xoops_getHandler('member'); |
||
295 | $options = $member_handler->getGroupList(); |
||
296 | $ret = array(); |
||
297 | foreach (array_keys($options) as $key) { |
||
298 | if (in_array($key, $value)) { |
||
299 | $ret[$key] = htmlspecialchars($options[$key]); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | return $ret; |
||
304 | break; |
||
305 | |||
306 | case 'longdate': |
||
307 | //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that |
||
308 | //when we cannot convert it to a UNIX timestamp? |
||
309 | return str_replace('-', '/', $value); |
||
310 | |||
311 | case 'date': |
||
312 | return formatTimestamp($value, 's'); |
||
313 | break; |
||
314 | |||
315 | case 'datetime': |
||
316 | if (!empty($value)) { |
||
317 | return formatTimestamp($value, 'm'); |
||
318 | } else { |
||
319 | return $value = _PROFILE_MI_NEVER_LOGGED_IN; |
||
320 | } |
||
321 | break; |
||
322 | |||
323 | case 'autotext': |
||
324 | $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it |
||
325 | $value = str_replace('{X_UID}', $user->getVar('uid'), $value); |
||
326 | $value = str_replace('{X_URL}', XOOPS_URL, $value); |
||
327 | $value = str_replace('{X_UNAME}', $user->getVar('uname'), $value); |
||
328 | |||
329 | return $value; |
||
330 | break; |
||
331 | |||
332 | case 'rank': |
||
333 | $userrank = $user->rank(); |
||
334 | $user_rankimage = ''; |
||
335 | if (isset($userrank['image']) && $userrank['image'] !== '') { |
||
336 | $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br>'; |
||
337 | } |
||
338 | |||
339 | return $user_rankimage . $userrank['title']; |
||
340 | break; |
||
341 | |||
342 | case 'yesno': |
||
343 | return $value ? _YES : _NO; |
||
344 | break; |
||
345 | |||
346 | case 'timezone': |
||
347 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
348 | $timezones = XoopsLists::getTimeZoneList(); |
||
349 | $value = empty($value) ? '0' : (string)$value; |
||
350 | |||
351 | return $timezones[str_replace('.0', '', $value)]; |
||
352 | break; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Returns a value ready to be saved in the database |
||
358 | * |
||
359 | * @param mixed $value Value to format |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | public function getValueForSave($value) |
||
364 | { |
||
365 | switch ($this->getVar('field_type')) { |
||
366 | default: |
||
367 | case 'textbox': |
||
368 | case 'textarea': |
||
369 | case 'dhtml': |
||
370 | case 'yesno': |
||
371 | case 'timezone': |
||
372 | case 'theme': |
||
373 | case 'language': |
||
374 | case 'list': |
||
375 | case 'select': |
||
376 | case 'radio': |
||
377 | case 'select_multi': |
||
378 | case 'group': |
||
379 | case 'group_multi': |
||
380 | case 'longdate': |
||
381 | return $value; |
||
382 | |||
383 | case 'checkbox': |
||
384 | return (array)$value; |
||
385 | |||
386 | case 'date': |
||
387 | if ($value !== '') { |
||
388 | return strtotime($value); |
||
389 | } |
||
390 | |||
391 | return $value; |
||
392 | break; |
||
393 | |||
394 | case 'datetime': |
||
395 | if (!empty($value)) { |
||
396 | return strtotime($value['date']) + (int)$value['time']; |
||
397 | } |
||
398 | |||
399 | return $value; |
||
400 | break; |
||
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Get names of user variables |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | public function getUserVars() |
||
415 | } |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @package kernel |
||
681 |