| Total Complexity | 40 |
| Total Lines | 258 |
| 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 additional info for BrowseServer - Button */ |
||
| 29 | protected string $strBrowseServer; |
||
| 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) |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add flags to existing element. |
||
| 56 | * Maybe overloaded in derived class(es) |
||
| 57 | * @param int $wFlags |
||
| 58 | * |
||
| 59 | * {@inheritDoc} |
||
| 60 | * @see \SKien\Formgenerator\FormElement::addFlags() |
||
| 61 | */ |
||
| 62 | public function addFlags($wFlags) : void |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * set image and title for select-button (leave strImg blank for default) |
||
| 69 | * @param string $strImg |
||
| 70 | * @param string $strTitle (default = '') |
||
| 71 | */ |
||
| 72 | public function setSelectImg(string $strImg, string $strTitle = '') : void |
||
| 73 | { |
||
| 74 | $this->strSelectImg = $strImg; |
||
| 75 | $this->strSelectImgTitle = $strTitle; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $strBrowseServer |
||
| 80 | */ |
||
| 81 | public function setBrowseServer(string $strBrowseServer) : void |
||
| 82 | { |
||
| 83 | $this->strBrowseServer = $strBrowseServer; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $strSuffix |
||
| 88 | */ |
||
| 89 | public function setSuffix(string $strSuffix) : void |
||
| 90 | { |
||
| 91 | $this->strSuffix = $strSuffix; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Build the HTML-notation for the input element. |
||
| 96 | * {@inheritDoc} |
||
| 97 | * @see \SKien\Formgenerator\FormElement::getHTML() |
||
| 98 | */ |
||
| 99 | public function getHTML() : string |
||
| 100 | { |
||
| 101 | $this->processFlags(); |
||
| 102 | $this->setSize(); |
||
| 103 | $strHTML = $this->buildContainerDiv(); |
||
| 104 | |||
| 105 | $this->strID = $this->strID ?: $this->strName; |
||
| 106 | |||
| 107 | $strHTML .= '<input'; |
||
| 108 | $strHTML .= ' type="' . $this->strType . '"'; |
||
| 109 | $strHTML .= ' name="' . $this->strName . '"'; |
||
| 110 | $strHTML .= $this->buildClass(); |
||
| 111 | $strHTML .= $this->buildID(); |
||
| 112 | $strHTML .= $this->buildStyle(); |
||
| 113 | $strHTML .= $this->buildAttributes(); |
||
| 114 | $strHTML .= $this->buildTab(); |
||
| 115 | $strHTML .= $this->buildValue(); |
||
| 116 | $strHTML .= '>'; |
||
| 117 | |||
| 118 | // some additional elements |
||
| 119 | if ($this->oFlags->isSet(FormFlags::ADD_DTU)) { |
||
| 120 | $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DTU) . '" alt="[X]"'; |
||
| 121 | $strHTML .= ' id="' . $this->strName . 'DTU"'; |
||
| 122 | $strHTML .= ' title="aktuelle Datum Uhrzeit / Benutzername eintragen"'; |
||
| 123 | $strHTML .= ' onclick="OnInsertDateTimeUser(' . "'" . $this->strName . "'" . ');">'; |
||
| 124 | } else if ($this->oFlags->isSet(FormFlags::ADD_DATE_PICKER)) { |
||
| 125 | $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DATE_PICKER) . '" alt="[X]"'; |
||
| 126 | $strHTML .= ' id="' . $this->strName . 'DP"'; |
||
| 127 | $strHTML .= ' title="Datum auswählen"'; |
||
| 128 | $strHTML .= ' onclick="OnDatePicker(' . "'" . $this->strName . "'" . ');">'; |
||
| 129 | } else if ($this->oFlags->isSet(FormFlags::ADD_TIME_PICKER)) { |
||
| 130 | $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_TIME_PICKER) . '" alt="[X]"'; |
||
| 131 | $strHTML .= ' id="' . $this->strName . 'TP"'; |
||
| 132 | $strHTML .= ' title="Uhrzeit auswählen"'; |
||
| 133 | $strHTML .= ' onclick="OnTimePicker(' . "'" . $this->strName . "'" . ');">'; |
||
| 134 | } |
||
| 135 | $strHTML .= $this->buildSelectImage(); |
||
| 136 | $strHTML .= $this->buildSuffix(); |
||
| 137 | |||
| 138 | $strHTML .= '</div>' . PHP_EOL; |
||
| 139 | |||
| 140 | return $strHTML; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Input elements don't need tab index if hidden, read-only or disabled |
||
| 145 | * {@inheritDoc} |
||
| 146 | * @see \SKien\Formgenerator\FormElement::hasTab() |
||
| 147 | */ |
||
| 148 | public function hasTab() : bool |
||
| 149 | { |
||
| 150 | return (!$this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Process the current flags before the HTML is generated. |
||
| 155 | */ |
||
| 156 | protected function processFlags() : void |
||
| 157 | { |
||
| 158 | $this->setTypeFromFlags(); |
||
| 159 | |||
| 160 | if ($this->oFlags->isSet(FormFlags::MANDATORY)) { |
||
| 161 | $this->addAttribute('required'); |
||
| 162 | } |
||
| 163 | if ($this->oFlags->isSet(FormFlags::READ_ONLY)) { |
||
| 164 | $this->addAttribute('readonly'); |
||
| 165 | } else if ($this->oFlags->isSet(FormFlags::DISABLED)) { |
||
| 166 | $this->addAttribute('disabled'); |
||
| 167 | } |
||
| 168 | if ($this->oFlags->isSet(FormFlags::ADD_EUR)) { |
||
| 169 | $this->strSuffix = 'EUR'; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the type depending on some flags |
||
| 175 | */ |
||
| 176 | protected function setTypeFromFlags() : void |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the size of the element. |
||
| 194 | * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a |
||
| 195 | * string a width information including dimension (px, em, ...) is assumed. |
||
| 196 | * |
||
| 197 | */ |
||
| 198 | protected function setSize() : void |
||
| 199 | { |
||
| 200 | if ($this->oFlags->isSet(FormFlags::HIDDEN)) { |
||
| 201 | $this->size = ''; |
||
| 202 | } |
||
| 203 | if ((is_numeric($this->size)) && ($this->size > 0)) { |
||
| 204 | $this->addAttribute('size', (string)$this->size); |
||
| 205 | } else if (!empty($this->size)) { |
||
| 206 | // size given as string including dimension |
||
| 207 | $this->addStyle('width', $this->size); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritDoc} |
||
| 213 | * @see \SKien\Formgenerator\FormElement::buildClass() |
||
| 214 | */ |
||
| 215 | protected function buildClass() : string |
||
| 216 | { |
||
| 217 | if (!empty($this->strClass)) { |
||
| 218 | $this->strClass .= ' '; |
||
| 219 | } |
||
| 220 | $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK'; |
||
| 221 | if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
||
| 222 | $this->strClass .= '_R'; |
||
| 223 | } |
||
| 224 | if ($this->oFlags->isSet(FormFlags::ADD_COLOR_PICKER)) { |
||
| 225 | $this->strClass .= ' jscolor {hash:true}'; |
||
| 226 | } |
||
| 227 | return parent::buildClass(); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Build the markup for a suffix succeeding the input element. |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | protected function buildSuffix() : string |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Build the markup for a select button. |
||
| 249 | * @param string $strClass |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | protected function buildSelectImage(string $strClass = 'picker') : string |
||
| 276 | } |
||
| 277 | } |
||
| 278 |