| Total Complexity | 57 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 13 | class FormInput extends FormElement |
||
| 14 | { |
||
| 15 | /** @var string value input type */ |
||
| 16 | protected string $strType = ''; |
||
| 17 | /** @var int|string size as number or as string including dimension ('%', 'px', 'em') */ |
||
| 18 | protected $size = 0; |
||
| 19 | /** @var string image displayed, if selectbutton is enabled */ |
||
| 20 | protected string $strSelectImg = ''; |
||
| 21 | /** @var string tooltip for selectbutton */ |
||
| 22 | protected string $strSelectImgTitle = ''; |
||
| 23 | /** @var string folder to expand when call the filemanager */ |
||
| 24 | protected string $strExpandFolder = ''; |
||
| 25 | /** @var string suffix directly after the element */ |
||
| 26 | protected string $strSuffix = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param string $strName Name (also used as ID, if not set separate) |
||
| 30 | * @param int|string $size number set the size-attribute, a string is used for the width attribute |
||
| 31 | * @param int $wFlags |
||
| 32 | */ |
||
| 33 | public function __construct(string $strName, $size, int $wFlags = 0, int $iMaxLength = 0) |
||
| 34 | { |
||
| 35 | parent::__construct($wFlags); |
||
| 36 | $this->strName = $strName; |
||
| 37 | $this->size = $size; |
||
| 38 | $this->strType = 'text'; |
||
| 39 | $this->strSelectImg = ''; |
||
| 40 | $this->strSelectImgTitle = ''; |
||
| 41 | $this->strSuffix = ''; |
||
| 42 | |||
| 43 | $this->addFlags($wFlags); |
||
| 44 | if ($iMaxLength > 0) { |
||
| 45 | $this->addAttribute('maxlength', (string)$iMaxLength); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set the maxlength attribute of the element. |
||
| 51 | * @param int $iMaxLength |
||
| 52 | */ |
||
| 53 | public function setMaxLength(int $iMaxLength) : void |
||
| 54 | { |
||
| 55 | $this->addAttribute('maxlength', (string)$iMaxLength); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set placeholder to display on empty input element. |
||
| 60 | * @param string $strPlaceholder |
||
| 61 | */ |
||
| 62 | public function setPlaceholder(string $strPlaceholder) : void |
||
| 63 | { |
||
| 64 | if (strlen($strPlaceholder) > 0) { |
||
| 65 | $this->addAttribute('placeholder', $strPlaceholder); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * set image and title for select-button (leave strImg blank for default 'search') |
||
| 71 | * @param string $strImg |
||
| 72 | * @param string $strTitle (default = '') |
||
| 73 | */ |
||
| 74 | public function setSelectImg(string $strImg, string $strTitle = '') : void |
||
| 75 | { |
||
| 76 | $this->strSelectImg = $strImg; |
||
| 77 | $this->strSelectImgTitle = $strTitle; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the folder to expand when call the filemanager. |
||
| 82 | * @param string $strExpandFolder |
||
| 83 | */ |
||
| 84 | public function setExpandFolder(string $strExpandFolder) : void |
||
| 85 | { |
||
| 86 | $this->strExpandFolder = $strExpandFolder; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $strSuffix |
||
| 91 | */ |
||
| 92 | public function setSuffix(string $strSuffix) : void |
||
| 93 | { |
||
| 94 | $this->strSuffix = $strSuffix; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritDoc} |
||
| 99 | * @see \SKien\Formgenerator\FormElement::onParentSet() |
||
| 100 | */ |
||
| 101 | protected function onParentSet() : void |
||
| 102 | { |
||
| 103 | if ($this->oFlags->isSet(FormFlags::BROWSE_SERVER)) { |
||
| 104 | $this->oFG->addConfigForJS('RichFilemanager', $this->oFG->getConfig()->getArray('RichFilemanager')); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Build the HTML-notation for the input element. |
||
| 110 | * {@inheritDoc} |
||
| 111 | * @see \SKien\Formgenerator\FormElement::getHTML() |
||
| 112 | */ |
||
| 113 | public function getHTML() : string |
||
| 114 | { |
||
| 115 | $this->processFlags(); |
||
| 116 | $this->setSize(); |
||
| 117 | $strHTML = $this->buildContainerDiv(); |
||
| 118 | |||
| 119 | $this->strID = $this->strID ?: $this->strName; |
||
| 120 | |||
| 121 | $strHTML .= '<input'; |
||
| 122 | $strHTML .= ' type="' . $this->strType . '"'; |
||
| 123 | $strHTML .= ' name="' . $this->strName . '"'; |
||
| 124 | $strHTML .= $this->buildClass(); |
||
| 125 | $strHTML .= $this->buildID(); |
||
| 126 | $strHTML .= $this->buildStyle(); |
||
| 127 | $strHTML .= $this->buildTabindex(); |
||
| 128 | $strHTML .= $this->buildValue(); |
||
| 129 | $strHTML .= $this->buildAttributes(); |
||
| 130 | $strHTML .= $this->buildListLink(); |
||
| 131 | $strHTML .= '>'; |
||
| 132 | |||
| 133 | $strHTML .= $this->buildSelectButton(); |
||
| 134 | $strHTML .= $this->buildSuffix(); |
||
| 135 | $strHTML .= $this->buildDatalist(); |
||
| 136 | |||
| 137 | $strHTML .= '</div>' . PHP_EOL; |
||
| 138 | |||
| 139 | return $strHTML; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set the tab index of the element. |
||
| 144 | * Method is called from the PageGenerator after an element is added to the form. |
||
| 145 | * @param int $iTabindex |
||
| 146 | * @return int the number of indexes, the element needs |
||
| 147 | */ |
||
| 148 | public function setTabindex(int $iTabindex) : int |
||
| 149 | { |
||
| 150 | if ($this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
||
| 151 | return 0; |
||
| 152 | } |
||
| 153 | $this->iTabindex = $iTabindex; |
||
| 154 | return 1; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Process the current flags before the HTML is generated. |
||
| 159 | */ |
||
| 160 | protected function processFlags() : void |
||
| 161 | { |
||
| 162 | $this->setTypeFromFlags(); |
||
| 163 | |||
| 164 | if ($this->oFlags->isSet(FormFlags::MANDATORY)) { |
||
| 165 | $this->addAttribute('required'); |
||
| 166 | } |
||
| 167 | if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
||
| 168 | $this->addAttribute('readonly'); |
||
| 169 | } else if ($this->oFlags->isSet(FormFlags::DISABLED)) { |
||
| 170 | $this->addAttribute('disabled'); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Set the type depending on some flags |
||
| 176 | */ |
||
| 177 | protected function setTypeFromFlags() : void |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Set the size of the element. |
||
| 195 | * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a |
||
| 196 | * string a width information including dimension (px, em, ...) is assumed. |
||
| 197 | * |
||
| 198 | */ |
||
| 199 | protected function setSize() : void |
||
| 200 | { |
||
| 201 | if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
||
| 202 | $this->size = ''; |
||
| 203 | } |
||
| 204 | if ((is_numeric($this->size)) && ($this->size > 0)) { |
||
| 205 | $this->addAttribute('size', (string)$this->size); |
||
| 206 | } else if (!empty($this->size)) { |
||
| 207 | // size given as string including dimension |
||
| 208 | $this->addStyle('width', $this->size); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritDoc} |
||
| 214 | * @see \SKien\Formgenerator\FormElement::buildClass() |
||
| 215 | */ |
||
| 216 | protected function buildClass() : string |
||
| 217 | { |
||
| 218 | if (!empty($this->strClass)) { |
||
| 219 | $this->strClass .= ' '; |
||
| 220 | } |
||
| 221 | $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK'; |
||
| 222 | if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
||
| 223 | $this->strClass .= '_R'; |
||
| 224 | } |
||
| 225 | return parent::buildClass(); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Build the markup for a suffix succeeding the input element. |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function buildSuffix() : string |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Build attrib for associated datalist. |
||
| 247 | * If the dataprovider contains a datalist in the selectoptions with the same name |
||
| 248 | * as the element, we add the attrib to conect to this list. |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | protected function buildListLink() : string |
||
| 252 | { |
||
| 253 | $strLink = ''; |
||
| 254 | $aOptions = $this->oFG->getData()->getSelectOptions($this->strName); |
||
| 255 | if (count($aOptions) > 0) { |
||
| 256 | $strLink = ' list="list' . $this->strName . '"'; |
||
| 257 | } |
||
| 258 | return $strLink; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Build the markup for associated datalist. |
||
| 263 | * If the dataprovider contains a datalist in the selectoptions with the same name |
||
| 264 | * as the element, we build this datalist. |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function buildDatalist() : string |
||
| 268 | { |
||
| 269 | $strHTML = ''; |
||
| 270 | $aOptions = $this->oFG->getData()->getSelectOptions($this->strName); |
||
| 271 | if (count($aOptions) > 0) { |
||
| 272 | $strHTML .= '<datalist id="list' . $this->strName . '">' . PHP_EOL; |
||
| 273 | foreach ($aOptions as $strValue) { |
||
| 274 | $strHTML .= ' '; |
||
| 275 | $strHTML .= '<option '; |
||
| 276 | $strHTML .= 'value="' . $strValue . '">' . PHP_EOL; |
||
| 277 | } |
||
| 278 | $strHTML .= '</datalist>' . PHP_EOL; |
||
| 279 | } |
||
| 280 | return $strHTML; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Build the markup for the select button(s). |
||
| 285 | * If input is set to readonly, an additional 'delete' button is appended. |
||
| 286 | * @param string $strCssClass |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | protected function buildSelectButton(string $strCssClass = 'picker') : string |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Build the markup for a selectimage. |
||
| 342 | * @param string $strImg |
||
| 343 | * @param string $strTitle |
||
| 344 | * @param string $strOnClick |
||
| 345 | * @param string $strID |
||
| 346 | * @param string $strCssClass |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | protected function buildSelectImage(string $strImg, string $strTitle, string $strOnClick, string $strID, string $strCssClass) : string |
||
| 366 | } |
||
| 367 | } |
||
| 368 |