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) { |
||
| 44 | |||
| 45 | public function getField(){ |
||
| 48 | |||
| 49 | public function getDataField(){ |
||
| 52 | |||
| 53 | public function addItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 58 | |||
| 59 | public function addIcon($icon,$before=true,$labeled=false){ |
||
| 64 | |||
| 65 | public function addIcons($icons){ |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Insert an item at a position |
||
| 74 | * @param mixed $item |
||
| 75 | * @param number $position |
||
| 76 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 77 | */ |
||
| 78 | public function insertItem($item,$position=0){ |
||
| 86 | |||
| 87 | protected function removeArrow(){ |
||
| 93 | |||
| 94 | protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 109 | |||
| 110 | /* (non-PHPdoc) |
||
| 111 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
| 112 | */ |
||
| 113 | public function fromDatabaseObject($object, $function) { |
||
| 116 | |||
| 117 | public function addInput($name){ |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Adds a search input item |
||
| 126 | * @param string $placeHolder |
||
| 127 | * @param string $icon |
||
| 128 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 129 | */ |
||
| 130 | public function addSearchInputItem($placeHolder=NULL,$icon=NULL){ |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Adds a divider item |
||
| 136 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 137 | */ |
||
| 138 | public function addDividerItem(){ |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Adds an header item |
||
| 144 | * @param string $caption |
||
| 145 | * @param string $icon |
||
| 146 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 147 | */ |
||
| 148 | public function addHeaderItem($caption=NULL,$icon=NULL){ |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds an item with a circular label |
||
| 154 | * @param string $caption |
||
| 155 | * @param string $color |
||
| 156 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 157 | */ |
||
| 158 | public function addCircularLabelItem($caption,$color){ |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $caption |
||
| 164 | * @param string $image |
||
| 165 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 166 | */ |
||
| 167 | public function addMiniAvatarImageItem($caption,$image){ |
||
| 170 | |||
| 171 | public function addItems($items){ |
||
| 182 | |||
| 183 | public function getItem($index){ |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return int |
||
| 189 | */ |
||
| 190 | public function count(){ |
||
| 193 | /** |
||
| 194 | * @param boolean $dropdown |
||
| 195 | */ |
||
| 196 | public function asDropdown($dropdown){ |
||
| 206 | |||
| 207 | public function setVertical(){ |
||
| 210 | |||
| 211 | public function setInline(){ |
||
| 214 | |||
| 215 | public function setSimple(){ |
||
| 218 | |||
| 219 | public function asButton($floating=false){ |
||
| 226 | |||
| 227 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
| 238 | |||
| 239 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
| 243 | |||
| 244 | public function setSelect($name=NULL,$multiple=false){ |
||
| 261 | |||
| 262 | public function asSubmenu($pointing=NULL){ |
||
| 269 | |||
| 270 | public function setPointing($value=Direction::NONE){ |
||
| 273 | |||
| 274 | public function setValue($value){ |
||
| 290 | |||
| 291 | /* |
||
| 292 | * (non-PHPdoc) |
||
| 293 | * @see BaseHtml::run() |
||
| 294 | */ |
||
| 295 | public function run(JsUtils $js) { |
||
| 305 | |||
| 306 | public function setCompact(){ |
||
| 309 | |||
| 310 | public function setAction($action){ |
||
| 313 | |||
| 314 | public function setFullTextSearch($value){ |
||
| 317 | |||
| 318 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 322 | |||
| 323 | public function getInput() { |
||
| 329 | |||
| 330 | public function jsAddItem($caption){ |
||
| 334 | } |
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: