Completed
Push — master ( fdc0c0...9de830 )
by Jean-Christophe
03:31
created

HtmlButton   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 166
Duplicated Lines 6.02 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 23
c 9
b 0
f 0
lcom 2
cbo 6
dl 10
loc 166
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setPositive() 0 3 1
A setNegative() 0 3 1
A setToggle() 0 3 1
A setCircular() 0 3 1
A setBasic() 0 3 1
A __construct() 10 10 3
A setValue() 0 4 1
A setStyle() 0 3 1
A setFocusable() 0 8 2
A setAnimated() 0 12 1
A asIcon() 0 9 2
A addLabel() 0 10 1
A fromArray() 0 7 2
A setActive() 0 3 1
A setEmphasis() 0 3 1
A setLoading() 0 3 1
A getSocial() 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\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\traits\LabeledIconTrait;
7
use Ajax\semantic\html\base\constants\Emphasis;
8
use Ajax\semantic\html\base\constants\Social;
9
10
/**
11
 * Semantic Button component
12
 * @see http://semantic-ui.com/elements/button.html
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlButton extends HtmlSemDoubleElement {
17
	use LabeledIconTrait;
18
	/**
19
	 * Constructs an HTML Semantic button
20
	 * @param string $identifier HTML id
21
	 * @param string $value value of the Button
22
	 * @param string $cssStyle btn-default, btn-primary...
23
	 * @param string $onClick JS Code for click event
24
	 */
25 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...
26
		parent::__construct($identifier, "button","ui button");
27
		$this->content=$value;
28
		if (isset($cssStyle)) {
29
			$this->setStyle($cssStyle);
30
		}
31
		if (isset($onClick)) {
32
			$this->onClick($onClick);
33
		}
34
	}
35
36
	/**
37
	 * Set the button value
38
	 * @param string $value
39
	 * @return \Ajax\semantic\html\HtmlButton
40
	 */
41
	public function setValue($value) {
42
		$this->content=$value;
43
		return $this;
44
	}
45
46
	/**
47
	 * define the button style
48
	 * @param string|int $cssStyle
49
	 * @return \Ajax\semantic\html\HtmlButton default : ""
50
	 */
51
	public function setStyle($cssStyle) {
52
		return $this->addToProperty("class",$cssStyle);
53
	}
54
55
	public function setFocusable($value=true){
56
		if($value===true)
57
			$this->setProperty("tabindex", "0");
58
		else{
59
			$this->removeProperty("tabindex");
60
		}
61
		return $this;
62
	}
63
64
	public function setAnimated($content,$animation=""){
65
		$this->setTagName("div");
66
		$this->addToProperty("class", "animated ".$animation);
67
		$visible=new HtmlSemDoubleElement("visible-".$this->identifier,"div");
68
		$visible->setClass("visible content");
69
		$visible->setContent($this->content);
70
		$hidden=new HtmlSemDoubleElement("hidden-".$this->identifier,"div");
71
		$hidden->setClass("hidden content");
72
		$hidden->setContent($content);
73
		$this->content=array($visible,$hidden);
74
		return $hidden;
75
	}
76
77
	/**
78
	 * @param string|HtmlIcon $icon
79
	 * @return \Ajax\semantic\html\elements\HtmlButton
80
	 */
81
	public function asIcon($icon){
82
		$iconO=$icon;
83
		if(\is_string($icon)){
84
			$iconO=new HtmlIcon("icon-".$this->identifier, $icon);
85
		}
86
		$this->addToProperty("class", "icon");
87
		$this->content=$iconO;
88
		return $this;
89
	}
90
91
	/**
92
	 * Add and return a button label
93
	 * @param string $caption
94
	 * @param string $before
95
	 * @return \Ajax\semantic\html\elements\HtmlLabel
96
	 */
97
	public function addLabel($caption,$before=false){
98
		$this->tagName="div";
99
		$this->addToProperty("class", "labeled");
100
		$this->content=new HtmlButton("button-".$this->identifier,$this->content);
101
		$this->content->setTagName("div");
102
		$label=new HtmlLabel("label-".$this->identifier,$caption,"a");
103
		$label->setBasic();
104
		$this->addContent($label,$before);
0 ignored issues
show
Bug introduced by
It seems like $before defined by parameter $before on line 97 can also be of type string; however, Ajax\common\html\HtmlDoubleElement::addContent() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
105
		return $label;
106
	}
107
	/*
108
	 * (non-PHPdoc)
109
	 * @see \Ajax\common\html\BaseHtml::fromArray()
110
	 */
111
	public function fromArray($array) {
112
		$array=parent::fromArray($array);
113
		foreach ( $array as $key => $value ) {
114
			$this->setProperty($key, $value);
115
		}
116
		return $array;
117
	}
118
119
	/**
120
	 *  show it is currently the active user selection
121
	 * @return \Ajax\semantic\html\elements\HtmlButton
122
	 */
123
	public function setActive(){
124
		return $this->addToProperty("class", "active");
125
	}
126
127
	/**
128
	 * hint towards a positive consequence
129
	 * @return \Ajax\semantic\html\elements\HtmlButton
130
	 */
131
	public function setPositive(){
132
		return $this->addToProperty("class", "positive");
133
	}
134
135
	/**
136
	 * hint towards a negative consequence
137
	 * @return \Ajax\semantic\html\elements\HtmlButton
138
	 */
139
	public function setNegative(){
140
		return $this->addToProperty("class", "negative");
141
	}
142
143
	/**
144
	 * formatted to toggle on/off
145
	 * @return \Ajax\semantic\html\elements\HtmlButton
146
	 */
147
	public function setToggle(){
148
		return $this->addToProperty("class", "toggle");
149
	}
150
151
	/**
152
	 * @return \Ajax\semantic\html\elements\HtmlButton
153
	 */
154
	public function setCircular(){
155
		return $this->addToProperty("class", "circular");
156
	}
157
158
	/**
159
	 *  button is less pronounced
160
	 * @return \Ajax\semantic\html\elements\HtmlButton
161
	 */
162
	public function setBasic(){
163
		return $this->addToProperty("class", "basic");
164
	}
165
166
	public function setEmphasis($value){
167
		return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants());
168
	}
169
170
	public function setLoading(){
171
		return $this->addToProperty("class", "loading");
172
	}
173
174
	public static function getSocial($identifier,$social,$value=NULL){
175
		if($value===NULL)
176
			$value=\ucfirst($social);
177
		$return=new HtmlButton($identifier,$value);
178
		$return->addIcon($social);
179
		return $return->addToPropertyCtrl("class", $social, Social::getConstants());
180
	}
181
}