block_functions.php ➔ b_altsys_admin_menu_show()   F
last analyzed

Complexity

Conditions 29
Paths 2204

Size

Total Lines 169

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 29
nc 2204
nop 1
dl 0
loc 169
rs 0
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
2
3
require_once dirname(__DIR__) . '/include/altsys_functions.php';
4
5
/**
6
 * @param $options
7
 * @return mixed
8
 */
9
function b_altsys_admin_menu_show($options)
10
{
11
    global $xoopsUser;
12
13
    $mydirname = empty($options[0]) ? 'altsys' : $options[0];
14
15
    $this_template = empty($options[1]) ? 'db:' . $mydirname . '_block_admin_menu.tpl' : trim($options[1]);
16
17
    if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) {
18
        die('Invalid mydirname');
19
    }
20
21
    if (!is_object(@$xoopsUser)) {
22
        return [];
23
    }
24
25
    // coretype
26
27
    $coretype = altsys_get_core_type();
28
29
    // mid_selected
30
31
    if (is_object(@$GLOBALS['xoopsModule'])) {
32
        $mid_selected = $GLOBALS['xoopsModule']->getVar('mid');
33
34
        // for system->preferences
35
36
        if (1 == $mid_selected && 'preferences' == @$_GET['fct'] && 'showmod' == @$_GET['op'] && !empty($_GET['mod'])) {
37
            $mid_selected = \Xmf\Request::getInt('mod', 0, 'GET');
38
        }
39
    } else {
40
        $mid_selected = 0;
41
    }
42
43
    $db = XoopsDatabaseFactory::getDatabaseConnection();
44
45
    (method_exists('MyTextSanitizer', 'sGetInstance') and $myts = MyTextSanitizer::sGetInstance()) || $myts = MyTextSanitizer::getInstance();
46
47
    $module_handler = xoops_getHandler('module');
48
49
    $current_module = $module_handler->getByDirname($mydirname);
50
51
    $configHandler = xoops_getHandler('config');
52
53
    $current_configs = $configHandler->getConfigList($current_module->mid());
54
55
    $moduleperm_handler = xoops_getHandler('groupperm');
56
57
    $admin_mids = $moduleperm_handler->getItemIds('module_admin', $xoopsUser->getGroups());
58
59
    $modules = $module_handler->getObjects(new Criteria('mid', '(' . implode(',', $admin_mids) . ')', 'IN'), true);
60
61
    $block = [
62
        'mydirname' => $mydirname,
63
        'mod_url' => XOOPS_URL . '/modules/' . $mydirname,
64
        'mod_imageurl' => XOOPS_URL . '/modules/' . $mydirname . '/' . $current_configs['images_dir'],
65
        'mod_config' => $current_configs,
66
    ];
67
68
    foreach ($modules as $mod) {
69
        $mid = (int)$mod->getVar('mid');
70
71
        $dirname = $mod->getVar('dirname');
72
73
        $modinfo = $mod->getInfo();
74
75
        $submenus4assign = [];
76
77
        $adminmenu = [];
78
79
        $adminmenu4altsys = [];
80
81
        unset($adminmenu_use_altsys);
82
83
        @include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/' . @$modinfo['adminmenu'];
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
84
85
        // from admin_menu.php etc.
86
87
        $adminmenu = array_merge($adminmenu, $adminmenu4altsys);
88
89
        foreach ($adminmenu as $sub) {
90
            $link = empty($sub['altsys_link']) ? $sub['link'] : $sub['altsys_link'];
91
92
            if (isset($sub['show']) && false === $sub['show']) {
93
                continue;
94
            }
95
96
            $submenus4assign[] = [
97
                'title' => htmlspecialchars($sub['title']),
98
                'url' => XOOPS_URL . '/modules/' . $dirname . '/' . htmlspecialchars($link, ENT_QUOTES),
99
            ];
100
        }
101
102
        // for modules overriding Module.class.php (eg. Analyzer for XC)
103
104
        if (empty($submenus4assign) && defined('XOOPS_CUBE_LEGACY') && !empty($modinfo['cube_style'])) {
105
            $module_handler = xoops_getHandler('module');
106
107
            $module = $module_handler->get($mid);
108
109
            $moduleObj = Legacy_Utils::createModule($module);
110
111
            $modinfo['adminindex'] = $moduleObj->getAdminIndex();
112
113
            $modinfo['adminindex_absolute'] = true;
114
115
            foreach ($moduleObj->getAdminMenu() as $sub) {
116
                if (false === @$sub['show']) {
117
                    continue;
118
                }
119
120
                $submenus4assign[] = [
121
                    'title' => htmlspecialchars($sub['title']),
122
                    'url' => 0 === strncmp($sub['link'], 'http', 4) ? htmlspecialchars($sub['link'], ENT_QUOTES) : XOOPS_URL . '/modules/' . $dirname . '/' . htmlspecialchars($sub['link'], ENT_QUOTES),
123
                ];
124
            }
125
        } elseif (empty($adminmenu4altsys)) {
126
            // add preferences
127
128
            if ($mod->getVar('hasconfig') && !in_array($mod->getVar('dirname'), ['system', 'legacy'])) {
129
                $submenus4assign[] = [
130
                    'title' => _PREFERENCES,
131
                    'url' => htmlspecialchars(altsys_get_link2modpreferences($mid, $coretype), ENT_QUOTES),
132
                ];
133
            }
134
135
            // add help
136
137
            if (defined('XOOPS_CUBE_LEGACY') && !empty($modinfo['help'])) {
138
                $submenus4assign[] = [
139
                    'title' => _HELP,
140
                    'url' => XOOPS_URL . '/modules/legacy/admin/index.php?action=Help&amp;dirname=' . $dirname,
141
                ];
142
            }
143
        }
144
145
        $module4assign = [
146
            'mid' => $mid,
147
            'dirname' => $dirname,
148
            'name' => $mod->getVar('name'),
149
            'version_in_db' => sprintf('%.2f', $mod->getVar('version') / 100.0),
150
            'version_in_file' => sprintf('%.2f', $modinfo['version']),
151
            'description' => htmlspecialchars(@$modinfo['description'], ENT_QUOTES),
152
            'image' => htmlspecialchars($modinfo['image'], ENT_QUOTES),
153
            'isactive' => $mod->getVar('isactive'),
154
            'hasmain' => $mod->getVar('hasmain'),
155
            'hasadmin' => $mod->getVar('hasadmin'),
156
            'hasconfig' => $mod->getVar('hasconfig'),
157
            'weight' => $mod->getVar('weight'),
158
            'adminindex' => htmlspecialchars(@$modinfo['adminindex'], ENT_QUOTES),
159
            'adminindex_absolute' => @$modinfo['adminindex_absolute'],
160
            'submenu' => $submenus4assign,
161
            'selected' => $mid == $mid_selected,
162
            'dot_suffix' => $mid == $mid_selected ? 'selected_opened' : 'closed',
163
        ];
164
165
        $block['modules'][] = $module4assign;
166
    }
167
168
    require_once XOOPS_TRUST_PATH . '/libs/altsys/class/D3Tpl.class.php';
169
170
    $tpl = new D3Tpl();
171
172
    $tpl->assign('block', $block);
173
174
    $ret['content'] = $tpl->fetch($this_template);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
175
176
    return $ret;
177
}
178
179
/**
180
 * @param $options
181
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
182
 */
183
function b_altsys_admin_menu_edit($options)
184
{
185
    $mydirname = empty($options[0]) ? 'd3forum' : $options[0];
186
187
    $this_template = empty($options[1]) ? 'db:' . $mydirname . '_block_admin_menu.tpl' : trim($options[1]);
188
189
    if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) {
190
        die('Invalid mydirname');
191
    }
192
193
    $form = "
194
        <input type='hidden' name='options[0]' value='$mydirname' />
195
        <label for='this_template'>" . _MB_ALTSYS_THISTEMPLATE . "</label>&nbsp;:
196
        <input type='text' size='60' name='options[1]' id='this_template' value='" . htmlspecialchars($this_template, ENT_QUOTES) . "' />
197
        <br />
198
    \n";
199
200
    return $form;
201
}
202