Passed
Branch master (465698)
by Michael
05:38
created

menu_block.php ➔ menu_block()   F

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 192
Code Lines 137

Duplication

Lines 24
Ratio 12.5 %

Importance

Changes 0
Metric Value
cc 39
eloc 137
nc 13056000
nop 0
dl 24
loc 192
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
//    pedigree
4
//        Copyright 2004, James Cotton
5
//         http://www.dobermannvereniging.nl
6
7
// Include any constants used for internationalizing templates.
8
$moduleDirName = basename(dirname(__DIR__));
9
xoops_loadLanguage('main', $moduleDirName);
10
//xoops_loadLanguage('main', 'pedigree');
11
// Include any common code for this module.
12
//require_once(XOOPS_ROOT_PATH . "/modules/" . $xoopsModule->dirname() . "/include/class_field.php");
13
//require_once(XOOPS_ROOT_PATH . "/modules/" . $xoopsModule->dirname() . "/include/functions.php");
14
require_once $GLOBALS['xoops']->path('modules/' . $moduleDirName . '/class/field.php');
15
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php';
16
17
/**
18
 * @todo: move hard coded language strings to language file
19
 *
20
 * @return XoopsTpl
21
 */
22
function menu_block()
23
{
24
    global $xoopsTpl, $xoopsUser, $pedigree;
25
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
26
    if (!class_exists('PedigreePedigree')) {
27
        $pedigree = PedigreePedigree::getInstance(false);
28
    }
29
/*
30
    //get module configuration
31
    $moduleHandler = xoops_getHandler('module');
32
    $module        = $moduleHandler->getByDirname($moduleDirName);
33
    $configHandler = xoops_getHandler('config');
34
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
35
*/
36
    //colour variables
37
    $colors  = explode(',', $pedigree->getConfig('colourscheme'));
38
    $actlink = $colors[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $actlink is dead and can be removed.
Loading history...
39
    $even    = $colors[1];
0 ignored issues
show
Unused Code introduced by
The assignment to $even is dead and can be removed.
Loading history...
40
    $odd     = $colors[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $odd is dead and can be removed.
Loading history...
41
    $text    = $colors[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $text is dead and can be removed.
Loading history...
42
    $hovlink = $colors[4];
0 ignored issues
show
Unused Code introduced by
The assignment to $hovlink is dead and can be removed.
Loading history...
43
    $head    = $colors[5];
0 ignored issues
show
Unused Code introduced by
The assignment to $head is dead and can be removed.
Loading history...
44
    $body    = $colors[6];
0 ignored issues
show
Unused Code introduced by
The assignment to $body is dead and can be removed.
Loading history...
45
    $title   = $colors[7];
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
46
47
    /*
48
        //inline-css
49
        echo '<style>';
50
        //text-colour
51
        echo 'body {margin: 0;padding: 0;background: ' . $body . ';color: ' . $text . ";font-size: 62.5%; font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; text-align: left;}";
52
        //link-colour
53
        echo 'a, h2 a:hover, h3 a:hover { color: ' . $actlink . '; text-decoration: none; }';
54
        //link hover colour
55
        echo 'a:hover { color: ' . $hovlink . '; text-decoration: underline; }';
56
        //th
57
        echo 'th {padding: 2px;color: #ffffff;background: ' . $title . ';font-family: Verdana, Arial, Helvetica, sans-serif;vertical-align: middle;}';
58
        echo 'td#centercolumn th { color: #fff; background: ' . $title . '; vertical-align: middle; }';
59
        //head
60
        echo '.head {background-color: ' . $head . '; padding: 3px; font-weight: normal;}';
61
        //even
62
        echo '.even {background-color: ' . $even . '; padding: 3px;}';
63
        echo 'tr.even td {background-color: ' . $even . '; padding: 3px;}';
64
        //odd
65
        echo '.odd {background-color: ' . $odd . '; padding: 3px;}';
66
        echo 'tr.odd td {background-color: ' . $odd . '; padding: 3px;}';
67
        echo '</style>';
68
    */
69
70
    //iscurrent user a module admin ?
71
    $xoopsModule = XoopsModule::getByDirname('pedigree');
72
    if ((!empty($xoopsUser)) && ($GLOBALS['xoopsUser'] instanceof XoopsUser) && $xoopsUser->isAdmin($xoopsModule->mid())) {
73
        $modadmin = true;
74
    } else {
75
        $modadmin = false;
76
    }
77
    $counter   = 1;
78
    $menuwidth = 4;
79
80
    $x       = $_SERVER['PHP_SELF'];
81
    $lastpos = my_strrpos($x, '/');
82
    $len     = strlen($x);
83
    $curpage = substr($x, $lastpos, $len);
0 ignored issues
show
Bug introduced by
It seems like $lastpos can also be of type false; however, parameter $start of substr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

83
    $curpage = substr($x, /** @scrutinizer ignore-type */ $lastpos, $len);
Loading history...
84
    if (1 == $pedigree->getConfig('showwelcome')) {
85
        if ('/welcome.php' === $curpage) {
86
            $title = '<b>Welcome</b>';
87
        } else {
88
            $title = 'Welcome';
89
        }
90
        $menuarray[] = array('title' => $title, 'link' => 'welcome.php', 'counter' => $counter);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$menuarray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $menuarray = array(); before regardless.
Loading history...
91
        ++$counter;
92
        if ($counter == $menuwidth) {
93
            $counter = 1;
94
        }
95
    }
96
    if ($curpage === '/index.php' || $curpage === '/result.php') {
97
        $title = '<b>View/Search ' . $pedigree->getConfig('animalTypes') . '</b>';
98
    } else {
99
        $title = 'View/Search ' . $pedigree->getConfig('animalTypes');
100
    }
101
    $menuarray[] = array('title' => $title, 'link' => 'index.php', 'counter' => $counter);
102
    ++$counter;
103
    if ($counter == $menuwidth) {
104
        $counter = 1;
105
    }
106
    if ($curpage === '/add_dog.php') {
107
        $title = '<b>Add a ' . $pedigree->getConfig('animalType') . '</b>';
108
    } else {
109
        $title = 'Add a ' . $pedigree->getConfig('animalType');
110
    }
111
    $menuarray[] = array('title' => $title, 'link' => 'add_dog.php', 'counter' => $counter);
112
    ++$counter;
113
    if ($counter == $menuwidth) {
114
        $counter = 1;
115
    }
116
    if ('1' == $pedigree->getConfig('uselitter')) {
117
        if ($curpage === '/add_litter.php') {
118
            $title = '<b>Add a ' . $pedigree->getConfig('litter') . '</b>';
119
        } else {
120
            $title = 'Add a ' . $pedigree->getConfig('litter');
121
        }
122
        $menuarray[] = array('title' => $title, 'link' => 'add_litter.php', 'counter' => $counter);
123
        ++$counter;
124
        if ($counter == $menuwidth) {
125
            $counter = 1;
126
        }
127
    }
128
    if ('1' == $pedigree->getConfig('ownerbreeder')) {
129
        if ($curpage === '/breeder.php' || $curpage === '/owner.php') {
130
            $title = '<b>View owners/breeders</b>';
131
        } else {
132
            $title = 'View owners/breeders';
133
        }
134
        $menuarray[] = array('title' => $title, 'link' => 'breeder.php', 'counter' => $counter);
135
        ++$counter;
136
        if ($counter == $menuwidth) {
137
            $counter = 1;
138
        }
139
        if ($curpage === '/add_breeder.php') {
140
            $title = '<b>Add an owner/breeder</b>';
141
        } else {
142
            $title = 'Add an owner/breeder';
143
        }
144
        $menuarray[] = array('title' => $title, 'link' => 'add_breeder.php', 'counter' => $counter);
145
        ++$counter;
146
        if ($counter == $menuwidth) {
147
            $counter = 1;
148
        }
149
    }
150
    if ($curpage === '/advanced.php') {
151
        $title = '<b>Advanced info</b>';
152
    } else {
153
        $title = 'Advanced info';
154
    }
155
    $menuarray[] = array('title' => $title, 'link' => 'advanced.php', 'counter' => $counter);
156
    ++$counter;
157
    if ($counter == $menuwidth) {
158
        $counter = 1;
159
    }
160
    if ('1' == $pedigree->getConfig('proversion')) {
161
        if ($curpage === '/virtual.php') {
162
            $title = '<b>Virtual mating</b>';
163
        } else {
164
            $title = 'Virtual Mating';
165
        }
166
        $menuarray[] = array('title' => $title, 'link' => 'virtual.php', 'counter' => $counter);
167
        ++$counter;
168
        if ($counter == $menuwidth) {
169
            $counter = 1;
170
        }
171
    }
172
    if ($curpage === '/latest.php') {
173
        $title = '<b>latest additions</b>';
174
    } else {
175
        $title = 'latest additions';
176
    }
177
    $menuarray[] = array('title' => $title, 'link' => 'latest.php', 'counter' => $counter);
178
    ++$counter;
179
    if ($counter == $menuwidth) {
180
        $counter = 1;
181
    }
182
    if ($modadmin === true) {
183
        if ($curpage === '/tools.php') {
184
            $title = '<b>Webmaster tools</b>';
185
        } else {
186
            $title = 'Webmaster tools';
187
        }
188
        $menuarray[] = array('title' => $title, 'link' => 'tools.php?op=index', 'counter' => $counter);
189
        ++$counter;
190
        if ($counter == $menuwidth) {
191
            $counter = 1;
192
        }
193
        $title       = 'Logout';
194
        $menuarray[] = array('title' => $title, 'link' => '../../user.php?op=logout', 'counter' => $counter);
195
        ++$counter;
196
        if ($counter == $menuwidth) {
197
            $counter = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $counter is dead and can be removed.
Loading history...
198
        }
199
    } else {
200
        if ($curpage === '/user.php') {
201
            $title = '<b>User login</b>';
202
        } else {
203
            $title = 'User login';
204
        }
205
        $menuarray[] = array('title' => $title, 'link' => '../../user.php', 'counter' => $counter);
206
        ++$counter;
207
        if ($counter == $menuwidth) {
208
            $counter = 1;
209
        }
210
    }
211
212
    //create path taken
213
    //showpath();
214
    $xoopsTpl->assign('menuarray', $menuarray);
215
    //return the template contents
216
    return $xoopsTpl;
217
}
218
219
/**
220
 * @param     $haystack
221
 * @param     $needle
222
 * @param int $offset
223
 *
224
 * @return bool|int
225
 */
226
function my_strrpos($haystack, $needle, $offset = 0)
227
{
228
    // same as strrpos, except $needle can be a string
229
    $strrpos = false;
230
    if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
231
        $strlen = strlen($haystack);
232
        $strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
233
        if (is_numeric($strpos)) {
0 ignored issues
show
introduced by
The condition is_numeric($strpos) is always true.
Loading history...
234
            $strrpos = $strlen - $strpos - strlen($needle);
235
        }
236
    }
237
238
    return $strrpos;
239
}
240