| Total Complexity | 49 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FormInput 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 FormInput, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class FormInput extends FormElement |
||
| 19 | { |
||
| 20 | /** @var string value input type */ |
||
| 21 | protected string $strType; |
||
| 22 | /** @var int|string size as number or as string including dimension ('%', 'px', 'em') */ |
||
| 23 | protected $size; |
||
| 24 | /** @var string image displayed, if selectbutton is enabled */ |
||
| 25 | protected string $strSelectImg; |
||
| 26 | /** @var string tooltip for selectbutton */ |
||
| 27 | protected string $strSelectImgTitle; |
||
| 28 | /** @var string folder to expand when call the filemanager */ |
||
| 29 | protected string $strExpandFolder; |
||
| 30 | /** @var string suffix directly after the element */ |
||
| 31 | protected string $strSuffix; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $strName Name (also used as ID, if not set separate) |
||
| 35 | * @param int|string $size number set the size-attribute, a string is used for the width attribute |
||
| 36 | * @param int $wFlags |
||
| 37 | */ |
||
| 38 | public function __construct(string $strName, $size, int $wFlags = 0) |
||
| 39 | { |
||
| 40 | parent::__construct($wFlags); |
||
| 41 | $this->strName = $strName; |
||
| 42 | $this->size = $size; |
||
| 43 | $this->strType = 'text'; |
||
| 44 | $this->strSelectImg = ''; |
||
| 45 | $this->strSelectImgTitle = ''; |
||
| 46 | $this->strSuffix = ''; |
||
| 47 | |||
| 48 | $this->addFlags($wFlags); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set placeholder to display on empty input element. |
||
| 53 | * @param string $strPlaceholder |
||
| 54 | */ |
||
| 55 | public function setPlaceholder(string $strPlaceholder) : void |
||
| 56 | { |
||
| 57 | if (strlen($strPlaceholder) > 0) { |
||
| 58 | $this->addAttribute('placeholder', $strPlaceholder); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * set image and title for select-button (leave strImg blank for default) |
||
| 64 | * @param string $strImg |
||
| 65 | * @param string $strTitle (default = '') |
||
| 66 | */ |
||
| 67 | public function setSelectImg(string $strImg, string $strTitle = '') : void |
||
| 68 | { |
||
| 69 | $this->strSelectImg = $strImg; |
||
| 70 | $this->strSelectImgTitle = $strTitle; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set the folder to expand when call the filemanager. |
||
| 75 | * @param string $strExpandFolder |
||
| 76 | */ |
||
| 77 | public function setExpandFolder(string $strExpandFolder) : void |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $strSuffix |
||
| 84 | */ |
||
| 85 | public function setSuffix(string $strSuffix) : void |
||
| 86 | { |
||
| 87 | $this->strSuffix = $strSuffix; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Build the HTML-notation for the input element. |
||
| 92 | * {@inheritDoc} |
||
| 93 | * @see \SKien\Formgenerator\FormElement::getHTML() |
||
| 94 | */ |
||
| 95 | public function getHTML() : string |
||
| 96 | { |
||
| 97 | $this->processFlags(); |
||
| 98 | $this->setSize(); |
||
| 99 | $strHTML = $this->buildContainerDiv(); |
||
| 100 | |||
| 101 | $this->strID = $this->strID ?: $this->strName; |
||
| 102 | |||
| 103 | $strHTML .= '<input'; |
||
| 104 | $strHTML .= ' type="' . $this->strType . '"'; |
||
| 105 | $strHTML .= ' name="' . $this->strName . '"'; |
||
| 106 | $strHTML .= $this->buildClass(); |
||
| 107 | $strHTML .= $this->buildID(); |
||
| 108 | $strHTML .= $this->buildStyle(); |
||
| 109 | $strHTML .= $this->buildTabindex(); |
||
| 110 | $strHTML .= $this->buildValue(); |
||
| 111 | $strHTML .= $this->buildAttributes(); |
||
| 112 | $strHTML .= '>'; |
||
| 113 | |||
| 114 | $strHTML .= $this->buildSelectButton(); |
||
| 115 | $strHTML .= $this->buildSuffix(); |
||
| 116 | |||
| 117 | $strHTML .= '</div>' . PHP_EOL; |
||
| 118 | |||
| 119 | return $strHTML; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set the tab index of the element. |
||
| 124 | * Method is called from the PageGenerator after an element is added to the form. |
||
| 125 | * @param int $iTabindex |
||
| 126 | * @return int the number of indexes, the element needs |
||
| 127 | */ |
||
| 128 | public function setTabindex(int $iTabindex) : int |
||
| 129 | { |
||
| 130 | if ($this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
||
| 131 | return 0; |
||
| 132 | } |
||
| 133 | $this->iTabindex = $iTabindex; |
||
| 134 | return 1; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Process the current flags before the HTML is generated. |
||
| 139 | */ |
||
| 140 | protected function processFlags() : void |
||
| 141 | { |
||
| 142 | $this->setTypeFromFlags(); |
||
| 143 | |||
| 144 | if ($this->oFlags->isSet(FormFlags::MANDATORY)) { |
||
| 145 | $this->addAttribute('required'); |
||
| 146 | } |
||
| 147 | if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
||
| 148 | $this->addAttribute('readonly'); |
||
| 149 | } else if ($this->oFlags->isSet(FormFlags::DISABLED)) { |
||
| 150 | $this->addAttribute('disabled'); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the type depending on some flags |
||
| 156 | */ |
||
| 157 | protected function setTypeFromFlags() : void |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the size of the element. |
||
| 175 | * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a |
||
| 176 | * string a width information including dimension (px, em, ...) is assumed. |
||
| 177 | * |
||
| 178 | */ |
||
| 179 | protected function setSize() : void |
||
| 180 | { |
||
| 181 | if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
||
| 182 | $this->size = ''; |
||
| 183 | } |
||
| 184 | if ((is_numeric($this->size)) && ($this->size > 0)) { |
||
| 185 | $this->addAttribute('size', (string)$this->size); |
||
| 186 | } else if (!empty($this->size)) { |
||
| 187 | // size given as string including dimension |
||
| 188 | $this->addStyle('width', $this->size); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritDoc} |
||
| 194 | * @see \SKien\Formgenerator\FormElement::buildClass() |
||
| 195 | */ |
||
| 196 | protected function buildClass() : string |
||
| 197 | { |
||
| 198 | if (!empty($this->strClass)) { |
||
| 199 | $this->strClass .= ' '; |
||
| 200 | } |
||
| 201 | $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK'; |
||
| 202 | if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
||
| 203 | $this->strClass .= '_R'; |
||
| 204 | } |
||
| 205 | return parent::buildClass(); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Build the markup for a suffix succeeding the input element. |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | protected function buildSuffix() : string |
||
| 213 | { |
||
| 214 | $strHTML = ''; |
||
| 215 | if (!empty($this->strSuffix)) { |
||
| 216 | if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
||
| 217 | $strHTML .= ' <span class="readonly">' . $this->strSuffix . '</span>'; |
||
| 218 | } else { |
||
| 219 | $strHTML .= ' ' . $this->strSuffix; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | return $strHTML; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Build the markup for the select button(s). |
||
| 227 | * If input is set to readonly, an additional 'delete' button is appended. |
||
| 228 | * @param string $strCssClass |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | protected function buildSelectButton(string $strCssClass='picker') : string |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Build the markup for a selectimage. |
||
| 289 | * @param string $strImg |
||
| 290 | * @param string $strTitle |
||
| 291 | * @param string $strOnClick |
||
| 292 | * @param string $strCssClass |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | protected function buildSelectImage(string $strImg, string $strTitle, string $strOnClick, string $strCssClass) : string |
||
| 296 | { |
||
| 297 | $strHTML = ''; |
||
| 298 | if (!empty($strImg)) { |
||
| 299 | $strHTML = '<img class="' . $strCssClass . '" src="' . $strImg . '" alt="[?]"'; |
||
| 300 | if (!empty($strTitle)) { |
||
| 301 | $strHTML .= ' title="' . $strTitle . '"'; |
||
| 302 | } |
||
| 303 | if (!empty($strOnClick)) { |
||
| 304 | $strHTML .= ' onclick="' . $strOnClick . '"'; |
||
| 305 | } |
||
| 306 | $strHTML .= '>'; |
||
| 307 | } |
||
| 308 | return $strHTML; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Check, if filemanager is connected. |
||
| 313 | * Triggers warning, if no filmanager is connected. |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | protected function isFilemanagerConnected() : bool |
||
| 323 | } |
||
| 324 | } |
||
| 325 |