APCalAdminMenuHandler   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 201
Duplicated Lines 13.93 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 28
loc 201
rs 9.84
c 0
b 0
f 0
wmc 32
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAddon() 0 4 1
A addMenuTop() 0 8 2
A addMenuTopArray() 14 14 5
A addMenuTabs() 0 8 2
A addMenuTabsArray() 14 14 5
A addHeader() 0 4 1
A addSubHeader() 0 4 1
A breadcrumb_nav() 0 22 4
C render() 0 59 10

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
 * Class for tab navigation
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
  * @copyright   {@link http://xoops.org/ XOOPS Project}
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      John Neill (AKA Catzwolf)
15
 * @author      Andricq Nicolas (AKA MusS)
16
 */
17
18
defined('XOOPS_ROOT_PATH') || die();
19
20
/**
21
 * Class APCalAdminMenuHandler
22
 */
23
class APCalAdminMenuHandler
24
{
25
    /**
26
     *
27
     * @var string
28
     */
29
    public $_menutop  = array();
30
    public $_menutabs = array();
31
    public $_obj;
32
    public $_header;
33
    public $_subheader;
34
35
    /**
36
     * Constructor
37
     */
38
    public function __construct()
39
    {
40
        global $xoopsModule;
41
        $this->_obj = $xoopsModule;
42
    }
43
44
    /**
45
     * @param $addon
46
     */
47
    public function getAddon($addon)
48
    {
49
        $this->_obj = $addon;
50
    }
51
52
    /**
53
     * @param        $value
54
     * @param string $name
55
     */
56
    public function addMenuTop($value, $name = '')
57
    {
58
        if ($name !== '') {
59
            $this->_menutop[$value] = $name;
60
        } else {
61
            $this->_menutop[$value] = $value;
62
        }
63
    }
64
65
    /**
66
     * @param      $options
67
     * @param bool $multi
68
     */
69 View Code Duplication
    public function addMenuTopArray($options, $multi = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        if (is_array($options)) {
72
            if ($multi === true) {
73
                foreach ($options as $k => $v) {
74
                    $this->addOptionTop($k, $v);
0 ignored issues
show
Bug introduced by
The method addOptionTop() does not seem to exist on object<APCalAdminMenuHandler>.

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...
75
                }
76
            } else {
77
                foreach ($options as $k) {
78
                    $this->addOptiontop($k, $k);
0 ignored issues
show
Bug introduced by
The method addOptiontop() does not seem to exist on object<APCalAdminMenuHandler>.

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...
79
                }
80
            }
81
        }
82
    }
83
84
    /**
85
     * @param        $value
86
     * @param string $name
87
     */
88
    public function addMenuTabs($value, $name = '')
89
    {
90
        if ($name !== '') {
91
            $this->_menutabs[$value] = $name;
92
        } else {
93
            $this->_menutabs[$value] = $value;
94
        }
95
    }
96
97
    /**
98
     * @param      $options
99
     * @param bool $multi
100
     */
101 View Code Duplication
    public function addMenuTabsArray($options, $multi = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        if (is_array($options)) {
104
            if ($multi === true) {
105
                foreach ($options as $k => $v) {
106
                    $this->addMenuTabsTop($k, $v);
0 ignored issues
show
Bug introduced by
The method addMenuTabsTop() does not exist on APCalAdminMenuHandler. Did you maybe mean addMenuTabs()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
107
                }
108
            } else {
109
                foreach ($options as $k) {
110
                    $this->addMenuTabsTop($k, $k);
0 ignored issues
show
Bug introduced by
The method addMenuTabsTop() does not exist on APCalAdminMenuHandler. Did you maybe mean addMenuTabs()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
111
                }
112
            }
113
        }
114
    }
115
116
    /**
117
     * @param $value
118
     */
119
    public function addHeader($value)
120
    {
121
        $this->_header = $value;
122
    }
123
124
    /**
125
     * @param $value
126
     */
127
    public function addSubHeader($value)
128
    {
129
        $this->_subheader = $value;
130
    }
131
132
    /**
133
     * @param  string $basename
134
     * @return string
135
     */
136
    public function breadcrumb_nav($basename = 'Home')
137
    {
138
        global $bc_site, $bc_label;
139
        $site       = $bc_site;
140
        $return_str = "<a href=\"/\">$basename</a>";
141
        $str        = substr(dirname(xoops_getenv('PHP_SELF')), 1);
142
143
        $arr = explode('/', $str);
144
        $num = count($arr);
145
146
        if ($num > 1) {
147
            foreach ($arr as $val) {
148
                $return_str .= ' &gt; <a href="' . $site . $val . '/">' . $bc_label[$val] . '</a>';
149
                $site       .= $val . '/';
150
            }
151
        } elseif ($num == 1) {
152
            $arr        = $str;
153
            $return_str .= ' &gt; <a href="' . $bc_site . $arr . '/">' . $bc_label[$arr] . '</a>';
154
        }
155
156
        return $return_str;
157
    }
158
159
    /**
160
     * @param  int  $currentoption
161
     * @param  bool $display
162
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
163
     */
164
    public function render($currentoption = 1, $display = true)
165
    {
166
        global $modversion;
167
        $_dirname = $this->_obj->getVar('dirname');
168
        $i        = 0;
169
170
        /**
171
         * Selects current menu tab
172
         */
173
        foreach ($this->_menutabs as $k => $menus) {
174
            $menuItems[] = $menus;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$menuItems was never initialized. Although not strictly required by PHP, it is generally a good practice to add $menuItems = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
175
        }
176
        $breadcrumb                = $menuItems[$currentoption];
0 ignored issues
show
Bug introduced by
The variable $menuItems does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
177
        $menuItems[$currentoption] = 'current';
178
        //Not the best method of adding CSS but the only method available at the moment since xoops is shitty with the backend
179
        $menu = '<style type="text/css" media="screen">@import "' . XOOPS_URL . '/modules/' . $this->_obj->getVar('dirname') . '/css/menu.css";</style>';
180
        $menu .= "<div id='buttontop_mod'>";
181
        $menu .= "<table style='width: 100%; padding: 0;' cellspacing='0'>\n<tr>";
182
        $menu .= "<td style='font-size: 10px; text-align: left; color: #2F5376; padding: 0 6px; line-height: 18px;'>";
183
        foreach ($this->_menutop as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this->_menutop of type string is not traversable.
Loading history...
184
            $menu .= " <a href=\"$k\">$v</a> |";
185
        }
186
        $menu = substr($menu, 0, -1);
187
188
        $menu .= '</td>';
189
        $menu .= "<td style='text-align: right;'><strong>" . $this->_obj->getVar('name') . '</strong> : ' . $breadcrumb . '</td>';
190
        $menu .= "</tr>\n</table>\n";
191
        $menu .= "</div>\n";
192
        $menu .= "<div id='buttonbar_mod'><ul>";
193
        foreach ($this->_menutabs as $k => $v) {
194
            $menu .= "<li id='" . $menuItems[$i] . "'><a href='" . XOOPS_URL . '/modules/' . $this->_obj->getVar('dirname') . '/' . $k . "'><span>$v</span></a></li>\n";
195
            ++$i;
196
        }
197
        $menu .= "</ul>\n</div>\n";
198
        if ($this->_header) {
199
            $menu .= "<h4 class='admin_header'>";
200
            if (isset($modversion['name'])) {
201
                if ($modversion['image'] && $this->_obj->getVar('mid') == 1) {
202
                    $system_image = XOOPS_URL . '/modules/system/images/system/' . $modversion['image'];
203
                } else {
204
                    $system_image = XOOPS_URL . '/modules/' . $_dirname . '/assets/images/' . $modversion['image'];
205
                }
206
                $menu .= "<img src='$system_image' align='middle' height='32' width='32' alt='' />";
207
                $menu .= ' ' . $modversion['name'] . "</h4>\n";
208
            } else {
209
                $menu .= ' ' . $this->_header . "</h4>\n";
210
            }
211
        }
212
        if ($this->_subheader) {
213
            $menu .= "<div class='admin_subheader'>" . $this->_subheader . "</div>\n";
214
        }
215
        $menu .= '<div class="clear">&nbsp;</div>';
216
        unset($this->_obj);
217
        if ($display === true) {
218
            echo $menu;
219
        } else {
220
            return $menu;
221
        }
222
    }
223
}
224