Completed
Push — master ( bb3350...c8eebb )
by Jean-Christophe
03:32
created

HtmlProgress   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 114
Duplicated Lines 1.75 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 3
cbo 2
dl 2
loc 114
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A setLabel() 0 4 1
A createBar() 0 5 1
A setTotal() 0 3 1
A setValue() 0 3 1
A setPercent() 0 3 1
A setIndicating() 0 3 1
A setWarning() 0 3 1
A setError() 0 3 1
A compile() 0 4 1
A jsSetValue() 0 3 1
A jsIncValue() 0 3 1
A jsDecValue() 0 3 1
A setTextValues() 0 12 2
A onChange() 0 3 1
A run() 2 6 2
A create() 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\modules;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\JsUtils;
7
use Ajax\service\JArray;
8
9
use Ajax\semantic\html\base\constants\State;
10
11
class HtmlProgress extends HtmlSemDoubleElement {
12
	private $_params=array ();
13
14
	public function __construct($identifier, $value=NULL, $label=NULL, $attributes=array()) {
15
		parent::__construct($identifier, "div", "ui progress");
16
		if (isset($value) === true)
17
			$this->setProperty("data-percent", $value);
18
		$this->createBar();
19
		if (isset($label) === true)
20
			$this->setLabel($label);
21
		$this->_states=[ State::SUCCESS,State::WARNING,State::ERROR,State::ACTIVE,State::DISABLED ];
22
		$this->addToProperty("class", $attributes);
23
	}
24
25
	public function setLabel($label) {
26
		$this->content["label"]=new HtmlSemDoubleElement("lbl-" . $this->identifier, "div", "label", $label);
27
		return $this;
28
	}
29
30
	private function createBar() {
31
		$bar=new HtmlSemDoubleElement("bar-" . $this->identifier, "div", "bar", new HtmlSemDoubleElement("progress-" . $this->identifier, "div", "progress"));
32
		$this->content["bar"]=$bar;
33
		return $this;
34
	}
35
36
	public function setTotal($value) {
37
		return $this->setProperty("data-total", $value);
38
	}
39
40
	public function setValue($value) {
41
		return $this->setProperty("data-value", $value);
42
	}
43
44
	public function setPercent($value) {
45
		return $this->setProperty("data-percent", $value);
46
	}
47
48
	public function setIndicating() {
49
		return $this->addToProperty("class", "indicating");
50
	}
51
52
	public function setWarning() {
53
		return $this->addToProperty("class", "warning");
54
	}
55
56
	public function setError() {
57
		return $this->addToProperty("class", "error");
58
	}
59
60
	/**
61
	 *
62
	 * {@inheritDoc}
63
	 *
64
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
65
	 */
66
	public function compile(JsUtils $js=NULL, &$view=NULL) {
67
		$this->content=JArray::sortAssociative($this->content, [ "bar","label" ]);
68
		return parent::compile($js, $view);
69
	}
70
71
	public function jsSetValue($value) {
72
		return '$("#' . $this->identifier . '").progress({value:' . $value . '});';
73
	}
74
75
	public function jsIncValue() {
76
		return '$("#' . $this->identifier . '").progress("increment");';
77
	}
78
79
	public function jsDecValue() {
80
		return '$("#' . $this->identifier . '").progress("decrement");';
81
	}
82
83
	/**
84
	 *
85
	 * @param mixed $active
86
	 * @param mixed $error
87
	 * @param mixed $success
88
	 * @param mixed $warning
89
	 * @param mixed $percent
90
	 * @param mixed $ratio
91
	 * @return HtmlProgress
92
	 */
93
	public function setTextValues($active=false, $error=false, $success=false, $warning=false, $percent="{percent}%", $ratio="{value} of {total}") {
94
		if (\is_array($active) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
95
			$array=$active;
96
			$active=JArray::getDefaultValue($array, "active", false);
97
			$success=JArray::getDefaultValue($array, "success", $success);
98
			$warning=JArray::getDefaultValue($array, "warning", $warning);
99
			$percent=JArray::getDefaultValue($array, "percent", $percent);
100
			$ratio=JArray::getDefaultValue($array, "ratio", $ratio);
101
		}
102
		$this->_params["text"]="%{active  : " . \var_export($active, true) . ",error: " . \var_export($error, true) . ",success : " . \var_export($success, true) . ",warning : " . \var_export($warning, true) . ",percent : " . \var_export($percent, true) . ",ratio   : " . \var_export($ratio, true) . "}%";
103
		return $this;
104
	}
105
106
	public function onChange($jsCode) {
107
		return $this->_params["onChange"]="%function(percent, value, total){" . $jsCode . "}%";
108
	}
109
110
	/*
111
	 * (non-PHPdoc)
112
	 * @see BaseHtml::run()
113
	 */
114
	public function run(JsUtils $js) {
115 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...
116
			$this->_bsComponent=$js->semantic()->progress("#" . $this->identifier, $this->_params);
117
		$this->addEventsOnRun($js);
118
		return $this->_bsComponent;
119
	}
120
121
	public static function create($identifier, $value=NULL, $label=NULL, $attributes=array()) {
122
		return new HtmlProgress($identifier, $value, $label, $attributes);
123
	}
124
}