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 classes like HtmlDropdown 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 HtmlDropdown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class HtmlDropdown extends HtmlSemDoubleElement { |
||
16 | use FieldTrait,LabeledIconTrait { |
||
17 | addIcon as addIconP; |
||
18 | } |
||
19 | protected $mClass="menu"; |
||
20 | protected $mTagName="div"; |
||
21 | protected $items=array (); |
||
22 | protected $_params=array("action"=>"nothing","on"=>"hover"); |
||
23 | protected $input; |
||
24 | protected $value; |
||
25 | protected $_associative; |
||
26 | |||
27 | public function __construct($identifier, $value="", $items=array(),$associative=true) { |
||
40 | |||
41 | public function getField(){ |
||
44 | |||
45 | public function getDataField(){ |
||
48 | |||
49 | public function addItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
54 | |||
55 | public function addIcon($icon,$before=true,$labeled=false){ |
||
59 | |||
60 | public function addIcons($icons){ |
||
66 | |||
67 | /** |
||
68 | * Insert an item at a position |
||
69 | * @param mixed $item |
||
70 | * @param number $position |
||
71 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
72 | */ |
||
73 | public function insertItem($item,$position=0){ |
||
81 | |||
82 | protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
97 | |||
98 | /* (non-PHPdoc) |
||
99 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
100 | */ |
||
101 | public function fromDatabaseObject($object, $function) { |
||
104 | |||
105 | public function addInput($name){ |
||
111 | |||
112 | /** |
||
113 | * Adds a search input item |
||
114 | * @param string $placeHolder |
||
115 | * @param string $icon |
||
116 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
117 | */ |
||
118 | public function addSearchInputItem($placeHolder=NULL,$icon=NULL){ |
||
121 | |||
122 | /** |
||
123 | * Adds a divider item |
||
124 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
125 | */ |
||
126 | public function addDividerItem(){ |
||
129 | |||
130 | /** |
||
131 | * Adds an header item |
||
132 | * @param string $caption |
||
133 | * @param string $icon |
||
134 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
135 | */ |
||
136 | public function addHeaderItem($caption=NULL,$icon=NULL){ |
||
139 | |||
140 | /** |
||
141 | * Adds an item with a circular label |
||
142 | * @param string $caption |
||
143 | * @param string $color |
||
144 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
145 | */ |
||
146 | public function addCircularLabelItem($caption,$color){ |
||
149 | |||
150 | /** |
||
151 | * @param string $caption |
||
152 | * @param string $image |
||
153 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
154 | */ |
||
155 | public function addMiniAvatarImageItem($caption,$image){ |
||
158 | |||
159 | public function addItems($items){ |
||
170 | |||
171 | public function getItem($index){ |
||
174 | |||
175 | /** |
||
176 | * @return int |
||
177 | */ |
||
178 | public function count(){ |
||
181 | /** |
||
182 | * @param boolean $dropdown |
||
183 | */ |
||
184 | public function asDropdown($dropdown){ |
||
194 | |||
195 | public function setVertical(){ |
||
198 | |||
199 | public function setInline(){ |
||
202 | |||
203 | public function setSimple(){ |
||
206 | |||
207 | public function asButton($floating=false){ |
||
213 | |||
214 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
225 | |||
226 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
230 | |||
231 | public function setSelect($name=NULL,$multiple=false){ |
||
248 | |||
249 | public function asSubmenu($pointing=NULL){ |
||
256 | |||
257 | public function setPointing($value=Direction::NONE){ |
||
260 | |||
261 | public function setValue($value){ |
||
277 | |||
278 | /* |
||
279 | * (non-PHPdoc) |
||
280 | * @see BaseHtml::run() |
||
281 | */ |
||
282 | public function run(JsUtils $js) { |
||
290 | |||
291 | public function setCompact(){ |
||
294 | |||
295 | public function setAction($action){ |
||
298 | |||
299 | public function setFullTextSearch($value){ |
||
302 | |||
303 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
307 | |||
308 | public function getInput() { |
||
311 | |||
312 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: