MylinksPageNav   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 148
Duplicated Lines 18.92 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 28
loc 148
rs 9.2
c 0
b 0
f 0
wmc 40
lcom 1
cbo 0

4 Methods

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

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
/**
4
 * Class MylinksPageNav
5
 */
6
class MylinksPageNav
7
{
8
    public $total;
9
    public $perpage;
10
    public $current;
11
    public $url;
12
13
    /**
14
     * mylinkspagenav constructor.
15
     * @param        $total_items
16
     * @param        $items_perpage
17
     * @param        $current_start
18
     * @param string $start_name
19
     * @param string $extra_arg
20
     */
21
    public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '')
22
    {
23
        $this->total   = (int)$total_items;
24
        $this->perpage = (int)$items_perpage;
25
        $this->current = (int)$current_start;
26
        if ($extra_arg != '' && (substr($extra_arg, -5) != '&amp;' || substr($extra_arg, -1) != '&')) {
27
            $extra_arg .= '&amp;';
28
        }
29
        $this->url = xoops_getenv('PHP_SELF') . '?' . $extra_arg . trim($start_name) . '=';
30
    }
31
32
    /**
33
     * @param int $offset
34
     * @return string
35
     */
36
    public function renderNav($offset = 5)
37
    {
38
        $ret = '';
39
        if ($this->total <= $this->perpage) {
40
            return $ret;
41
        }
42
        $total_pages = ceil($this->total / $this->perpage);
43
        if ($total_pages > 1) {
44
            $prev = $this->current - $this->perpage;
45
            if ($prev >= 0) {
46
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
47
            }
48
            $counter      = 1;
49
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
50 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...
51
                if ($counter == $current_page) {
52
                    $ret .= '<strong>(' . $counter . ')</strong> ';
53
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1
54
                          || $counter == $total_pages
55
                ) {
56
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
57
                        $ret .= '... ';
58
                    }
59
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
60
                    if ($counter == 1 && $current_page > 1 + $offset) {
61
                        $ret .= '... ';
62
                    }
63
                }
64
                $counter++;
65
            }
66
            $next = $this->current + $this->perpage;
67
            if ($this->total > $next) {
68
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
69
            }
70
        }
71
72
        return $ret;
73
    }
74
75
    /**
76
     * @param bool $showbutton
77
     * @return string|void
78
     */
79
    public function renderSelect($showbutton = false)
80
    {
81
        if ($this->total < $this->perpage) {
82
            return;
83
        }
84
        $total_pages = ceil($this->total / $this->perpage);
85
        $ret         = '';
86
        if ($total_pages > 1) {
87
            $ret = '<form name="pagenavform" action="' . xoops_getenv('PHP_SELF') . '">';
88
            $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
89
            $counter      = 1;
90
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
91
            while ($counter <= $total_pages) {
92
                if ($counter == $current_page) {
93
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected="selected">' . $counter . '</option>';
94
                } else {
95
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>';
96
                }
97
                $counter++;
98
            }
99
            $ret .= '</select>';
100
            if ($showbutton) {
101
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
102
            }
103
            $ret .= '</form>';
104
        }
105
106
        return $ret;
107
    }
108
109
    /**
110
     * @param int $offset
111
     * @return string|void
112
     */
113
    public function renderImageNav($offset = 5)
114
    {
115
        if ($this->total < $this->perpage) {
116
            return;
117
        }
118
        $total_pages = ceil($this->total / $this->perpage);
119
        $ret         = '';
120
        if ($total_pages > 1) {
121
            $ret  = '<table><tr>';
122
            $prev = $this->current - $this->perpage;
123
            if ($prev >= 0) {
124
                $ret .= '<td class="pagneutral"><a href="' . $this->url . $prev . '">&lt;</a></td><td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td>';
125
            }
126
            $counter      = 1;
127
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
128 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...
129
                if ($counter == $current_page) {
130
                    $ret .= '<td class="pagact"><b>' . $counter . '</b></td>';
131
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1
132
                          || $counter == $total_pages
133
                ) {
134
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
135
                        $ret .= '<td class="paginact">...</td>';
136
                    }
137
                    $ret .= '<td class="paginact"><a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a></td>';
138
                    if ($counter == 1 && $current_page > 1 + $offset) {
139
                        $ret .= '<td class="paginact">...</td>';
140
                    }
141
                }
142
                $counter++;
143
            }
144
            $next = $this->current + $this->perpage;
145
            if ($this->total > $next) {
146
                $ret .= '<td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td><td class="pagneutral"><a href="' . $this->url . $next . '">&gt;</a></td>';
147
            }
148
            $ret .= '</tr></table>';
149
        }
150
151
        return $ret;
152
    }
153
}
154