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

SystemPreferencesForm::getForm()   F

Complexity

Conditions 52
Paths > 20000

Size

Total Lines 290
Code Lines 221

Duplication

Lines 197
Ratio 67.93 %
Metric Value
dl 197
loc 290
rs 2
cc 52
eloc 221
nc 343440
nop 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\Kernel\Criteria;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Criteria.

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
use Xoops\Core\Kernel\CriteriaCompo;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaCompo.

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...
14
use Xoops\Core\Kernel\Handlers\XoopsModule;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModule.

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...
15
16
/**
17
 * Preference Form Class
18
 *
19
 * @category  Modules/system/class/form
20
 * @package   SystemPreferencesForm
21
 * @author    Andricq Nicolas (AKA MusS)
22
 * @author    trabis <[email protected]>
23
 * @copyright 2000-2014 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 * @since     2.0
27
 */
28
class SystemPreferencesForm extends Xoops\Form\SimpleForm
29
{
30
    /**
31
     * __construct
32
     *
33
     * @param null $obj unused object
34
     */
35
    public function __construct($obj = null)
36
    {
37
    }
38
39
    /**
40
     * getForm
41
     *
42
     * @param array       &$obj array of config objects
43
     * @param XoopsModule $mod module
44
     *
45
     * @return void
46
     */
47
    public function getForm(&$obj, XoopsModule $mod)
0 ignored issues
show
Coding Style introduced by
getForm uses the super-global variable $_REQUEST 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...
48
    {
49
        $xoops = Xoops::getInstance();
50
        $config_handler = $xoops->getHandlerConfig();
51
52
        parent::__construct('', 'pref_form', 'admin.php?fct=preferences', 'post', true);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__construct() instead of getForm()). Are you sure this is correct? If so, you might want to change this to $this->__construct().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
53
        if ($mod->getVar('dirname') !== 'system') {
54
            $xoops->loadLanguage('modinfo', $mod->getVar('dirname'));
55
        }
56
        $xoops->loadLocale($mod->getVar('dirname'));
57
        $configs = $mod->getInfo('config');
58
        $configNames = array();
59 View Code Duplication
        foreach (array_keys($configs) as $i) {
60
            $configNames[$configs[$i]['name']] =& $configs[$i];
61
        }
62
        $configCats = $mod->getInfo('configcat');
63
        if (!$configCats) {
64
            $configCats = array(
65
                'default' => array(
66
                    'name'        => SystemLocale::OTHER_SETTINGS,
67
                    'description' => ''
68
                )
69
            );
70
        }
71
72 View Code Duplication
        if (!in_array('default', array_keys($configCats))) {
73
            $configCats['default'] = array(
74
                'name'        => SystemLocale::OTHER_SETTINGS,
75
                'description' => ''
76
            );
77
        }
78
79 View Code Duplication
        foreach (array_keys($configNames) as $name) {
80
            if (!isset($configNames[$name]['category'])) {
81
                $configNames[$name]['category'] = 'default';
82
            }
83
        }
84
85
        $tabTray = new Xoops\Form\TabTray('', 'pref_tabtay');
86
        $tabs = array();
87 View Code Duplication
        foreach ($configCats as $name => $info) {
0 ignored issues
show
Bug introduced by
The expression $configCats of type array<string,array<strin...:\"string\"}>"}>|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
88
            $tabs[$name] = new Xoops\Form\Tab($info['name'], 'pref_tab_' . $name);
89
            if (isset($info['description']) && $info['description'] != '') {
90
                $tabs[$name]->addElement(new Xoops\Form\Label('', $info['description']));
91
            }
92
        }
93
94
        $xoops->events()->triggerEvent('system.preferences.form', $mod);
95
96
        if (!empty($_REQUEST["redirect"])) {
97
            $myts = \Xoops\Core\Text\Sanitizer::getInstance();
98
            $this->addElement(new Xoops\Form\Hidden('redirect', $myts->htmlSpecialChars($_REQUEST["redirect"])));
99
        } elseif ($mod->getInfo('adminindex')) {
100
            $this->addElement(new Xoops\Form\Hidden(
101
                'redirect',
102
                \XoopsBaseConfig::get('url') . '/modules/' . $mod->getVar('dirname') . '/' . $mod->getInfo('adminindex')
103
            ));
104
        }
105
        $count = count($obj);
106
        for ($i = 0; $i < $count; ++$i) {
107
            $title = \Xoops\Locale::translate($obj[$i]->getVar('conf_title'), $mod->getVar('dirname'));
108
            $desc = ($obj[$i]->getVar('conf_desc') != '') ?
109
                \Xoops\Locale::translate($obj[$i]->getVar('conf_desc'), $mod->getVar('dirname')) : '';
110
            switch ($obj[$i]->getVar('conf_formtype')) {
111
112 View Code Duplication
                case 'textarea':
113
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
114
                    if ($obj[$i]->getVar('conf_valuetype') === 'array') {
115
                        // this is exceptional.. only when value type is arrayneed a smarter way for this
116
                        $ele = ($obj[$i]->getVar('conf_value') != '')
117
                            ? new Xoops\Form\TextArea(
118
                                $title,
119
                                $obj[$i]->getVar('conf_name'),
120
                                $myts->htmlSpecialChars(implode('|', $obj[$i]->getConfValueForOutput())),
121
                                5,
122
                                5
123
                            )
124
                            : new Xoops\Form\TextArea($title, $obj[$i]->getVar('conf_name'), '', 5, 5);
125
                    } else {
126
                        $ele = new Xoops\Form\TextArea(
127
                            $title,
128
                            $obj[$i]->getVar('conf_name'),
129
                            $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()),
130
                            5,
131
                            5
132
                        );
133
                    }
134
                    break;
135
136 View Code Duplication
                case 'select':
137
                    $ele = new Xoops\Form\Select(
138
                        $title,
139
                        $obj[$i]->getVar('conf_name'),
140
                        $obj[$i]->getConfValueForOutput()
141
                    );
142
                    $options = $config_handler->getConfigOptions(new Criteria('conf_id', $obj[$i]->getVar('conf_id')));
143
                    $opcount = count($options);
144
                    for ($j = 0; $j < $opcount; ++$j) {
145
                        $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
146
                        $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), $mod->getVar('dirname'));
147
                        $ele->addOption($optval, $optkey);
148
                    }
149
                    break;
150
151
                case 'select_editor':
152
                    $ele = new Xoops\Form\Select(
153
                        $title,
154
                        $obj[$i]->getVar('conf_name'),
155
                        $obj[$i]->getConfValueForOutput()
156
                    );
157
                    \Xoops\Core\Lists\Editor::setOptionsArray($ele);
158
                    break;
159
160 View Code Duplication
                case 'select_multi':
161
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput(), 5, true);
162
                    $options = $config_handler->getConfigOptions(new Criteria('conf_id', $obj[$i]->getVar('conf_id')));
163
                    $opcount = count($options);
164
                    for ($j = 0; $j < $opcount; ++$j) {
165
                        $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
166
                        $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), $mod->getVar('dirname'));
167
                        $ele->addOption($optval, $optkey);
168
                    }
169
                    break;
170
171 View Code Duplication
                case 'yesno':
172
                    $ele = new Xoops\Form\RadioYesNo($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
173
                    break;
174
175
                case 'theme':
176 View Code Duplication
                case 'theme_multi':
177
                    $ele = ($obj[$i]->getVar('conf_formtype') !== 'theme_multi') ? new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput()) : new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput(), 5, true);
178
                    $dirlist = XoopsLists::getThemesList();
179
                    if (!empty($dirlist)) {
180
                        asort($dirlist);
181
                        $ele->addOptionArray($dirlist);
182
                    }
183
                    break;
184 View Code Duplication
                case 'tplset':
185
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
186
                    $tplset_handler = $xoops->getHandlerTplSet();
187
                    $tplsetlist = $tplset_handler->getNameList();
188
                    asort($tplsetlist);
189
                    foreach ($tplsetlist as $key => $name) {
190
                        $ele->addOption($key, $name);
191
                    }
192
                    break;
193
194 View Code Duplication
                case 'cpanel':
195
                    $ele = new Xoops\Form\Hidden($obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
196
                    /*
197
                    $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
198
                    XoopsLoad::load("cpanel", "system");
199
                    $list = XoopsSystemCpanel::getGuis();
200
                    $ele->addOptionArray($list);  */
201
                    break;
202
203 View Code Duplication
                case 'timezone':
204
                    $ele = new Xoops\Form\SelectTimeZone($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
205
                    break;
206
207 View Code Duplication
                case 'language':
208
                    $ele = new Xoops\Form\SelectLanguage($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
209
                    break;
210
211 View Code Duplication
                case 'locale':
212
                    $ele = new Xoops\Form\SelectLocale($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
213
                    break;
214
215 View Code Duplication
                case 'startpage':
216
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
217
218
                    $module_handler = $xoops->getHandlerModule();
219
                    $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
220
                    $criteria->add(new Criteria('isactive', 1));
221
                    $moduleslist = $module_handler->getNameList($criteria, true);
222
                    $moduleslist['--'] = XoopsLocale::NONE;
223
                    $ele->addOptionArray($moduleslist);
224
                    break;
225
226
                case 'group':
227
                    $ele = new Xoops\Form\SelectGroup($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 1, false);
228
                    break;
229
230
                case 'group_multi':
231
                    $ele = new Xoops\Form\SelectGroup($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 5, true);
232
                    break;
233
234
                // RMV-NOTIFY: added 'user' and 'user_multi'
235 View Code Duplication
                case 'user':
236
                    $ele = new Xoops\Form\SelectUser($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 1, false);
237
                    break;
238
239 View Code Duplication
                case 'user_multi':
240
                    $ele = new Xoops\Form\SelectUser($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 5, true);
241
                    break;
242 View Code Duplication
                case 'module_cache':
243
                    $module_handler = $xoops->getHandlerModule();
244
                    $modules = $module_handler->getObjectsArray(new Criteria('hasmain', 1), true);
245
                    $currrent_val = $obj[$i]->getConfValueForOutput();
246
                    $cache_options = array(
247
                        '0'       => XoopsLocale::NO_CACHE,
248
                        '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
249
                        '60'      => XoopsLocale::ONE_MINUTE,
250
                        '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
251
                        '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
252
                        '3600'    => XoopsLocale::ONE_HOUR,
253
                        '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
254
                        '86400'   => XoopsLocale::ONE_DAY,
255
                        '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
256
                        '604800'  => XoopsLocale::ONE_WEEK,
257
                        '2592000' => XoopsLocale::ONE_MONTH
258
                    );
259
                    if (count($modules) > 0) {
260
                        $ele = new Xoops\Form\ElementTray($title, '<br />');
261
                        foreach (array_keys($modules) as $mid) {
262
                            $c_val = isset($currrent_val[$mid]) ? (int)($currrent_val[$mid]) : null;
263
                            $selform = new Xoops\Form\Select($modules[$mid]->getVar('name'), $obj[$i]->getVar('conf_name') . "[$mid]", $c_val);
264
                            $selform->addOptionArray($cache_options);
265
                            $ele->addElement($selform);
266
                            unset($selform);
267
                        }
268
                    } else {
269
                        $ele = new Xoops\Form\Label($title, SystemLocale::NO_MODULE_TO_CACHE);
270
                    }
271
                    break;
272
273 View Code Duplication
                case 'site_cache':
274
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
275
                    $ele->addOptionArray(array(
276
                        '0'       => XoopsLocale::NO_CACHE,
277
                        '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
278
                        '60'      => XoopsLocale::ONE_MINUTE,
279
                        '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
280
                        '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
281
                        '3600'    => XoopsLocale::ONE_HOUR,
282
                        '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
283
                        '86400'   => XoopsLocale::ONE_DAY,
284
                        '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
285
                        '604800'  => XoopsLocale::ONE_WEEK,
286
                        '2592000' => XoopsLocale::ONE_MONTH
287
                    ));
288
                    break;
289
290 View Code Duplication
                case 'password':
291
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
292
                    $ele = new Xoops\Form\Password($title, $obj[$i]->getVar('conf_name'), 32, 255, $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
293
                    break;
294
295 View Code Duplication
                case 'color':
296
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
297
                    $ele = new Xoops\Form\ColorPicker($title, $obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
298
                    break;
299
300 View Code Duplication
                case 'hidden':
301
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
302
                    $ele = new Xoops\Form\Hidden($obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
303
                    break;
304
305
                case 'textbox':
306 View Code Duplication
                default:
307
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
308
                    $ele = new Xoops\Form\Text($title, $obj[$i]->getVar('conf_name'), 5, 255, $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
309
                    break;
310
            }
311
            $hidden = new Xoops\Form\Hidden('conf_ids[]', $obj[$i]->getVar('conf_id'));
312 View Code Duplication
            if (isset($ele)) {
313
                $ele->setDescription($desc);
314
                if ($obj[$i]->getVar('conf_formtype') !== 'hidden') {
315
                    $name = 'default';
316
                    if (isset($configNames[$obj[$i]->getVar('conf_name')]['category'])) {
317
                        $name = $configNames[$obj[$i]->getVar('conf_name')]['category'];
318
                    }
319
                    $tabs[$name]->addElement($ele);
320
                } else {
321
                    $this->addElement($ele);
322
                }
323
                $this->addElement($hidden);
324
                unset($ele);
325
                unset($hidden);
326
            }
327
        }
328
        foreach (array_keys($tabs) as $name) {
329
            if ($tabs[$name]->getElements()) {
330
                $tabTray->addElement($tabs[$name]);
331
            }
332
        }
333
        $this->addElement($tabTray);
334
        $this->addElement(new Xoops\Form\Hidden('op', 'save'));
335
        $this->addElement(new Xoops\Form\Button('', 'button', XoopsLocale::A_SUBMIT, 'submit'));
336
    }
337
}
338