Completed
Push — master ( d34ae1...d2cd4a )
by kill
10:24
created

Page::page_links()   D

Complexity

Conditions 18
Paths 41

Size

Total Lines 85
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 58
nc 41
nop 2
dl 0
loc 85
rs 4.7996
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
4
namespace puck\helpers;
5
6
class Page{
7
    /**
8
     * set the number of items per page.
9
     *
10
     * @var numeric
11
     */
12
    private $_perPage;
13
14
    /**
15
     * set get parameter for fetching the page number
16
     *
17
     * @var string
18
     */
19
    private $_instance;
20
21
    /**
22
     * sets the page number.
23
     *
24
     * @var numeric
25
     */
26
    private $_page;
27
28
    /**
29
     * set the limit for the data source
30
     *
31
     * @var string
32
     */
33
    private $_limit;
0 ignored issues
show
Unused Code introduced by
The property $_limit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
    /**
36
     * set the total number of records/items.
37
     *
38
     * @var numeric
39
     */
40
    private $_totalRows = 0;
41
42
43
44
    /**
45
     *  __construct
46
     *
47
     *  pass values when class is istantiated
48
     *
49
     * @param numeric  $_perPage  sets the number of iteems per page
0 ignored issues
show
Documentation introduced by
There is no parameter named $_perPage. Did you maybe mean $perPage?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
50
     * @param numeric  $_instance sets the instance for the GET parameter
0 ignored issues
show
Documentation introduced by
There is no parameter named $_instance. Did you maybe mean $instance?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
51
     */
52
    public function __construct($perPage,$instance){
53
        $this->_instance = $instance;
54
        $this->_perPage = $perPage;
55
        $this->set_instance();
56
    }
57
58
    /**
59
     * get_start
60
     *
61
     * creates the starting point for limiting the dataset
62
     * @return numeric
63
     */
64
    public function get_start(){
65
        return ($this->_page * $this->_perPage) - $this->_perPage;
66
    }
67
68
    /**
69
     * set_instance
70
     *
71
     * sets the instance parameter, if numeric value is 0 then set to 1
72
     *
73
     * @var numeric
74
     */
75
    private function set_instance(){
0 ignored issues
show
Coding Style introduced by
set_instance uses the super-global variable $_GET 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...
76
        $this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]);
0 ignored issues
show
Documentation Bug introduced by
It seems like (int) (!isset($_GET[$thi..._GET[$this->_instance]) of type integer is incompatible with the declared type object<puck\helpers\numeric> of property $_page.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->_page = ($this->_page == 0 ? 1 : $this->_page);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_page == 0 ? 1 : $this->_page of type integer is incompatible with the declared type object<puck\helpers\numeric> of property $_page.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
    }
79
80
    /**
81
     * set_total
82
     *
83
     * collect a numberic value and assigns it to the totalRows
84
     *
85
     * @var numeric
86
     */
87
    public function set_total($_totalRows){
88
        $this->_totalRows = $_totalRows;
89
    }
90
91
    /**
92
     * get_limit
93
     *
94
     * returns the limit for the data source, calling the get_start method and passing in the number of items perp page
95
     *
96
     * @return string
97
     */
98
    public function get_limit(){
99
        return "LIMIT ".$this->get_start().",$this->_perPage";
100
    }
101
102
103
    private function getParam(){
0 ignored issues
show
Coding Style introduced by
getParam uses the super-global variable $_GET 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...
104
        $vars=$_GET;
105
        if(isset($vars[$this->_instance])){
106
            unset($vars[$this->_instance]);
107
        }
108
        $str=http_build_query($vars);
109
        $str ='&'.$str;
110
        return $str;
111
    }
112
113
    /**
114
     * page_links
115
     *
116
     * create the html links for navigating through the dataset
117
     *
118
     * @var sting $path optionally set the path for the link
119
     * @var sting $ext optionally pass in extra parameters to the GET
120
     * @return string returns the html menu
121
     */
122
    public function page_links($path='?',$ext=null)
123
    {
124
        $adjacents = "2";
125
        $prev = $this->_page - 1;
126
        $next = $this->_page + 1;
127
        $lastpage = ceil($this->_totalRows/$this->_perPage);
128
        $lpm1 = $lastpage - 1;
129
        $pagination = "";
130
        if($lastpage > 1)
131
        {
132
            if($ext==null){
133
                $ext=$this->getParam();
134
            }
135
            $pagination .= "<ul class='pager'>";
136
            if ($this->_page > 1)
137
                $pagination.= "<li class='previous'><a href='".$path."$this->_instance=$prev"."$ext'><<</a></li>";
138
            else
139
                $pagination.= "<li class='disabled previous'><span class='disabled'><<</span></li>";
140
141
            if ($lastpage < 7 + ($adjacents * 2))
142
            {
143
                for ($counter = 1; $counter <= $lastpage; $counter++)
144
                {
145
                    if ($counter == $this->_page)
146
                        $pagination.= "<li class='current'><span>$counter</span></li>";
147
                    else
148
                        $pagination.= "<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
149
                }
150
            }
151
            elseif($lastpage > 5 + ($adjacents * 2))
152
            {
153
                if($this->_page < 1 + ($adjacents * 2))
154
                {
155
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
156
                    {
157
                        if ($counter == $this->_page)
158
                            $pagination.= "<li class='current'><span>$counter</span></li>";
159
                        else
160
                            $pagination.= "<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
161
                    }
162
                    $pagination.= "...";
163
                    $pagination.= "<li><a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a></li>";
164
                    $pagination.= "<li><a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a></li>";
165
                }
166
                elseif($lastpage - ($adjacents * 2) > $this->_page && $this->_page > ($adjacents * 2))
167
                {
168
                    $pagination.= "<li><a href='".$path."$this->_instance=1"."$ext'>1</a></li>";
169
                    $pagination.= "<li><a href='".$path."$this->_instance=2"."$ext'>2</a></li>";
170
                    $pagination.= "...";
171
                    for ($counter = $this->_page - $adjacents; $counter <= $this->_page + $adjacents; $counter++)
172
                    {
173
                        if ($counter == $this->_page)
174
                            $pagination.= "<li class='current'><span>$counter</span></li>";
175
                        else
176
                            $pagination.= "<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
177
                    }
178
                    $pagination.= "..";
179
                    $pagination.= "<li><a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a></li>";
180
                    $pagination.= "<li><a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a></li>";
181
                }
182
                else
183
                {
184
                    $pagination.= "<li><a href='".$path."$this->_instance=1"."$ext'>1</a></li>";
185
                    $pagination.= "<li><a href='".$path."$this->_instance=2"."$ext'>2</a></li>";
186
                    $pagination.= "..";
187
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
188
                    {
189
                        if ($counter == $this->_page)
190
                            $pagination.= "<li class='current'><span>$counter</span></li>";
191
                        else
192
                            $pagination.= "<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
193
                    }
194
                }
195
            }
196
197
            if ($this->_page < $counter - 1)
0 ignored issues
show
Bug introduced by
The variable $counter 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...
198
                $pagination.= "<li class='next'><a href='".$path."$this->_instance=$next"."$ext'>>></a></li>";
199
            else
200
                $pagination.= "<li class='next disabled'><span class='disabled'>>></span></li>";
201
            $pagination.= "</ul>\n";
202
        }
203
204
205
        return $pagination;
206
    }
207
}