| Total Complexity | 50 |
| Total Lines | 186 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like QueryInputTrait 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 QueryInputTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | trait QueryInputTrait |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @param TableFieldEntity $field |
||
| 25 | * @param string $name |
||
| 26 | * @param string|null $function |
||
| 27 | * @param array $functions |
||
| 28 | * |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | private function getEntryFunction(TableFieldEntity $field, string $name, $function, array $functions): array |
||
| 32 | { |
||
| 33 | // Input for functions |
||
| 34 | if ($field->type == "enum") { |
||
| 35 | return [ |
||
| 36 | 'type' => 'name', |
||
| 37 | 'name' => $this->util->html($functions[""] ?? ''), |
||
| 38 | ]; |
||
| 39 | } |
||
| 40 | if (count($functions) > 1) { |
||
| 41 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
| 42 | return [ |
||
| 43 | 'type' => 'select', |
||
| 44 | 'name' => "function[$name]", |
||
| 45 | 'options' => $functions, |
||
| 46 | 'selected' => $function === null || $hasFunction ? $function : "", |
||
| 47 | ]; |
||
| 48 | } |
||
| 49 | return [ |
||
| 50 | 'type' => 'name', |
||
| 51 | 'name' => $this->util->html(reset($functions)), |
||
| 52 | ]; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param TableFieldEntity $field |
||
| 57 | * @param array $attrs |
||
| 58 | * @param mixed $value |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | private function getSetInput(TableFieldEntity $field, array $attrs, $value): array |
||
| 63 | { |
||
| 64 | $values = []; |
||
| 65 | preg_match_all("~'((?:[^']|'')*)'~", $field->length, $matches); |
||
| 66 | foreach ($matches[1] as $i => $val) { |
||
| 67 | $val = stripcslashes(str_replace("''", "'", $val)); |
||
| 68 | $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); |
||
| 69 | $values[] = [$this->util->html($val), $checked]; |
||
| 70 | } |
||
| 71 | return ['type' => 'checkbox', 'attrs' => $attrs, 'values' => $values]; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param TableFieldEntity $field |
||
| 76 | * @param array $attrs |
||
| 77 | * @param mixed $value |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | private function getBlobInput(TableFieldEntity $field, array $attrs, $value): array |
||
| 82 | { |
||
| 83 | if (preg_match('~text|lob|memo~i', $field->type) && $this->driver->jush() != "sqlite") { |
||
| 84 | $attrs['cols'] = 50; |
||
| 85 | $attrs['rows'] = 12; |
||
| 86 | } else { |
||
| 87 | $rows = min(12, substr_count($value, "\n") + 1); |
||
| 88 | $attrs['cols'] = 30; |
||
| 89 | $attrs['rows'] = $rows; |
||
| 90 | if ($rows == 1) { |
||
| 91 | $attrs['style'] = 'height: 1.2em;'; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | return ['type' => 'blob', 'attrs' => $attrs, 'value' => $this->util->html($value)]; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param TableFieldEntity $field |
||
| 99 | * @param array $attrs |
||
| 100 | * @param mixed $value |
||
| 101 | * @param string|null $function |
||
| 102 | * @param array $functions |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | private function getDefaultInput(TableFieldEntity $field, array $attrs, $value, $function, array $functions): array |
||
| 107 | { |
||
| 108 | $unsigned = $field->unsigned ?? false; |
||
| 109 | // int(3) is only a display hint |
||
| 110 | $maxlength = (!preg_match('~int~', $field->type) && |
||
| 111 | preg_match('~^(\d+)(,(\d+))?$~', $field->length, $match) ? |
||
| 112 | ((preg_match("~binary~", $field->type) ? 2 : 1) * $match[1] + (($match[3] ?? null) ? 1 : 0) + |
||
| 113 | (($match[2] ?? false) && !$unsigned ? 1 : 0)) : |
||
| 114 | ($this->driver->typeExists($field->type) ? $this->driver->type($field->type) + ($unsigned ? 0 : 1) : 0)); |
||
| 115 | if ($this->driver->jush() == 'sql' && $this->driver->minVersion(5.6) && preg_match('~time~', $field->type)) { |
||
| 116 | $maxlength += 7; // microtime |
||
| 117 | } |
||
| 118 | if ($maxlength > 0) { |
||
| 119 | $attrs['data-maxlength'] = $maxlength; |
||
| 120 | } |
||
| 121 | // type='date' and type='time' display localized value which may be confusing, |
||
| 122 | // type='datetime' uses 'T' as date and time separator |
||
| 123 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
| 124 | if ((!$hasFunction || $function === "") && |
||
| 125 | preg_match('~(?<!o)int(?!er)~', $field->type) && |
||
| 126 | !preg_match('~\[\]~', $field->fullType)) { |
||
| 127 | $attrs['type'] = 'number'; |
||
| 128 | } |
||
| 129 | if (preg_match('~char|binary~', $field->type) && $maxlength > 20) { |
||
| 130 | $attrs['size'] = 40; |
||
| 131 | } |
||
| 132 | return ['type' => 'input', 'attrs' => $attrs, 'value' => $this->util->html($value)]; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param TableFieldEntity $field |
||
| 137 | * @param string $name |
||
| 138 | * @param mixed $value |
||
| 139 | * @param string|null $function |
||
| 140 | * @param array $functions |
||
| 141 | * @param array $options |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | private function getEntryInput(TableFieldEntity $field, string $name, $value, $function, array $functions, array $options): array |
||
| 146 | { |
||
| 147 | $attrs = ['name' => "fields[$name]"]; |
||
| 148 | if ($field->type == "enum") { |
||
| 149 | return ['type' => 'radio', 'attrs' => $attrs, 'values' => [isset($options["select"]), $field, $attrs, $value]]; |
||
| 150 | } |
||
| 151 | if (preg_match('~bool~', $field->type)) { |
||
| 152 | return ['type' => 'checkbox', 'attrs' => $attrs, 'values' => [preg_match('~^(1|t|true|y|yes|on)$~i', $value)]]; |
||
| 153 | } |
||
| 154 | if ($field->type == "set") { |
||
| 155 | return $this->getSetInput($field, $attrs, $value); |
||
| 156 | } |
||
| 157 | if (preg_match('~blob|bytea|raw|file~', $field->type) && $this->util->iniBool("file_uploads")) { |
||
| 158 | return ['type' => 'upload', 'attrs' => $attrs, 'value' => $name]; |
||
| 159 | } |
||
| 160 | if (preg_match('~text|lob|memo~i', $field->type) || preg_match("~\n~", $value)) { |
||
| 161 | return $this->getBlobInput($field, $attrs, $value); |
||
| 162 | } |
||
| 163 | if ($function == "json" || preg_match('~^jsonb?$~', $field->type)) { |
||
| 164 | $attrs['cols'] = 50; |
||
| 165 | $attrs['rows'] = 12; |
||
| 166 | return ['type' => 'json', 'attrs' => $attrs, 'value' => $this->util->html($value)]; |
||
| 167 | } |
||
| 168 | return $this->getDefaultInput($field, $attrs, $value, $function, $functions); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get data for an input field |
||
| 173 | * |
||
| 174 | * @param TableFieldEntity $field |
||
| 175 | * @param mixed $value |
||
| 176 | * @param string|null $function |
||
| 177 | * @param array $options |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function getFieldInput(TableFieldEntity $field, $value, $function, array $options): array |
||
| 207 | ]; |
||
| 208 | |||
| 209 | // Input for value |
||
| 210 | // The HTML code generated by Adminer is kept here. |
||
| 211 | /*$attrs = " name='fields[$name]'"; |
||
| 212 | $entry['input'] = ['type' => '']; |
||
| 267 |