Completed
Push — develop ( cb7ecf...5e631f )
by Dmytro
17s
created

DataSetPager::render()   F

Complexity

Conditions 28
Paths 684

Size

Total Lines 105
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 64
nc 684
nop 0
dl 0
loc 105
rs 2.1804
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 namespace EvolutionCMS\Support;
2
3
use EvolutionCMS\Interfaces\DataSetPagerInterface;
4
5
#
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
6
# DataSetPager Class
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
7
# Created By Raymond Irving 2-Dec,2004
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
8
# Based on CLASP 2.0 (www.claspdev.com)
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
9
# -----------------------------------------
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
10
# Licensed under the GPL
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
11
# -----------------------------------------
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
12
#
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
13
14
class DataSetPager implements DataSetPagerInterface{
15
16
	public $ds; // datasource
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ds. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
17
    public $pageSize;
18
    public $pageNumber;
19
    public $rows;
20
    public $pager;
21
    public $id;
22
23
	// normal page
24
    public $pageStyle;
25
    public $pageClass;
26
27
	// selected page
28
    public $selPageStyle;
29
    public $selPageClass;
30
    public $renderRowFnc;
31
    public $renderRowFncArgs;
32
    public $renderPagerFnc;
33
    public $renderPagerFncArgs;
34
    public static $dataSetPagerCnt;
35
36
    public function __construct($id, $ds, $pageSize = 10, $pageNumber = -1) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ds. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
__construct 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...
37
		global $_PAGE; // use view state object
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
38
39
		// set id
40
		self::$dataSetPagerCnt++;
41
		$this->id = !empty($id) ? $id : "dsp" . self::$dataSetPagerCnt;
42
43
		// get pagenumber
44
		// by setting pager to -1 cause pager to load it's last page number
45
		if($pageNumber == -1) {
46
			$pageNumber = 1;
47
			if(isset($_GET["dpgn" . $this->id])) {
48
				$pageNumber = $_GET["dpgn" . $this->id];
49
			} elseif(isset($_PAGE['vs'][$id . '_dpgn'])) {
50
				$pageNumber = $_PAGE['vs'][$id . '_dpgn'];
51
			}
52
		}
53
		if(!is_numeric($pageNumber)) {
54
			$pageNumber = 1;
55
		}
56
57
		$this->ds = $ds; // datasource
58
		$this->pageSize = $pageSize;
59
		$this->pageNumber = $pageNumber;
60
		$this->rows = '';
61
		$this->pager = '';
62
	}
63
64
    public function getRenderedPager() {
65
		return $this->pager;
66
	}
67
68
    public function getRenderedRows() {
69
		return $this->rows;
70
	}
71
72
    public function setDataSource($ds) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ds. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
73
		$this->ds = $ds;
74
	}
75
76
    public function setPageSize($ps) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ps. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
77
		$this->pageSize = $ps;
78
	}
79
80
    public function setRenderRowFnc($fncName, $args = "") {
81
		$this->renderRowFnc = &$fncName;
82
		$this->renderRowFncArgs = $args;    // extra agruments
83
84
85
	}
86
87
    public function setRenderPagerFnc($fncName, $args = "") {
88
		$this->renderPagerFnc = $fncName;
89
		$this->renderPagerFncArgs = $args;    // extra agruments
90
	}
91
92
    public function render() {
0 ignored issues
show
Coding Style introduced by
render 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
render 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...
93
		$modx = evolutionCMS(); global $_PAGE;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
94
95
		$isDataset = $modx->getDatabase()->isResult($this->ds);
96
97
		if(!$this->selPageStyle) {
98
			$this->selPageStyle = "font-weight:bold";
99
		}
100
101
		// get total number of rows
102
		$tnr = ($isDataset) ? $modx->getDatabase()->getRecordCount($this->ds) : count($this->ds);
103
104
		// render: no records found
105
		if($tnr <= 0) {
106
			$fnc = $this->renderRowFnc;
107
			$args = $this->renderRowFncArgs;
108
			if(isset($fnc)) {
109
				if($args != "") {
110
					$this->rows .= $fnc(0, null, $args);
111
				} // if agrs was specified then we will pass three params
112
				else {
113
					$this->rows .= $fnc(0, null);
114
				}                 // otherwise two will be passed
115
			}
116
			return;
117
		}
118
119
		// get total pages
120
		$tp = ceil($tnr / $this->pageSize);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $tp. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
121
		if($this->pageNumber > $tp) {
122
			$this->pageNumber = 1;
123
		}
124
125
		// get page number
126
		$p = $this->pageNumber;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
127
128
		// save page number to view state if available
129
		if(isset($_PAGE['vs'])) {
130
			$_PAGE['vs'][$this->id . '_dpgn'] = $p;
131
		}
132
133
		// render pager : renderPagerFnc($cuurentPage,$pagerNumber,$arguments="");
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
134
		if($tp > 1) {
135
		    $url = '';
136
			$fnc = $this->renderPagerFnc;
137
			$args = $this->renderPagerFncArgs;
138
			if(!isset($fnc)) {
139
				if($modx->isFrontend()) {
140
					$url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full') . '?';
141
				} else {
142
					$url = $_SERVER['PHP_SELF'] . '?';
143
				}
144
				$i = 0;
145
				foreach($_GET as $n => $v) if($n != 'dpgn' . $this->id) {
146
					$i++;
147
					$url .= (($i > 1) ? "&" : "") . "$n=$v";
148
				}
149
				if($i >= 1) {
150
					$url .= "&";
151
				}
152
			}
153
			for($i = 1; $i <= $tp; $i++) {
154
				if(isset($fnc)) {
155
					if($args != "") {
156
						$this->pager .= $fnc($p, $i, $args);
157
					} else {
158
						$this->pager .= $fnc($p, $i);
159
					}
160
				} else {
161
					$this->pager .= ($p == $i) ? " <span class='" . $this->selPageClass . "' style='" . $this->selPageStyle . "'>$i</span> " : " <a href='" . $url . "dpgn" . $this->id . "=$i' class='" . $this->pageClass . "' style='" . $this->pageStyle . "'>$i</a> ";
162
				}
163
			}
164
		}
165
166
		// render row : renderRowFnc($rowNumber,$row,$arguments="")
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
167
		$fnc = $this->renderRowFnc;
168
		$args = $this->renderRowFncArgs;
169
170
		if(isset($fnc)) {
171
			$i = 1;
172
			$fncObject = is_object($fnc);
173
			$minitems = (($p - 1) * $this->pageSize) + 1;
174
			$maxitems = (($p - 1) * $this->pageSize) + $this->pageSize;
175
			while($i <= $maxitems && ($row = ($isDataset) ? $modx->getDatabase()->getRow($this->ds) : $this->ds[$i - 1])) {
176
				if($i >= $minitems && $i <= $maxitems) {
177
					if($fncObject) {
178
						if($args != "") {
179
							$this->rows .= $fnc->RenderRowFnc($i, $row, $args);
180
						} else {
181
							$this->rows .= $fnc->RenderRowFnc($i, $row);
182
						}
183
					} else {
184
						if($args != "") {
185
							$this->rows .= $fnc($i, $row, $args);
186
						} // if agrs was specified then we wil pass three params
187
						else {
188
							$this->rows .= $fnc($i, $row);
189
						}                 // otherwise two will be passed
190
					}
191
192
				}
193
				$i++;
194
			}
195
		}
196
	}
197
}
198