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 | protected $value; |
||
| 24 | |||
| 25 | public function __construct($identifier, $value="", $items=array()) { |
||
| 37 | |||
| 38 | public function addItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 43 | |||
| 44 | public function addIcon($icon,$before=true,$labeled=false){ |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Insert an item at a position |
||
| 51 | * @param mixed $item |
||
| 52 | * @param number $position |
||
| 53 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 54 | */ |
||
| 55 | public function insertItem($item,$position=0){ |
||
| 63 | |||
| 64 | protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 79 | |||
| 80 | /* (non-PHPdoc) |
||
| 81 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
| 82 | */ |
||
| 83 | public function fromDatabaseObject($object, $function) { |
||
| 86 | |||
| 87 | public function addInput($name){ |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Adds a search input item |
||
| 96 | * @param string $placeHolder |
||
| 97 | * @param string $icon |
||
| 98 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 99 | */ |
||
| 100 | public function addSearchInputItem($placeHolder=NULL,$icon=NULL){ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Adds a divider item |
||
| 106 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 107 | */ |
||
| 108 | public function addDividerItem(){ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Adds an header item |
||
| 114 | * @param string $caption |
||
| 115 | * @param string $icon |
||
| 116 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 117 | */ |
||
| 118 | public function addHeaderItem($caption=NULL,$icon=NULL){ |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Adds an item with a circular label |
||
| 124 | * @param string $caption |
||
| 125 | * @param string $color |
||
| 126 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 127 | */ |
||
| 128 | public function addCircularLabelItem($caption,$color){ |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $caption |
||
| 134 | * @param string $image |
||
| 135 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 136 | */ |
||
| 137 | public function addMiniAvatarImageItem($caption,$image){ |
||
| 140 | |||
| 141 | View Code Duplication | public function addItems($items){ |
|
| 152 | |||
| 153 | public function getItem($index){ |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | public function count(){ |
||
| 163 | /** |
||
| 164 | * @param boolean $dropdown |
||
| 165 | */ |
||
| 166 | public function asDropdown($dropdown){ |
||
| 176 | |||
| 177 | public function setVertical(){ |
||
| 180 | |||
| 181 | public function setInline(){ |
||
| 184 | |||
| 185 | public function setSimple(){ |
||
| 188 | |||
| 189 | public function asButton($floating=false){ |
||
| 195 | |||
| 196 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
| 207 | |||
| 208 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
| 212 | |||
| 213 | public function setSelect($name=NULL,$multiple=false){ |
||
| 230 | |||
| 231 | public function asSubmenu($pointing=NULL){ |
||
| 238 | |||
| 239 | public function setPointing($value=Direction::NONE){ |
||
| 242 | |||
| 243 | public function setValue($value){ |
||
| 259 | |||
| 260 | /* |
||
| 261 | * (non-PHPdoc) |
||
| 262 | * @see BaseHtml::run() |
||
| 263 | */ |
||
| 264 | public function run(JsUtils $js) { |
||
| 272 | |||
| 273 | public function setCompact(){ |
||
| 276 | |||
| 277 | public function setAction($action){ |
||
| 280 | |||
| 281 | public function setFullTextSearch($value){ |
||
| 284 | |||
| 285 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 289 | } |
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: