HtmlDropdown   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 202
Duplicated Lines 8.91 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 43
c 2
b 0
f 0
lcom 1
cbo 9
dl 18
loc 202
rs 8.3157

23 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() 5 14 4
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() 2 8 3
A setCompact() 0 3 1
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\semantic\html\elements\HtmlIcon;
8
use Ajax\common\html\html5\HtmlInput;
9
use Ajax\service\JArray;
10
use Ajax\semantic\html\base\constants\Direction;
11
use Ajax\semantic\html\base\traits\LabeledIconTrait;
12
use Ajax\JsUtils;
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 View Code Duplication
		if(\is_array($item)){
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...
65
			$value=JArray::getValue($item, "value", 1);
66
			$image=JArray::getValue($item, "image", 2);
67
			$item=JArray::getValue($item, "item", 0);
68
		}
69
		if(!$item instanceof HtmlDropdownItem){
70
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image);
71
		}elseif($itemO instanceof HtmlDropdownItem){
72
			$this->addToProperty("class", "vertical");
73
		}
74
		return $itemO;
75
	}
76
77
	/* (non-PHPdoc)
78
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
79
	 */
80
	public function fromDatabaseObject($object, $function) {
81
		$this->addItem($function($object));
82
	}
83
84
	public function addInput($name){
85
		if(!isset($name))
86
			$name="input-".$this->identifier;
87
		$this->setAction("activate");
88
		$this->input=new HtmlInput($name,"hidden");
89
	}
90
91 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...
92
		if(JArray::isAssociative($items)){
93
			foreach ($items as $k=>$v){
94
				$this->addItem($v)->setData($k);
95
			}
96
		}else{
97
			foreach ($items as $item){
98
				$this->addItem($item);
99
			}
100
		}
101
	}
102
103
	public function count(){
104
		return \sizeof($this->items);
105
	}
106
	/**
107
	 * @param boolean $dropdown
108
	 */
109
	public function asDropdown($dropdown){
110
		if($dropdown===false){
111
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
112
			$dropdown="menu";
113
		}else{
114
			$dropdown="dropdown";
115
			$this->mClass="menu";
116
		}
117
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
118
	}
119
120
	public function setVertical(){
121
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
122
	}
123
124
	public function setSimple(){
125
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
126
	}
127
128
	public function asButton($floating=false){
129
		if($floating)
130
			$this->addToProperty("class", "floating");
131
		return $this->addToProperty("class", "button");
132
	}
133
134
	public function asSelect($name=NULL,$multiple=false,$selection=true){
135
		if(isset($name))
136
			$this->addInput($name);
137
		if($multiple)
138
			$this->addToProperty("class", "multiple");
139
		if ($selection)
140
			$this->addToPropertyCtrl("class", "selection",array("selection"));
141
		return $this;
142
	}
143
144
	public function asSearch($name=NULL,$multiple=false,$selection=false){
145
		$this->asSelect($name,$multiple,$selection);
146
		return $this->addToProperty("class", "search");
147
	}
148
149
	public function setSelect($name=NULL,$multiple=false){
150
		if(!isset($name))
151
			$name="select-".$this->identifier;
152
		$this->input=null;
153
		if($multiple){
154
			$this->setProperty("multiple", true);
155
			$this->addToProperty("class", "multiple");
156
		}
157
		$this->setAction("activate");
158
		$this->tagName="select";
159
		$this->setProperty("name", $name);
160
		$this->content=null;
161
		foreach ($this->items as $item){
162
			$item->asOption();
163
		}
164
		return $this;
165
	}
166
167
	public function asSubmenu($pointing=NULL){
168
		$this->setClass("ui dropdown link item");
169
		if(isset($pointing)){
170
			$this->setPointing($pointing);
171
		}
172
		return $this;
173
	}
174
175
	public function setPointing($value=Direction::NONE){
176
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
177
	}
178
179
	public function setValue($value){
180
		if(isset($this->input)){
181
			$this->input->setProperty("value", $value);
182
		}else{
183
			$this->setProperty("value", $value);
184
		}
185
		$textElement=$this->getElementById("text-".$this->identifier, $this->content);
186
		if(isset($textElement))
187
			$textElement->setContent($value);
188
		return $this;
189
	}
190
191
	/*
192
	 * (non-PHPdoc)
193
	 * @see BaseHtml::run()
194
	 */
195
	public function run(JsUtils $js) {
196
		if($this->propertyContains("class", "simple")===false){
197 View Code Duplication
			if(isset($this->_bsComponent)===false)
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...
198
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->_params);
199
			$this->addEventsOnRun($js);
200
			return $this->_bsComponent;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_bsComponent; (Ajax\semantic\components...otstrap\components\Tabs) is incompatible with the return type of the parent method Ajax\semantic\html\base\HtmlSemDoubleElement::run of type Ajax\common\components\GenericComponent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
201
		}
202
	}
203
204
	public function setCompact(){
205
		return $this->addToPropertyCtrl("class", "compact", array("compact"));
206
	}
207
208
	public function setAction($action){
209
		$this->_params["action"]=$action;
210
	}
211
212
	public function setFullTextSearch($value){
213
		$this->_params["fullTextSearch"]=$value;
214
	}
215
}