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 |
||
19 | trait FieldAsTrait{ |
||
20 | |||
21 | abstract protected function _getFieldIdentifier($prefix); |
||
22 | abstract public function setValueFunction($index,$callback); |
||
23 | |||
24 | private function _getLabelField($caption,$icon=NULL){ |
||
25 | $label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon); |
||
26 | return $label; |
||
27 | } |
||
28 | |||
29 | protected function _addRules($element,$attributes){} |
||
30 | |||
31 | protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){ |
||
32 | $this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){ |
||
33 | $name=$this->_instanceViewer->getCaption($index)."[]"; |
||
34 | if(isset($attributes["name"])===true){ |
||
35 | $name=$attributes["name"]; |
||
36 | } |
||
37 | $element=$elementCallback($this->_getFieldIdentifier($prefix),$name,$value,""); |
||
38 | if(\is_array($attributes)) |
||
39 | $this->_applyAttributes($element, $attributes,$index); |
||
40 | return $element; |
||
41 | }); |
||
42 | return $this; |
||
43 | } |
||
44 | |||
45 | |||
46 | View Code Duplication | public function fieldAsProgress($index,$label=NULL, $attributes=array()){ |
|
53 | |||
54 | View Code Duplication | public function fieldAsRating($index,$max=5, $icon=""){ |
|
61 | |||
62 | public function fieldAsLabel($index,$icon=NULL){ |
||
69 | |||
70 | public function fieldAsImage($index,$size=Size::SMALL,$circular=false){ |
||
77 | |||
78 | public function fieldAsAvatar($index,$attributes=NULL){ |
||
79 | return $this->_fieldAs(function($id,$name,$value){ |
||
80 | $img=new HtmlImage($id,$value); |
||
81 | $img->asAvatar(); |
||
82 | return $img; |
||
83 | }, $index,$attributes,"avatar"); |
||
84 | } |
||
85 | |||
86 | public function fieldAsRadio($index,$attributes=NULL){ |
||
92 | |||
93 | View Code Duplication | public function fieldAsInput($index,$attributes=NULL){ |
|
94 | return $this->_fieldAs(function($id,$name,$value){ |
||
95 | $input= new HtmlInput($id,"text",$value); |
||
96 | //TODO check getField |
||
97 | $input->setName($name); |
||
98 | return $input; |
||
101 | |||
102 | public function fieldAsHidden($index,$attributes=NULL){ |
||
109 | |||
110 | public function fieldAsCheckbox($index,$attributes=NULL){ |
||
118 | |||
119 | View Code Duplication | public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){ |
|
126 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.