| Total Complexity | 40 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 16 | class FormInput extends FormElement |
||
| 17 | { |
||
| 18 | /** @var string value of the element */ |
||
| 19 | protected string $strValue; |
||
| 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 and ID |
||
| 35 | * @param string $strValue |
||
| 36 | * @param int|string $size number set the size-attribute, a string is used for the width attribute |
||
| 37 | * @param int $wFlags |
||
| 38 | */ |
||
| 39 | public function __construct(string $strName, string $strValue, $size, int $wFlags = 0) |
||
| 40 | { |
||
| 41 | parent::__construct(); |
||
| 42 | $this->strName = $strName; |
||
| 43 | $this->strValue = $strValue; |
||
| 44 | if (($wFlags & self::TRIM) != 0) { |
||
| 45 | $this->strValue = trim($strValue); |
||
| 46 | } |
||
| 47 | $this->size = $size; |
||
| 48 | $this->iTab = -1; |
||
| 49 | $this->strValidate = 'aEdit'; |
||
| 50 | $this->wFlags = $wFlags; |
||
| 51 | if (strlen($this->strType) == 0) { |
||
| 52 | $this->strType = 'text'; |
||
| 53 | } |
||
| 54 | if (($wFlags & self::HIDDEN) != 0) { |
||
| 55 | $this->strType = 'hidden'; |
||
| 56 | } |
||
| 57 | $this->addFlags($wFlags); |
||
| 58 | $this->strSelectImg = ''; |
||
| 59 | $this->strSelectImgTitle = ''; |
||
| 60 | $this->strBrowseServer = ''; |
||
| 61 | $this->strSuffix = ''; |
||
| 62 | if (($wFlags & self::ADD_EUR) != 0) { |
||
| 63 | $this->strSuffix = 'EUR'; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Aadd flags too existing element. |
||
| 69 | * Maybe overloaded in derived class(es) |
||
| 70 | * @param int $wFlags |
||
| 71 | * |
||
| 72 | * {@inheritDoc} |
||
| 73 | * @see \SKien\Formgenerator\FormElement::addFlags() |
||
| 74 | */ |
||
| 75 | public function addFlags($wFlags) : void |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Input elements don't need tab position if hidden, read-only or disabled |
||
| 109 | * {@inheritDoc} |
||
| 110 | * @see \SKien\Formgenerator\FormElement::hasTab() |
||
| 111 | */ |
||
| 112 | public function hasTab() : bool |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the size of the element. |
||
| 119 | * TODO: explain int|string usage |
||
| 120 | */ |
||
| 121 | protected function setSize() : void |
||
| 122 | { |
||
| 123 | if (is_numeric($this->size)) { |
||
| 124 | if ($this->size > 0) { |
||
| 125 | if ($this->strType == 'number') { |
||
| 126 | // HTML5 number input doesn't support size attribute :-( |
||
| 127 | // TODO: move to FormInt where strType == 'number' |
||
| 128 | $this->addStyle('width', $this->size . 'em'); |
||
| 129 | } else { |
||
| 130 | $this->addAttribute('size', (string)$this->size); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } else if (!empty($this->size)) { |
||
| 134 | // size given as string including dimension |
||
| 135 | $this->addStyle('width', $this->size); |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Build the markup for the value attribute. |
||
| 142 | * @param mixed $value |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | protected function buildValue($value) : string |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Build the markup for a select button. |
||
| 156 | * @param string $strClass |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | protected function buildSelectImage(string $strClass = 'picker') : string |
||
| 160 | { |
||
| 161 | $strHTML = ''; |
||
| 162 | if (($this->wFlags & self::ADD_SELBTN) != 0) { |
||
| 163 | $strImg = $this->strSelectImg; |
||
| 164 | $strTitle = $this->strSelectImgTitle; |
||
| 165 | if (empty($strImg) && $this->oFG !== null) { |
||
| 166 | $strImg = $this->oFG->getImagePath() . '16x16/search.png'; |
||
| 167 | } |
||
| 168 | $strHTML .= '<img class="' . $strClass . '" src="' . $strImg . '" alt="Auswahl"'; |
||
| 169 | if (!empty($strTitle)) { |
||
| 170 | $strHTML .= ' title="' . $strTitle . '"'; |
||
| 171 | } |
||
| 172 | if (!empty($this->strBrowseServer)) { |
||
| 173 | $strHTML .= " onclick=\"BrowseServer('" . $this->strName . "','','" . $this->strBrowseServer . "');\">"; |
||
| 174 | if (($this->wFlags & self::READ_ONLY) != 0) { |
||
| 175 | $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/admin_delete.png" alt="Löschen" title="Löschen" '; |
||
|
|
|||
| 176 | $strHTML .= " onclick=\"ResetInput('" . $this->strName . "');\">"; |
||
| 177 | } |
||
| 178 | } else { |
||
| 179 | $strHTML .= ' onclick="OnSelect(' . "'" . $this->strName . "'" . ');">'; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | return $strHTML; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Build the HTML-notation for the input element. |
||
| 187 | * {@inheritDoc} |
||
| 188 | * @see \SKien\Formgenerator\FormElement::getHTML() |
||
| 189 | */ |
||
| 190 | public function getHTML() : string |
||
| 191 | { |
||
| 192 | $this->setSize(); |
||
| 193 | $strHTML = $this->buildContainerDiv(); |
||
| 194 | |||
| 195 | $strHTML .= '<input'; |
||
| 196 | $strHTML .= ' type="' . $this->strType . '"'; |
||
| 197 | $strHTML .= ' class="' . $this->strClass . '"'; |
||
| 198 | $strHTML .= ' name="' . $this->strName . '"'; |
||
| 199 | $strHTML .= ' id="' . $this->strName . '"'; |
||
| 200 | $strHTML .= $this->buildStyle(); |
||
| 201 | $strHTML .= $this->buildAttributes(); |
||
| 202 | $strHTML .= $this->buildTab($this->iTab); |
||
| 203 | $strHTML .= $this->buildValue($this->strValue); |
||
| 204 | $strHTML .= '>'; |
||
| 205 | |||
| 206 | // some additional elements |
||
| 207 | if (($this->wFlags & self::ADD_DTU) != 0) { |
||
| 208 | $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/admin_dtu.png" alt="[X]"'; |
||
| 209 | $strHTML .= ' id="' . $this->strName . 'DTU"'; |
||
| 210 | $strHTML .= ' title="aktuelle Datum Uhrzeit / Benutzername eintragen"'; |
||
| 211 | $strHTML .= ' onclick="OnInsertDateTimeUser(' . "'" . $this->strName . "'" . ');">'; |
||
| 212 | } else if (($this->wFlags & self::ADD_DATE_PICKER) != 0) { |
||
| 213 | $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/datepicker.png" alt="[X]"'; |
||
| 214 | $strHTML .= ' id="' . $this->strName . 'DP"'; |
||
| 215 | $strHTML .= ' title="Datum auswählen"'; |
||
| 216 | $strHTML .= ' onclick="OnDatePicker(' . "'" . $this->strName . "'" . ');">'; |
||
| 217 | } else if (($this->wFlags & self::ADD_TIME_PICKER) != 0) { |
||
| 218 | $strHTML .= '<img class="picker" src="' . $this->oFG->getImagePath() . '16x16/timepicker.png" alt="[X]"'; |
||
| 219 | $strHTML .= ' id="' . $this->strName . 'TP"'; |
||
| 220 | $strHTML .= ' title="Uhrzeit auswählen"'; |
||
| 221 | $strHTML .= ' onclick="OnTimePicker(' . "'" . $this->strName . "'" . ');">'; |
||
| 222 | } |
||
| 223 | $strHTML .= $this->buildSelectImage(); |
||
| 224 | if (!empty($this->strSuffix)) { |
||
| 225 | if (($this->wFlags & self::READ_ONLY) != 0) { |
||
| 226 | $strHTML .= ' <span class="readonly">' . $this->strSuffix . '</span>'; |
||
| 227 | } else { |
||
| 228 | $strHTML .= ' ' . $this->strSuffix; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | $strHTML .= '</div>' . PHP_EOL; |
||
| 233 | |||
| 234 | return $strHTML; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * set image and title for select-button (leave strImg blank for default) |
||
| 239 | * default image is 16x16/search.png |
||
| 240 | * @param string $strImg |
||
| 241 | * @param string $strTitle (default = '') |
||
| 242 | */ |
||
| 243 | public function setSelectImg(string $strImg, string $strTitle='') : void |
||
| 244 | { |
||
| 245 | $this->strSelectImg = $strImg; |
||
| 246 | $this->strSelectImgTitle = $strTitle; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $strBrowseServer |
||
| 251 | */ |
||
| 252 | public function setBrowseServer(string $strBrowseServer) : void |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $strSuffix |
||
| 259 | */ |
||
| 260 | public function setSuffix(string $strSuffix) : void |
||
| 263 | } |
||
| 264 | } |
||
| 265 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.