Completed
Push — master ( 871d94...af939e )
by Michael
03:14
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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');
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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 . '/include/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()
0 ignored issues
show
Coding Style introduced by
menu_block 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...
Coding Style introduced by
menu_block uses the super-global variable $_SERVER 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...
23
{
24
    global $xoopsTpl, $xoopsUser;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
25
    $moduleDirName = basename(dirname(__DIR__));
26
    //get module configuration
27
    $moduleHandler = xoops_getHandler('module');
28
    $module        = $moduleHandler->getByDirname($moduleDirName);
29
    $configHandler = xoops_getHandler('config');
30
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
31
32
    //colour variables
33
    $colors  = explode(',', $moduleConfig['colourscheme']);
34
    $actlink = $colors[0];
0 ignored issues
show
Unused Code introduced by
$actlink 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...
35
    $even    = $colors[1];
0 ignored issues
show
Unused Code introduced by
$even 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...
36
    $odd     = $colors[2];
0 ignored issues
show
Unused Code introduced by
$odd 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...
37
    $text    = $colors[3];
0 ignored issues
show
Unused Code introduced by
$text 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...
38
    $hovlink = $colors[4];
0 ignored issues
show
Unused Code introduced by
$hovlink 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...
39
    $head    = $colors[5];
0 ignored issues
show
Unused Code introduced by
$head 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...
40
    $body    = $colors[6];
0 ignored issues
show
Unused Code introduced by
$body 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...
41
    $title   = $colors[7];
0 ignored issues
show
Unused Code introduced by
$title 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...
42
43
    /*
44
        //inline-css
45
        echo '<style>';
46
        //text-colour
47
        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;}";
48
        //link-colour
49
        echo 'a, h2 a:hover, h3 a:hover { color: ' . $actlink . '; text-decoration: none; }';
50
        //link hover colour
51
        echo 'a:hover { color: ' . $hovlink . '; text-decoration: underline; }';
52
        //th
53
        echo 'th {padding: 2px;color: #ffffff;background: ' . $title . ';font-family: Verdana, Arial, Helvetica, sans-serif;vertical-align: middle;}';
54
        echo 'td#centercolumn th { color: #fff; background: ' . $title . '; vertical-align: middle; }';
55
        //head
56
        echo '.head {background-color: ' . $head . '; padding: 3px; font-weight: normal;}';
57
        //even
58
        echo '.even {background-color: ' . $even . '; padding: 3px;}';
59
        echo 'tr.even td {background-color: ' . $even . '; padding: 3px;}';
60
        //odd
61
        echo '.odd {background-color: ' . $odd . '; padding: 3px;}';
62
        echo 'tr.odd td {background-color: ' . $odd . '; padding: 3px;}';
63
        echo '</style>';
64
    */
65
66
    //iscurrent user a module admin ?
67
    $xoopsModule = XoopsModule::getByDirname('pedigree');
68
    if ((!empty($xoopsUser)) && ($GLOBALS['xoopsUser'] instanceof XoopsUser) && $xoopsUser->isAdmin($xoopsModule->mid())) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
69
        $modadmin = true;
70
    } else {
71
        $modadmin = false;
72
    }
73
    $counter   = 1;
74
    $menuwidth = 4;
75
76
    $x       = $_SERVER['PHP_SELF'];
77
    $lastpos = my_strrpos($x, '/');
78
    $len     = strlen($x);
79
    $curpage = substr($x, $lastpos, $len);
80
    if (1 == $moduleConfig['showwelcome']) {
81
        if ('/welcome.php' === $curpage) {
82
            $title = '<b>Welcome</b>';
83
        } else {
84
            $title = 'Welcome';
85
        }
86
        $menuarray[] = array('title' => $title, 'link' => 'welcome.php', 'counter' => $counter);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$menuarray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $menuarray = 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...
87
        ++$counter;
88
        if ($counter == $menuwidth) {
89
            $counter = 1;
90
        }
91
    }
92
    if ($curpage === '/index.php' || $curpage === '/result.php') {
93
        $title = '<b>View/Search ' . $moduleConfig['animalTypes'] . '</b>';
94
    } else {
95
        $title = 'View/Search ' . $moduleConfig['animalTypes'];
96
    }
97
    $menuarray[] = array('title' => $title, 'link' => 'index.php', 'counter' => $counter);
0 ignored issues
show
Bug introduced by
The variable $menuarray 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...
98
    ++$counter;
99
    if ($counter == $menuwidth) {
100
        $counter = 1;
101
    }
102
    if ($curpage === '/add_dog.php') {
103
        $title = '<b>Add a ' . $moduleConfig['animalType'] . '</b>';
104
    } else {
105
        $title = 'Add a ' . $moduleConfig['animalType'];
106
    }
107
    $menuarray[] = array('title' => $title, 'link' => 'add_dog.php', 'counter' => $counter);
108
    ++$counter;
109
    if ($counter == $menuwidth) {
110
        $counter = 1;
111
    }
112 View Code Duplication
    if ($moduleConfig['uselitter'] == '1') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
        if ($curpage === '/add_litter.php') {
114
            $title = '<b>Add a ' . $moduleConfig['litter'] . '</b>';
115
        } else {
116
            $title = 'Add a ' . $moduleConfig['litter'];
117
        }
118
        $menuarray[] = array('title' => $title, 'link' => 'add_litter.php', 'counter' => $counter);
119
        ++$counter;
120
        if ($counter == $menuwidth) {
121
            $counter = 1;
122
        }
123
    }
124
    if ($moduleConfig['ownerbreeder'] == '1') {
125
        if ($curpage === '/breeder.php' || $curpage === '/owner.php') {
126
            $title = '<b>View owners/breeders</b>';
127
        } else {
128
            $title = 'View owners/breeders';
129
        }
130
        $menuarray[] = array('title' => $title, 'link' => 'breeder.php', 'counter' => $counter);
131
        ++$counter;
132
        if ($counter == $menuwidth) {
133
            $counter = 1;
134
        }
135
        if ($curpage === '/add_breeder.php') {
136
            $title = '<b>Add an owner/breeder</b>';
137
        } else {
138
            $title = 'Add an owner/breeder';
139
        }
140
        $menuarray[] = array('title' => $title, 'link' => 'add_breeder.php', 'counter' => $counter);
141
        ++$counter;
142
        if ($counter == $menuwidth) {
143
            $counter = 1;
144
        }
145
    }
146
    if ($curpage === '/advanced.php') {
147
        $title = '<b>Advanced info</b>';
148
    } else {
149
        $title = 'Advanced info';
150
    }
151
    $menuarray[] = array('title' => $title, 'link' => 'advanced.php', 'counter' => $counter);
152
    ++$counter;
153
    if ($counter == $menuwidth) {
154
        $counter = 1;
155
    }
156
    if ($moduleConfig['proversion'] == '1') {
157
        if ($curpage === '/virtual.php') {
158
            $title = '<b>Virtual mating</b>';
159
        } else {
160
            $title = 'Virtual Mating';
161
        }
162
        $menuarray[] = array('title' => $title, 'link' => 'virtual.php', 'counter' => $counter);
163
        ++$counter;
164
        if ($counter == $menuwidth) {
165
            $counter = 1;
166
        }
167
    }
168
    if ($curpage === '/latest.php') {
169
        $title = '<b>latest additions</b>';
170
    } else {
171
        $title = 'latest additions';
172
    }
173
    $menuarray[] = array('title' => $title, 'link' => 'latest.php', 'counter' => $counter);
174
    ++$counter;
175
    if ($counter == $menuwidth) {
176
        $counter = 1;
177
    }
178
    if ($modadmin === true) {
179
        if ($curpage === '/tools.php') {
180
            $title = '<b>Webmaster tools</b>';
181
        } else {
182
            $title = 'Webmaster tools';
183
        }
184
        $menuarray[] = array('title' => $title, 'link' => 'tools.php?op=index', 'counter' => $counter);
185
        ++$counter;
186
        if ($counter == $menuwidth) {
187
            $counter = 1;
188
        }
189
        $title       = 'Logout';
190
        $menuarray[] = array('title' => $title, 'link' => '../../user.php?op=logout', 'counter' => $counter);
191
        ++$counter;
192
        if ($counter == $menuwidth) {
193
            $counter = 1;
0 ignored issues
show
Unused Code introduced by
$counter 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...
194
        }
195 View Code Duplication
    } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
196
        if ($curpage === '/user.php') {
197
            $title = '<b>User login</b>';
198
        } else {
199
            $title = 'User login';
200
        }
201
        $menuarray[] = array('title' => $title, 'link' => '../../user.php', 'counter' => $counter);
202
        ++$counter;
203
        if ($counter == $menuwidth) {
204
            $counter = 1;
0 ignored issues
show
Unused Code introduced by
$counter 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...
205
        }
206
    }
207
208
    //create path taken
209
    //showpath();
210
    $xoopsTpl->assign('menuarray', $menuarray);
211
    //return the template contents
212
    return $xoopsTpl;
213
}
214
215
/**
216
 * @param     $haystack
217
 * @param     $needle
218
 * @param int $offset
219
 *
220
 * @return bool|int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double|false?

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...
221
 */
222 View Code Duplication
function my_strrpos($haystack, $needle, $offset = 0)
0 ignored issues
show
Duplication introduced by
This function 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...
223
{
224
    // same as strrpos, except $needle can be a string
225
    $strrpos = false;
226
    if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
227
        $strlen = strlen($haystack);
228
        $strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
229
        if (is_numeric($strpos)) {
230
            $strrpos = $strlen - $strpos - strlen($needle);
231
        }
232
    }
233
234
    return $strrpos;
235
}
236