Total Complexity | 55 |
Total Lines | 237 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
20 | trait QueryInputTrait |
||
21 | { |
||
22 | /** |
||
23 | * @param TableFieldEntity $field |
||
24 | * @param string $name |
||
25 | * @param string|null $function |
||
26 | * @param array $functions |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | private function getEntryFunctions(TableFieldEntity $field, string $name, $function, array $functions): array |
||
31 | { |
||
32 | // Input for functions |
||
33 | if ($field->type == "enum") { |
||
34 | return [ |
||
35 | 'type' => 'name', |
||
36 | 'name' => $this->util->html($functions[""] ?? ''), |
||
37 | ]; |
||
38 | } |
||
39 | if (count($functions) > 1) { |
||
40 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
41 | return [ |
||
42 | 'type' => 'select', |
||
43 | 'name' => "function[$name]", |
||
44 | 'options' => $functions, |
||
45 | 'selected' => $function === null || $hasFunction ? $function : "", |
||
46 | ]; |
||
47 | } |
||
48 | return [ |
||
49 | 'type' => 'name', |
||
50 | 'name' => $this->util->html(reset($functions)), |
||
51 | ]; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get options to display edit field |
||
56 | * |
||
57 | * @param TableFieldEntity $field |
||
58 | * @param bool $select |
||
59 | * @param mixed $value |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | private function getEnumValues(TableFieldEntity $field, bool $select, $value): array |
||
64 | { |
||
65 | $values = []; |
||
66 | if (($select)) { |
||
67 | $values[] = ['value' => '-1', 'checked' => true, |
||
68 | 'text' => '<i>' . $this->trans->lang('original') . '</i>']; |
||
69 | } |
||
70 | if ($field->null) { |
||
71 | $values[] = ['value' => '', 'checked' => $value === null && !$select, 'text' => '<i>NULL</i>']; |
||
72 | } |
||
73 | |||
74 | // From functions.inc.php (function enum_input()) |
||
75 | $empty = 0; // 0 - empty |
||
76 | $values[] = ['value' => $empty, 'checked' => is_array($value) ? in_array($empty, $value) : $value === 0, |
||
77 | 'text' => '<i>' . $this->trans->lang('empty') . '</i>']; |
||
78 | |||
79 | preg_match_all("~'((?:[^']|'')*)'~", $field->fullType, $matches); |
||
80 | foreach ($matches[1] as $i => $val) { |
||
81 | $val = stripcslashes(str_replace("''", "'", $val)); |
||
82 | $checked = (is_int($value) ? $value == $i + 1 : |
||
83 | (is_array($value) ? in_array($i+1, $value) : $value === $val)); |
||
84 | $values[] = ['value' => $i + 1, 'checked' => $checked, 'text' => $this->util->html($val)]; |
||
85 | } |
||
86 | |||
87 | return $values; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param TableFieldEntity $field |
||
92 | * @param array $attrs |
||
93 | * @param mixed $value |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | private function getSetInput(TableFieldEntity $field, array $attrs, $value): array |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param TableFieldEntity $field |
||
111 | * @param array $attrs |
||
112 | * @param mixed $value |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | private function getBlobInput(TableFieldEntity $field, array $attrs, $value): array |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param TableFieldEntity $field |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | private function _getMaxLength(TableFieldEntity $field): int |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param TableFieldEntity $field |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | private function getMaxLength(TableFieldEntity $field): int |
||
159 | { |
||
160 | $maxlength = $this->_getMaxLength($field); |
||
161 | if ($this->driver->jush() == 'sql' && $this->driver->minVersion(5.6) && |
||
162 | preg_match('~time~', $field->type)) { |
||
163 | return $maxlength + 7; // microtime |
||
164 | } |
||
165 | return $maxlength; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param TableFieldEntity $field |
||
170 | * @param array $attrs |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | private function setDataLength(TableFieldEntity $field, array &$attrs) |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param TableFieldEntity $field |
||
187 | * @param array $attrs |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | private function setDataType(TableFieldEntity $field, array &$attrs) |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param TableFieldEntity $field |
||
202 | * @param array $attrs |
||
203 | * @param mixed $value |
||
204 | * @param string|null $function |
||
205 | * @param array $functions |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | private function getDefaultInput(TableFieldEntity $field, array $attrs, $value, $function, array $functions): array |
||
210 | { |
||
211 | $this->setDataLength($field, $attrs); |
||
212 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
213 | if (!$hasFunction || $function === "") { |
||
214 | $this->setDataType($field, $attrs); |
||
215 | } |
||
216 | $attrs['value'] = $this->util->html($value); |
||
217 | return ['type' => 'input', 'attrs' => $attrs]; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param TableFieldEntity $field |
||
222 | * @param string $name |
||
223 | * @param mixed $value |
||
224 | * @param string|null $function |
||
225 | * @param array $functions |
||
226 | * @param array $options |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | private function getEntryInput(TableFieldEntity $field, string $name, $value, $function, array $functions, array $options): array |
||
257 | } |
||
258 | } |
||
259 |