Completed
Push — master ( e9bdcb...b0560a )
by Jean-Christophe
04:52
created

HtmlButtonGroups   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 109
Duplicated Lines 7.34 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 22
c 4
b 0
f 0
lcom 1
cbo 2
dl 8
loc 109
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A addElement() 0 11 3
A addElements() 0 6 2
A insertOr() 0 6 1
A fromArray() 0 3 1
A asIcons() 0 6 2
A setVertical() 0 3 1
A setLabeled() 0 3 1
A getElement() 8 8 2
A setElement() 0 4 1
A on() 0 6 2
A getElements() 0 3 1
A addClasses() 0 7 2
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\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
7
/**
8
 * Semantic UI Buttongroups component
9
 * @see http://semantic-ui.com/elements/button.html
10
 * @author jc
11
 * @version 1.001
12
 */
13
class HtmlButtonGroups extends HtmlSemDoubleElement {
14
15
	public function __construct($identifier, $elements=array(), $asIcons=false) {
16
		parent::__construct($identifier, "div", "ui buttons");
17
		$this->content=array ();
18
		if ($asIcons === true)
19
			$this->asIcons();
20
		$this->addElements($elements, $asIcons);
21
	}
22
23
	public function addElement($element, $asIcon=false) {
24
		$elementO=$element;
25
		if (\is_string($element)) {
26
			if ($asIcon) {
27
				$elementO=new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content));
28
				$elementO->asIcon($element);
29
			} else
30
				$elementO=new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content), $element);
31
		}
32
		$this->addContent($elementO);
33
	}
34
35
	public function addElements($elements, $asIcons=false) {
36
		foreach ( $elements as $element ) {
37
			$this->addElement($element, $asIcons);
38
		}
39
		return $this;
40
	}
41
42
	public function insertOr($aferIndex=0, $or="or") {
43
		$orElement=new HtmlSemDoubleElement("or-" . $this->identifier, "div", "or");
44
		$orElement->setProperty("data-text", $or);
45
		array_splice($this->content, $aferIndex + 1, 0, array ($orElement ));
46
		return $this;
47
	}
48
49
	/*
50
	 * (non-PHPdoc)
51
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
52
	 */
53
	public function fromArray($array) {
54
		$this->addElements($array);
55
	}
56
57
	public function asIcons() {
58
		foreach ( $this->content as $item ) {
59
			$item->asIcon($item->getContent());
60
		}
61
		return $this->addToProperty("class", "icons");
62
	}
63
64
	public function setVertical() {
65
		return $this->addToProperty("class", "vertical");
66
	}
67
68
	public function setLabeled() {
69
		return $this->addToProperty("class", "labeled icon");
70
	}
71
72
	/**
73
	 * Return the element at index
74
	 * @param int $index
75
	 * @return HtmlButton
76
	 */
77 View Code Duplication
	public function getElement($index) {
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...
78
		if (is_int($index))
79
			return $this->content[$index];
80
		else {
81
			$elm=$this->getElementById($index, $this->content);
82
			return $elm;
83
		}
84
	}
85
86
	public function setElement($index, $button) {
87
		$this->content[$index]=$button;
88
		return $this;
89
	}
90
91
	/*
92
	 * (non-PHPdoc)
93
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
94
	 */
95
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
96
		foreach ( $this->content as $element ) {
97
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
98
		}
99
		return $this;
100
	}
101
102
	public function getElements() {
103
		return $this->content;
104
	}
105
106
	public function addClasses($classes=array()) {
107
		$i=0;
108
		foreach ( $this->content as $button ) {
109
			$button->addToProperty("class", $classes[$i++]);
110
		}
111
		return $this;
112
	}
113
114
	/*
115
	 * (non-PHPdoc)
116
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
117
	 */
118
	public function fromDatabaseObject($object, $function) {
119
		$this->addElement($function($object));
120
	}
121
}