Completed
Push — master ( 3a9bcf...4c862f )
by Jean-Christophe
03:54
created

HtmlBreadcrumb   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 207
Duplicated Lines 12.08 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
c 2
b 0
f 0
lcom 1
cbo 4
dl 25
loc 207
rs 9.8

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 2
A autoGetOnClick() 0 3 1
A contentAsString() 0 6 2
A setActive() 0 8 2
A fromDispatcher() 0 3 1
A getHref() 12 12 3
A setHrefFunction() 0 4 1
A setStartIndex() 0 4 1
A setAutoActive() 0 4 1
A compile() 0 10 3
A on() 0 6 2
A _ajaxOn() 0 8 3
A createItem() 0 10 2
A addIconAt() 0 7 2
A addItem() 0 7 1
A asLinks() 0 3 1
A asTexts() 0 3 1
A setAbsolutePaths() 0 7 2

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
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\semantic\html\base\HtmlSemNavElement;
6
use Ajax\semantic\html\base\HtmlSemDoubleElement;
7
8
use Ajax\semantic\html\elements\HtmlIcon;
9
use Ajax\JsUtils;
10
11
/**
12
 * Semantic UI Breadcrumb component
13
 * @see http://semantic-ui.com/collections/breadcrumb.html
14
 * @method _hrefFunction($e)
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlBreadcrumb extends HtmlSemNavElement {
19
	/**
20
	 *
21
	 * @var integer the start index for href generation
22
	 */
23
	protected $startIndex=0;
24
	/**
25
	 *
26
	 * @var boolean $autoActive sets the last element's class to <b>active</b> if true
27
	 */
28
	protected $autoActive;
29
30
	/**
31
	 *
32
	 * @var boolean if set to true, the path of the elements is absolute
33
	 */
34
	protected $absolutePaths=false;
35
36
	/**
37
	 *
38
	 * @var object<Closure> the function who generates the href elements. default : function($e){return $e->getContent()}
39
	 */
40
	protected $_hrefFunction;
41
42
	/**
43
	 *
44
	 * @param string $identifier
45
	 * @param array $items
46
	 * @param boolean $autoActive sets the last element's class to <b>active</b> if true
47
	 * @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
48
	 */
49 View Code Duplication
	public function __construct($identifier, $items=array(), $autoActive=true, $startIndex=0, $hrefFunction=NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
		parent::__construct($identifier, "div", "ui breadcrumb");
51
		$this->startIndex=$startIndex;
52
		$this->autoActive=$autoActive;
53
		$this->_contentSeparator="<div class='divider'> / </div>";
54
		$this->_hrefFunction=function ($e) {
55
			return $e->getContent();
56
		};
57
		if (isset($hrefFunction)) {
58
			$this->_hrefFunction=$hrefFunction;
59
		}
60
		$this->addItems($items);
61
	}
62
63
	/**
64
	 * Associate an ajax get to the breadcrumb elements, displayed in $targetSelector
65
	 * $attr member is used to build each element url
66
	 * @param string $targetSelector the target of the get
67
	 * @param string $attr the html attribute used to build the elements url
0 ignored issues
show
Bug introduced by
There is no parameter named $attr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
68
	 * @return HtmlBreadcrumbs
69
	 */
70
	public function autoGetOnClick($targetSelector) {
71
		return $this->getOnClick($this->root, $targetSelector, array ("attr" => $this->attr ));
72
	}
73
74
	public function contentAsString() {
75
		if ($this->autoActive) {
76
			$this->setActive();
77
		}
78
		return parent::contentAsString();
79
	}
80
81
	public function setActive($index=null) {
82
		if (!isset($index)) {
83
			$index=sizeof($this->content) - 1;
84
		}
85
		$activeItem=$this->content[$index];
86
		$activeItem->addToProperty("class", "active");
87
		$activeItem->setTagName("div");
88
	}
89
90
	/**
91
	 * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
92
	 * @param JsUtils $js
93
	 * @param Dispatcher $dispatcher the request dispatcher
94
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
95
	 */
96
	public function fromDispatcher(JsUtils $js,$dispatcher, $startIndex=0) {
97
		return $this->addItems($js->fromDispatcher($dispatcher));
98
	}
99
100
	/**
101
	 * Return the url of the element at $index or the breadcrumbs url if $index is ommited
102
	 * @param int $index
103
	 * @param string $separator
104
	 * @return string
105
	 */
106 View Code Duplication
	public function getHref($index=null, $separator="/") {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
		if (!isset($index)) {
108
			$index=sizeof($this->content);
109
		}
110
		if ($this->absolutePaths === true) {
111
			return $this->_hrefFunction($this->content[$index]);
112
		} else {
113
			return $this->root . implode($separator, array_slice(array_map(function ($e) {
114
				return $this->_hrefFunction($e);
115
			}, $this->content), $this->startIndex, $index + 1));
116
		}
117
	}
118
119
	/**
120
	 * sets the function who generates the href elements.
121
	 * default : function($element){return $element->getContent()}
122
	 * @param function $_hrefFunction
123
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
124
	 */
125
	public function setHrefFunction($_hrefFunction) {
126
		$this->_hrefFunction=$_hrefFunction;
127
		return $this;
128
	}
129
130
	public function setStartIndex($startIndex) {
131
		$this->startIndex=$startIndex;
132
		return $this;
133
	}
134
135
	public function setAutoActive($autoActive) {
136
		$this->autoActive=$autoActive;
137
		return $this;
138
	}
139
140
	/*
141
	 * (non-PHPdoc)
142
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
143
	 */
144
	public function compile(JsUtils $js=NULL, &$view=NULL) {
145
		if ($this->autoActive) {
146
			$this->setActive();
147
		}
148
		$count=$this->count();
149
		for($i=1; $i < $count; $i++) {
150
			$this->content[$i]->wrap($this->getContentDivider($i - 1));
151
		}
152
		return parent::compile($js, $view);
153
	}
154
155
	/*
156
	 * (non-PHPdoc)
157
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
158
	 */
159
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
160
		foreach ( $this->content as $element ) {
161
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
162
		}
163
		return $this;
164
	}
165
166
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
167
		foreach ( $this->content as $element ) {
168
			if ($element->getProperty($this->attr) != NULL){
169
				$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
170
			}
171
		}
172
		return $this;
173
	}
174
175
	/**
176
	 *
177
	 * {@inheritDoc}
178
	 *
179
	 * @see \Ajax\common\html\HtmlCollection::createItem()
180
	 */
181
	protected function createItem($value) {
182
		$count=$this->count();
183
		$itemO=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "a", "section");
184
		if (\is_array($value))
185
			$itemO->fromArray($value);
186
		else {
187
			$itemO->setContent($value);
188
		}
189
		return $itemO;
190
	}
191
192
	public function addIconAt($icon, $index) {
193
		$item=$this->getItem($index);
194
		if (isset($item)) {
195
			$icon=new HtmlIcon("icon-" . $this->identifier, $icon);
196
			$item->wrapContent($icon);
197
		}
198
	}
199
200
	public function addItem($item) {
201
		$count=$this->count();
202
		$itemO=parent::addItem($item);
203
		$this->addToPropertyCtrl("class", "section", array ("section" ));
204
		$itemO->setProperty($this->attr, $this->getHref($count));
205
		return $itemO;
206
	}
207
208
	public function asLinks() {
209
		$this->contentAs("a");
210
	}
211
212
	public function asTexts() {
213
		$this->contentAs("div");
214
	}
215
216
	public function setAbsolutePaths($absolutePaths) {
217
		$this->absolutePaths=$absolutePaths;
218
		for($i=0;$i<\sizeof($this->content);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
219
			$this->content[$i]->setProperty($this->attr, $this->getHref($i));
220
		}
221
		return $this;
222
	}
223
224
}