Completed
Push — master ( 06f62d...424d4e )
by Jean-Christophe
03:13
created

HtmlBreadcrumb   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 181
Duplicated Lines 19.34 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
c 1
b 0
f 0
lcom 1
cbo 4
dl 35
loc 181
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A autoGetOnClick() 0 3 1
A contentAsString() 0 6 2
A setActive() 0 8 2
B fromDispatcher() 14 14 5
A getHref() 10 10 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 6 2
A createItem() 0 11 2
A addIcon() 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
use Ajax\JsUtils;
8
use Phalcon\Mvc\View;
9
use Ajax\semantic\html\elements\HtmlIcon;
10
/**
11
 * Semantic UI Breadcrumb component
12
 * @see http://semantic-ui.com/collections/breadcrumb.html
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlBreadcrumb extends HtmlSemNavElement{
17
	/**
18
	 * @var integer the start index for href generation
19
	 */
20
	protected $startIndex=0;
21
	/**
22
	 * @var boolean $autoActive sets the last element's class to <b>active</b> if true
23
	 */
24
	protected $autoActive;
25
26
	/**
27
	 * @var boolean if set to true, the path of the elements is absolute
28
	 */
29
	protected $absolutePaths;
30
31
	/**
32
	 * @var function the function who generates the href elements. default : function($e){return $e->getContent()}
33
	 */
34
	protected $_hrefFunction;
35
36
	/**
37
	 * @param string $identifier
38
	 * @param array $items
39
	 * @param boolean $autoActive sets the last element's class to <b>active</b> if true
40
	 * @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
41
	 */
42 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...
43
		parent::__construct( $identifier, "div", "ui breadcrumb");
44
		$this->startIndex=$startIndex;
45
		$this->autoActive=$autoActive;
46
		$this->_contentSeparator="<div class='divider'> / </div>";
47
		$this->_hrefFunction=function ($e){return $e->getContent();};
0 ignored issues
show
Documentation Bug introduced by
It seems like function ($e) { return $e->getContent(); } of type object<Closure> is incompatible with the declared type object<Ajax\semantic\html\collections\function> of property $_hrefFunction.

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...
48
		if(isset($hrefFunction)){
49
			$this->_hrefFunction=$hrefFunction;
50
		}
51
		$this->addItems($items);
52
	}
53
54
	/**
55
	 * Associate an ajax get to the breadcrumb elements, displayed in $targetSelector
56
	 * $attr member is used to build each element url
57
	 * @param string $targetSelector the target of the get
58
	 * @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...
59
	 * @return HtmlBreadcrumbs
60
	 */
61
	public function autoGetOnClick($targetSelector){
62
		return $this->getOnClick($this->root, $targetSelector,array("attr"=>$this->attr));
63
	}
64
65
	public function contentAsString(){
66
		if($this->autoActive){
67
			$this->setActive();
68
		}
69
		return parent::contentAsString();
70
	}
71
72
	public function setActive($index=null){
73
		if(!isset($index)){
74
			$index=sizeof($this->content)-1;
75
		}
76
		$activeItem=$this->content[$index];
77
		$activeItem->addToProperty("class","active");
78
		$activeItem->setTagName("div");
79
	}
80
81
	/**
82
	 * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
83
	 * @param Dispatcher $dispatcher the request dispatcher
84
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
85
	 */
86 View Code Duplication
	public function fromDispatcher($dispatcher,$startIndex=0){
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...
87
		$this->startIndex=$startIndex;
88
		$params=$dispatcher->getParams();
89
		$action=$dispatcher->getActionName();
90
		$items=array($dispatcher->getControllerName());
91
		if(\sizeof($params)>0 || \strtolower($action)!="index" ){
92
			$items[]=$action;
93
			foreach ($params as $p){
94
				if(\is_object($p)===false)
95
					$items[]=$p;
96
			}
97
		}
98
		return $this->addItems($items);
99
	}
100
101
	/**
102
	 * Return the url of the element at $index or the breadcrumbs url if $index is ommited
103
	 * @param int $index
104
	 * @param string $separator
105
	 * @return string
106
	 */
107 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...
108
		if(!isset($index)){
109
			$index=sizeof($this->content);
110
		}
111
		if($this->absolutePaths===true){
112
			return $this->_hrefFunction($this->content[$index]);
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\semantic\htm...ections\HtmlBreadcrumb>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
113
		}else{
114
			return $this->root.implode($separator, array_slice(array_map(function($e){return $this->_hrefFunction($e);}, $this->content),$this->startIndex,$index+1));
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\semantic\htm...ections\HtmlBreadcrumb>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
115
		}
116
	}
117
118
	/**
119
	 * sets the function who generates the href elements. default : function($element){return $element->getContent()}
120
	 * @param function $_hrefFunction
121
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
122
	 */
123
	public function setHrefFunction($_hrefFunction) {
124
		$this->_hrefFunction = $_hrefFunction;
125
		return $this;
126
	}
127
128
	public function setStartIndex($startIndex) {
129
		$this->startIndex=$startIndex;
130
		return $this;
131
	}
132
133
	public function setAutoActive($autoActive) {
134
		$this->autoActive = $autoActive;
135
		return $this;
136
	}
137
138
	/*
139
	 * (non-PHPdoc)
140
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
141
	 */
142
	public function compile(JsUtils $js=NULL, View $view=NULL) {
143
		if($this->autoActive){
144
			$this->setActive();
145
		}
146
		$count=$this->count();
147
		for($i=1;$i<$count;$i++){
148
			$this->content[$i]->wrap($this->_contentSeparator);
149
		}
150
		return parent::compile($js, $view);
151
	}
152
153
	/*
154
	 * (non-PHPdoc)
155
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
156
	 */
157
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
158
		foreach ($this->content as $element){
159
			$element->on($event,$jsCode,$stopPropagation,$preventDefault);
160
		}
161
		return $this;
162
	}
163
164
165
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
166
		foreach ($this->content as $element){
167
			$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
168
		}
169
		return $this;
170
	}
171
172
	/**
173
	 * {@inheritDoc}
174
	 * @see \Ajax\common\html\HtmlCollection::createItem()
175
	 */
176
	protected function createItem($value) {
177
		$count=$this->count();
178
		$itemO=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"a","section");
179
		if(\is_array($value))
180
			$itemO->fromArray($value);
181
		else{
182
			$itemO->setContent($value);
183
			$itemO->setProperty($this->attr, $this->getHref($count));
184
		}
185
		return $itemO;
186
	}
187
188
	public function addIcon($icon,$index){
189
		$item=$this->getItem($index);
190
		if(isset($item)){
191
			$icon=new HtmlIcon("icon-".$this->identifier, $icon);
192
			$item->wrapContent($icon);
193
		}
194
	}
195
196
}