Passed
Push — master ( 5c9a79...879726 )
by Michael
02:51 queued 01:58
created

PluginItem::getModuleMenus()   B

Complexity

Conditions 11
Paths 42

Size

Total Lines 68
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 43
nc 42
nop 2
dl 0
loc 68
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace XoopsModules\Mymenus\Plugins\Dynamic;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright       XOOPS Project (https://xoops.org)
15
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
16
 * @package         Mymenus
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 */
20
21
use XoopsModules\Mymenus;
22
23
defined('XOOPS_ROOT_PATH') || die('Restricted access');
24
25
/**
26
 * Class PluginItem
27
 */
28
class PluginItem extends Mymenus\PluginItem
29
{
30
    public static function eventEnd()
31
    {
32
        $newmenus = [];
33
        $registry = Mymenus\Registry::getInstance();
34
        $menus    = $registry->getEntry('menus');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $menus is correct as $registry->getEntry('menus') targeting XoopsModules\Mymenus\Registry::getEntry() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

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.

Loading history...
35
        foreach ($menus as $menu) {
0 ignored issues
show
Bug introduced by
The expression $menus of type null is not traversable.
Loading history...
36
            if (!preg_match('/{(MODULE\|.*)}/i', $menu['title'], $reg)) {
37
                $newmenus[] = $menu;
38
                continue;
39
            }
40
            $result      = array_map('mb_strtolower', explode('|', $reg[1]));
41
            $moduleMenus = self::getModuleMenus($result[1], $menu['pid']);
42
            foreach ($moduleMenus as $mMenu) {
43
                $newmenus[] = $mMenu;
44
            }
45
        }
46
        $registry->setEntry('menus', $newmenus);
47
    }
48
49
    /**
50
     * @param $module
51
     * @param $pid
52
     *
53
     * @return array
54
     */
55
    protected static function getModuleMenus($module, $pid)
56
    {
57
        global $xoopsModule;
58
        static $id = -1;
59
        /** @var \XoopsModules\Mymenus\Helper $helper */
60
        $helper = \XoopsModules\Mymenus\Helper::getInstance();
61
62
63
        $ret = [];
64
        //Sanitizing $module
65
        if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $module)) {
66
            return $ret;
67
        }
68
69
        $path = "modules/{$module}";
70
        $file = $GLOBALS['xoops']->path("{$path}/xoops_version.php");
71
72
        if (!file_exists($file)) {
73
            return $ret;
74
        }
75
        $helper->loadLanguage('modinfo');
76
77
78
        $overwrite = false;
79
        if (true === $force) {  //can set to false for debug
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $force seems to be never defined.
Loading history...
80
            if (!($xoopsModule instanceof \XoopsModule) || ($xoopsModule->getVar('dirname') != $module)) {
81
                // @TODO: check the following 2 statements, they're basically just assigns - is this intended?
82
                $_xoopsModule       = ($xoopsModule instanceof \XoopsModule) ? $xoopsModule : $xoopsModule;
83
                $_xoopsModuleConfig = is_object($xoopsModuleConfig) ? $xoopsModuleConfig : $xoopsModuleConfig;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $xoopsModuleConfig does not exist. Did you maybe mean $_xoopsModuleConfig?
Loading history...
84
                /** @var \XoopsModuleHandler $moduleHandler */
85
                $moduleHandler          = xoops_getHandler('module');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
                $moduleHandler          = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
86
                $xoopsModule            = $moduleHandler->getByDirname($module);
87
                $GLOBALS['xoopsModule'] = $xoopsModule;
88
                if ($xoopsModule instanceof \XoopsModule) {
89
                    /** @var \XoopsConfigHandler $configHandler */
90
                    $configHandler                = xoops_getHandler('config');
91
                    $xoopsModuleConfig            = $configHandler->getConfigsByCat(0, $xoopsModule->getVar('mid'));
92
                    $GLOBALS['xoopsModuleConfig'] = $xoopsModuleConfig;
93
                }
94
                $overwrite = true;
95
            }
96
        }
97
        $modversion['sub'] = [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$modversion was never initialized. Although not strictly required by PHP, it is generally a good practice to add $modversion = array(); before regardless.
Loading history...
98
        require $file;
99
100
        /** @var  \XoopsModules\Mymenus\LinksHandler $linksHandler */
101
        $linksHandler = $helper->getHandler('Links');
102
        foreach ($modversion['sub'] as $links) {
103
            $obj = $linksHandler->create();
104
            $obj->setVars([
105
                              'title'     => $links['name'],
106
                              'alt_title' => $links['name'],
107
                              'link'      => $GLOBALS['xoops']->url("{$path}/{$links['url']}"),
108
                              'id'        => $id,
109
                              'pid'       => (int)$pid
110
                          ]);
111
            $ret[] = $obj->getValues();
112
            $id--;
113
        }
114
115
        if ($overwrite) {
116
            $xoopsModule                  = $_xoopsModule;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_xoopsModule does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
            $GLOBALS['xoopsModule']       = $xoopsModule;
118
            $xoopsModuleConfig            = $_xoopsModuleConfig;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_xoopsModuleConfig does not seem to be defined for all execution paths leading up to this point.
Loading history...
119
            $GLOBALS['xoopsModuleConfig'] = $xoopsModuleConfig;
120
        }
121
122
        return $ret;
123
    }
124
}
125