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 |
||
| 14 | class HtmlDropdown extends HtmlSemDoubleElement { |
||
| 15 | use LabeledIconTrait { |
||
| 16 | addIcon as addIconP; |
||
| 17 | } |
||
| 18 | protected $mClass="menu"; |
||
| 19 | protected $mTagName="div"; |
||
| 20 | protected $items=array (); |
||
| 21 | protected $_params=array("action"=>"nothing","on"=>"hover"); |
||
| 22 | protected $input; |
||
| 23 | |||
| 24 | public function __construct($identifier, $value="", $items=array()) { |
||
| 36 | |||
| 37 | public function addItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 38 | $itemO=$this->beforeAddItem($item,$value,$image,$description); |
||
| 39 | $this->items[]=$itemO; |
||
| 40 | return $itemO; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function addIcon($icon,$before=true,$labeled=false){ |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Insert an item at a position |
||
| 50 | * @param mixed $item |
||
| 51 | * @param number $position |
||
| 52 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 53 | */ |
||
| 54 | public function insertItem($item,$position=0){ |
||
| 62 | |||
| 63 | protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 78 | |||
| 79 | /* (non-PHPdoc) |
||
| 80 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
| 81 | */ |
||
| 82 | public function fromDatabaseObject($object, $function) { |
||
| 85 | |||
| 86 | public function addInput($name){ |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Adds a search input item |
||
| 95 | * @param string $placeHolder |
||
| 96 | * @param string $icon |
||
| 97 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 98 | */ |
||
| 99 | public function addSearchInputItem($placeHolder=NULL,$icon=NULL){ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Adds a divider item |
||
| 105 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 106 | */ |
||
| 107 | public function addDividerItem(){ |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Adds an header item |
||
| 113 | * @param string $caption |
||
| 114 | * @param string $icon |
||
| 115 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 116 | */ |
||
| 117 | public function addHeaderItem($caption=NULL,$icon=NULL){ |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Adds an item with a circular label |
||
| 123 | * @param string $caption |
||
| 124 | * @param string $color |
||
| 125 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 126 | */ |
||
| 127 | public function addCircularLabelItem($caption,$color){ |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $caption |
||
| 133 | * @param string $image |
||
| 134 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 135 | */ |
||
| 136 | public function addMiniAvatarImageItem($caption,$image){ |
||
| 139 | |||
| 140 | View Code Duplication | public function addItems($items){ |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return int |
||
| 154 | */ |
||
| 155 | public function count(){ |
||
| 158 | /** |
||
| 159 | * @param boolean $dropdown |
||
| 160 | */ |
||
| 161 | public function asDropdown($dropdown){ |
||
| 171 | |||
| 172 | public function setVertical(){ |
||
| 175 | |||
| 176 | public function setSimple(){ |
||
| 179 | |||
| 180 | public function asButton($floating=false){ |
||
| 186 | |||
| 187 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
| 198 | |||
| 199 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
| 203 | |||
| 204 | public function setSelect($name=NULL,$multiple=false){ |
||
| 221 | |||
| 222 | public function asSubmenu($pointing=NULL){ |
||
| 229 | |||
| 230 | public function setPointing($value=Direction::NONE){ |
||
| 233 | |||
| 234 | public function setValue($value){ |
||
| 245 | |||
| 246 | /* |
||
| 247 | * (non-PHPdoc) |
||
| 248 | * @see BaseHtml::run() |
||
| 249 | */ |
||
| 250 | public function run(JsUtils $js) { |
||
| 258 | |||
| 259 | public function setCompact(){ |
||
| 262 | |||
| 263 | public function setAction($action){ |
||
| 266 | |||
| 267 | public function setFullTextSearch($value){ |
||
| 270 | } |
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: