| Total Complexity | 50 |
| Total Lines | 301 |
| 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 = 0; |
||
| 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 |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritDoc} |
||
| 92 | * @see \SKien\Formgenerator\FormElement::onParentSet() |
||
| 93 | */ |
||
| 94 | protected function onParentSet() : void |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Build the HTML-notation for the input element. |
||
| 103 | * {@inheritDoc} |
||
| 104 | * @see \SKien\Formgenerator\FormElement::getHTML() |
||
| 105 | */ |
||
| 106 | public function getHTML() : string |
||
| 107 | { |
||
| 108 | $this->processFlags(); |
||
| 109 | $this->setSize(); |
||
| 110 | $strHTML = $this->buildContainerDiv(); |
||
| 111 | |||
| 112 | $this->strID = $this->strID ?: $this->strName; |
||
| 113 | |||
| 114 | $strHTML .= '<input'; |
||
| 115 | $strHTML .= ' type="' . $this->strType . '"'; |
||
| 116 | $strHTML .= ' name="' . $this->strName . '"'; |
||
| 117 | $strHTML .= $this->buildClass(); |
||
| 118 | $strHTML .= $this->buildID(); |
||
| 119 | $strHTML .= $this->buildStyle(); |
||
| 120 | $strHTML .= $this->buildTabindex(); |
||
| 121 | $strHTML .= $this->buildValue(); |
||
| 122 | $strHTML .= $this->buildAttributes(); |
||
| 123 | $strHTML .= '>'; |
||
| 124 | |||
| 125 | $strHTML .= $this->buildSelectButton(); |
||
| 126 | $strHTML .= $this->buildSuffix(); |
||
| 127 | |||
| 128 | $strHTML .= '</div>' . PHP_EOL; |
||
| 129 | |||
| 130 | return $strHTML; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the tab index of the element. |
||
| 135 | * Method is called from the PageGenerator after an element is added to the form. |
||
| 136 | * @param int $iTabindex |
||
| 137 | * @return int the number of indexes, the element needs |
||
| 138 | */ |
||
| 139 | public function setTabindex(int $iTabindex) : int |
||
| 140 | { |
||
| 141 | if ($this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
||
| 142 | return 0; |
||
| 143 | } |
||
| 144 | $this->iTabindex = $iTabindex; |
||
| 145 | return 1; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Process the current flags before the HTML is generated. |
||
| 150 | */ |
||
| 151 | protected function processFlags() : void |
||
| 152 | { |
||
| 153 | $this->setTypeFromFlags(); |
||
| 154 | |||
| 155 | if ($this->oFlags->isSet(FormFlags::MANDATORY)) { |
||
| 156 | $this->addAttribute('required'); |
||
| 157 | } |
||
| 158 | if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
||
| 159 | $this->addAttribute('readonly'); |
||
| 160 | } else if ($this->oFlags->isSet(FormFlags::DISABLED)) { |
||
| 161 | $this->addAttribute('disabled'); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the type depending on some flags |
||
| 167 | */ |
||
| 168 | protected function setTypeFromFlags() : void |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set the size of the element. |
||
| 186 | * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a |
||
| 187 | * string a width information including dimension (px, em, ...) is assumed. |
||
| 188 | * |
||
| 189 | */ |
||
| 190 | protected function setSize() : void |
||
| 191 | { |
||
| 192 | if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
||
| 193 | $this->size = ''; |
||
| 194 | } |
||
| 195 | if ((is_numeric($this->size)) && ($this->size > 0)) { |
||
| 196 | $this->addAttribute('size', (string)$this->size); |
||
| 197 | } else if (!empty($this->size)) { |
||
| 198 | // size given as string including dimension |
||
| 199 | $this->addStyle('width', $this->size); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritDoc} |
||
| 205 | * @see \SKien\Formgenerator\FormElement::buildClass() |
||
| 206 | */ |
||
| 207 | protected function buildClass() : string |
||
| 208 | { |
||
| 209 | if (!empty($this->strClass)) { |
||
| 210 | $this->strClass .= ' '; |
||
| 211 | } |
||
| 212 | $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK'; |
||
| 213 | if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
||
| 214 | $this->strClass .= '_R'; |
||
| 215 | } |
||
| 216 | return parent::buildClass(); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Build the markup for a suffix succeeding the input element. |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | protected function buildSuffix() : string |
||
| 224 | { |
||
| 225 | $strHTML = ''; |
||
| 226 | if (!empty($this->strSuffix)) { |
||
| 227 | if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
||
| 228 | $strHTML .= ' <span class="readonly">' . $this->strSuffix . '</span>'; |
||
| 229 | } else { |
||
| 230 | $strHTML .= ' ' . $this->strSuffix; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | return $strHTML; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Build the markup for the select button(s). |
||
| 238 | * If input is set to readonly, an additional 'delete' button is appended. |
||
| 239 | * @param string $strCssClass |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | protected function buildSelectButton(string $strCssClass = 'picker') : string |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Build the markup for a selectimage. |
||
| 295 | * @param string $strImg |
||
| 296 | * @param string $strTitle |
||
| 297 | * @param string $strOnClick |
||
| 298 | * @param string $strID |
||
| 299 | * @param string $strCssClass |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | protected function buildSelectImage(string $strImg, string $strTitle, string $strOnClick, string $strID, string $strCssClass) : string |
||
| 319 | } |
||
| 320 | } |
||
| 321 |