Total Complexity | 87 |
Total Lines | 657 |
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 array |
||
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 | 'id' => $submenu['id']?? '', |
||
235 | 'name' => $submenu['name'], |
||
236 | 'url' => $submenu['url'], |
||
237 | 'icon' => $submenu['icon']?? '', |
||
238 | ]; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | return $ret; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Load the admin menu for the module |
||
247 | */ |
||
248 | public function loadAdminMenu() |
||
249 | { |
||
250 | $adminmenu = []; |
||
251 | if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) { |
||
252 | include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'); |
||
253 | } |
||
254 | $this->adminmenu = & $adminmenu; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get the admin menu for the module |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | public function &getAdminMenu() |
||
263 | { |
||
264 | if (!isset($this->adminmenu)) { |
||
265 | $this->loadAdminMenu(); |
||
266 | } |
||
267 | |||
268 | return $this->adminmenu; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Load the module info for this module |
||
273 | * |
||
274 | * @param string $dirname Module directory |
||
275 | * @param bool $verbose Give an error on fail? |
||
276 | * |
||
277 | * @return bool true if loaded |
||
278 | */ |
||
279 | public function loadInfo($dirname, $verbose = true) |
||
280 | { |
||
281 | static $modVersions; |
||
282 | $dirname = basename($dirname); |
||
283 | if (isset($modVersions[$dirname])) { |
||
284 | $this->modinfo = $modVersions[$dirname]; |
||
285 | |||
286 | return true; |
||
287 | } |
||
288 | global $xoopsConfig; |
||
289 | if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) { |
||
290 | include_once $file; |
||
291 | } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) { |
||
292 | include_once $file; |
||
293 | } |
||
294 | |||
295 | if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) { |
||
296 | if (false !== (bool) $verbose) { |
||
297 | echo "Module File for $dirname Not Found!"; |
||
298 | } |
||
299 | |||
300 | return false; |
||
301 | } |
||
302 | include $file; |
||
303 | $modVersions[$dirname] = $modversion; |
||
304 | $this->modinfo = $modVersions[$dirname]; |
||
305 | |||
306 | return true; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Search contents within a module |
||
311 | * |
||
312 | * @param string $term |
||
313 | * @param string $andor 'AND' or 'OR' |
||
314 | * @param integer $limit |
||
315 | * @param integer $offset |
||
316 | * @param integer $userid |
||
317 | * @return mixed Search result. |
||
318 | */ |
||
319 | public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0) |
||
320 | { |
||
321 | if ($this->getVar('hassearch') != 1) { |
||
322 | return false; |
||
323 | } |
||
324 | $search = & $this->getInfo('search'); |
||
325 | if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') { |
||
326 | return false; |
||
327 | } |
||
328 | if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) { |
||
329 | include_once $file; |
||
330 | } else { |
||
331 | return false; |
||
332 | } |
||
333 | if (function_exists($search['func'])) { |
||
334 | $func = $search['func']; |
||
335 | |||
336 | return $func($term, $andor, $limit, $offset, $userid); |
||
337 | } |
||
338 | |||
339 | return false; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Returns Class Base Variable mid |
||
344 | * @param string $format |
||
345 | * @return mixed |
||
346 | */ |
||
347 | public function id($format = 'N') |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Returns Class Base Variable mid |
||
354 | * @param string $format |
||
355 | * @return mixed |
||
356 | */ |
||
357 | public function mid($format = '') |
||
358 | { |
||
359 | return $this->getVar('mid', $format); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Returns Class Base Variable name |
||
364 | * @param string $format |
||
365 | * @return mixed |
||
366 | */ |
||
367 | public function name($format = '') |
||
368 | { |
||
369 | return $this->getVar('name', $format); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Returns Class Base Variable version |
||
374 | * @param string $format |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function version($format = '') |
||
378 | { |
||
379 | return $this->getVar('version', $format); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Returns Class Base Variable last_update |
||
384 | * @param string $format |
||
385 | * @return mixed |
||
386 | */ |
||
387 | public function last_update($format = '') |
||
388 | { |
||
389 | return $this->getVar('last_update', $format); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Returns Class Base Variable weight |
||
394 | * @param string $format |
||
395 | * @return mixed |
||
396 | */ |
||
397 | public function weight($format = '') |
||
398 | { |
||
399 | return $this->getVar('weight', $format); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Returns Class Base Variable isactive |
||
404 | * @param string $format |
||
405 | * @return mixed |
||
406 | */ |
||
407 | public function isactive($format = '') |
||
408 | { |
||
409 | return $this->getVar('isactive', $format); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Returns Class Base Variable dirname |
||
414 | * @param string $format |
||
415 | * @return mixed |
||
416 | */ |
||
417 | public function dirname($format = '') |
||
418 | { |
||
419 | return $this->getVar('dirname', $format); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Returns Class Base Variable hasmain |
||
424 | * @param string $format |
||
425 | * @return mixed |
||
426 | */ |
||
427 | public function hasmain($format = '') |
||
428 | { |
||
429 | return $this->getVar('hasmain', $format); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Returns Class Base Variable hasadmin |
||
434 | * @param string $format |
||
435 | * @return mixed |
||
436 | */ |
||
437 | public function hasadmin($format = '') |
||
438 | { |
||
439 | return $this->getVar('hasadmin', $format); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Returns Class Base Variable hassearch |
||
444 | * @param string $format |
||
445 | * @return mixed |
||
446 | */ |
||
447 | public function hassearch($format = '') |
||
448 | { |
||
449 | return $this->getVar('hassearch', $format); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Returns Class Base Variable hasconfig |
||
454 | * @param string $format |
||
455 | * @return mixed |
||
456 | */ |
||
457 | public function hasconfig($format = '') |
||
458 | { |
||
459 | return $this->getVar('hasconfig', $format); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Returns Class Base Variable hascomments |
||
464 | * @param string $format |
||
465 | * @return mixed |
||
466 | */ |
||
467 | public function hascomments($format = '') |
||
468 | { |
||
469 | return $this->getVar('hascomments', $format); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Returns Class Base Variable hasnotification |
||
474 | * @param string $format |
||
475 | * @return mixed |
||
476 | */ |
||
477 | public function hasnotification($format = '') |
||
478 | { |
||
479 | return $this->getVar('hasnotification', $format); |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * @param $dirname |
||
484 | * |
||
485 | * @return mixed |
||
486 | */ |
||
487 | public static function getByDirname($dirname) |
||
488 | { |
||
489 | /** @var XoopsModuleHandler $modhandler */ |
||
490 | $modhandler = xoops_getHandler('module'); |
||
491 | $inst = $modhandler->getByDirname($dirname); |
||
492 | |||
493 | return $inst; |
||
494 | } |
||
495 | |||
496 | |||
497 | /** |
||
498 | * Returns Class Base Variable icon |
||
499 | * @param string $format |
||
500 | * @return mixed |
||
501 | */ |
||
502 | public function icon($format = '') |
||
503 | { |
||
504 | return $this->getVar('icon', $format); |
||
505 | } |
||
506 | |||
507 | |||
508 | ##################### Deprecated Methods ###################### |
||
509 | |||
510 | /**#@+ |
||
511 | * @deprecated |
||
512 | */ |
||
513 | public function checkAccess() |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * @param string $type |
||
522 | * |
||
523 | * @return bool |
||
524 | */ |
||
525 | public function loadLanguage($type = 'main') |
||
526 | { |
||
527 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
528 | |||
529 | return false; |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * @return bool |
||
534 | */ |
||
535 | public function loadErrorMessages() |
||
536 | { |
||
537 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
538 | |||
539 | return false; |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * @return bool |
||
544 | */ |
||
545 | public function getCurrentPage() |
||
546 | { |
||
547 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
548 | |||
549 | return false; |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * @param array $admingroups |
||
554 | * @param array $accessgroups |
||
555 | * |
||
556 | * @return bool |
||
557 | */ |
||
558 | public function install($admingroups = [], $accessgroups = []) |
||
559 | { |
||
560 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
561 | |||
562 | return false; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * @return bool |
||
567 | */ |
||
568 | public function update() |
||
569 | { |
||
570 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
571 | |||
572 | return false; |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * @return bool |
||
577 | */ |
||
578 | public function insert() |
||
579 | { |
||
580 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
581 | |||
582 | return false; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * @return bool |
||
587 | */ |
||
588 | public function executeSQL() |
||
589 | { |
||
590 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
591 | |||
592 | return false; |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * @return bool |
||
597 | */ |
||
598 | public function insertTemplates() |
||
599 | { |
||
600 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
601 | |||
602 | return false; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * @param $template |
||
607 | * @param bool $block |
||
608 | * |
||
609 | * @return bool |
||
610 | */ |
||
611 | public function gettemplate($template, $block = false) |
||
612 | { |
||
613 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
614 | |||
615 | return false; |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * @return bool |
||
620 | */ |
||
621 | public function insertBlocks() |
||
622 | { |
||
623 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
624 | |||
625 | return false; |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * @return bool |
||
630 | */ |
||
631 | public function insertConfigCategories() |
||
632 | { |
||
633 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
634 | |||
635 | return false; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @return bool |
||
640 | */ |
||
641 | public function insertConfig() |
||
642 | { |
||
643 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
644 | |||
645 | return false; |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * @return bool |
||
650 | */ |
||
651 | public function insertProfileFields() |
||
652 | { |
||
653 | $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated'); |
||
654 | |||
655 | return false; |
||
656 | } |
||
657 | |||
658 | /** |
||
659 | * @param $type |
||
660 | * @param int $state |
||
661 | * |
||
662 | * @return bool |
||
663 | */ |
||
664 | public function executeScript($type, $state = 2) |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * @param $groups |
||
673 | * @param $type |
||
674 | * |
||
675 | * @return bool |
||
676 | */ |
||
677 | public function insertGroupPermissions($groups, $type) |
||
682 | } |
||
683 | /**#@-*/ |
||
684 | } |
||
685 | |||
686 | /** |
||
687 | * XOOPS module handler class. |
||
688 | * |
||
689 | * This class is responsible for providing data access mechanisms to the data source |
||
690 | * of XOOPS module class objects. |
||
691 | * |
||
692 | * @package kernel |
||
693 | * @author Kazumi Ono <[email protected]> |
||
694 | * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org |
||
695 | * |
||
696 | * @todo Why is this not a XoopsPersistableObjectHandler? |
||
697 | */ |
||
698 | class XoopsModuleHandler extends XoopsObjectHandler |
||
699 | { |
||
700 | /** |
||
701 | * holds an array of cached module references, indexed by module id |
||
702 | * |
||
703 | * @var array |
||
704 | * @access private |
||
705 | */ |
||
706 | public $_cachedModule_mid = []; |
||
707 | |||
708 | /** |
||
1043 |