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

MylinksPageNav::renderNav()   C

Complexity

Conditions 15
Paths 6

Size

Total Lines 36
Code Lines 25

Duplication

Lines 14
Ratio 38.89 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 14
loc 36
rs 5.0504
cc 15
eloc 25
nc 6
nop 1

How to fix   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
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