Completed
Push — master ( b3219e...613cdf )
by Jean-Christophe
03:22
created

BaseHtml   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 197
Duplicated Lines 9.14 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 52
lcom 1
cbo 5
dl 18
loc 197
rs 7.9487
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getBsComponent() 0 3 1
A setBsComponent() 0 4 1
A getTemplate() 0 3 1
B compile() 0 20 7
A ctrl() 9 12 4
A setMemberCtrl() 0 6 2
A addToMemberUnique() 0 7 2
A addToMemberCtrl() 0 9 3
A addToMember() 0 4 1
A removeOldValues() 0 4 1
run() 0 1 ?
A getTagName() 0 3 1
A setTagName() 0 4 1
B fromArray() 9 12 5
A _callSetter() 0 13 4
A fromDatabaseObjects() 0 8 3
A fromDatabaseObject() 0 2 1
A wrap() 0 7 2
A getElementById() 0 3 1
B _getElementBy() 0 17 8
A __toString() 0 3 1
A setWrapBefore() 0 4 1
A setWrapAfter() 0 4 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BaseHtml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BaseHtml, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ajax\common\html;
4
5
6
use Ajax\service\JString;
7
use Ajax\common\components\SimpleExtComponent;
8
use Ajax\JsUtils;
9
use Ajax\common\html\traits\BaseHtmlEventsTrait;
10
use Ajax\common\html\traits\BaseHtmlPropertiesTrait;
11
12
/**
13
 * BaseHtml for HTML components
14
 * @author jc
15
 * @version 1.2
16
 */
17
abstract class BaseHtml extends BaseWidget {
18
	use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait;
19
	protected $_template;
20
	protected $tagName;
21
	protected $_wrapBefore=array ();
22
	protected $_wrapAfter=array ();
23
	protected $_bsComponent;
24
25
	public function getBsComponent() {
26
		return $this->_bsComponent;
27
	}
28
29
	public function setBsComponent($bsComponent) {
30
		$this->_bsComponent=$bsComponent;
31
		return $this;
32
	}
33
34
	protected function getTemplate(JsUtils $js=NULL) {
35
		return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js);
36
	}
37
38
	public function compile(JsUtils $js=NULL, &$view=NULL) {
39
		$result=$this->getTemplate($js);
40
		foreach ( $this as $key => $value ) {
41
			if (JString::startswith($key, "_") === false && $key !== "events") {
42
				if (\is_array($value)) {
43
					$v=PropertyWrapper::wrap($value, $js);
44
				} else {
45
					$v=$value;
46
				}
47
				$result=str_ireplace("%" . $key . "%", $v, $result);
48
			}
49
		}
50
		if (isset($js)===true) {
51
			$this->run($js);
52
			if (isset($view) === true) {
53
				$js->addViewElement($this->identifier, $result, $view);
54
			}
55
		}
56
		return $result;
57
	}
58
59
	protected function ctrl($name, $value, $typeCtrl) {
60 View Code Duplication
		if (\is_array($typeCtrl)) {
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...
61
			if (array_search($value, $typeCtrl) === false) {
62
				throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}");
63
			}
64
		} else {
65
			if (!$typeCtrl($value)) {
66
				throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name);
67
			}
68
		}
69
		return true;
70
	}
71
72
73
74
	protected function setMemberCtrl(&$name, $value, $typeCtrl) {
75
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
76
			return $name=$value;
77
		}
78
		return $this;
79
	}
80
81
	protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") {
82
		if (\is_array($typeCtrl)) {
83
			$this->removeOldValues($name, $typeCtrl);
84
			$name.=$separator . $value;
85
		}
86
		return $this;
87
	}
88
89
90
91
	protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
92
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
93
			if (\is_array($typeCtrl)) {
94
				$this->removeOldValues($name, $typeCtrl);
95
			}
96
			$name.=$separator . $value;
97
		}
98
		return $this;
99
	}
100
101
	protected function addToMember(&$name, $value, $separator=" ") {
102
		$name=str_ireplace($value, "", $name) . $separator . $value;
103
		return $this;
104
	}
105
106
107
108
	protected function removeOldValues(&$oldValue, $allValues) {
109
		$oldValue=str_ireplace($allValues, "", $oldValue);
110
		$oldValue=trim($oldValue);
111
	}
112
113
	/**
114
	 *
115
	 * @param JsUtils $js
116
	 * @return SimpleExtComponent
117
	 */
118
	abstract public function run(JsUtils $js);
119
120
	public function getTagName() {
121
		return $this->tagName;
122
	}
123
124
	public function setTagName($tagName) {
125
		$this->tagName=$tagName;
126
		return $this;
127
	}
128
129
	public function fromArray($array) {
130 View Code Duplication
		foreach ( $this as $key => $value ) {
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...
131
			if(array_key_exists($key, $array)===true)
132
				$this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array);
133
		}
134 View Code Duplication
		foreach ( $array as $key => $value ) {
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...
135
			if($this->_callSetter($key, $key, $value, $array)===false){
136
				$this->_callSetter("set" . ucfirst($key), $key, $value, $array);
137
			}
138
		}
139
		return $array;
140
	}
141
142
	private function _callSetter($setter,$key,$value,&$array){
143
		$result=false;
144
		if (method_exists($this, $setter) && !JString::startswith($key, "_")) {
145
			try {
146
				$this->$setter($value);
147
				unset($array[$key]);
148
				$result=true;
149
			} catch ( \Exception $e ) {
150
				$result=false;
151
			}
152
		}
153
		return $result;
154
	}
155
156
	public function fromDatabaseObjects($objects, $function) {
157
		if (isset($objects)) {
158
			foreach ( $objects as $object ) {
159
				$this->fromDatabaseObject($object, $function);
160
			}
161
		}
162
		return $this;
163
	}
164
165
	public function fromDatabaseObject($object, $function) {
166
	}
167
168
	public function wrap($before, $after="") {
169
		if (isset($before)) {
170
			array_unshift($this->_wrapBefore, $before);
171
		}
172
		$this->_wrapAfter[]=$after;
173
		return $this;
174
	}
175
176
177
178
	public function getElementById($identifier, $elements) {
179
		return $this->_getElementBy(function($element) use ($identifier){return $element->getIdentifier()===$identifier;}, $elements);
180
	}
181
182
	protected function _getElementBy($callback,$elements){
183
		if (\is_array($elements)) {
184
			$flag=false;
185
			$index=0;
186
			while ( !$flag && $index < sizeof($elements) ) {
187
				if ($elements[$index] instanceof BaseHtml)
188
					$flag=($callback($elements[$index]));
189
					$index++;
190
			}
191
			if ($flag === true)
192
				return $elements[$index - 1];
193
		} elseif ($elements instanceof BaseHtml) {
194
			if ($callback($elements))
195
				return $elements;
196
		}
197
		return null;
198
	}
199
200
	public function __toString() {
201
		return $this->compile();
202
	}
203
204
	protected function setWrapBefore($wrapBefore) {
205
		$this->_wrapBefore=$wrapBefore;
206
		return $this;
207
	}
208
209
	protected function setWrapAfter($wrapAfter) {
210
		$this->_wrapAfter=$wrapAfter;
211
		return $this;
212
	}
213
}