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 fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){ |
||
71 | return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){ |
||
72 | $header=new HtmlHeader($id,$niveau,$value); |
||
73 | if(isset($icon)) |
||
74 | $header->asIcon($icon, $value); |
||
78 | |||
79 | |||
80 | public function fieldAsImage($index,$size=Size::MINI,$circular=false){ |
||
87 | |||
88 | public function fieldAsAvatar($index,$attributes=NULL){ |
||
95 | |||
96 | public function fieldAsRadio($index,$attributes=NULL){ |
||
102 | |||
103 | View Code Duplication | public function fieldAsInput($index,$attributes=NULL){ |
|
111 | |||
112 | public function fieldAsHidden($index,$attributes=NULL){ |
||
119 | |||
120 | public function fieldAsCheckbox($index,$attributes=NULL){ |
||
128 | |||
129 | View Code Duplication | public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){ |
|
136 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.