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 |
||
14 | class HtmlButtongroups extends HtmlBsDoubleElement { |
||
15 | |||
16 | /** |
||
17 | * @var array[HtmlButton] |
||
18 | */ |
||
19 | protected $elements; |
||
20 | |||
21 | View Code Duplication | public function __construct($identifier, $elements=array(), $cssStyle=NULL, $size=NULL, $tagName="div") { |
|
34 | |||
35 | /** |
||
36 | * define the buttons size |
||
37 | * available values : "btn-group-lg","","btn-group-sm","btn-group-xs" |
||
38 | * @param string|int $size |
||
39 | * @return HtmlButtongroups default : "" |
||
40 | */ |
||
41 | public function setSize($size) { |
||
50 | |||
51 | public function setStyle($value) { |
||
55 | |||
56 | private function dropdownAsButton($bt) { |
||
62 | |||
63 | private function addExistingDropDown($bt) { |
||
68 | |||
69 | public function addElement($element) { |
||
70 | $result=$element; |
||
71 | $iid=sizeof($this->elements)+1; |
||
72 | if (($element instanceof HtmlDropdown)||($element instanceof HtmlSplitbutton)) { |
||
73 | $this->addExistingDropDown($element); |
||
74 | } elseif (\is_array($element)) { |
||
75 | $result=$this->_addArrayElement($element,$iid); |
||
76 | } elseif (is_string($element)) { |
||
77 | $result=new HtmlButton($this->identifier."-button-".$iid,$element); |
||
78 | } |
||
79 | if($result instanceof HtmlButton) |
||
80 | $this->elements[]=$result; |
||
81 | return $result; |
||
82 | } |
||
83 | |||
84 | private function _addArrayElement(array $element,int $iid){ |
||
85 | if (array_key_exists("glyph", $element)) |
||
86 | $bt=new HtmlGlyphButton($this->identifier."-button-".$iid); |
||
87 | elseif (array_key_exists("btnCaption", $element)) { |
||
88 | if (array_key_exists("split", $element)) |
||
89 | $bt=new HtmlSplitbutton($this->identifier."-dropdown-".$iid); |
||
90 | else{ |
||
91 | $bt=new HtmlDropdown($this->identifier."-dropdown-".$iid); |
||
92 | $this->dropdownAsButton($bt); |
||
93 | } |
||
94 | } else |
||
95 | $bt=new HtmlButton($this->identifier."-button-".$iid); |
||
96 | $bt->fromArray($element); |
||
97 | return $bt; |
||
98 | } |
||
99 | |||
100 | public function addElements($elements) { |
||
106 | |||
107 | /* |
||
108 | * (non-PHPdoc) |
||
109 | * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
||
110 | */ |
||
111 | public function fromArray($array) { |
||
114 | |||
115 | public function setAlignment($value) { |
||
127 | |||
128 | /** |
||
129 | * Return the element at index |
||
130 | * @param int $index |
||
131 | * @return HtmlButton |
||
132 | */ |
||
133 | public function getElement($index) { |
||
141 | |||
142 | public function setElement($index, $button) { |
||
146 | |||
147 | /* |
||
148 | * (non-PHPdoc) |
||
149 | * @see \Ajax\bootstrap\html\base\BaseHtml::on() |
||
150 | */ |
||
151 | public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
157 | |||
158 | public function getElements() { |
||
161 | |||
162 | /* (non-PHPdoc) |
||
163 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
164 | */ |
||
165 | public function fromDatabaseObject($object, $function) { |
||
168 | |||
169 | } |
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.