| Total Complexity | 52 |
| Total Lines | 361 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DataFieldInput 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 DataFieldInput, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DataFieldInput |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The constructor |
||
| 28 | * |
||
| 29 | * @param AppPage $page |
||
| 30 | * @param DriverInterface $driver |
||
| 31 | * @param Utils $utils |
||
| 32 | * @param string $action |
||
| 33 | * @param string $operation |
||
| 34 | */ |
||
| 35 | public function __construct(private AppPage $page, private DriverInterface $driver, |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param FieldEditEntity $editField |
||
| 41 | * @param string $fieldValue |
||
| 42 | * @param string|null $enumValue |
||
| 43 | * |
||
| 44 | * @return bool |
||
| 45 | */ |
||
| 46 | private function isChecked(FieldEditEntity $editField, string $fieldValue, string|null $enumValue): bool |
||
| 47 | { |
||
| 48 | return !is_array($editField->value) ? $editField->value === $enumValue : |
||
| 49 | in_array($fieldValue, $editField->value); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get data for enum or set input field |
||
| 54 | * |
||
| 55 | * @param FieldEditEntity $editField |
||
| 56 | */ |
||
| 57 | private function getItemList(FieldEditEntity $editField, array $attrs, string $default = ""): array|null |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param FieldEditEntity $editField |
||
| 99 | * @param array $attrs |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | private function getEnumFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 104 | { |
||
| 105 | // From adminer.inc.php: function editInput(?string $table, array $field, string $attrs, $value): string |
||
| 106 | $items = $this->getItemList($editField, $attrs, 'NULL'); |
||
| 107 | if ($this->action === 'select') { |
||
| 108 | // Prepend the value to the item list |
||
| 109 | $items = [[ |
||
| 110 | 'attrs' => $attrs, |
||
| 111 | 'label' => '<i>' . $this->utils->trans->lang('original') . '</i>', |
||
| 112 | 'value' => 'orig', |
||
| 113 | 'checked' => true, |
||
| 114 | ], ...$items]; |
||
| 115 | } |
||
| 116 | |||
| 117 | return [ |
||
| 118 | 'field' => 'enum', |
||
| 119 | 'items' => $items, |
||
| 120 | ]; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param FieldEditEntity $editField |
||
| 125 | * @param array $attrs |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | private function getSetFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 130 | { |
||
| 131 | if (is_string($editField->value)) { |
||
| 132 | $editField->value = explode(",", $editField->value); |
||
| 133 | } |
||
| 134 | |||
| 135 | return [ |
||
| 136 | 'field' => 'set', |
||
| 137 | 'items' => $this->getItemList($editField, $attrs), |
||
| 138 | ]; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param FieldEditEntity $editField |
||
| 143 | * @param array $attrs |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | private function getBoolFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 148 | { |
||
| 149 | return [ |
||
| 150 | 'field' => 'bool', |
||
| 151 | 'attrs' => [ |
||
| 152 | 'hidden' => [ |
||
| 153 | ...$attrs, |
||
| 154 | 'id' => '', // Unset the id value in the $attrs array |
||
| 155 | 'value' => '0', |
||
| 156 | ], |
||
| 157 | 'checkbox' => [ |
||
| 158 | ...$attrs, |
||
| 159 | 'value' => '1', |
||
| 160 | ], |
||
| 161 | ], |
||
| 162 | 'checked' => $editField->isChecked(), |
||
| 163 | ]; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param FieldEditEntity $editField |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | private function isBlob(FieldEditEntity $editField): bool |
||
| 172 | { |
||
| 173 | return $this->utils->isBlob($editField->field) && $this->utils->iniBool("file_uploads"); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param FieldEditEntity $editField |
||
| 178 | * @param array $attrs |
||
| 179 | * |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | private function getFileFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 189 | ], |
||
| 190 | ]; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param FieldEditEntity $editField |
||
| 195 | * @param array $attrs |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | private function getJsonFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 200 | { |
||
| 201 | return [ |
||
| 202 | 'field' => 'json', |
||
| 203 | 'attrs' => [ |
||
| 204 | ...$attrs, |
||
| 205 | 'cols' => '50', |
||
| 206 | 'rows' => '5', |
||
| 207 | 'class' => 'jush-js', |
||
| 208 | ], |
||
| 209 | 'value' => $this->utils->str->html($editField->value ?? ''), |
||
| 210 | ]; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param FieldEditEntity $editField |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | private function textSizeIsFixed(FieldEditEntity $editField): bool |
||
| 219 | { |
||
| 220 | return ($editField->isText() && $this->driver->jush() !== 'sqlite') || $editField->isSearch(); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param FieldEditEntity $editField |
||
| 225 | * @param array $attrs |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | private function getTextFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 245 | ]; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param FieldEditEntity $editField |
||
| 250 | * |
||
| 251 | * @return int |
||
| 252 | */ |
||
| 253 | private function getInputFieldMaxLength(FieldEditEntity $editField): int |
||
| 254 | { |
||
| 255 | $unsigned = $editField->field->unsigned; |
||
| 256 | $length = $editField->field->length; |
||
| 257 | $type = $editField->type; |
||
| 258 | $types = $this->driver->types(); |
||
| 259 | |||
| 260 | $maxlength = (!preg_match('~int~', $type) && |
||
| 261 | preg_match('~^(\d+)(,(\d+))?$~', $length, $match) ? |
||
| 262 | ((preg_match("~binary~", $type) ? 2 : 1) * |
||
| 263 | ($match[1] ?? 0) + (($match[3] ?? false) ? 1 : 0) + |
||
| 264 | (($match[2] ?? false) && !$unsigned ? 1 : 0)) : |
||
| 265 | (isset($types[$type]) ? $types[$type] + ($unsigned ? 0 : 1) : 0) |
||
| 266 | ); |
||
| 267 | |||
| 268 | return $this->driver->jush() === 'sql' && |
||
| 269 | $this->driver->minVersion(5.6) && |
||
| 270 | preg_match('~time~', $type) ? |
||
| 271 | $maxlength += 7 : // microtime |
||
| 272 | $maxlength; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param FieldEditEntity $editField |
||
| 277 | * @param array $attrs |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | private function getDefaultFieldInput(FieldEditEntity $editField, array $attrs): array |
||
| 301 | ]; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the input field for value |
||
| 306 | * |
||
| 307 | * @param FieldEditEntity $editField |
||
| 308 | * @param bool|null $autofocus |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | private function getFieldValueInput(FieldEditEntity $editField, bool|null $autofocus): array |
||
| 339 | }; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the input field for function |
||
| 344 | * |
||
| 345 | * @param FieldEditEntity $editField |
||
| 346 | * |
||
| 347 | * @return array|null |
||
| 348 | */ |
||
| 349 | private function getFieldFunctionInput(FieldEditEntity $editField): array|null |
||
| 350 | { |
||
| 351 | // From html.inc.php: function input(array $field, $value, ?string $function, ?bool $autofocus = false) |
||
| 352 | if ($editField->type === 'enum' || $editField->function === null) { |
||
| 353 | return null; // No function for enum values |
||
| 354 | } |
||
| 355 | |||
| 356 | if (count($editField->functions) < 2) { |
||
| 357 | return [ |
||
| 358 | 'label' => $this->utils->str->html($editField->functions[0] ?? ''), |
||
| 359 | ]; |
||
| 360 | } |
||
| 361 | |||
| 362 | $disabledAttr = $editField->isDisabled() ? ['disabled' => 'disabled'] : []; |
||
| 363 | return [ |
||
| 364 | 'select' => [ |
||
| 365 | 'attrs' => [ |
||
| 366 | 'name' => "field_functions[{$editField->name}]", |
||
| 367 | ...$disabledAttr, |
||
| 368 | ], |
||
| 369 | 'options' => $editField->functions, |
||
| 370 | 'value' => $editField->functionValue(), |
||
| 371 | ], |
||
| 372 | ]; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param FieldEditEntity $editField |
||
| 377 | * @param bool|null $autofocus |
||
| 378 | * |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | public function setFieldInputValues(FieldEditEntity $editField, bool|null $autofocus): void |
||
| 385 | } |
||
| 386 | } |
||
| 387 |