| Total Complexity | 40 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HtmlButton 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.
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 HtmlButton, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class HtmlButton extends HtmlSemDoubleElement { |
||
| 19 | use LabeledIconTrait; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Constructs an HTML Semantic button |
||
| 23 | * |
||
| 24 | * @param string $identifier |
||
| 25 | * HTML id |
||
| 26 | * @param string $value |
||
| 27 | * value of the Button |
||
| 28 | * @param string $cssStyle |
||
| 29 | * btn-default, btn-primary... |
||
| 30 | * @param string $onClick |
||
| 31 | * JS Code for click event |
||
| 32 | */ |
||
| 33 | public function __construct($identifier, $value = null, $cssStyle = null, $onClick = null) { |
||
| 34 | parent::__construct($identifier, "button", "ui button"); |
||
| 35 | $this->content = $value; |
||
| 36 | if (isset($cssStyle)) { |
||
| 37 | $this->setStyle($cssStyle); |
||
| 38 | } |
||
| 39 | if (isset($onClick)) { |
||
| 40 | $this->onClick($onClick); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set the button value |
||
| 46 | * |
||
| 47 | * @param string $value |
||
| 48 | * @return HtmlButton |
||
| 49 | */ |
||
| 50 | public function setValue($value) { |
||
| 51 | if (is_array($this->content)) { |
||
| 52 | foreach ($this->content as $i => $content) { |
||
| 53 | if (is_string($content)) { |
||
| 54 | $this->content[$i] = $value; |
||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | $this->content = $value; |
||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * define the button style |
||
| 65 | * |
||
| 66 | * @param string|int $cssStyle |
||
| 67 | * @return HtmlButton default : "" |
||
| 68 | */ |
||
| 69 | public function setStyle($cssStyle) { |
||
| 70 | return $this->addToProperty("class", $cssStyle); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function setFocusable($value = true) { |
||
| 74 | if ($value === true) |
||
| 75 | $this->setProperty("tabindex", "0"); |
||
| 76 | else { |
||
| 77 | $this->removeProperty("tabindex"); |
||
| 78 | } |
||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function setAnimated($content, $animation = "") { |
||
| 83 | $this->setTagName("div"); |
||
| 84 | $this->addToProperty("class", "animated " . $animation); |
||
| 85 | $visible = new HtmlSemDoubleElement("visible-" . $this->identifier, "div"); |
||
| 86 | $visible->setClass("visible content"); |
||
| 87 | $visible->setContent($this->content); |
||
| 88 | $hidden = new HtmlSemDoubleElement("hidden-" . $this->identifier, "div"); |
||
| 89 | $hidden->setClass("hidden content"); |
||
| 90 | $hidden->setContent($content); |
||
| 91 | $this->content = array( |
||
| 92 | $visible, |
||
| 93 | $hidden |
||
| 94 | ); |
||
| 95 | return $hidden; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * |
||
| 100 | * @param string|HtmlIcon $icon |
||
| 101 | * @return HtmlButton |
||
| 102 | */ |
||
| 103 | public function asIcon($icon) { |
||
| 104 | $iconO = $icon; |
||
| 105 | if (\is_string($icon)) { |
||
| 106 | $iconO = new HtmlIcon("icon-" . $this->identifier, $icon); |
||
| 107 | } |
||
| 108 | $this->addToProperty("class", "icon"); |
||
| 109 | $this->content = $iconO; |
||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function asSubmit() { |
||
| 114 | $this->setProperty("type", "submit"); |
||
| 115 | return $this->setTagName("button"); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Add and return a button label |
||
| 120 | * |
||
| 121 | * @param string $label |
||
| 122 | * @param boolean $before |
||
| 123 | * @param string $icon |
||
| 124 | * @return HtmlLabel |
||
| 125 | */ |
||
| 126 | public function addLabel($label, $before = false, $icon = NULL) { |
||
| 127 | $this->tagName = "div"; |
||
| 128 | $prefix = ""; |
||
| 129 | if ($before) |
||
| 130 | $prefix = "left "; |
||
| 131 | $this->addToProperty("class", $prefix . "labeled"); |
||
| 132 | $isIcon = (isset($this->content[0]) && $this->content[0] instanceof HtmlIcon); |
||
| 133 | $this->content = new HtmlButton("button-" . $this->identifier, $this->content); |
||
| 134 | if ($isIcon) { |
||
| 135 | $this->content->addClass("icon"); |
||
| 136 | } |
||
| 137 | $this->content->setTagName("div"); |
||
| 138 | $label = new HtmlLabel("label-" . $this->identifier, $label, $icon, "a"); |
||
| 139 | $label->setBasic(); |
||
| 140 | $this->addContent($label, $before); |
||
| 141 | return $label; |
||
| 142 | } |
||
| 143 | |||
| 144 | /* |
||
| 145 | * (non-PHPdoc) |
||
| 146 | * @see \Ajax\common\html\BaseHtml::fromArray() |
||
| 147 | */ |
||
| 148 | public function fromArray($array) { |
||
| 149 | $array = parent::fromArray($array); |
||
| 150 | foreach ($array as $key => $value) { |
||
| 151 | $this->setProperty($key, $value); |
||
| 152 | } |
||
| 153 | return $array; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * hint towards a positive consequence |
||
| 158 | * |
||
| 159 | * @return HtmlButton |
||
| 160 | */ |
||
| 161 | public function setPositive() { |
||
| 163 | } |
||
| 164 | |||
| 165 | public function setColor($color) { |
||
| 166 | if (\is_array($this->content)) { |
||
| 167 | foreach ($this->content as $content) { |
||
| 168 | if ($content instanceof HtmlButton) |
||
| 169 | $content->setColor($color); |
||
| 170 | } |
||
| 171 | } else |
||
| 172 | parent::setColor($color); |
||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * hint towards a negative consequence |
||
| 178 | * |
||
| 179 | * @return HtmlButton |
||
| 180 | */ |
||
| 181 | public function setNegative() { |
||
| 182 | return $this->addToProperty("class", "negative"); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * formatted to toggle on/off |
||
| 187 | * |
||
| 188 | * @return HtmlButton |
||
| 189 | */ |
||
| 190 | public function setToggle($active = "") { |
||
| 191 | $this->onCreate("$('#" . $this->identifier . "').state();"); |
||
| 192 | return $this->addToProperty("class", "toggle " . $active); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * |
||
| 197 | * @return HtmlButton |
||
| 198 | */ |
||
| 199 | public function setCircular() { |
||
| 200 | return $this->addToProperty("class", "circular"); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * button is less pronounced |
||
| 205 | * |
||
| 206 | * @return HtmlButton |
||
| 207 | */ |
||
| 208 | public function setBasic() { |
||
| 209 | return $this->addToProperty("class", "basic"); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function setEmphasis($value) { |
||
| 213 | return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants()); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function setLoading() { |
||
| 217 | return $this->addToProperty("class", "loading"); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns a new social Button |
||
| 222 | * |
||
| 223 | * @param string $identifier |
||
| 224 | * @param string $social |
||
| 225 | * @param string $value |
||
| 226 | * @return HtmlButton |
||
| 227 | */ |
||
| 228 | public static function social($identifier, $social, $value = NULL) { |
||
| 229 | if ($value === NULL) |
||
| 230 | $value = \ucfirst($social); |
||
| 231 | $return = new HtmlButton($identifier, $value); |
||
| 232 | $return->addIcon($social); |
||
| 233 | return $return->addToPropertyCtrl("class", $social, Social::getConstants()); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns a new labeled Button |
||
| 238 | * |
||
| 239 | * @param string $identifier |
||
| 240 | * @param string $value |
||
| 241 | * @param string $icon |
||
| 242 | * @param boolean $before |
||
| 243 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
| 244 | */ |
||
| 245 | public static function labeled($identifier, $value, $icon, $before = true) { |
||
| 246 | $result = new HtmlButton($identifier, $value); |
||
| 247 | $result->addIcon($icon, $before, true); |
||
| 248 | return $result; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns a new icon Button |
||
| 253 | * |
||
| 254 | * @param string $identifier |
||
| 255 | * @param string $icon |
||
| 256 | * @return HtmlButton |
||
| 257 | */ |
||
| 258 | public static function icon($identifier, $icon) { |
||
| 259 | $result = new HtmlButton($identifier); |
||
| 260 | $result->asIcon($icon); |
||
| 261 | return $result; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * |
||
| 266 | * {@inheritdoc} |
||
| 267 | * @see HtmlSemDoubleElement::asLink() |
||
| 268 | */ |
||
| 269 | public function asLink($href = NULL, $target = NULL) { |
||
| 270 | parent::asLink($href, $target); |
||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns a button with a dropdown button |
||
| 276 | * |
||
| 277 | * @param string $identifier |
||
| 278 | * @param string $value |
||
| 279 | * @param array $items |
||
| 280 | * @param boolean $asCombo |
||
| 281 | * @param string $icon |
||
| 282 | * @return HtmlButtonGroups |
||
| 283 | */ |
||
| 284 | public static function dropdown($identifier, $value, $items = [], $asCombo = false, $icon = null) { |
||
| 285 | $result = new HtmlButtonGroups($identifier, [ |
||
| 286 | $value |
||
| 287 | ]); |
||
| 288 | $dd = $result->addDropdown($items, $asCombo); |
||
| 289 | if (isset($icon) && $dd instanceof HtmlDropdown) |
||
| 290 | $dd->setIcon($icon); |
||
| 291 | return $result; |
||
| 292 | } |
||
| 293 | |||
| 294 | public function addPopupConfirmation($message, $buttons = ["Okay","Cancel"]) { |
||
| 303 | } |
||
| 304 | } |
||
| 305 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.