Total Complexity | 87 |
Total Lines | 658 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like XoopsModule 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 XoopsModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class XoopsModule extends XoopsObject |
||
26 | { |
||
27 | /** |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | public $modinfo; |
||
32 | /** |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | public $adminmenu; |
||
37 | /** |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | public $_msg; |
||
42 | |||
43 | //PHP 8.2 Dynamic properties deprecated |
||
44 | public $mid; |
||
45 | public $name; |
||
46 | public $version; |
||
47 | public $last_update; |
||
48 | public $weight; |
||
49 | public $isactive; |
||
50 | public $dirname; |
||
51 | public $hasmain; |
||
52 | public $hasadmin; |
||
53 | public $hassearch; |
||
54 | public $hasconfig; |
||
55 | public $hascomments; |
||
56 | // RMV-NOTIFY |
||
57 | public $hasnotification; |
||
58 | |||
59 | /** |
||
60 | * Constructor |
||
61 | */ |
||
62 | public function __construct() |
||
63 | { |
||
64 | parent::__construct(); |
||
65 | $this->initVar('mid', XOBJ_DTYPE_INT, null, false); |
||
66 | $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150); |
||
67 | $this->initVar('version', XOBJ_DTYPE_TXTBOX, null, false); |
||
68 | $this->initVar('last_update', XOBJ_DTYPE_INT, null, false); |
||
69 | $this->initVar('weight', XOBJ_DTYPE_INT, 0, false); |
||
70 | $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false); |
||
71 | $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true); |
||
72 | $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false); |
||
73 | $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false); |
||
74 | $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false); |
||
75 | $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false); |
||
76 | $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false); |
||
77 | // RMV-NOTIFY |
||
78 | $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Load module info |
||
83 | * |
||
84 | * @param string $dirname Directory Name |
||
85 | * @param boolean $verbose |
||
86 | */ |
||
87 | public function loadInfoAsVar($dirname, $verbose = true) |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * add a message |
||
114 | * |
||
115 | * @param string $str message to add |
||
116 | * @access public |
||
117 | */ |
||
118 | public function setMessage($str) |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * return the messages for this object as an array |
||
125 | * |
||
126 | * @return array an array of messages |
||
127 | * @access public |
||
128 | */ |
||
129 | public function getMessages() |
||
130 | { |
||
131 | return $this->_msg; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Set module info |
||
136 | * |
||
137 | * @param string $name |
||
138 | * @param mixed $value |
||
139 | * @return bool |
||
140 | **/ |
||
141 | public function setInfo($name, $value) |
||
142 | { |
||
143 | if (empty($name)) { |
||
144 | $this->modinfo = $value; |
||
145 | } else { |
||
146 | $this->modinfo[$name] = $value; |
||
147 | } |
||
148 | |||
149 | return true; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get module info |
||
154 | * |
||
155 | * @param string $name |
||
156 | * @return array|string Array of module information. |
||
157 | * If {@link $name} is set, returns a single module information item as string. |
||
158 | */ |
||
159 | public function &getInfo($name = null) |
||
160 | { |
||
161 | if (!isset($this->modinfo)) { |
||
162 | $this->loadInfo($this->getVar('dirname')); |
||
|
|||
163 | } |
||
164 | if (isset($name)) { |
||
165 | if (isset($this->modinfo[$name])) { |
||
166 | return $this->modinfo[$name]; |
||
167 | } |
||
168 | $return = false; |
||
169 | |||
170 | return $return; |
||
171 | } |
||
172 | |||
173 | return $this->modinfo; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get status |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getStatus() |
||
182 | { |
||
183 | return substr(strrchr($this->getVar('version'), '-'), 1); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Compares two "XOOPS-standardized" version number strings. |
||
188 | * |
||
189 | * @param string $version1 |
||
190 | * @param string $version2 |
||
191 | * @param string $operator |
||
192 | * @return boolean The function will return true if the relationship is the one specified by the operator, false otherwise. |
||
193 | */ |
||
194 | public function versionCompare($version1 = '', $version2 = '', $operator = '<') |
||
195 | { |
||
196 | $version1 = strtolower($version1); |
||
197 | $version2 = strtolower($version2); |
||
198 | if (false !== strpos($version2, '-stable')) { |
||
199 | $version2 = substr($version2, 0, strpos($version2, '-stable')); |
||
200 | } |
||
201 | if (false !== strpos($version1, '-stable')) { |
||
202 | $version1 = substr($version1, 0, strpos($version1, '-stable')); |
||
203 | } |
||
204 | return version_compare($version1, $version2, $operator); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Get a link to the modules main page |
||
209 | * |
||
210 | * @return string FALSE on fail |
||
211 | */ |
||
212 | public function mainLink() |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Get links to the subpages |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function subLink() |
||
229 | { |
||
230 | $ret = []; |
||
231 | if ($this->getInfo('sub') && \is_array($this->getInfo('sub'))) { |
||
232 | foreach ($this->getInfo('sub') as $submenu) { |
||
233 | $ret[] = [ |
||
234 | 'name' => $submenu['name'], |
||
235 | 'url' => $submenu['url'], |
||
236 | |||
237 | 'icon' => $submenu['icon']?? '', |
||
238 | |||
239 | ]; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | return $ret; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Load the admin menu for the module |
||
248 | */ |
||
249 | public function loadAdminMenu() |
||
250 | { |
||
251 | $adminmenu = []; |
||
252 | if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) { |
||
253 | include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'); |
||
254 | } |
||
255 | $this->adminmenu = & $adminmenu; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Get the admin menu for the module |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | public function &getAdminMenu() |
||
264 | { |
||
265 | if (!isset($this->adminmenu)) { |
||
266 | $this->loadAdminMenu(); |
||
267 | } |
||
268 | |||
269 | return $this->adminmenu; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Load the module info for this module |
||
274 | * |
||
275 | * @param string $dirname Module directory |
||
276 | * @param bool $verbose Give an error on fail? |
||
277 | * |
||
278 | * @return bool true if loaded |
||
279 | */ |
||
280 | public function loadInfo($dirname, $verbose = true) |
||
281 | { |
||
282 | static $modVersions; |
||
283 | $dirname = basename($dirname); |
||
284 | if (isset($modVersions[$dirname])) { |
||
285 | $this->modinfo = $modVersions[$dirname]; |
||
286 | |||
287 | return true; |
||
288 | } |
||
289 | global $xoopsConfig; |
||
290 | if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) { |
||
291 | include_once $file; |
||
292 | } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) { |
||
293 | include_once $file; |
||
294 | } |
||
295 | |||
296 | if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) { |
||
297 | if (false !== (bool) $verbose) { |
||
298 | echo "Module File for $dirname Not Found!"; |
||
299 | } |
||
300 | |||
301 | return false; |
||
302 | } |
||
303 | include $file; |
||
304 | $modVersions[$dirname] = $modversion; |
||
305 | $this->modinfo = $modVersions[$dirname]; |
||
306 | |||
307 | return true; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Search contents within a module |
||
312 | * |
||
313 | * @param string $term |
||
314 | * @param string $andor 'AND' or 'OR' |
||
315 | * @param integer $limit |
||
316 | * @param integer $offset |
||
317 | * @param integer $userid |
||
318 | * @return mixed Search result. |
||
319 | */ |
||
320 | public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0) |
||
321 | { |
||
322 | if ($this->getVar('hassearch') != 1) { |
||
323 | return false; |
||
324 | } |
||
325 | $search = & $this->getInfo('search'); |
||
326 | if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') { |
||
327 | return false; |
||
328 | } |
||
329 | if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) { |
||
330 | include_once $file; |
||
331 | } else { |
||
332 | return false; |
||
333 | } |
||
334 | if (function_exists($search['func'])) { |
||
335 | $func = $search['func']; |
||
336 | |||
337 | return $func($term, $andor, $limit, $offset, $userid); |
||
338 | } |
||
339 | |||
340 | return false; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Returns Class Base Variable mid |
||
345 | * @param string $format |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function id($format = 'N') |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Returns Class Base Variable mid |
||
355 | * @param string $format |
||
356 | * @return mixed |
||
357 | */ |
||
358 | public function mid($format = '') |
||
359 | { |
||
360 | return $this->getVar('mid', $format); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Returns Class Base Variable name |
||
365 | * @param string $format |
||
366 | * @return mixed |
||
367 | */ |
||
368 | public function name($format = '') |
||
369 | { |
||
370 | return $this->getVar('name', $format); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Returns Class Base Variable version |
||
375 | * @param string $format |
||
376 | * @return mixed |
||
377 | */ |
||
378 | public function version($format = '') |
||
379 | { |
||
380 | return $this->getVar('version', $format); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Returns Class Base Variable last_update |
||
385 | * @param string $format |
||
386 | * @return mixed |
||
387 | */ |
||
388 | public function last_update($format = '') |
||
389 | { |
||
390 | return $this->getVar('last_update', $format); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns Class Base Variable weight |
||
395 | * @param string $format |
||
396 | * @return mixed |
||
397 | */ |
||
398 | public function weight($format = '') |
||
399 | { |
||
400 | return $this->getVar('weight', $format); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Returns Class Base Variable isactive |
||
405 | * @param string $format |
||
406 | * @return mixed |
||
407 | */ |
||
408 | public function isactive($format = '') |
||
409 | { |
||
410 | return $this->getVar('isactive', $format); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns Class Base Variable dirname |
||
415 | * @param string $format |
||
416 | * @return mixed |
||
417 | */ |
||
418 | public function dirname($format = '') |
||
419 | { |
||
420 | return $this->getVar('dirname', $format); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Returns Class Base Variable hasmain |
||
425 | * @param string $format |
||
426 | * @return mixed |
||
427 | */ |
||
428 | public function hasmain($format = '') |
||
429 | { |
||
430 | return $this->getVar('hasmain', $format); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Returns Class Base Variable hasadmin |
||
435 | * @param string $format |
||
436 | * @return mixed |
||
437 | */ |
||
438 | public function hasadmin($format = '') |
||
439 | { |
||
440 | return $this->getVar('hasadmin', $format); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Returns Class Base Variable hassearch |
||
445 | * @param string $format |
||
446 | * @return mixed |
||
447 | */ |
||
448 | public function hassearch($format = '') |
||
449 | { |
||
450 | return $this->getVar('hassearch', $format); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Returns Class Base Variable hasconfig |
||
455 | * @param string $format |
||
456 | * @return mixed |
||
457 | */ |
||
458 | public function hasconfig($format = '') |
||
459 | { |
||
460 | return $this->getVar('hasconfig', $format); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Returns Class Base Variable hascomments |
||
465 | * @param string $format |
||
466 | * @return mixed |
||
467 | */ |
||
468 | public function hascomments($format = '') |
||
469 | { |
||
470 | return $this->getVar('hascomments', $format); |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Returns Class Base Variable hasnotification |
||
475 | * @param string $format |
||
476 | * @return mixed |
||
477 | */ |
||
478 | public function hasnotification($format = '') |
||
479 | { |
||
480 | return $this->getVar('hasnotification', $format); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param $dirname |
||
485 | * |
||
486 | * @return mixed |
||
487 | */ |
||
488 | public static function getByDirname($dirname) |
||
489 | { |
||
490 | /** @var XoopsModuleHandler $modhandler */ |
||
491 | $modhandler = xoops_getHandler('module'); |
||
492 | $inst = $modhandler->getByDirname($dirname); |
||
493 | |||
494 | return $inst; |
||
495 | } |
||
496 | |||
497 | |||
498 | /** |
||
499 | * Returns Class Base Variable icon |
||
500 | * @param string $format |
||
501 | * @return mixed |
||
502 | */ |
||
503 | public function icon($format = '') |
||
504 | { |
||
505 | return $this->getVar('icon', $format); |
||
506 | } |
||
507 | |||
508 | |||
509 | ##################### Deprecated Methods ###################### |
||
510 | |||
511 | /**#@+ |
||
512 | * @deprecated |
||
513 | */ |
||
514 | public function checkAccess() |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * @param string $type |
||
523 | * |
||
524 | * @return bool |
||
525 | */ |
||
526 | public function loadLanguage($type = 'main') |
||
527 | { |
||
528 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
529 | |||
530 | return false; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @return bool |
||
535 | */ |
||
536 | public function loadErrorMessages() |
||
537 | { |
||
538 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
539 | |||
540 | return false; |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * @return bool |
||
545 | */ |
||
546 | public function getCurrentPage() |
||
547 | { |
||
548 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
549 | |||
550 | return false; |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * @param array $admingroups |
||
555 | * @param array $accessgroups |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | public function install($admingroups = [], $accessgroups = []) |
||
560 | { |
||
561 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
562 | |||
563 | return false; |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * @return bool |
||
568 | */ |
||
569 | public function update() |
||
570 | { |
||
571 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
572 | |||
573 | return false; |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * @return bool |
||
578 | */ |
||
579 | public function insert() |
||
580 | { |
||
581 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
582 | |||
583 | return false; |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * @return bool |
||
588 | */ |
||
589 | public function executeSQL() |
||
590 | { |
||
591 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
592 | |||
593 | return false; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @return bool |
||
598 | */ |
||
599 | public function insertTemplates() |
||
600 | { |
||
601 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
602 | |||
603 | return false; |
||
604 | } |
||
605 | |||
606 | /** |
||
607 | * @param $template |
||
608 | * @param bool $block |
||
609 | * |
||
610 | * @return bool |
||
611 | */ |
||
612 | public function gettemplate($template, $block = false) |
||
613 | { |
||
614 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
615 | |||
616 | return false; |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * @return bool |
||
621 | */ |
||
622 | public function insertBlocks() |
||
623 | { |
||
624 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
625 | |||
626 | return false; |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * @return bool |
||
631 | */ |
||
632 | public function insertConfigCategories() |
||
633 | { |
||
634 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
635 | |||
636 | return false; |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * @return bool |
||
641 | */ |
||
642 | public function insertConfig() |
||
643 | { |
||
644 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
645 | |||
646 | return false; |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function insertProfileFields() |
||
653 | { |
||
654 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
655 | |||
656 | return false; |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * @param $type |
||
661 | * @param int $state |
||
662 | * |
||
663 | * @return bool |
||
664 | */ |
||
665 | public function executeScript($type, $state = 2) |
||
670 | } |
||
671 | |||
672 | /** |
||
673 | * @param $groups |
||
674 | * @param $type |
||
675 | * |
||
676 | * @return bool |
||
677 | */ |
||
678 | public function insertGroupPermissions($groups, $type) |
||
683 | } |
||
684 | /**#@-*/ |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * XOOPS module handler class. |
||
689 | * |
||
690 | * This class is responsible for providing data access mechanisms to the data source |
||
691 | * of XOOPS module class objects. |
||
692 | * |
||
693 | * @package kernel |
||
694 | * @author Kazumi Ono <[email protected]> |
||
695 | * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org |
||
696 | * |
||
697 | * @todo Why is this not a XoopsPersistableObjectHandler? |
||
698 | */ |
||
699 | class XoopsModuleHandler extends XoopsObjectHandler |
||
700 | { |
||
701 | /** |
||
702 | * holds an array of cached module references, indexed by module id |
||
703 | * |
||
704 | * @var array |
||
705 | * @access private |
||
706 | */ |
||
707 | public $_cachedModule_mid = []; |
||
708 | |||
709 | /** |
||
1044 |