Completed
Pull Request — master (#563)
by Richard
08:33
created

menus_block_show()   F

Complexity

Conditions 19
Paths 7690

Size

Total Lines 110
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 63
nc 7690
nop 1
dl 0
loc 110
rs 2
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
use Xoops\Core\XoopsTpl;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsTpl. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
/**
15
 * @copyright       2012-2014 XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL V2 or later http://www.gnu.org/licenses/gpl-2.0.html
17
 * @package         Menus
18
 * @since           1.0
19
 * @author          trabis <[email protected]>
20
 */
21
22
function menus_block_show($options)
23
{
24
    $block = array();
25
    $xoops = Xoops::getInstance();
26
    $helper = Xoops::getModuleHelper('menus');
27
28
    /* @var $decorator MenusDecoratorInterface */
29
    $decorators = MenusDecorator::getAvailableDecorators();
30
31
    foreach ($decorators as $decorator) {
32
        $decorator->start();
33
    }
34
35
    $menu_id = $options[0];
36
    $criteria = new CriteriaCompo(new Criteria('mid', $menu_id));
37
    $criteria->setSort('weight');
38
    $criteria->setOrder('ASC');
39
    //get menus as an array with ids as keys
40
    $menus = $helper->getHandlerMenu()->getAll($criteria, null, false, false);
0 ignored issues
show
Bug introduced by
The method getHandlerMenu() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()? ( Ignorable by Annotation )

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

40
    $menus = $helper->/** @scrutinizer ignore-call */ getHandlerMenu()->getAll($criteria, null, false, false);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
    unset($criteria);
42
43
    foreach ($menus as $key => $menu) {
44
        $hasAccess = true;
45
        foreach ($decorators as $decorator) {
46
            $decorator->hasAccess($menu, $hasAccess);
47
        }
48
        if (!$hasAccess) {
49
            unset($menus[$key]);
50
        }
51
    }
52
53
    $count = count($menus);
54
    if ($count == 0) {
55
        return $block;
56
    }
57
58
    foreach ($menus as $key => $menu) {
59
        foreach ($decorators as $decorator) {
60
            $decorator->decorateMenu($menu);
61
        }
62
        $menus[$key] = $menu;
63
    }
64
65
    foreach ($decorators as $decorator) {
66
        $decorator->end($menus);
67
    }
68
69
    $builder = new MenusBuilder($menus);
70
    $block = $builder->render();
71
72
    /*--------------------------------------------------------------*/
73
    //default files to load
74
    $css = array();
75
    $js = array();
76
77
    //get extra files from skins
78
    $skin = $options[1];
79
    $skin_info = $helper->getSkinInfo($skin, $options[2]);
0 ignored issues
show
Bug introduced by
The method getSkinInfo() does not exist on Xoops\Module\Helper\HelperAbstract. It seems like you code against a sub-type of Xoops\Module\Helper\HelperAbstract such as Menus. ( Ignorable by Annotation )

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

79
    /** @scrutinizer ignore-call */ 
80
    $skin_info = $helper->getSkinInfo($skin, $options[2]);
Loading history...
80
81
    if (isset($skin_info['css'])) {
82
        $css = array_merge($css, $skin_info['css']);
83
    }
84
85
    if (isset($skin_info['js'])) {
86
        $js = array_merge($js, $skin_info['js']);
87
    }
88
89
    if ($helper->getConfig('assign_method') === 'xoopstpl') {
90
        $tpl_vars = '';
91
        foreach ($css as $file) {
92
            $tpl_vars .= "\n" . '<link rel="stylesheet" type="text/css" media="all" href="' . $file . '" />';
93
        }
94
95
        foreach ($js as $file) {
96
            $tpl_vars .= "\n" . '<script type="text/javascript" src="' . $file . '"></script>';
97
        }
98
99
        if (isset($skin_info['header'])) {
100
            $tpl_vars .= "\n" . $skin_info['header'];
101
        }
102
103
        $xoops->tpl()->assign('xoops_module_header', $tpl_vars . @$xoops->tpl()->getTemplateVars("xoops_module_header"));
104
    } else {
105
        foreach ($css as $file) {
106
            $xoops->theme()->addStylesheet($file);
107
        }
108
109
        foreach ($js as $file) {
110
            $xoops->theme()->addScript($file);
111
        }
112
113
        if (isset($skin_info['header'])) {
114
            $xoops->tpl()->assign('xoops_footer', @$xoops->tpl()->getTemplateVars("xoops_footer") . "\n" . $skin_info['header']);
115
        }
116
    }
117
118
    $blockTpl = new XoopsTpl();
119
    $blockTpl->assign('block', $block);
120
    $blockTpl->assign('config', $skin_info['config']);
121
    $blockTpl->assign('skinurl', $skin_info['url']);
122
    $blockTpl->assign('skinpath', $skin_info['path']);
123
124
    $block['content'] = $blockTpl->fetch($skin_info['template']);
125
126
    if ($options[3] === 'template') {
127
        $xoops->tpl()->assign('xoops_menu_' . $options[4], $block['content']);
128
        $block = array();
129
    }
130
131
    return $block;
132
}
133
134
function menus_block_edit($options)
135
{
136
    //Unique ID
137
    if (!$options[4] || (isset($_GET['op']) && $_GET['op'] === 'clone')) {
138
        $options[4] = uniqid();
139
    }
140
141
    $helper = Xoops::getModuleHelper('menus');
142
    $helper->loadLanguage('admin');
143
144
    $criteria = new CriteriaCompo();
145
    $criteria->setSort('title');
146
    $criteria->setOrder('ASC');
147
    $menus = $helper->getHandlerMenus()->getList($criteria);
0 ignored issues
show
Bug introduced by
The method getHandlerMenus() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()? ( Ignorable by Annotation )

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

147
    $menus = $helper->/** @scrutinizer ignore-call */ getHandlerMenus()->getList($criteria);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
    unset($criteria);
149
150
    if (count($menus) == 0) {
151
        $form = "<a href='" . $helper->url('admin/admin_menus.php') . "'>" . _AM_MENUS_MSG_NOMENUS . "</a>";
152
        return $form;
153
    }
154
155
    //Menu
156
    $form = new Xoops\Form\BlockForm();
157
    $element = new Xoops\Form\Select(_MB_MENUS_SELECT_MENU, 'options[0]', $options[0], 1);
158
    $element->addOptionArray($menus);
159
    $element->setDescription(_MB_MENUS_SELECT_MENU_DSC);
160
    $form->addElement($element);
161
162
    //Skin
163
    $temp_skins = XoopsLists::getDirListAsArray(\XoopsBaseConfig::get('root-path') . "/modules/menus/skins/", "");
0 ignored issues
show
Unused Code introduced by
The call to XoopsLists::getDirListAsArray() has too many arguments starting with ''. ( Ignorable by Annotation )

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

163
    /** @scrutinizer ignore-call */ 
164
    $temp_skins = XoopsLists::getDirListAsArray(\XoopsBaseConfig::get('root-path') . "/modules/menus/skins/", "");

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
164
    $skins_options = array();
165
    foreach ($temp_skins as $skin) {
166
        if (XoopsLoad::fileExists($helper->path('skins/' . $skin . '/skin_version.php'))) {
167
            $skins_options[$skin] = $skin;
168
        }
169
    }
170
    $element = new Xoops\Form\Select(_MB_MENUS_SELECT_SKIN, 'options[1]', $options[1], 1);
171
    $element->addOptionArray($skins_options);
172
    $element->setDescription(_MB_MENUS_SELECT_SKIN_DSC);
173
    $form->addElement($element);
174
175
    //Use skin from,theme
176
    $element = new Xoops\Form\RadioYesNo(_MB_MENUS_USE_THEME_SKIN, 'options[2]', $options[2]);
177
    $element->setDescription(_MB_MENUS_USE_THEME_SKIN_DSC);
178
    $form->addElement($element);
179
180
    //Display method
181
    $display_options = array(
182
        'block'    => _MB_MENUS_DISPLAY_METHOD_BLOCK,
183
        'template' => _MB_MENUS_DISPLAY_METHOD_TEMPLATE
184
    );
185
    $element = new Xoops\Form\Select(_MB_MENUS_DISPLAY_METHOD, 'options[3]', $options[3], 1);
186
    $element->addOptionArray($display_options);
187
    $element->setDescription(sprintf(_MB_MENUS_DISPLAY_METHOD_DSC, $options[4]));
188
    $form->addElement($element);
189
190
    //Unique ID
191
    $element = new Xoops\Form\Text(_MB_MENUS_UNIQUEID, 'options[4]', 2, 20, $options[4]);
192
    $element->setDescription(_MB_MENUS_UNIQUEID_DSC);
193
    $form->addElement($element);
194
195
    return $form->render();
196
}
197
198
function menus_mainmenu_show()
199
{
200
    $block = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $block is dead and can be removed.
Loading history...
201
    $xoops = Xoops::getInstance();
202
    $helper = Xoops::getModuleHelper('menus');
203
204
    $module_handler = $xoops->getHandlerModule();
205
    $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
206
    $criteria->add(new Criteria('isactive', 1));
207
    $criteria->add(new Criteria('weight', 0, '>'));
208
    $modules = $module_handler->getObjectsArray($criteria, true);
209
    $moduleperm_handler = $xoops->getHandlerGroupPermission();
210
    $groups = $xoops->getUserGroups();
211
    $read_allowed = $moduleperm_handler->getItemIds('module_read', $groups);
212
    $menus = array();
213
    $menu = $helper->getHandlerMenu()->create();
214
    $menu->setVar('id', 1);
215
    $menu->setVar('pid', 0);
216
    $menu->setVar('alt_title', _MB_MENUS_HOME);
217
    $menu->setVar('title', _MB_MENUS_HOME);
218
    $menu->setVar('link', \XoopsBaseConfig::get('url'));
219
    $menu->setVar('image', 'icon-home');
220
    $menus[] = $menu->getValues();
221
    foreach (array_keys($modules) as $i) {
222
        if (in_array($i, $read_allowed)) {
223
            /* @var $plugin MenusPluginInterface */
224
            $menu = $helper->getHandlerMenu()->create();
225
            $menu->setVar('id', $i);
226
            $menu->setVar('pid', 0);
227
            $menu->setVar('title', $modules[$i]->getVar('name'));
228
            $menu->setVar('alt_title', $modules[$i]->getVar('name'));
229
            $menu->setVar('link', \XoopsBaseConfig::get('url') . '/modules/' . $modules[$i]->getVar('dirname'));
230
            $menu->setVar('image', 'icon-tags');
231
            $menus[] = $menu->getValues();
232
            if ($xoops->isModule() && $xoops->module->getVar('dirname') == $modules[$i]->getVar('dirname') && $plugin = \Xoops\Module\Plugin::getPlugin($modules[$i]->getVar('dirname'), 'menus')) {
0 ignored issues
show
Bug introduced by
The method getVar() does not exist on null. ( Ignorable by Annotation )

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

232
            if ($xoops->isModule() && $xoops->module->/** @scrutinizer ignore-call */ getVar('dirname') == $modules[$i]->getVar('dirname') && $plugin = \Xoops\Module\Plugin::getPlugin($modules[$i]->getVar('dirname'), 'menus')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
233
                $sublinks = $plugin->subMenus();
234
                $j = -1;
235
                foreach ($sublinks as $sublink) {
236
                    $menu = $helper->getHandlerMenu()->create();
237
                    $menu->setVar('id', $j);
238
                    $menu->setVar('pid', $i);
239
                    $menu->setVar('title', $sublink['name']);
240
                    $menu->setVar('alt_title', $sublink['name']);
241
                    $menu->setVar('link', \XoopsBaseConfig::get('url') . '/modules/' . $modules[$i]->getVar('dirname') . '/'. $sublink['url']);
242
                    $menus[] = $menu->getValues();
243
                    $j--;
244
                }
245
            }
246
        }
247
    }
248
    $builder = new MenusBuilder($menus);
249
    $block = $builder->render();
250
251
    $skin_info = $helper->getSkinInfo('mainmenu', false);
252
    $blockTpl = new XoopsTpl();
253
    $blockTpl->assign('block', $block);
254
    $blockTpl->assign('config', $skin_info['config']);
255
    $blockTpl->assign('skinurl', $skin_info['url']);
256
    $blockTpl->assign('skinpath', $skin_info['path']);
257
258
    $block['content'] = $blockTpl->fetch($skin_info['template']);
259
260
    return $block;
261
}
262