Completed
Pull Request — master (#489)
by Richard
10:59
created

menus_block.php ➔ menus_block_show()   F

Complexity

Conditions 19
Paths 7690

Size

Total Lines 111
Code Lines 64

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 19
eloc 64
nc 7690
nop 1
dl 0
loc 111
rs 2

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.

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);
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]);
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)
0 ignored issues
show
Coding Style introduced by
menus_block_edit uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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);
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 ''.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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')) {
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