Completed
Push — master ( 701cf4...686575 )
by Jean-Christophe
03:12
created

FieldAsTrait   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 216
Duplicated Lines 6.48 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 36
c 0
b 0
f 0
lcom 1
cbo 15
dl 14
loc 216
rs 8.0666

27 Methods

Rating   Name   Duplication   Size   Complexity  
_getFieldIdentifier() 0 1 ?
setValueFunction() 0 1 ?
_getFieldName() 0 1 ?
_getFieldCaption() 0 1 ?
_buttonAsSubmit() 0 1 ?
A _applyAttributes() 0 11 3
A _getLabelField() 0 4 1
A _addRules() 0 12 3
A _prepareFormFields() 0 5 1
A _fieldAs() 0 18 3
A fieldAsProgress() 7 7 1
A fieldAsRating() 7 7 1
A fieldAsLabel() 0 7 1
A fieldAsHeader() 0 8 2
A fieldAsImage() 0 7 2
A fieldAsAvatar() 0 7 1
A fieldAsRadio() 0 6 1
A fieldAsRadios() 0 5 1
A fieldAsInput() 0 6 1
A fieldAsTextarea() 0 7 1
A fieldAsHidden() 0 7 2
A fieldAsCheckbox() 0 8 1
A fieldAsDropDown() 0 7 1
A fieldAsMessage() 0 7 1
A fieldsAs() 0 12 4
A fieldAs() 0 12 3
A fieldAsSubmit() 0 7 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
namespace Ajax\semantic\widgets\base;
3
use Ajax\service\JString;
4
use Ajax\semantic\html\elements\HtmlImage;
5
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
6
use Ajax\semantic\html\base\constants\Size;
7
use Ajax\semantic\html\elements\HtmlLabel;
8
use Ajax\semantic\html\modules\HtmlProgress;
9
use Ajax\semantic\html\modules\HtmlRating;
10
use Ajax\semantic\html\elements\HtmlHeader;
11
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
12
use Ajax\semantic\html\collections\form\HtmlFormInput;
13
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
14
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15
use Ajax\semantic\html\collections\form\HtmlFormFields;
16
use Ajax\semantic\html\collections\HtmlMessage;
17
use Ajax\semantic\html\elements\HtmlButton;
18
use Ajax\service\JArray;
19
20
/**
21
 * @author jc
22
 * @property InstanceViewer $_instanceViewer
23
 * @property boolean $_edition
24
 * @property mixed _modelInstance
25
 */
26
trait FieldAsTrait{
27
28
	abstract protected function _getFieldIdentifier($prefix,$name="");
29
	abstract public function setValueFunction($index,$callback);
30
	abstract protected function _getFieldName($index);
31
	abstract protected function _getFieldCaption($index);
32
	abstract protected function _buttonAsSubmit(&$button,$event,$url,$responseElement=NULL);
33
34
	/**
35
	 * @param HtmlFormField $element
36
	 * @param array $attributes
37
	 */
38
	protected function _applyAttributes($element,&$attributes,$index){
39
		if(isset($attributes["callback"])){
40
			$callback=$attributes["callback"];
41
			if(\is_callable($callback)){
42
				$callback($element,$this->_modelInstance,$index);
43
				unset($attributes["callback"]);
44
			}
45
		}
46
		unset($attributes["rules"]);
47
		$element->fromArray($attributes);
48
	}
49
50
	private function _getLabelField($caption,$icon=NULL){
51
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
52
		return $label;
53
	}
54
55
	protected function _addRules($element,&$attributes){
56
		if(isset($attributes["rules"])){
57
			$rules=$attributes["rules"];
58
			if(\is_array($rules)){
59
				$element->addRules($rules);
60
			}
61
			else{
62
				$element->addRule($rules);
63
			}
64
			unset($attributes["rules"]);
65
		}
66
	}
67
68
	protected function _prepareFormFields(&$field,$name,&$attributes){
69
		$field->setName($name);
70
		$this->_addRules($field, $attributes);
71
		return $field;
72
	}
73
74
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
75
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
76
			$caption=$this->_getFieldCaption($index);
77
			$name=$this->_getFieldName($index);
78
			$id=$this->_getFieldIdentifier($prefix,$name);
79
			if(isset($attributes["name"])){
80
				$name=$attributes["name"];
81
				unset($attributes["name"]);
82
			}
83
			$element=$elementCallback($id,$name,$value,$caption);
84
			if(\is_array($attributes)){
85
				$this->_applyAttributes($element, $attributes,$index);
86
			}
87
			$element->setDisabled(!$this->_edition);
88
			return $element;
89
		});
90
			return $this;
91
	}
92
93
94 View Code Duplication
	public function fieldAsProgress($index,$label=NULL, $attributes=array()){
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...
95
		$this->setValueFunction($index,function($value) use($label,$attributes){
96
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
97
			return $pb;
98
		});
99
			return $this;
100
	}
101
102 View Code Duplication
	public function fieldAsRating($index,$max=5, $icon=""){
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...
103
		$this->setValueFunction($index,function($value) use($max,$icon){
104
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
105
			return $rating;
106
		});
107
			return $this;
108
	}
109
110
	public function fieldAsLabel($index,$icon=NULL){
111
		$this->setValueFunction($index,function($caption) use($icon){
112
			$lbl=$this->_getLabelField($caption,$icon);
113
			return $lbl;
114
		});
115
			return $this;
116
	}
117
118
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
119
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
120
			$header=new HtmlHeader($id,$niveau,$value);
121
			if(isset($icon))
122
				$header->asIcon($icon, $value);
123
			return $header;
124
		}, $index,$attributes,"header");
125
	}
126
127
128
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
129
		$this->setValueFunction($index,function($img) use($size,$circular){
130
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
131
			return $image;
132
		});
133
			return $this;
134
	}
135
136
	public function fieldAsAvatar($index,$attributes=NULL){
137
		return $this->_fieldAs(function($id,$name,$value){
138
			$img=new HtmlImage($id,$value);
139
			$img->asAvatar();
140
			return $img;
141
		}, $index,$attributes,"avatar");
142
	}
143
144
	public function fieldAsRadio($index,$attributes=NULL){
145
		return $this->_fieldAs(function($id,$name,$value){
146
			$input= new HtmlRadio($id,$name,$value,$value);
147
			return $input;
148
		}, $index,$attributes,"radio");
149
	}
150
151
	public function fieldAsRadios($index,$elements=[],$attributes=NULL){
152
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
153
			return HtmlFormFields::radios($name,$elements,$caption,$value);
154
		}, $index,$attributes,"radios");
155
	}
156
157
	public function fieldAsInput($index,$attributes=NULL){
158
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
159
			$input= new HtmlFormInput($id,$caption,"text",$value);
160
			return $this->_prepareFormFields($input, $name, $attributes);
161
		}, $index,$attributes,"input");
162
	}
163
164
	public function fieldAsTextarea($index,$attributes=NULL){
165
		return $this->_fieldAs(function($id,$name,$value){
166
			$textarea=new HtmlFormTextarea($id,null,$value);
167
			$textarea->setName($name);
168
			return $textarea;
169
		}, $index,$attributes,"textarea");
170
	}
171
172
	public function fieldAsHidden($index,$attributes=NULL){
173
		if(!\is_array($attributes)){
174
			$attributes=[];
175
		}
176
		$attributes["imputType"]="hidden";
177
		return $this->fieldAsInput($index,$attributes);
178
	}
179
180
	public function fieldAsCheckbox($index,$attributes=NULL){
181
		return $this->_fieldAs(function($id,$name,$value){
182
			$input=new HtmlFormCheckbox($id,NULL,$this->_instanceViewer->getIdentifier());
183
			$input->setChecked(JString::isBooleanTrue($value));
184
			$input->setName($name);
185
			return $input;
186
		}, $index,$attributes,"ck");
187
	}
188
189
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
190
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
191
			$dd=new HtmlFormDropdown($id,$elements,NULL,$value);
192
			$dd->asSelect($name,$multiple);
193
			return $dd;
194
		}, $index,$attributes,"dd");
195
	}
196
197
	public function fieldAsMessage($index,$attributes=NULL){
198
		return $this->_fieldAs(function($id,$name,$value,$caption){
199
			$mess= new HtmlMessage("message-".$id,$value);
200
			$mess->addHeader($caption);
201
			return $mess;
202
		}, $index,$attributes,"message");
203
	}
204
205
	/**Change fields type
206
	 * @param array $types an array or associative array $type=>$attribute
207
	 */
208
	public function fieldsAs(array $types){
209
		$i=0;
210
		if(JArray::isAssociative($types)){
211
			foreach ($types as $type=>$attributes){
212
				$this->fieldAs($i++,$type,$attributes);
213
			}
214
		}else{
215
			foreach ($types as $type){
216
				$this->fieldAs($i++,$type);
217
			}
218
		}
219
	}
220
221
	public function fieldAs($index,$type,$attributes=NULL){
222
		$method="fieldAs".\ucfirst($type);
223
224
		if(\method_exists($this, $method)){
225
			if(!\is_array($attributes)){
226
				$attributes=[$index];
227
			}else{
228
				\array_unshift($attributes, $index);
229
			}
230
			\call_user_func_array([$this,$method], $attributes);
231
		}
232
	}
233
234
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
235
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle){
0 ignored issues
show
Unused Code introduced by
The parameter $caption is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
236
			$button=new HtmlButton($id,$value,$cssStyle);
237
			$this->_buttonAsSubmit($button,"click",$url,$responseElement);
238
			return $button;
239
		}, $index,$attributes);
240
	}
241
}