GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Cf_PHPPaginator::show()   F
last analyzed

Complexity

Conditions 13
Paths 1200

Size

Total Lines 42
Code Lines 21

Duplication

Lines 4
Ratio 9.52 %

Importance

Changes 0
Metric Value
cc 13
eloc 21
nc 1200
nop 0
dl 4
loc 42
rs 2.7716
c 0
b 0
f 0

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
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * @category   
19
 * @package    sistema/ayudantes
20
 * @copyright  Copyright (c) 2006 - 2014 webcol.net (http://www.webcol.net/calima)
21
 * @license	https://github.com/webcol/Calima/blob/master/LICENSE	MIT
22
 * @version	##BETA 1.0##, ##2014 - 2015##
23
 * <http://www.calimaframework.com>.
24
 */
25
26
// se agradece a ArrayZone por el aporte de esta clase
27
28
/**
29
 * @name paginator for Kernel Web
30
 * @version 1.1
31
 * @copyright ArrayZone 2014
32
 * @license AZPL or later; see License.txt or http://arrayzone.com/license
33
 * @category plugin
34
 * 
35
 * Description: This script do a pagination to you web very easy and flexible
36
 * It paginate VIA $_GET[], and if you specify "$cur_page" and edit some of code, useing $_POST[] too.
37
 * 
38
 * NOTICE: If you are using KernelWeb, this script NOT FILTER GET petition because it will not be executed 
39
 * (you can pass the most critical GETs manually if you like)
40
 */
41
 
42
 //Esta clase fue editada por programadores de Cf con la aprobacion de Ruben Arroyo
43
44
namespace Sistema\Ayudantes;
45
46
class Cf_PHPPaginator {
47
	/*
48
	 * MAIN Settings (required)
49
	 */
50
	
51
	/**
52
	 * 
53
	 * @total_records string Total of records to paginate (specify manually)
54
	 */
55
	public $total_records = 0;
56
	
57
	
58
	/*
59
	 * MAIN Settings (optional)
60
	 */
61
	
62
	/**
63
	 * @style string  Specify Class to define
64
	 * @tutorial If is specified, it would create <div class="style"> otherwise it not create div
65
	 */
66
	public $style = '';
67
	
68
	/**
69
	 * @records number Max records to show on every page
70
	 */
71
	public $records = 20;
72
	
73
	/** 
74
	 * @max_links number Maxium quantity of links to show before and after actual page
75
	 * @example If current page is 4 and MAX is 2, it show "2 3 4 5 6"
76
	 */
77
	public $max_links = 3;
78
	
79
	/**
80
	 * @get_name string Specify INDEX of GET that is used
81
	 * 
82
	 * It will load automatically the current page. 
83
	 * If you like, you can specify manually in "$cur_page" 
84
	 * (if you like filter previously or the script will not load directly from URL)  
85
	 */
86
	public $get_name = 'pag';
87
	
88
	/**
89
	 * @max_records number Specify maxium quantity of registers will be loaded. "0" disable this
90
	 */
91
	public $max_records = 0;
92
	
93
	
94
	/**
95
	 * @recicle_url boolean If is true, modify directly tue current URL to put the pagination (if not exist any, add &pag= to the end)
96
	 * If is true, $url_start and $url_end will be ignored
97
	 */
98
	public $recicle_url = true;
99
	
100
	/**
101
	 * @specific_get array Specify what GETs (and order) you like recicle.
102
	 * Set array() to ALL (not recomended to avoid atacks DoS)  
103
	 * Set '' for any
104
	 * @example array('section', 'pag', 'subsection');
105
	 * If you don't need 'pag' in the midle, you can ignore it.
106
	 * WARNING: If the order is very important, use the pag at THE END of url   
107
	 */
108
	public $specific_get = '';
109
	
110
	/**
111
	 * @url_start string specify how the URL of every link start. IF $recicle_url is TRUE, this will be ignored 
112
	 * @example If you use MOD_REWRITE or JAVASCRIPT, you can put "http://mywebsite.com/subdir/pag-" or "javascript:paginate("
113
	 * 
114
	 * This value will auto completed if you leave empty 
115
	 */
116
	public $url_start = '';
117
	
118
	/**
119
	 * @url_end string specify how the URL of every link ends. IF $recicle_url is TRUE, this will be ignored
120
	 * @example If you use MOD_REWRITE or JAVASCRIPT, you can put "/" or ");"
121
	 * 
122
	 * This value will auto completed if you leave empty
123
	 */
124
	public $url_end = '';
125
	
126
	
127
	
128
	/*
129
	 * Design settings
130
	 */
131
	
132
	// Icons to show "First | Previous | Next | Last"
133
	//& laquo; = � | & lt; = <
134
    //& raquo; = � | & gt; = >
135
    // Leave empty '' to not show or ' ' to show empty
136
	public $first = '[&lt;&lt;]';
137
	public $previous = '[&lt;]';
138
	public $next = '[&gt;]';
139
	public $last = '[&gt;&gt;]';
140
	
141
	
142
	/*
143
	 * RETURNS FROM PAGINATOR and internal use
144
	 */
145
	// Return current page (auto loaded)
146
	public $cur_page;
147
	
148
	// original page loaded
149
	// it contains the real value in GET (ex: -1, 5...) to compare later
150
	public $original_page;
151
	
152
	// Return total pages
153
	public $total_pages;
154
	
155
	// LIMIT_START / Specify the first record of the page (ex, in page 3 and 20 records is record num 40)
156
	// Limit max is $records
157
	public $first_record;
158
	
159
	// Specify limit to put directly in mysql query ( LIMIT 0,20 )
160
	public $limit;
161
	
162
	
163
/*
164
 * Functions
165
 */
166
	/**
167
	 * @name paginate
168
	 * @tutorial This function is called directly to load all params
169
	 * @example $pag->paginate();
170
	 */
171
	public function paginate() {
172
		$this->get(); // Filtering and obtaining variables
173
		$this->calculate(); // Calc current page
174
		$this->prepareUrl(); // Preparing URL to show
175
	}
176
	
177
	/**
178
	 * @name show
179
	 * @tutorial Show links of pagination
180
	 * @example $pag->show();
181
	 */
182
	public function show() {
183
		// Prepare string with links
184
		// Reading here variables is faster than "$this->var"
185
		$cur_page = $this->cur_page; 
186
		$url_start = $this->url_start;
187
		$url_end = $this->url_end;
188
		
189
		$start = $cur_page - $this->max_links;
190
		$end = $cur_page + $this->max_links;
191
		
192
		if ($start < 1) $start = 1;
193
		if ($end > $this->total_pages) $end = $this->total_pages;
194
		
195
		// Showing all clickable pages (create div if class is defined)
196
		if ($this->style != '') $r = '<div class="'.$this->style.'">';
197
		else $r = '';
198
		
199
		
200
		// First / previous
201
		if ($this->cur_page > 1) {
202 View Code Duplication
			if ($this->first != '') $r .= '<a class="first" href="' . $url_start . '1' . $url_end . '">'.$this->first.'</a> ';
203 View Code Duplication
			if ($this->previous != '') $r .= '<a class="previous" href="' . $url_start . ($cur_page - 1) . $url_end . '">'.$this->previous.'</a> ';
204
		}
205
		
206
		// You can optimize this separating BEFORE and AFTER current page in two for (to avoid load "if" in each loop)
207
		// but it would be difficult on changes
208
		for ($n=$start; $n<=$end; $n++) {
209
			if ($n != $cur_page) $r .= '<a class="link" href="'. $url_start . $n . $url_end . '">'.$n.'</a> ';
210
			else $r .= '<a class="current">'.$n.'</a> ';
211
		}
212
		
213
		// next / last
214
		if ($this->cur_page < $this->total_pages) {
215 View Code Duplication
			if ($this->next != '') $r .= '<a class="next" href="' . $url_start . ($cur_page + 1) . $url_end . '">'.$this->next.'</a> ';
216 View Code Duplication
			if ($this->last != '') $r .= '<a class="last" href="' . $url_start . $this->total_pages . $url_end .'">'.$this->last.'</a> ';
217
		}
218
		
219
		// End div (if exist)
220
		if ($this->style != '') $r .= '</div>';
221
		
222
		return $r;
223
	}
224
	
225
	
226
	/**
227
	 * @name get
228
	 * @tutorial This function is autoloaded, it get the current page and filter number to avoid hackers
229
	 */	 
230
	private function get() {
0 ignored issues
show
Coding Style introduced by
get 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...
231
		if (!is_numeric($this->cur_page)) {
232
			// First get the actual page, by default 1
233
			$cur_page = isset($_GET[$this->get_name]) ? $_GET[$this->get_name] : 1;
234
			$this->original_page = $cur_page;
235
			
236
			// Filter values
237
			if (!is_numeric($cur_page) or $cur_page < 1) $cur_page = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
238
			
239
			// Set new filtered values (is faster this method)
240
			$this->cur_page = $cur_page;
241
		} else {
242
			$this->original_page = $cur_page;
0 ignored issues
show
Bug introduced by
The variable $cur_page seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
243
		}
244
	}
245
	
246
	/**
247
	 * @name calculate
248
	 * @tutorial Do all calcs about current and last page
249
	 */
250
	private function calculate() {
251
		// This vars are very used, so is faster do this
252
		$max_records = $this->max_records;
253
		$records = $this->records;
254
		
255
		// Force maxium records loaded (only if is specified by user)
256
		if ($max_records  > 0 and $max_records  > $total_records) 
0 ignored issues
show
Bug introduced by
The variable $total_records does not exist. Did you mean $records?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
257
			$this->total_records = $max_records;
258
		
259
		// Calculate total pages that have
260
		$total_pages = ceil($this->total_records / $records);
261
		
262
		// Is correct current page?
263
		if ($this->cur_page > $total_pages) $this->cur_page = $total_pages;
264
		$this->total_pages = $total_pages;
265
		
266
		// Specify LIMIT to do a query
267
		$start = ($this->cur_page - 1) * $records;
268
		
269
		// Forcing maixum records to show (only if is specified by user)
270
		if ($max_records > 0 and $records > $max_records) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
271
			$records = $max_records;
272
			$this->records = $records;
273
		}
274
			
275
		
276
		// Saving changes
277
		$this->first_record = $start;
278
		$this->limit = ' LIMIT '.$start.','.$records.' ';
279
	}
280
	
281
	/**
282
	 * @name prepareUrl
283
	 * @tutorial It prepare the url to show in each link, specified by user
284
	 * If recicle URL is false, it will auto load $url_start and $url_end
285
	 */
286
	private function prepareUrl() {
0 ignored issues
show
Coding Style introduced by
prepareUrl uses the super-global variable $_SERVER 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...
Coding Style introduced by
prepareUrl 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...
287
		// This script use three methods to recicle GET depending user selection
288
		if ($this->recicle_url) {
289
			// gonna to recicle the URL
290
			$gets = $this->specific_get;
291
			$get_name = $this->get_name;
292
			
293
			// If user specified an array
294
			if (is_array($gets)) {
295
				if (empty($gets)) {
296
					// And is empty, we have to Recicle ALL GET
297
					// To know that page needs to be replaced we will use the "original_page" (the real get)
298
					// Cortamos "pag=2" para obtener lo que va antes y despues, as� poder modificar directamente ese pag=2 por la pagina actual
299
					$query_string = explode($this->get_name.'='.$this->original_page, $_SERVER['QUERY_STRING']);
300
					
301
					// How start the new URL? To know it, we need to see the last character from current string
302
					if (!in_array(substr($query_string[0], -1), array('?', '&'))) {
303
						// Current string haven't any at end and it isn't "&" or "?" ?
304
						// If initial string have one character at least it means that not is the first index 
305
						if (isset($query_string[0][0])) $this->url_start = '?'.$query_string[0].'&';
306
						else $this->url_start = '?'.$query_string[0];
307
					} else {
308
						// Current string already have ? or &,
309
						$this->url_start = '?'.$query_string[0];
310
					}
311
					
312
					$this->url_end = isset($query_string[1]) ? $query_string[1] : '';
313
				} else {
314
					// Only specifics GET, reading all and finally leave in the same order
315
					// With this we can clean all GET that we don't need (like hacker attempts)
316
					$tmp = '';
317
					$tmp_start = ''; // If we found the current page, we move here all $tmp
318
					foreach ($gets as $get) {
319
						if ($get != $get_name) {
320
							// Trying to get the GET
321
							if (isset($_GET[$get])) $tmp .= $get.'='.$_GET[$get].'&';
322
						} else {
323
							// Pour the $tmp content to $tmp_start
324
							if ($tmp_start == '') $tmp_start .= '?';
325
							$tmp_start .= $tmp;
326
							$tmp = '';
327
						}
328
					}
329
					
330
					// Finally, write the changes in the object
331
					if ($tmp_start != '') {
332
						// If have start and end
333
						$this->url_start = $tmp_start;
334
						$this->url_end = $tmp;
335
					}
336
					else $this->url_start = '?'.$tmp;
337
				}
338
			} else{
339
				// Non recicle
340
				$this->url_start = '?';
341
				$this->url_end = ''; 
342
			}
343
			
344
			// Add the pagination
345
			$this->url_start .= $this->get_name.'=';
346
		}
347
	}
348
}
349
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...