mymenus_block_show()   F
last analyzed

Complexity

Conditions 17
Paths 771

Size

Total Lines 134
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 75
nc 771
nop 1
dl 0
loc 134
rs 1.368
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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       {@link http://sourceforge.net/projects/xoops/ The XOOPS Project}
14
 * @license         {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
15
 * @package         Mymenus
16
 * @since           1.0
17
 * @author          trabis <[email protected]>
18
 */
19
20
use Xmf\Request;
0 ignored issues
show
Bug introduced by
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use XoopsModules\Mymenus;
22
use XoopsModules\Mymenus\Utility;
23
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
require  dirname(__DIR__) . '/include/common.php';
27
28
/**
29
 * @param array $options array(0 => menu, 1 => moduleSkin, 2 => useThemeSkin, 3 => displayMethod, 4 => unique_id, 5 => themeSkin)
30
 *
31
 * @return array|bool
32
 */
33
function mymenus_block_show($options)
34
{
35
    global $xoopsTpl, $xoopsLogger;
36
    /** @var \XoopsModules\Mymenus\Helper $helper */
37
    $helper = \XoopsModules\Mymenus\Helper::getInstance();
38
39
    $block = [];
40
    $xoopsLogger->startTime('My Menus Block');
41
    $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
42
43
        $registry = Mymenus\Registry::getInstance();
44
    $plugin   = Mymenus\Plugin::getInstance();
45
    $plugin->triggerEvent('Boot');
46
47
    $mid = $options[0];
48
49
    $linksCriteria = new \CriteriaCompo(new \Criteria('mid', $mid));
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
    $linksCriteria->setSort('weight');
51
    $linksCriteria->setOrder('ASC');
52
    //get menu links as an array with ids as keys
53
    $linksArray = $helper->getHandler('Links')->getAll($linksCriteria, null, false, false); // as array
54
    unset($linksCriteria);
55
56
    foreach ($linksArray as $key => $links) {
57
        $registry->setEntry('menu', $links);
58
        $registry->setEntry('has_access', 'yes');
59
        $plugin->triggerEvent('HasAccess');
60
        if ('no' === $registry->getEntry('has_access')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $registry->getEntry('has_access') 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 used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
            unset($linksArray[$key]);
62
        }
63
    }
64
65
    $linksCount = count($linksArray);
66
    if (0 === $linksCount) {
67
        return $block;
68
    }
69
70
    foreach ($linksArray as $key => $links) {
71
        $registry->setEntry('link_array', $links);
72
        $plugin->triggerEvent('TitleDecoration');
73
        $plugin->triggerEvent('AlttitleDecoration');
74
        $plugin->triggerEvent('LinkDecoration');
75
        $plugin->triggerEvent('ImageDecoration');
76
        $linksArray[$key] = $registry->getEntry('link_array');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $linksArray[$key] is correct as $registry->getEntry('link_array') 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...
77
    }
78
    $registry->setEntry('menus', $linksArray);
79
    $plugin->triggerEvent('End');
80
    $linksArray = $registry->getEntry('menus');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $linksArray 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...
81
82
    $menuBuilder = new Mymenus\Builder($linksArray);
83
    $block       = $menuBuilder->render();
84
85
    /*--------------------------------------------------------------*/
86
    // Default files to load
87
    $cssArray = [];
88
    $jsArray  = [];
89
90
    // Get extra files from skins
91
    $skinInfo = Utility::getSkinInfo($options[1], $options[2], isset($options[5]) ? $options[5] : '');
92
93
    //
94
    if (isset($skinInfo['css'])) {
95
        $cssArray = array_merge($cssArray, $skinInfo['css']);
96
    }
97
    if (isset($skinInfo['js'])) {
98
        $jsArray = array_merge($jsArray, $skinInfo['js']);
99
    }
100
    //
101
    if ('xoopstpl' === $helper->getConfig('assign_method')) {
102
        $tpl_vars = '';
103
        foreach ($cssArray as $file) {
104
            $tpl_vars .= "\n<link rel='stylesheet' type='text/css' media='all' href='{$file}'>";
105
        }
106
        foreach ($jsArray as $file) {
107
            $tpl_vars .= "\n<script type='text/javascript' src='{$file}'></script>";
108
        }
109
        if (isset($skinInfo['header'])) {
110
            $tpl_vars .= "\n{$skinInfo['header']}";
111
        }
112
        $GLOBALS['xoopsTpl']->assign('xoops_module_header', $tpl_vars . @$GLOBALS['xoopsTpl']->get_template_vars('xoops_module_header'));
113
    } else {
114
        foreach ($cssArray as $file) {
115
            $GLOBALS['xoTheme']->addStylesheet($file);
116
        }
117
        foreach ($jsArray as $file) {
118
            $GLOBALS['xoTheme']->addScript($file);
119
        }
120
        if (isset($skinInfo['header'])) {
121
            $GLOBALS['xoopsTpl']->assign('xoops_footer', @$GLOBALS['xoopsTpl']->get_template_vars('xoops_footer') . "\n" . $skinInfo['header']);
122
        }
123
    }
124
    //
125
    $blockTpl = new \XoopsTpl();
0 ignored issues
show
Bug introduced by
The type XoopsTpl was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
126
    $blockTpl->assign([
127
                          'block'     => $block,
128
                          'config'    => $skinInfo['config'],
129
                          'skinurl'   => $skinInfo['url'],
130
                          'skinpath'  => $skinInfo['path'],
131
                          'xlanguage' => xoops_isActiveModule('xlanguage') ? true : false // xLanguage check
0 ignored issues
show
Bug introduced by
The function xoops_isActiveModule 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

131
                          'xlanguage' => /** @scrutinizer ignore-call */ xoops_isActiveModule('xlanguage') ? true : false // xLanguage check
Loading history...
132
                      ]);
133
    // Assign ul class
134
    $menusObj = $helper->getHandler('Menus')->get($mid);
135
    $blockTpl->assign('menucss', $menusObj->getVar('css'));
136
    /*
137
        $menuCss      = '';
138
        $menusHandler = $helper->getHandler('menus', 'mymenus');
139
        $menuCriteria = new \CriteriaCompo(new \Criteria('id', $mid));
140
        $menuArray    = $menusHandler->getAll($menuCriteria, null, false, false);
141
142
        if (is_array($menuArray) && (count($menuArray) > 0)) {
143
            foreach ($menuArray as $menu) {
144
                   $menuCss = isset($menu['css']) ? "{$menu['css']} " : '';
145
            }
146
            $menuCss = trim($menuCss);
147
        }
148
        if (!($menuCss)) {
149
             $menuCss = "";
150
        } else {
151
            $menuCss = implode(' ', $menuCss);
152
        }
153
        $blockTpl->assign('menucss', $menuCss);
154
    */
155
    $block['content'] = $blockTpl->fetch($skinInfo['template']);
156
157
    if ('template' === $options[3]) {
158
        $GLOBALS['xoopsTpl']->assign($helper->getConfig('unique_id_prefix') . $options[4], $block['content']);
159
        $block = false;
160
    }
161
162
    $registry->unsetAll();
163
    unset($registry, $plugin);
164
    $xoopsLogger->stopTime('My Menus Block');
165
166
    return $block;
167
}
168
169
/**
170
 * @param array $options array(0 => menu, 1 => moduleSkin, 2 => useThemeSkin, 3 => displayMethod, 4 => unique_id, 5 => themeSkin)
171
 *
172
 * @return string
173
 */
174
function mymenus_block_edit($options)
175
{
176
    /** @var \XoopsModules\Mymenus\Helper $helper */
177
    $helper = \XoopsModules\Mymenus\Helper::getInstance();
178
    //
179
    xoops_loadLanguage('admin', 'mymenus');
0 ignored issues
show
Bug introduced by
The function xoops_loadLanguage 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

179
    /** @scrutinizer ignore-call */ 
180
    xoops_loadLanguage('admin', 'mymenus');
Loading history...
180
    xoops_load('XoopsFormLoader');
0 ignored issues
show
Bug introduced by
The function xoops_load 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

180
    /** @scrutinizer ignore-call */ 
181
    xoops_load('XoopsFormLoader');
Loading history...
181
    // option 0: menu
182
    $menusCriteria = new \CriteriaCompo();
183
    $menusCriteria->setSort('title');
184
    $menusCriteria->setOrder('ASC');
185
    $menusList = $helper->getHandler('Menus')->getList($menusCriteria);
186
    unset($menusCriteria);
187
    if (0 === count($menusList)) {
188
        $form = "<a href='" . $GLOBALS['xoops']->url("modules/{$helper->getDirname()}/admin/menus.php") . "'>" . _AM_MYMENUS_MSG_NOMENUS . "</a>\n";
189
190
        return $form;
191
    }
192
    $form            = '<b>' . _MB_MYMENUS_SELECT_MENU . '</b>&nbsp;';
193
    $formMenusSelect = new \XoopsFormSelect('', 'options[0]', $options[0], 1, false);
0 ignored issues
show
Bug introduced by
The type XoopsFormSelect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
194
    $formMenusSelect->addOptionArray($menusList);
195
    $form .= $formMenusSelect->render();
196
    $form .= "</select>\n&nbsp;<i>" . _MB_MYMENUS_SELECT_MENU_DSC . "</i>\n<br><br>\n";
197
    // option 1: moduleSkin
198
    xoops_load('XoopsLists');
199
    $tempModuleSkinsList = \XoopsLists::getDirListAsArray($GLOBALS['xoops']->path("modules/{$helper->getDirname()}/skins/"));
0 ignored issues
show
Bug introduced by
The type XoopsLists was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
200
    $moduleSkinsList     = [];
201
    foreach ($tempModuleSkinsList as $key => $moduleSkin) {
202
        if (file_exists($GLOBALS['xoops']->path("modules/{$helper->getDirname()}/skins/{$moduleSkin}/skin_version.php"))) {
203
            $moduleSkinsList[$moduleSkin] = $moduleSkin;
204
        }
205
    }
206
    $form                 .= '<b>' . _MB_MYMENUS_SELECT_SKIN . '</b>&nbsp;';
207
    $formModuleSkinSelect = new \XoopsFormSelect('', 'options[1]', $options[1], 1, false);
208
    $formModuleSkinSelect->addOptionArray($moduleSkinsList);
209
    $form .= $formModuleSkinSelect->render();
210
    $form .= "\n&nbsp;<i>" . _MB_MYMENUS_SELECT_SKIN_DSC . "</i>\n<br><br>\n";
211
    // option 2: useThemeSkin
212
    $form                  .= '<b>' . _MB_MYMENUS_USE_THEME_SKIN . '</b>&nbsp;';
213
    $formUseThemeSkinRadio = new \XoopsFormRadioYN('', 'options[2]', $options[2]);
0 ignored issues
show
Bug introduced by
The type XoopsFormRadioYN was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
214
    $form                  .= $formUseThemeSkinRadio->render();
215
    $form                  .= "\n&nbsp;<i>" . _MB_MYMENUS_USE_THEME_SKIN_DSC . "</i>\n<br><br>\n";
216
    // option 3: displayMethod
217
    $displayMethodsList      = [
218
        'block'    => _MB_MYMENUS_DISPLAY_METHOD_BLOCK,
219
        'template' => _MB_MYMENUS_DISPLAY_METHOD_TEMPLATE
220
    ];
221
    $form                    .= '<b>' . _MB_MYMENUS_DISPLAY_METHOD . '</b>&nbsp;';
222
    $formDisplayMethodSelect = new \XoopsFormSelect('', 'options[3]', $options[3], 1);
223
    $formDisplayMethodSelect->addOptionArray($displayMethodsList);
224
    $form .= $formDisplayMethodSelect->render();
225
    $form .= "\n&nbsp;<i>" . sprintf(_MB_MYMENUS_DISPLAY_METHOD_DSC, $helper->getConfig('unique_id_prefix')) . "</i>\n<br><br>\n";
226
    // option 4: unique_id
227
    if (!$options[4] || ('clone' === Request::getCmd('op', '', 'GET'))) {
228
        $options[4] = time();
229
    }
230
    $form             .= '<b>' . _MB_MYMENUS_UNIQUEID . '</b>&nbsp;';
231
    $formUniqueIdText = new \XoopsFormText('', 'options[4]', 50, 255, $options[4]);
0 ignored issues
show
Bug introduced by
The type XoopsFormText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
232
    $form             .= $formUniqueIdText->render();
233
    $form             .= "\n&nbsp;<i>" . _MB_MYMENUS_UNIQUEID_DSC . "</i>\n<br><br>\n";
234
    // option 5: themeSkin
235
    if (file_exists($GLOBALS['xoops']->path('/themes/' . $GLOBALS['xoopsConfig']['theme_set'] . "/modules/{$helper->getDirname()}/skins/"))) {
236
        xoops_load('XoopsLists');
237
        $tempThemeSkinsList = \XoopsLists::getDirListAsArray($GLOBALS['xoops']->path('/themes/' . $GLOBALS['xoopsConfig']['theme_set'] . "/modules/{$helper->getDirname()}/skins/"));
238
        if (isset($tempThemeSkinsList)) {
239
            $themeSkinsList = [];
240
            foreach ($tempThemeSkinsList as $key => $themeSkin) {
241
                if (file_exists($GLOBALS['xoops']->path('/themes/' . $GLOBALS['xoopsConfig']['theme_set'] . "/modules/{$helper->getDirname()}/skins/{$themeSkin}/skin_version.php"))) {
242
                    $themeSkinsList[$themeSkin] = '/themes/' . $GLOBALS['xoopsConfig']['theme_set'] . "/modules/{$helper->getDirname()}/skins/{$themeSkin}";
243
                }
244
            }
245
            $form                .= '<b>' . _MB_MYMENUS_SELECT_SKIN_FROM_THEME . '</b>&nbsp;';
246
            $formThemeSkinSelect = new \XoopsFormSelect('', 'options[5]', $options[5], 1, false);
247
            $formThemeSkinSelect->addOptionArray($themeSkinsList);
248
            $form .= $formThemeSkinSelect->render();
249
            $form .= "\n&nbsp;&nbsp;<i>" . _MB_MYMENUS_SELECT_SKIN_FROM_THEME_DSC . "</i>\n<br><br>\n";
250
        }
251
    }
252
253
    return $form;
254
}
255