Completed
Push — master ( 8e32f8...32ad89 )
by Jean-Christophe
03:26
created

HtmlDropdown   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 193
Duplicated Lines 5.7 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 41
c 7
b 0
f 0
lcom 1
cbo 9
dl 11
loc 193
rs 8.2769

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A addItem() 0 5 1
A addIcon() 0 4 1
A insertItem() 0 8 1
A beforeAddItem() 0 9 3
A fromDatabaseObject() 0 3 1
A addInput() 0 6 2
A addItems() 11 11 4
A count() 0 3 1
A asDropdown() 0 10 2
A setVertical() 0 3 1
A setSimple() 0 3 1
A asButton() 0 5 2
A asSelect() 0 9 4
A asSearch() 0 4 1
A setSelect() 0 17 4
A asSubmenu() 0 7 2
A setPointing() 0 3 1
A setValue() 0 11 3
A run() 0 8 3
A setAction() 0 3 1
A setFullTextSearch() 0 3 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like HtmlDropdown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HtmlDropdown, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ajax\semantic\html\modules;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\content\HtmlDropdownItem;
7
use Ajax\JsUtils;
8
use Ajax\semantic\html\elements\HtmlIcon;
9
use Ajax\common\html\html5\HtmlInput;
10
use Ajax\service\JArray;
11
use Ajax\semantic\html\base\constants\Direction;
12
use Ajax\semantic\html\base\traits\LabeledIconTrait;
13
14
class HtmlDropdown extends HtmlSemDoubleElement {
15
	use LabeledIconTrait {
16
		addIcon as addIconP;
17
	}
18
	protected $mClass="menu";
19
	protected $mTagName="div";
20
	protected $items=array ();
21
	protected $params=array("action"=>"nothing","on"=>"hover");
22
	protected $input;
23
24
	public function __construct($identifier, $value="", $items=array()) {
25
		parent::__construct($identifier, "div");
26
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
27
		$this->setProperty("class", "ui dropdown");
28
		$content=new HtmlSemDoubleElement("text-".$this->identifier,"div");
29
		$content->setClass("text");
30
		$content->setContent($value);
31
		$content->wrap("",new HtmlIcon("", "dropdown"));
32
		$this->content=array($content);
33
		$this->tagName="div";
34
		$this->addItems($items);
35
	}
36
37
	public function addItem($item,$value=NULL,$image=NULL){
38
		$itemO=$this->beforeAddItem($item,$value,$image);
39
		$this->items[]=$itemO;
40
		return $itemO;
41
	}
42
43
	public function addIcon($icon,$before=true,$labeled=false){
44
		$this->addIconP($icon,$before,$labeled);
45
		return $this->getElementById("text-".$this->identifier, $this->content)->setWrapAfter("");
46
	}
47
	/**
48
	 * Insert an item at a position
49
	 * @param mixed $item
50
	 * @param number $position
51
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
52
	 */
53
	public function insertItem($item,$position=0){
54
		$itemO=$this->beforeAddItem($item);
55
		 $start = array_slice($this->items, 0, $position);
56
		 $end = array_slice($this->items, $position);
57
		 $start[] = $item;
58
		 $this->items=array_merge($start, $end);
59
		 return $itemO;
60
	}
61
62
	protected function beforeAddItem($item,$value=NULL,$image=NULL){
63
		$itemO=$item;
64
		if(!$item instanceof HtmlDropdownItem){
65
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image);
66
		}elseif($itemO instanceof HtmlDropdownItem){
67
			$this->addToProperty("class", "vertical");
68
		}
69
		return $itemO;
70
	}
71
72
	/* (non-PHPdoc)
73
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
74
	 */
75
	public function fromDatabaseObject($object, $function) {
76
		$this->addItem($function($object));
77
	}
78
79
	public function addInput($name){
80
		if(!isset($name))
81
			$name="input-".$this->identifier;
82
		$this->setAction("activate");
83
		$this->input=new HtmlInput($name,"hidden");
84
	}
85
86 View Code Duplication
	public function addItems($items){
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
		if(JArray::isAssociative($items)){
88
			foreach ($items as $k=>$v){
89
				$this->addItem($v)->setData($k);
90
			}
91
		}else{
92
			foreach ($items as $item){
93
				$this->addItem($item);
94
			}
95
		}
96
	}
97
98
	public function count(){
99
		return \sizeof($this->items);
100
	}
101
	/**
102
	 * @param boolean $dropdown
103
	 */
104
	public function asDropdown($dropdown){
105
		if($dropdown===false){
106
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
107
			$dropdown="menu";
108
		}else{
109
			$dropdown="dropdown";
110
			$this->mClass="menu";
111
		}
112
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
113
	}
114
115
	public function setVertical(){
116
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
117
	}
118
119
	public function setSimple(){
120
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
121
	}
122
123
	public function asButton($floating=false){
124
		if($floating)
125
			$this->addToProperty("class", "floating");
126
		return $this->addToProperty("class", "button");
127
	}
128
129
	public function asSelect($name=NULL,$multiple=false,$selection=true){
130
		if(isset($name))
131
			$this->addInput($name);
132
		if($multiple)
133
			$this->addToProperty("class", "multiple");
134
		if ($selection)
135
			$this->addToPropertyCtrl("class", "selection",array("selection"));
136
		return $this;
137
	}
138
139
	public function asSearch($name=NULL,$multiple=false,$selection=false){
140
		$this->asSelect($name,$multiple,$selection);
141
		return $this->addToProperty("class", "search");
142
	}
143
144
	public function setSelect($name=NULL,$multiple=false){
145
		if(!isset($name))
146
			$name="select-".$this->identifier;
147
		$this->input=null;
148
		if($multiple){
149
			$this->setProperty("multiple", true);
150
			$this->addToProperty("class", "multiple");
151
		}
152
		$this->setAction("activate");
153
		$this->tagName="select";
154
		$this->setProperty("name", $name);
155
		$this->content=null;
156
		foreach ($this->items as $item){
157
			$item->asOption();
158
		}
159
		return $this;
160
	}
161
162
	public function asSubmenu($pointing=NULL){
163
		$this->setClass("ui dropdown link item");
164
		if(isset($pointing)){
165
			$this->setPointing($pointing);
166
		}
167
		return $this;
168
	}
169
170
	public function setPointing($value=Direction::NONE){
171
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
172
	}
173
174
	public function setValue($value){
175
		if(isset($this->input)){
176
			$this->input->setProperty("value", $value);
177
		}else{
178
			$this->setProperty("value", $value);
179
		}
180
		$textElement=$this->getElementById("text-".$this->identifier, $this->content);
181
		if(isset($textElement))
182
			$textElement->setContent($value);
183
		return $this;
184
	}
185
186
	/*
187
	 * (non-PHPdoc)
188
	 * @see BaseHtml::run()
189
	 */
190
	public function run(JsUtils $js) {
191
		if($this->propertyContains("class", "simple")===false){
192
			if(isset($this->_bsComponent)===false)
193
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->params);
194
			$this->addEventsOnRun($js);
195
			return $this->_bsComponent;
196
		}
197
	}
198
199
	public function setAction($action){
200
		$this->params["action"]=$action;
201
	}
202
203
	public function setFullTextSearch($value){
204
		$this->params["fullTextSearch"]=$value;
205
	}
206
}