Page::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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
50
     * @param numeric  $instance sets the instance for the GET parameter
51
     */
52
    public function __construct($perPage, $instance) {
53
        $this->_instance=$instance;
0 ignored issues
show
Documentation Bug introduced by
It seems like $instance of type object<puck\helpers\numeric> is incompatible with the declared type string of property $_instance.

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...
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
142
            if ($lastpage < 7 + ($adjacents * 2))
143
            {
144
                for ($counter=1; $counter <= $lastpage; $counter++)
145
                {
146
                    if ($counter == $this->_page) {
147
                                            $pagination.="<li class='current'><span>$counter</span></li>";
148
                    } else {
149
                                            $pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
150
                    }
151
                }
152
            } elseif ($lastpage > 5 + ($adjacents * 2))
153
            {
154
                if ($this->_page < 1 + ($adjacents * 2))
155
                {
156
                    for ($counter=1; $counter < 4 + ($adjacents * 2); $counter++)
157
                    {
158
                        if ($counter == $this->_page) {
159
                                                    $pagination.="<li class='current'><span>$counter</span></li>";
160
                        } else {
161
                                                    $pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
162
                        }
163
                    }
164
                    $pagination.="...";
165
                    $pagination.="<li><a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a></li>";
166
                    $pagination.="<li><a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a></li>";
167
                } elseif ($lastpage - ($adjacents * 2) > $this->_page && $this->_page > ($adjacents * 2))
168
                {
169
                    $pagination.="<li><a href='".$path."$this->_instance=1"."$ext'>1</a></li>";
170
                    $pagination.="<li><a href='".$path."$this->_instance=2"."$ext'>2</a></li>";
171
                    $pagination.="...";
172
                    for ($counter=$this->_page - $adjacents; $counter <= $this->_page + $adjacents; $counter++)
173
                    {
174
                        if ($counter == $this->_page) {
175
                                                    $pagination.="<li class='current'><span>$counter</span></li>";
176
                        } else {
177
                                                    $pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
178
                        }
179
                    }
180
                    $pagination.="..";
181
                    $pagination.="<li><a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a></li>";
182
                    $pagination.="<li><a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a></li>";
183
                } else
184
                {
185
                    $pagination.="<li><a href='".$path."$this->_instance=1"."$ext'>1</a></li>";
186
                    $pagination.="<li><a href='".$path."$this->_instance=2"."$ext'>2</a></li>";
187
                    $pagination.="..";
188
                    for ($counter=$lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
189
                    {
190
                        if ($counter == $this->_page) {
191
                                                    $pagination.="<li class='current'><span>$counter</span></li>";
192
                        } else {
193
                                                    $pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>";
194
                        }
195
                    }
196
                }
197
            }
198
199
            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...
200
                            $pagination.="<li class='next'><a href='".$path."$this->_instance=$next"."$ext'>>></a></li>";
201
            } else {
202
                            $pagination.="<li class='next disabled'><span class='disabled'>>></span></li>";
203
            }
204
            $pagination.="</ul>\n";
205
        }
206
207
208
        return $pagination;
209
    }
210
}