HtmlButtongroups   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 152
Duplicated Lines 10.53 %

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 16
loc 152
rs 9.3999

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 3
A setSize() 3 9 3
A setStyle() 0 4 2
A dropdownAsButton() 0 6 1
A addExistingDropDown() 0 5 1
D addElement() 0 30 9
A addElements() 0 6 2
A fromArray() 0 3 1
A setAlignment() 0 12 4
A getElement() 0 8 2
A setElement() 0 4 1
A on() 0 6 2
A getElements() 0 3 1
A fromDatabaseObject() 0 3 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
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\bootstrap\html\base\CssRef;
6
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
7
8
/**
9
 * Twitter Bootstrap Buttongroups component
10
 * @see http://getbootstrap.com/components/#btn-groups
11
 * @author jc
12
 * @version 1.001
13
 */
14
class HtmlButtongroups extends HtmlBsDoubleElement {
15
	protected $elements;
16
17 View Code Duplication
	public function __construct($identifier, $elements=array(), $cssStyle=NULL, $size=NULL, $tagName="div") {
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...
18
		parent::__construct($identifier, $tagName);
19
		$this->_template="<%tagName% id='%identifier%' %properties%>%elements%</%tagName%>";
20
		$this->setProperty("class", "btn-group");
21
		$this->setRole("group");
22
		$this->addElements($elements);
23
		if (isset($cssStyle)) {
24
			$this->setStyle($cssStyle);
25
		}
26
		if (isset($size)) {
27
			$this->setSize($size);
28
		}
29
	}
30
31
	/**
32
	 * define the buttons size
33
	 * available values : "btn-group-lg","","btn-group-sm","btn-group-xs"
34
	 * @param string|int $size
35
	 * @return HtmlButtongroups default : ""
36
	 */
37
	public function setSize($size) {
38
		foreach ( $this->elements as $element ) {
39
			$element->setSize($size);
40
		}
41 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...
42
			return $this->addToPropertyUnique("class", CssRef::sizes("btn-group")[$size], CssRef::sizes("btn-group"));
43
		}
44
		return $this->addToPropertyCtrl("class", $size, CssRef::sizes("btn-group"));
45
	}
46
47
	public function setStyle($value) {
48
		foreach ( $this->elements as $element )
49
			$element->setStyle($value);
50
	}
51
52
	private function dropdownAsButton($bt) {
53
		$this->addExistingDropDown($bt);
54
		$bt->setTagName("button");
55
		$bt->addBtnClass("dropdown-toogle");
56
		$bt->addBtnClass("btn-default");
57
	}
58
59
	private function addExistingDropDown($bt) {
60
		$bt->setMTagName("div");
61
		$bt->setRole("group");
62
		$bt->setMClass("btn-group");
63
	}
64
65
	public function addElement($element) {
66
		$result=$element;
67
		$iid=sizeof($this->elements)+1;
68
		if (($element instanceof HtmlDropdown)||($element instanceof HtmlSplitbutton)) {
69
			$this->addExistingDropDown($element);
70
			$this->elements[]=$element;
71
		} elseif ($element instanceof HtmlButton) {
72
			$this->elements[]=$element;
73
		} elseif (is_array($element)) {
74
			if (array_key_exists("glyph", $element))
75
				$bt=new HtmlGlyphButton($this->identifier."-button-".$iid);
76
			elseif (array_key_exists("btnCaption", $element)) {
77
				if (array_key_exists("split", $element))
78
					$bt=new HtmlSplitbutton($this->identifier."-dropdown-".$iid);
79
				else
80
					$bt=new HtmlDropdown($this->identifier."-dropdown-".$iid);
81
				$this->dropdownAsButton($bt);
82
			} else
83
				$bt=new HtmlButton($this->identifier."-button-".$iid);
84
			$bt->fromArray($element);
85
			$this->elements[]=$bt;
86
			$result=$bt;
87
		} elseif (is_string($element)) {
88
			$bt=new HtmlButton($this->identifier."-button-".$iid);
89
			$bt->setValue($element);
90
			$this->elements[]=$bt;
91
			$result=$bt;
92
		}
93
		return $result;
94
	}
95
96
	public function addElements($elements) {
97
		foreach ( $elements as $element ) {
98
			$this->addElement($element);
99
		}
100
		return $this;
101
	}
102
103
	/*
104
	 * (non-PHPdoc)
105
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
106
	 */
107
	public function fromArray($array) {
108
		$this->addElements($array);
109
	}
110
111
	public function setAlignment($value) {
112
		if (is_int($value)) {
113
			$value=CssRef::alignment("btn-group")[$value];
114
		} else
115
			$value="btn-group-".$value;
116
		if (strstr($value, "justified")) {
117
			foreach ( $this->elements as $element ) {
118
				$element->wrap('<div class="btn-group" role="group">', '</div>');
119
			}
120
		}
121
		$this->addToPropertyCtrl("class", $value, CssRef::alignment("btn-group-"));
122
	}
123
124
	/**
125
	 * Return the element at index
126
	 * @param int $index
127
	 * @return HtmlButton
128
	 */
129
	public function getElement($index) {
130
		if (is_int($index))
131
			return $this->elements[$index];
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->elements[$index]; of type Ajax\bootstrap\html\Html...p\html\HtmlButtongroups adds the type Ajax\bootstrap\html\HtmlButtongroups to the return on line 131 which is incompatible with the return type documented by Ajax\bootstrap\html\HtmlButtongroups::getElement of type Ajax\bootstrap\html\HtmlButton.
Loading history...
132
		else {
133
			$elm=$this->getElementById($index, $this->elements);
134
			return $elm;
135
		}
136
	}
137
138
	public function setElement($index, $button) {
139
		$this->elements[$index]=$button;
140
		return $this;
141
	}
142
143
	/*
144
	 * (non-PHPdoc)
145
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
146
	 */
147
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
148
		foreach ( $this->elements as $element ) {
149
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
150
		}
151
		return $this;
152
	}
153
154
	public function getElements() {
155
		return $this->elements;
156
	}
157
158
	/* (non-PHPdoc)
159
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
160
	 */
161
	public function fromDatabaseObject($object, $function) {
162
		$this->addElement($function($object));
163
	}
164
165
}