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 UserlogSetting 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 UserlogSetting, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class UserlogSetting extends XoopsObject |
||
32 | { |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | public $all_logby = ['uid' => _AM_USERLOG_UID, 'gid' => _AM_USERLOG_SET_GID, 'ip' => _AM_USERLOG_SET_IP]; |
||
37 | |||
38 | public $userlog = null; |
||
39 | |||
40 | /** |
||
41 | * constructor |
||
42 | */ |
||
43 | public function __construct() |
||
53 | |||
54 | /** |
||
55 | * @param string $method |
||
56 | * @param array $args |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function __call($method, $args) |
||
66 | |||
67 | /** |
||
68 | * @return UserlogSetting |
||
69 | */ |
||
70 | public static function getInstance() |
||
79 | |||
80 | /** |
||
81 | * @return mixed|string |
||
82 | */ |
||
83 | public function unique_id() |
||
91 | |||
92 | /** |
||
93 | * @param bool $force |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function storeSet($force = true) |
||
108 | |||
109 | /** |
||
110 | * @return array|bool |
||
111 | */ |
||
112 | public function getSet() |
||
179 | |||
180 | /** |
||
181 | * @param bool $force |
||
182 | * |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function setDb($force = true) |
||
192 | |||
193 | public function getDb() |
||
196 | |||
197 | /** |
||
198 | * @param string $logby |
||
199 | * @param $unique_id |
||
200 | * @param $options |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function setFile($logby = 'uid', $unique_id, $options) |
||
208 | |||
209 | /** |
||
210 | * @param string $logby |
||
211 | * @param $unique_id |
||
212 | * |
||
213 | * @return bool|mixed |
||
214 | */ |
||
215 | public function getFile($logby = 'uid', $unique_id) |
||
219 | |||
220 | /** |
||
221 | * @param string $logby |
||
222 | * @param $unique_id |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function deleteFile($logby = 'uid', $unique_id) |
||
230 | |||
231 | /** |
||
232 | * @param $data |
||
233 | * @param null $name |
||
234 | * @param string $root_path |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | private function _createCacheFile($data, $name = null, $root_path = XOOPS_CACHE_PATH) |
||
246 | |||
247 | /** |
||
248 | * @param null $name |
||
249 | * @param string $root_path |
||
250 | * |
||
251 | * @return bool|mixed |
||
252 | */ |
||
253 | View Code Duplication | private function _loadCacheFile($name = null, $root_path = XOOPS_CACHE_PATH) |
|
262 | |||
263 | /** |
||
264 | * @param null $name |
||
265 | * @param string $root_path |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | View Code Duplication | private function _deleteCacheFile($name = null, $root_path = XOOPS_CACHE_PATH) |
|
278 | |||
279 | /** |
||
280 | * @param null $option |
||
281 | * @param string $V |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getOptions($option = null, $V = 'value') |
||
286 | { |
||
287 | $V = strtolower($V); |
||
288 | |||
289 | if ($this->userlog->getUser()) { |
||
290 | $uid = $this->userlog->getUser()->getVar('uid'); |
||
291 | $uname = $this->userlog->getUser()->getVar('uname'); |
||
292 | $last_login = $this->userlog->getUser()->getVar('last_login'); |
||
293 | $admin = $this->userlog->getUser()->isAdmin(); |
||
294 | $groups = 'g' . implode('g', array_unique($this->userlog->getUser()->getGroups())); // g1g2 |
||
295 | } else { |
||
296 | $uid = 0; |
||
297 | $uname = ''; |
||
298 | $last_login = 0; |
||
299 | $admin = 0; |
||
300 | $groups = 'g' . XOOPS_GROUP_ANONYMOUS; // g3 |
||
301 | } |
||
302 | $tempUserLog = explode('/', $_SERVER['PHP_SELF']); |
||
303 | $options = [ |
||
304 | 'log_id' => [ |
||
305 | 'type' => 'int', |
||
306 | 'title' => _AM_USERLOG_LOG_ID, |
||
307 | 'value' => null // null for now |
||
308 | ], |
||
309 | 'log_time' => [ |
||
310 | 'type' => 'int', |
||
311 | 'title' => _AM_USERLOG_LOG_TIME, |
||
312 | 'value' => time() |
||
313 | ], |
||
314 | 'uid' => [ |
||
315 | 'type' => 'int', |
||
316 | 'title' => _AM_USERLOG_UID, |
||
317 | 'value' => $uid |
||
318 | ], |
||
319 | 'uname' => [ |
||
320 | 'type' => 'text', |
||
321 | 'title' => _AM_USERLOG_UNAME, |
||
322 | 'value' => $uname |
||
323 | ], |
||
324 | 'admin' => [ |
||
325 | 'type' => 'bool', |
||
326 | 'title' => _AM_USERLOG_ADMIN, |
||
327 | 'value' => $admin |
||
328 | ], |
||
329 | 'groups' => [ |
||
330 | 'type' => 'text', |
||
331 | 'title' => _AM_USERLOG_GROUPS, |
||
332 | 'value' => $groups |
||
333 | ], |
||
334 | 'last_login' => [ |
||
335 | 'type' => 'int', |
||
336 | 'title' => _AM_USERLOG_LAST_LOGIN, |
||
337 | 'value' => $last_login |
||
338 | ], |
||
339 | 'user_ip' => [ |
||
340 | 'type' => 'text', |
||
341 | 'title' => _AM_USERLOG_USER_IP, |
||
342 | 'value' => $_SERVER['REMOTE_ADDR'] |
||
343 | ], |
||
344 | 'user_agent' => [ |
||
345 | 'type' => 'text', |
||
346 | 'title' => _AM_USERLOG_USER_AGENT, |
||
347 | 'value' => $_SERVER['HTTP_USER_AGENT'] |
||
348 | ], |
||
349 | 'url' => [ |
||
350 | 'type' => 'text', |
||
351 | 'title' => _AM_USERLOG_URL, |
||
352 | 'value' => $_SERVER['REQUEST_URI'] |
||
353 | ], |
||
354 | 'script' => [ |
||
355 | 'type' => 'text', |
||
356 | 'title' => _AM_USERLOG_SCRIPT, |
||
357 | 'value' => end($tempUserLog) |
||
358 | ], |
||
359 | 'referer' => [ |
||
360 | 'type' => 'text', |
||
361 | 'title' => _AM_USERLOG_REFERER, |
||
362 | 'value' => !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '' |
||
363 | ], |
||
364 | 'pagetitle' => [ |
||
365 | 'type' => 'text', |
||
366 | 'title' => _AM_USERLOG_PAGETITLE, |
||
367 | 'value' => isset($GLOBALS['xoopsTpl']) ? $GLOBALS['xoopsTpl']->get_template_vars('xoops_pagetitle') : '' |
||
368 | ], |
||
369 | 'pageadmin' => [ |
||
370 | 'type' => 'bool', |
||
371 | 'title' => _AM_USERLOG_PAGEADMIN, |
||
372 | 'value' => (isset($GLOBALS['xoopsOption']['pagetype']) |
||
373 | && 'admin' === $GLOBALS['xoopsOption']['pagetype']) ? 1 : 0 |
||
374 | ], |
||
375 | 'module' => [ |
||
376 | 'type' => 'text', |
||
377 | 'title' => _AM_USERLOG_MODULE, |
||
378 | 'value' => $this->userlog->getLogModule()->getVar('dirname') |
||
379 | ], |
||
380 | 'module_name' => [ |
||
381 | 'type' => 'text', |
||
382 | 'title' => _AM_USERLOG_MODULE_NAME, |
||
383 | 'value' => $this->userlog->getLogModule()->getVar('name') |
||
384 | ], |
||
385 | 'item_name' => [ |
||
386 | 'type' => 'text', |
||
387 | 'title' => _AM_USERLOG_ITEM_NAME, |
||
388 | 'value' => null |
||
389 | ], |
||
390 | 'item_id' => [ |
||
391 | 'type' => 'int', |
||
392 | 'title' => _AM_USERLOG_ITEM_ID, |
||
393 | 'value' => null |
||
394 | ], |
||
395 | // user data input method |
||
396 | 'request_method' => [ |
||
397 | 'type' => 'text', |
||
398 | 'title' => _AM_USERLOG_REQUEST_METHOD, |
||
399 | 'value' => $_SERVER['REQUEST_METHOD'] |
||
400 | ], |
||
401 | 'zget' => [ |
||
402 | 'type' => 'text', |
||
403 | 'title' => _AM_USERLOG_GET, |
||
404 | 'value' => $_GET |
||
405 | ], |
||
406 | 'post' => [ |
||
407 | 'type' => 'text', |
||
408 | 'title' => _AM_USERLOG_POST, |
||
409 | 'value' => $_POST |
||
410 | ], |
||
411 | 'request' => [ |
||
412 | 'type' => 'text', |
||
413 | 'title' => _AM_USERLOG_REQUEST, |
||
414 | 'value' => $_REQUEST |
||
415 | ], |
||
416 | 'files' => [ |
||
417 | 'type' => 'text', |
||
418 | 'title' => _AM_USERLOG_FILES, |
||
419 | 'value' => $_FILES |
||
420 | ], |
||
421 | 'env' => [ |
||
422 | 'type' => 'text', |
||
423 | 'title' => _AM_USERLOG_ENV, |
||
424 | 'value' => $_ENV |
||
425 | ], |
||
426 | 'session' => [ |
||
427 | 'type' => 'text', |
||
428 | 'title' => _AM_USERLOG_SESSION, |
||
429 | 'value' => $_SESSION |
||
430 | ], |
||
431 | 'cookie' => [ |
||
432 | 'type' => 'text', |
||
433 | 'title' => _AM_USERLOG_COOKIE, |
||
434 | 'value' => $_COOKIE |
||
435 | ], |
||
436 | 'header' => [ |
||
437 | 'type' => 'text', |
||
438 | 'title' => _AM_USERLOG_HEADER, |
||
439 | 'value' => headers_list() |
||
440 | ], |
||
441 | 'logger' => [ |
||
442 | 'type' => 'text', |
||
443 | 'title' => _AM_USERLOG_LOGGER, |
||
444 | 'value' => $GLOBALS['xoopsLogger']->errors |
||
445 | ], |
||
446 | // settings will not be logged |
||
447 | 'active' => [ |
||
448 | 'type' => 'int', |
||
449 | 'title' => _AM_USERLOG_SET_ACTIVE, |
||
450 | 'value' => 1 |
||
451 | ], |
||
452 | 'inside' => [ |
||
453 | 'type' => 'int', |
||
454 | 'title' => _AM_USERLOG_INSIDE, |
||
455 | 'value' => 1 |
||
456 | ], |
||
457 | 'outside' => [ |
||
458 | 'type' => 'int', |
||
459 | 'title' => _AM_USERLOG_OUTSIDE, |
||
460 | 'value' => 1 |
||
461 | ], |
||
462 | 'unset_pass' => [ |
||
463 | 'type' => 'int', |
||
464 | 'title' => _AM_USERLOG_UNSET_PASS, |
||
465 | 'value' => 1 |
||
466 | ], |
||
467 | 'store_file' => [ |
||
468 | 'type' => 'int', |
||
469 | 'title' => _AM_USERLOG_STORE_FILE, |
||
470 | 'value' => 1 |
||
471 | ], |
||
472 | 'store_db' => [ |
||
473 | 'type' => 'int', |
||
474 | 'title' => _AM_USERLOG_STORE_DB, |
||
475 | 'value' => 1 |
||
476 | ], |
||
477 | 'views' => [ |
||
478 | 'type' => 'int', |
||
479 | 'title' => _AM_USERLOG_VIEWS, |
||
480 | 'value' => 1 // for item_name and item_id |
||
481 | ] |
||
482 | ]; |
||
483 | $ret = $this->userlog->getFromKeys($options, $option); |
||
484 | // patch Login/Register History |
||
485 | if (isset($ret['post']['value'])) { |
||
486 | $ret['post']['value'] = $this->userlog->patchLoginHistory($ret['post']['value'], $uid, !empty($ret['unset_pass']['value'])); |
||
487 | } |
||
488 | if (empty($V)) { |
||
489 | return $ret; |
||
490 | } |
||
491 | if ('key' === $V) { |
||
492 | return array_keys($ret); |
||
493 | } |
||
494 | $ret2 = []; |
||
495 | $emptyAll = 'value' === $V; // check if all values are empty |
||
496 | foreach ($ret as $option => $val) { |
||
497 | $ret2[$option] = $val[$V]; |
||
498 | // if there is a value || exceptions continue |
||
499 | if (!$emptyAll |
||
500 | || in_array($option, [ |
||
501 | 'log_id', |
||
502 | 'log_time', |
||
503 | 'active', |
||
504 | 'inside', |
||
505 | 'outside', |
||
506 | 'unset_pass', |
||
507 | 'store_file', |
||
508 | 'store_db', |
||
509 | 'views' |
||
510 | ]) |
||
511 | ) { |
||
512 | continue; |
||
513 | } |
||
514 | // check values |
||
515 | if (!empty($val[$V])) { |
||
516 | $emptyAll = false; |
||
517 | } |
||
518 | } |
||
519 | |||
520 | return $emptyAll ? [] : $ret2; |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * @param null $options |
||
525 | * |
||
526 | * @return array |
||
527 | */ |
||
528 | public function logForm($options = null) |
||
566 | |||
567 | /** |
||
568 | * @return int |
||
569 | */ |
||
570 | public function cleanCache() |
||
579 | } |
||
580 | |||
632 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.