Completed
Push — master ( 234b0e...1578b3 )
by Michael
14s
created

MylinksPageNav   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 124
Duplicated Lines 22.58 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 40
c 4
b 0
f 1
lcom 1
cbo 0
dl 28
loc 124
rs 8.2608

4 Methods

Rating   Name   Duplication   Size   Complexity  
C renderNav() 14 36 15
B renderSelect() 0 29 6
C renderImageNav() 14 38 15
A __construct() 0 10 4

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MylinksPageNav often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MylinksPageNav, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
class MylinksPageNav
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    var $total;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $total.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
6
    var $perpage;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $perpage.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
7
    var $current;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $current.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
8
    var $url;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $url.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
9
10
    function __construct($total_items, $items_perpage, $current_start, $start_name= 'start', $extra_arg= '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
    {
12
        $this->total   = intval($total_items);
13
        $this->perpage = intval($items_perpage);
14
        $this->current = intval($current_start);
15
        if ( $extra_arg != '' && ( substr($extra_arg, -5) != '&amp;' || substr($extra_arg, -1) != '&' ) ) {
16
            $extra_arg .= '&amp;';
17
        }
18
        $this->url = xoops_getenv('PHP_SELF') . '?' . $extra_arg . trim($start_name) . '=';
19
    }
20
21
    function renderNav($offset = 5)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        $ret = '';
24
        if ( $this->total <= $this->perpage ) {
25
            return $ret;
26
        }
27
        $total_pages = ceil($this->total / $this->perpage);
28
        if ( $total_pages > 1 ) {
29
            $prev = $this->current - $this->perpage;
30
            if ( $prev >= 0 ) {
31
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
32
            }
33
            $counter = 1;
34
            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
35 View Code Duplication
            while ( $counter <= $total_pages ) {
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...
36
                if ( $counter == $current_page ) {
37
                    $ret .= '<strong>(' . $counter . ')</strong> ';
38
                } elseif ( ($counter > $current_page-$offset && $counter < $current_page + $offset ) || $counter == 1 || $counter == $total_pages ) {
39
                    if ( $counter == $total_pages && $current_page < $total_pages - $offset ) {
40
                        $ret .= '... ';
41
                    }
42
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
43
                    if ( $counter == 1 && $current_page > 1 + $offset ) {
44
                        $ret .= '... ';
45
                    }
46
                }
47
                $counter++;
48
            }
49
            $next = $this->current + $this->perpage;
50
            if ( $this->total > $next ) {
51
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
52
            }
53
        }
54
55
        return $ret;
56
    }
57
58
    function renderSelect($showbutton = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
    {
60
        if ( $this->total < $this->perpage ) {
61
            return;
62
        }
63
        $total_pages = ceil($this->total / $this->perpage);
64
        $ret = '';
65
        if ( $total_pages > 1 ) {
66
            $ret = '<form name="pagenavform" action="' . xoops_getenv('PHP_SELF') . '">';
67
            $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
68
            $counter = 1;
69
            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
70
            while ( $counter <= $total_pages ) {
71
                if ( $counter == $current_page ) {
72
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected="selected">' . $counter.'</option>';
73
                } else {
74
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">'.$counter . '</option>';
75
                }
76
                $counter++;
77
            }
78
            $ret .= '</select>';
79
            if ($showbutton) {
80
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
81
            }
82
            $ret .= '</form>';
83
        }
84
85
        return $ret;
86
    }
87
88
    function renderImageNav($offset = 5)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
    {
90
        if ( $this->total < $this->perpage ) {
91
            return;
92
        }
93
        $total_pages = ceil($this->total / $this->perpage);
94
        $ret = '';
95
        if ( $total_pages > 1 ) {
96
            $ret = '<table><tr>';
97
            $prev = $this->current - $this->perpage;
98
            if ( $prev >= 0 ) {
99
                $ret .= '<td class="pagneutral"><a href="'.$this->url.$prev.'">&lt;</a></td><td><img src="'.XOOPS_URL.'/images/blank.gif" width="6" alt=""></td>';
100
            }
101
            $counter = 1;
102
            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
103 View Code Duplication
            while ( $counter <= $total_pages ) {
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...
104
                if ( $counter == $current_page ) {
105
                    $ret .= '<td class="pagact"><b>'.$counter.'</b></td>';
106
                } elseif ( ($counter > $current_page-$offset && $counter < $current_page + $offset ) || $counter == 1 || $counter == $total_pages ) {
107
                    if ( $counter == $total_pages && $current_page < $total_pages - $offset ) {
108
                        $ret .= '<td class="paginact">...</td>';
109
                    }
110
                    $ret .= '<td class="paginact"><a href="'.$this->url.(($counter - 1) * $this->perpage).'">'.$counter.'</a></td>';
111
                    if ( $counter == 1 && $current_page > 1 + $offset ) {
112
                        $ret .= '<td class="paginact">...</td>';
113
                    }
114
                }
115
                $counter++;
116
            }
117
            $next = $this->current + $this->perpage;
118
            if ( $this->total > $next ) {
119
                $ret .= '<td><img src="'.XOOPS_URL.'/images/blank.gif" width="6" alt=""></td><td class="pagneutral"><a href="'.$this->url.$next.'">&gt;</a></td>';
120
            }
121
            $ret .= '</tr></table>';
122
        }
123
124
        return $ret;
125
    }
126
}
127