Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

Admin   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 436
Duplicated Lines 16.06 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 39
c 2
b 1
f 0
lcom 1
cbo 3
dl 70
loc 436
rs 8.2857
ccs 0
cts 199
cp 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 19 3
B addConfigBoxLine() 0 18 6
A addInfoBox() 0 6 1
A addInfoBoxLine() 0 10 1
A addItemButton() 0 4 1
A renderButton() 0 8 2
A displayButton() 0 4 1
A renderInfoBox() 0 4 1
A displayInfoBox() 0 4 1
A renderIndex() 0 4 1
A displayIndex() 0 4 1
A displayNavigation() 0 4 1
A renderAbout() 0 4 1
A displayAbout() 0 4 1
A addConfigError() 13 13 1
A addConfigAccept() 13 13 1
A addConfigWarning() 13 13 1
B addConfigModuleVersion() 31 31 4
A isXng() 0 4 1
A menuIconPath() 0 10 2
B iconUrl() 0 23 5
A setPaypal() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
namespace Xmf\Module;
13
14
use Xmf\Language;
15
16
/**
17
 * Xmf\Module\Admin provides helpful methods for module administration
18
 * uses.
19
 *
20
 * Xmf\Module\Admin provides a method compatible subset of the Xoops\Module\Admin class
21
 * (introduced in 2.6) and other convenience methods useful in transition
22
 *
23
 * @category  Xmf\Module\Admin
24
 * @package   Xmf
25
 * @author    Richard Griffith <[email protected]>
26
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
27
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28
 * @version   Release: 1.0
29
 * @link      http://xoops.org
30
 * @since     1.0
31
 */
32
class Admin
33
{
34
35
    /**
36
     * The real ModuleAdmin object
37
     *
38
     * @var object
39
     */
40
    protected static $ModuleAdmin = null;
41
    protected $lastInfoBoxTitle = null;
42
    protected static $paypal = '';
43
44
    /**
45
     * Constructor
46
     */
47
    protected function __construct()
48
    {
49
    }
50
51
    /**
52
     * Retrieve a module admin instance
53
     *
54
     * If we are on a next generation system this will be the a native Xoops\Module\Admin instance.
55
     * Older systems with the Frameworks based admin class will get an instance of this class which
56
     * provides compatible methods built from the old Frameworks version.
57
     *
58
     * @return object a ModuleAdmin or Xoops\Module\Admin instance.
59
     *
60
     * @since  1.0
61
     */
62
    public static function getInstance()
0 ignored issues
show
Coding Style introduced by
getInstance uses the super-global variable $GLOBALS 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...
63
    {
64
65
        static $instance;
66
67
        if ($instance === null) {
68
            if (class_exists('\Xoops\Module\Admin', true)) {
69
                $instance = new \Xoops\Module\Admin;
70
                static::$ModuleAdmin = $instance;
71
            } else {
72
                include_once $GLOBALS['xoops']->path('Frameworks/moduleclasses/moduleadmin/moduleadmin.php');
73
                static::$ModuleAdmin = new \ModuleAdmin;
74
                Language::load('xmf');
75
                $instance = new static();
76
            }
77
        }
78
79
        return $instance;
80
    }
81
82
    /**
83
     * Add config line
84
     *
85
     * @param string $value message to include in config box
86
     * @param string $type  type of line to add
87
     *                       minimal set of acceptable types and value expectation
88
     *                       'default' - value is message displayed directly (also used for unknown types)
89
     *                       'folder'  - value is directory name, will display accept if exists, error if not
90
     *                       'chmod'   - value is array(directory, permission) accept if exists with permission,
91
     *                                   else error
92
     *                       'module'  - value is string module name, or array(module name, errortype)
93
     *                                   If module is active, an accept line displays, otherwise, a warning
94
     *                                   (if value is array(module, "warning") or an error displays.
95
     *
96
     * @return bool
97
     */
98
    public function addConfigBoxLine($value = '', $type = 'default')
99
    {
100
        if ($type === 'module') {
101
            $mod = (is_array($value)) ? $value[0] : $value;
102
            if (xoops_isActiveModule($mod)) {
0 ignored issues
show
Deprecated Code introduced by
The function xoops_isActiveModule() has been deprecated.

This function has been deprecated.

Loading history...
103
                return $this->addConfigAccept(sprintf(_AM_XMF_MODULE_INSTALLED, $mod));
104
            } else {
105
                $nomod = (is_array($value)) ? $value[1] : 'error';
106
                $line = sprintf(_AM_XMF_MODULE_NOT_INSTALLED, $mod);
107
                if ($nomod === 'warning') {
108
                    return $this->addConfigWarning($line);
109
                } else {
110
                    return $this->addConfigError($line);
111
                }
112
            }
113
        }
114
        return static::$ModuleAdmin->addConfigBoxLine($value, $type);
115
    }
116
117
    /**
118
     * Add Info box
119
     *
120
     * @param string $title info box title
121
     * @param string $type  for compatibility only
122
     * @param string $extra for compatibility only
123
     *
124
     * @return bool
125
     */
126
    public function addInfoBox($title, $type = 'default', $extra = '')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        $this->lastInfoBoxTitle = $title;
129
130
        return static::$ModuleAdmin->addInfoBox($title);
131
    }
132
133
    /**
134
     * Add line to the info box
135
     *
136
     * @param string $text  text to add to info box
137
     * @param string $type  type of infobox line
138
     * @param string $color color for infobox line
139
     *
140
     * @return bool
141
     */
142
    public function addInfoBoxLine($text = '', $type = 'default', $color = 'inherit')
143
    {
144
        return static::$ModuleAdmin->addInfoBoxLine(
145
            $this->lastInfoBoxTitle,
146
            $text,
147
            '',
148
            $color,
149
            $type
150
        );
151
    }
152
153
    /**
154
     * Add Item button
155
     *
156
     * @param string $title title of button
157
     * @param string $link  link for button
158
     * @param string $icon  icon for button
159
     * @param string $extra extra
160
     *
161
     * @return bool
162
     */
163
    public function addItemButton($title, $link, $icon = 'add', $extra = '')
164
    {
165
        return static::$ModuleAdmin->addItemButton($title, $link, $icon, $extra);
166
    }
167
168
    /**
169
     * Render all items buttons
170
     *
171
     * @param string $position  button position (left, right)
172
     * @param string $delimiter delimiter between buttons
173
     *
174
     * @return string
175
     */
176
    public function renderButton($position = null, $delimiter = "&nbsp;")
177
    {
178
        if (null === $position) {
179
            $position = 'right';
180
        }
181
182
        return static::$ModuleAdmin->renderButton($position, $delimiter);
183
    }
184
185
    /**
186
     * Display all item buttons
187
     *
188
     * @param string $position  button position (left, right)
189
     * @param string $delimiter delimiter between buttons
190
     *
191
     * @return void
192
     */
193
    public function displayButton($position = null, $delimiter = "&nbsp;")
194
    {
195
        echo $this->renderButton($position, $delimiter);
196
    }
197
198
    /**
199
     * Render InfoBox
200
     *
201
     * @return string HTML rendered info box
202
     */
203
    public function renderInfoBox()
204
    {
205
        return static::$ModuleAdmin->renderInfoBox();
206
    }
207
208
    /**
209
     * Display InfoBox
210
     *
211
     * @return void
212
     */
213
    public function displayInfoBox()
214
    {
215
        echo $this->renderInfoBox();
216
    }
217
218
    /**
219
     * Render index page for admin
220
     *
221
     * @return string HTML rendered info box
222
     */
223
    public function renderIndex()
224
    {
225
        return static::$ModuleAdmin->renderIndex();
226
    }
227
228
    /**
229
     * Display index page for admin
230
     *
231
     * @return void
232
     */
233
    public function displayIndex()
234
    {
235
        echo $this->renderIndex();
236
    }
237
238
    /**
239
     * Display the navigation menu
240
     *
241
     * @param string $menu menu key (script name, i.e. index.php)
242
     *
243
     * @return void
244
     */
245
    public function displayNavigation($menu = '')
246
    {
247
        echo static::$ModuleAdmin->addNavigation($menu);
248
    }
249
250
    /**
251
     * Render about page
252
     *
253
     * @param bool $logo_xoops display XOOPS logo
254
     *
255
     * @return bool|mixed|string
256
     */
257
    public function renderAbout($logo_xoops = true)
258
    {
259
        return static::$ModuleAdmin->renderAbout(static::$paypal, $logo_xoops);
260
    }
261
262
    /**
263
     * Display about page
264
     *
265
     * @param bool $logo_xoops display XOOPS logo
266
     *
267
     * @return void
268
     */
269
    public function displayAbout($logo_xoops = true)
270
    {
271
        echo $this->renderAbout($logo_xoops);
272
    }
273
274
    /**
275
     * Add error to config box
276
     *
277
     * @param string $value the error message
278
     *
279
     * @return bool
280
     */
281 View Code Duplication
    public function addConfigError($value = '')
282
    {
283
        $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/';
284
        $line = "";
285
        $line .= "<span style='color : red; font-weight : bold;'>";
286
        $line .= "<img src='" . $path . "0.png' >";
287
        $line .= $value;
288
        $line .= "</span>";
289
        $value = $line;
290
        $type = 'default';
291
292
        return static::$ModuleAdmin->addConfigBoxLine($value, $type);
293
    }
294
295
    /**
296
     * Add accept (OK) message to config box
297
     *
298
     * @param string $value the OK message
299
     *
300
     * @return bool
301
     */
302 View Code Duplication
    public function addConfigAccept($value = '')
303
    {
304
        $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/';
305
        $line = "";
306
        $line .= "<span style='color : green;'>";
307
        $line .= "<img src='" . $path . "1.png' >";
308
        $line .= $value;
309
        $line .= "</span>";
310
        $value = $line;
311
        $type = 'default';
312
313
        return static::$ModuleAdmin->addConfigBoxLine($value, $type);
314
    }
315
316
    /**
317
     * Add warning to config box
318
     *
319
     * @param string $value the warning message
320
     *
321
     * @return bool
322
     */
323 View Code Duplication
    public function addConfigWarning($value = '')
324
    {
325
        $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/';
326
        $line = "";
327
        $line .= "<span style='color : orange; font-weight : bold;'>";
328
        $line .= "<img src='" . $path . "warning.png' >";
329
        $line .= $value;
330
        $line .= "</span>";
331
        $value = $line;
332
        $type = 'default';
333
334
        return static::$ModuleAdmin->addConfigBoxLine($value, $type);
335
    }
336
337
338
    /**
339
     * Check for installed module and version and do addConfigBoxLine()
340
     *
341
     * @param string  $moddir     - module directory name
342
     * @param integer $minversion - minimum acceptable module version (100 = V1.00)
343
     *
344
     * @return bool true if requested version of the module is available
345
     */
346 View Code Duplication
    public function addConfigModuleVersion($moddir, $minversion)
347
    {
348
        $return = false;
349
        $helper = Helper::getHelper($moddir);
350
        if (is_object($helper) && is_object($helper->getModule())) {
351
            $mod_modversion = $helper->getModule()->getVar('version');
0 ignored issues
show
Bug introduced by
The method getModule does only exist in Xmf\Module\Helper, but not in Xoops\Module\Helper.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
352
            $mod_version_f = $mod_modversion / 100;
353
            $min_version_f = $minversion / 100;
354
            $value = sprintf(
355
                _AM_XMF_MODULE_VERSION,
356
                strtoupper($moddir),
357
                $min_version_f,
358
                $mod_version_f
359
            );
360
            if ($mod_modversion >= $minversion) {
361
                $this->addConfigAccept($value);
362
                $return = true;
363
            } else {
364
                $this->addConfigError($value);
365
            }
366
        } else {
367
            $value = sprintf(
368
                _AM_XMF_MODULE_NOTFOUND,
369
                strtoupper($moddir),
370
                $minversion / 100
371
            );
372
            $this->addConfigError($value);
373
        }
374
375
        return $return;
376
    }
377
378
    // the following not part of next generation Xoops\Module\Admin
379
380
    /**
381
     * Are we in a next generation environment?
382
     *
383
     * not part of next generation Xoops\Module\Admin
384
     *
385
     * @return bool true if we are in a post XOOPS 2.5.x environment
386
     */
387
    protected static function isXng()
388
    {
389
        return class_exists('\Xoops', false);
390
    }
391
392
    /**
393
     * Get an appropriate imagePath for menu.php use.
394
     *
395
     * just to help with other admin things than ModuleAdmin
396
     *
397
     * not part of next generation Xoops\Module\Admin
398
     *
399
     * @param string $image icon name to prepend with path
400
     *
401
     * @return string the icon path
402
     */
403
    public static function menuIconPath($image)
404
    {
405
        if (static::isXng()) {
406
            return($image);
407
        } else {
408
            $path = '../../Frameworks/moduleclasses/icons/32/';
409
410
            return($path . $image);
411
        }
412
    }
413
414
    /**
415
     * Get an appropriate URL for system provided icons.
416
     *
417
     * Things which were in Frameworks in 2.5 are in media in later versions,
418
     * making it harder to use and rely on the standard icons.
419
     *
420
     * not part of next generation Xoops\Module\Admin
421
     *
422
     * @param string $name the image name to provide URL for, or blank
423
     *                     to just get the URL path.
424
     * @param string $size the icon size (directory). Valid values are
425
     *                     16, 32 or /. A '/' slash will simply set the
426
     *                     path to the icon directory and append $image.
427
     *
428
     * @return string path to icons
429
     */
430
    public static function iconUrl($name = '', $size = '32')
431
    {
432
        switch ($size) {
433
            case '16':
434
                $path = '16/';
435
                break;
436
            case '/':
437
                $path = '';
438
                break;
439
            case '32':
440
            default:
441
                $path = '32/';
442
                break;
443
        }
444
445
        if (static::isXng()) {
446
            $path = '/media/xoops/images/icons/' . $path;
447
        } else {
448
            $path = '/Frameworks/moduleclasses/icons/' . $path;
449
        }
450
451
        return(XOOPS_URL . $path . $name);
452
    }
453
454
    /**
455
     * set paypal for 2.5.x renderAbout
456
     *
457
     * not part of next generation Xoops\Module\Admin
458
     *
459
     * @param string $paypal PayPal identifier for donate button
460
     *
461
     * @return void
462
     */
463
    public static function setPaypal($paypal = '')
464
    {
465
        static::$paypal = $paypal;
466
    }
467
}
468