Completed
Push — master ( 6e3264...0dd83f )
by Jean-Christophe
03:39
created

HtmlPagination   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 180
Duplicated Lines 1.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
c 1
b 0
f 0
lcom 1
cbo 6
dl 3
loc 180
rs 9.4

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A createElement() 0 21 4
A createContent() 0 10 2
A half() 0 3 1
A getStart() 0 7 2
A _addEvent() 0 7 3
A fromDispatcher() 0 15 4
A getUrl() 0 3 1
A setSize() 3 8 4
A getFrom() 0 3 1
A setFrom() 0 5 1
A getTo() 0 3 1
A setTo() 0 5 1
A getActive() 0 3 1
A setActive() 0 5 1
A getUrlMask() 0 3 1
A setUrlMask() 0 5 1
A getCountVisible() 0 3 1
A setCountVisible() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Ajax\bootstrap\html;
3
4
use Ajax\bootstrap\html\base\HtmlDoubleElement;
5
use Ajax\bootstrap\html\base\HtmlNavElement;
6
use Ajax\bootstrap\html\base\BaseHtml;
7
use Ajax\bootstrap\html\base\CssRef;
8
use Ajax\service\JString;
9
use Ajax\service\PhalconUtils;
10
11
/**
12
 * Twitter Bootstrap Pagination component
13
 * @see http://getbootstrap.com/components/#pagination
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlPagination extends HtmlNavElement {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
18
	
19
	/**
20
	 * @var int
21
	 */
22
	protected $from;
23
	
24
	/**
25
	 * @var int
26
	 */
27
	protected $to;
28
	
29
	
30
	/**
31
	 * @var int
32
	 */
33
	protected $countVisible;
34
	
35
	/**
36
	 * @var int
37
	 */
38
	protected $active;
39
	
40
	/**
41
	 * @var string
42
	 */
43
	protected $urlMask;
44
	
45
	/**
46
	 * @param string $identifier
47
	 */
48
	public function __construct($identifier,$from=1,$to=1,$active=NULL,$countVisible=NULL){
49
		parent::__construct($identifier,"ul");
50
		$this->setProperty("class", "pagination");
51
		$this->active=$active;
52
		$this->from=$from;
53
		$this->to=$to;
54
		$this->urlMask="%page%";
55
		if(!isset($countVisible))
56
			$this->countVisible=$to-$from+1;
57
		else
58
			$this->countVisible=$countVisible;
59
		$this->createContent();
60
	}
61
	
62
	private function createElement($num,$content,$disabled=false,$current=false){
63
		$count=sizeof($this->content)+1;
1 ignored issue
show
Coding Style introduced by
As per coding-style, the use of the aliased function sizeof() should be avoided; please use count() instead.
Loading history...
64
		$elem=new HtmlDoubleElement("li-".$this->identifier."-".$count,"li");
65
		if($disabled){
66
			$elem->setProperty("class", "disabled");
67
		}
68
		if($current){
69
			$content.="<span class='sr-only'>(current)</span>";
70
			$elem->setProperty("class", "active");
71
		}
72
		if(!$disabled){
73
			$url=$this->getUrl($num);
74
			$href=new HtmlLink("a-".$this->identifier."-".$count,$url,$content);
75
			$href->setProperty($this->attr, $url);
76
			$elem->setContent($href);
77
		}else{
78
			$elem->setContent($content);
79
		}
80
		$this->content[]=$elem;
81
		return $this;
82
	}
83
	
84
	protected function createContent(){
85
		$this->content=array();
86
		$this->createElement($this->active-1,"<span aria-hidden='true'>&laquo;</span>",$this->active===1);
87
		$start=$this->getStart();
88
		$end=min($start+$this->countVisible-1,$this->to);
89
		for($index=$start;$index<=$end;$index++){
90
			$this->createElement($index,$index,false,$index===$this->active);
91
		}
92
		$this->createElement($this->active+1,"<span aria-hidden='true'>&raquo;</span>",$this->active===$this->to);
93
	}
94
	
95
	protected function half(){
96
		return (int)($this->countVisible/2);
97
	}
98
	
99
	protected function getStart(){
100
		$result=1;
101
		if($this->countVisible!==$this->to-$this->from+1){
102
			$result=max($this->active-$this->half(),$result);
103
		}
104
		return $result;
105
	}
106
107
	public function _addEvent($event, $jsCode) {
1 ignored issue
show
Coding Style introduced by
Method name "_addEvent" should not be prefixed with an underscore to indicate visibility
Loading history...
108
		foreach ($this->content as $li){
109
			$content=$li->getContent();
110
			if($content instanceof BaseHtml)
111
				$content->_addEvent($event,$jsCode);
112
		}
113
	}
114
	/**
115
	 * set the active page corresponding to request dispatcher : controllerName, actionName, parameters and $urlMask
116
	 * @param Dispatcher $dispatcher the request dispatcher
117
	 * @return \Ajax\bootstrap\html\HtmlPagination
118
	 */
119
	public function fromDispatcher($dispatcher){
120
		$items=array($dispatcher->getControllerName(),$dispatcher->getActionName());
121
		$items=array_merge($items,$dispatcher->getParams());
122
		$url=implode("/", $items);
123
		if($this->urlMask==="%page%"){
124
			$this->urlMask=preg_replace("/[0-9]/", "%page%", $url);
125
		}
126
		for($index=$this->from;$index<=$this->to;$index++){
127
			if($this->getUrl($index)==$url){
128
				$this->setActive($index);
129
				break;
130
			}
131
		}
132
		return $this;
133
	}
134
	
135
	public function getUrl($index){
136
		return str_ireplace("%page%", $index, $this->urlMask);
137
	}
138
	
139
	/**
140
	 * define the buttons size
141
	 * available values : "lg","","sm","xs"
142
	 * @param string|int $size
143
	 * @return HtmlPagination default : ""
144
	 */
145
	public function setSize($size) {
146 View Code Duplication
		if (is_int($size)) {
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...
147
			return $this->addToPropertyUnique("class", CssRef::sizes("pagination")[$size], CssRef::sizes("pagination"));
148
		}
149
		if(!PhalconUtils::startsWith($size, "pagination-") && $size!=="")
150
			$size="pagination-".$size;
151
		return $this->addToPropertyCtrl("class", $size, CssRef::sizes("pagination"));
152
	}
153
	
154
	public function getFrom() {
155
		return $this->from;
156
	}
157
	public function setFrom($from) {
158
		$this->from = $from;
159
		$this->createContent();
160
		return $this;
161
	}
162
	public function getTo() {
163
		return $this->to;
164
	}
165
	public function setTo($to) {
166
		$this->to = $to;
167
		$this->createContent();
168
		return $this;
169
	}
170
	public function getActive() {
171
		return $this->active;
172
	}
173
	public function setActive($active) {
174
		$this->active = $active;
175
		$this->createContent();
176
		return $this;
177
	}
178
	public function getUrlMask() {
179
		return $this->urlMask;
180
	}
181
	public function setUrlMask($urlMask) {
182
		$this->urlMask = $urlMask;
183
		$this->createContent();
184
		return $this;
185
	}
186
	public function getCountVisible() {
187
		return $this->countVisible;
188
	}
189
	public function setCountVisible($countVisible) {
190
		$this->countVisible = $countVisible;
191
		$this->createContent();
192
		return $this;
193
	}
194
	
195
	
196
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...