HtmlButton   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 11.32 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 2
cbo 5
dl 12
loc 106
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 4 1
A __construct() 12 12 3
A setStyle() 0 3 1
A setSize() 0 6 2
A setActive() 0 5 1
A setDisabled() 0 5 1
A setToggled() 0 3 1
A run() 0 8 2
A fromArray() 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\bootstrap\html;
4
5
use Ajax\JsUtils;
6
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
7
use Ajax\bootstrap\html\base\CssRef;
8
9
/**
10
 * Twitter Bootstrap Button component
11
 * @see http://getbootstrap.com/css/#buttons
12
 * @author jc
13
 * @version 1.001
14
 */
15
class HtmlButton extends HtmlBsDoubleElement {
16
17
	/**
18
	 * Constructs an HTML Bootstrap button
19
	 * @param string $identifier HTML id
20
	 * @param string $value value of the Button
21
	 * @param string $cssStyle btn-default, btn-primary...
22
	 * @param string $onClick JS Code for click event
23
	 */
24 View Code Duplication
	public function __construct($identifier, $value="", $cssStyle=null, $onClick=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...
25
		parent::__construct($identifier, "button");
26
		$this->setProperty("class", "btn btn-default");
27
		$this->setProperty("role", "button");
28
		$this->content=$value;
29
		if (isset($cssStyle)) {
30
			$this->setStyle($cssStyle);
31
		}
32
		if (isset($onClick)) {
33
			$this->onClick($onClick);
34
		}
35
	}
36
37
	/**
38
	 * Set the button value
39
	 * @param string $value
40
	 * @return \Ajax\bootstrap\html\HtmlButton
41
	 */
42
	public function setValue($value) {
43
		$this->content=$value;
44
		return $this;
45
	}
46
47
	/**
48
	 * define the button style
49
	 * avaible values : "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger"
50
	 * @param string|int $cssStyle
51
	 * @return \Ajax\bootstrap\html\HtmlButton default : "btn-default"
52
	 */
53
	public function setStyle($cssStyle) {
54
		return $this->addToPropertyCtrl("class", CssRef::getStyle($cssStyle, "btn"), CssRef::Styles("btn"));
55
	}
56
57
	/**
58
	 * define the button size
59
	 * available values : "btn-lg","","btn-sm","btn-xs"
60
	 * @param string|int $size
61
	 * @return \Ajax\bootstrap\html\HtmlButton default : ""
62
	 */
63
	public function setSize($size) {
64
		if (is_int($size)) {
65
			return $this->addToPropertyUnique("class", CssRef::sizes()[$size], CssRef::sizes());
66
		}
67
		return $this->addToPropertyCtrl("class", $size, CssRef::sizes());
68
	}
69
70
	/**
71
	 * Sets the "active" css class to the button
72
	 * @return \Ajax\bootstrap\html\HtmlButton
73
	 */
74
	public function setActive() {
75
		return $this->addToPropertyCtrl("class", "active", array (
76
				"active"
77
		));
78
	}
79
80
	/**
81
	 * Disables the button
82
	 * @return \Ajax\bootstrap\html\HtmlButton
83
	 */
84
	public function setDisabled() {
85
		return $this->addToPropertyCtrl("class", "disabled", array (
86
				"disabled"
87
		));
88
	}
89
90
	public function setToggled() {
91
		return $this->setProperty("data-toggle", "button");
92
	}
93
94
	/*
95
	 * (non-PHPdoc)
96
	 * @see BaseHtml::run()
97
	 */
98
	public function run(JsUtils $js) {
99
		$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
100
		if (array_key_exists("data-toggle", $this->properties)) {
101
			$this->_bsComponent->addCode("$.fn.toggled=function(){return this.hasClass('active');};");
102
		}
103
		$this->addEventsOnRun($js);
104
		return $this->_bsComponent;
105
	}
106
107
108
109
	/*
110
	 * (non-PHPdoc)
111
	 * @see \Ajax\bootstrap\html\BaseHtml::fromArray()
112
	 */
113
	public function fromArray($array) {
114
		$array=parent::fromArray($array);
115
		foreach ( $array as $key => $value ) {
116
			$this->setProperty($key, $value);
117
		}
118
		return $array;
119
	}
120
}