Total Complexity | 100 |
Total Lines | 1097 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MiscTrait 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 MiscTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | trait MiscTrait |
||
22 | { |
||
23 | |||
24 | public function getSubjectParams($subject) |
||
25 | { |
||
26 | $plugin_manager = $this->plugin_manager; |
||
27 | |||
28 | $vars = []; |
||
29 | |||
30 | switch ($subject) { |
||
31 | case 'root': |
||
32 | $vars = [ |
||
33 | 'params' => [ |
||
34 | 'subject' => 'root', |
||
35 | ], |
||
36 | ]; |
||
37 | |||
38 | break; |
||
39 | case 'server': |
||
40 | $vars = ['params' => [ |
||
41 | 'server' => $_REQUEST['server'], |
||
42 | 'subject' => 'server', |
||
43 | ]]; |
||
44 | |||
45 | break; |
||
46 | case 'role': |
||
47 | $vars = ['params' => [ |
||
48 | 'server' => $_REQUEST['server'], |
||
49 | 'subject' => 'role', |
||
50 | 'action' => 'properties', |
||
51 | 'rolename' => $_REQUEST['rolename'], |
||
52 | ]]; |
||
53 | |||
54 | break; |
||
55 | case 'database': |
||
56 | $vars = ['params' => [ |
||
57 | 'server' => $_REQUEST['server'], |
||
58 | 'subject' => 'database', |
||
59 | 'database' => $_REQUEST['database'], |
||
60 | ]]; |
||
61 | |||
62 | break; |
||
63 | case 'schema': |
||
64 | $vars = ['params' => [ |
||
65 | 'server' => $_REQUEST['server'], |
||
66 | 'subject' => 'schema', |
||
67 | 'database' => $_REQUEST['database'], |
||
68 | 'schema' => $_REQUEST['schema'], |
||
69 | ]]; |
||
70 | |||
71 | break; |
||
72 | case 'table': |
||
73 | $vars = ['params' => [ |
||
74 | 'server' => $_REQUEST['server'], |
||
75 | 'subject' => 'table', |
||
76 | 'database' => $_REQUEST['database'], |
||
77 | 'schema' => $_REQUEST['schema'], |
||
78 | 'table' => $_REQUEST['table'], |
||
79 | ]]; |
||
80 | |||
81 | break; |
||
82 | case 'selectrows': |
||
83 | $vars = [ |
||
84 | 'url' => 'tables', |
||
85 | 'params' => [ |
||
86 | 'server' => $_REQUEST['server'], |
||
87 | 'subject' => 'table', |
||
88 | 'database' => $_REQUEST['database'], |
||
89 | 'schema' => $_REQUEST['schema'], |
||
90 | 'table' => $_REQUEST['table'], |
||
91 | 'action' => 'confselectrows', |
||
92 | ]]; |
||
93 | |||
94 | break; |
||
95 | case 'view': |
||
96 | $vars = ['params' => [ |
||
97 | 'server' => $_REQUEST['server'], |
||
98 | 'subject' => 'view', |
||
99 | 'database' => $_REQUEST['database'], |
||
100 | 'schema' => $_REQUEST['schema'], |
||
101 | 'view' => $_REQUEST['view'], |
||
102 | ]]; |
||
103 | |||
104 | break; |
||
105 | case 'matview': |
||
106 | $vars = ['params' => [ |
||
107 | 'server' => $_REQUEST['server'], |
||
108 | 'subject' => 'matview', |
||
109 | 'database' => $_REQUEST['database'], |
||
110 | 'schema' => $_REQUEST['schema'], |
||
111 | 'matview' => $_REQUEST['matview'], |
||
112 | ]]; |
||
113 | |||
114 | break; |
||
115 | case 'fulltext': |
||
116 | case 'ftscfg': |
||
117 | $vars = ['params' => [ |
||
118 | 'server' => $_REQUEST['server'], |
||
119 | 'subject' => 'fulltext', |
||
120 | 'database' => $_REQUEST['database'], |
||
121 | 'schema' => $_REQUEST['schema'], |
||
122 | 'action' => 'viewconfig', |
||
123 | 'ftscfg' => $_REQUEST['ftscfg'], |
||
124 | ]]; |
||
125 | |||
126 | break; |
||
127 | case 'function': |
||
128 | $vars = ['params' => [ |
||
129 | 'server' => $_REQUEST['server'], |
||
130 | 'subject' => 'function', |
||
131 | 'database' => $_REQUEST['database'], |
||
132 | 'schema' => $_REQUEST['schema'], |
||
133 | 'function' => $_REQUEST['function'], |
||
134 | 'function_oid' => $_REQUEST['function_oid'], |
||
135 | ]]; |
||
136 | |||
137 | break; |
||
138 | case 'aggregate': |
||
139 | $vars = ['params' => [ |
||
140 | 'server' => $_REQUEST['server'], |
||
141 | 'subject' => 'aggregate', |
||
142 | 'action' => 'properties', |
||
143 | 'database' => $_REQUEST['database'], |
||
144 | 'schema' => $_REQUEST['schema'], |
||
145 | 'aggrname' => $_REQUEST['aggrname'], |
||
146 | 'aggrtype' => $_REQUEST['aggrtype'], |
||
147 | ]]; |
||
148 | |||
149 | break; |
||
150 | case 'column': |
||
151 | if (isset($_REQUEST['table'])) { |
||
152 | $vars = ['params' => [ |
||
153 | 'server' => $_REQUEST['server'], |
||
154 | 'subject' => 'column', |
||
155 | 'database' => $_REQUEST['database'], |
||
156 | 'schema' => $_REQUEST['schema'], |
||
157 | 'table' => $_REQUEST['table'], |
||
158 | 'column' => $_REQUEST['column'], |
||
159 | ]]; |
||
160 | } else { |
||
161 | $vars = ['params' => [ |
||
162 | 'server' => $_REQUEST['server'], |
||
163 | 'subject' => 'column', |
||
164 | 'database' => $_REQUEST['database'], |
||
165 | 'schema' => $_REQUEST['schema'], |
||
166 | 'view' => $_REQUEST['view'], |
||
167 | 'column' => $_REQUEST['column'], |
||
168 | ]]; |
||
169 | } |
||
170 | |||
171 | break; |
||
172 | case 'plugin': |
||
173 | $vars = [ |
||
174 | 'url' => 'plugin', |
||
175 | 'params' => [ |
||
176 | 'server' => $_REQUEST['server'], |
||
177 | 'subject' => 'plugin', |
||
178 | 'plugin' => $_REQUEST['plugin'], |
||
179 | ]]; |
||
180 | |||
181 | if (!is_null($plugin_manager->getPlugin($_REQUEST['plugin']))) { |
||
182 | $vars['params'] = array_merge($vars['params'], $plugin_manager->getPlugin($_REQUEST['plugin'])->get_subject_params()); |
||
183 | } |
||
184 | |||
185 | break; |
||
186 | default: |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | if (!isset($vars['url'])) { |
||
191 | $vars['url'] = SUBFOLDER . '/redirect'; |
||
192 | } |
||
193 | if ($vars['url'] == SUBFOLDER . '/redirect' && isset($vars['params']['subject'])) { |
||
194 | $vars['url'] = SUBFOLDER . '/redirect/' . $vars['params']['subject']; |
||
195 | unset($vars['params']['subject']); |
||
196 | } |
||
197 | |||
198 | return $vars; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Render a value into HTML using formatting rules specified |
||
203 | * by a type name and parameters. |
||
204 | * |
||
205 | * @param string $str The string to change |
||
206 | * @param string $type Field type (optional), this may be an internal PostgreSQL type, or: |
||
207 | * yesno - same as bool, but renders as 'Yes' or 'No'. |
||
208 | * pre - render in a <pre> block. |
||
209 | * nbsp - replace all spaces with 's |
||
210 | * verbatim - render exactly as supplied, no escaping what-so-ever. |
||
211 | * callback - render using a callback function supplied in the 'function' param. |
||
212 | * @param array $params Type parameters (optional), known parameters: |
||
213 | * null - string to display if $str is null, or set to TRUE to use a default 'NULL' string, |
||
214 | * otherwise nothing is rendered. |
||
215 | * clip - if true, clip the value to a fixed length, and append an ellipsis... |
||
216 | * cliplen - the maximum length when clip is enabled (defaults to $conf['max_chars']) |
||
217 | * ellipsis - the string to append to a clipped value (defaults to $lang['strellipsis']) |
||
218 | * tag - an HTML element name to surround the value. |
||
219 | * class - a class attribute to apply to any surrounding HTML element. |
||
220 | * align - an align attribute ('left','right','center' etc.) |
||
221 | * true - (type='bool') the representation of true. |
||
222 | * false - (type='bool') the representation of false. |
||
223 | * function - (type='callback') a function name, accepts args ($str, $params) and returns a rendering. |
||
224 | * lineno - prefix each line with a line number. |
||
225 | * map - an associative array. |
||
226 | * |
||
227 | * @return string The HTML rendered value |
||
228 | */ |
||
229 | public function printVal($str, $type = null, $params = []) |
||
230 | { |
||
231 | $lang = $this->lang; |
||
232 | $data = $this->getDatabaseAccessor(); |
||
|
|||
233 | |||
234 | // Shortcircuit for a NULL value |
||
235 | if (!$str) { |
||
236 | return isset($params['null']) |
||
237 | ? ($params['null'] === true ? '<i>NULL</i>' : $params['null']) |
||
238 | : ''; |
||
239 | } |
||
240 | |||
241 | if (isset($params['map'], $params['map'][$str])) { |
||
242 | $str = $params['map'][$str]; |
||
243 | } |
||
244 | |||
245 | // Clip the value if the 'clip' parameter is true. |
||
246 | if (isset($params['clip']) && $params['clip'] === true) { |
||
247 | $maxlen = isset($params['cliplen']) && is_integer($params['cliplen']) ? $params['cliplen'] : $this->conf['max_chars']; |
||
248 | $ellipsis = isset($params['ellipsis']) ? $params['ellipsis'] : $lang['strellipsis']; |
||
249 | if (strlen($str) > $maxlen) { |
||
250 | $str = substr($str, 0, $maxlen - 1) . $ellipsis; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | $out = ''; |
||
255 | $class = ''; |
||
256 | |||
257 | switch ($type) { |
||
258 | case 'int2': |
||
259 | case 'int4': |
||
260 | case 'int8': |
||
261 | case 'float4': |
||
262 | case 'float8': |
||
263 | case 'money': |
||
264 | case 'numeric': |
||
265 | case 'oid': |
||
266 | case 'xid': |
||
267 | case 'cid': |
||
268 | case 'tid': |
||
269 | $align = 'right'; |
||
270 | $out = nl2br(htmlspecialchars(\PHPPgAdmin\Traits\HelperTrait::br2ln($str))); |
||
271 | |||
272 | break; |
||
273 | case 'yesno': |
||
274 | if (!isset($params['true'])) { |
||
275 | $params['true'] = $lang['stryes']; |
||
276 | } |
||
277 | |||
278 | if (!isset($params['false'])) { |
||
279 | $params['false'] = $lang['strno']; |
||
280 | } |
||
281 | |||
282 | // no break - fall through to boolean case. |
||
283 | case 'bool': |
||
284 | case 'boolean': |
||
285 | if (is_bool($str)) { |
||
286 | $str = $str ? 't' : 'f'; |
||
287 | } |
||
288 | |||
289 | switch ($str) { |
||
290 | case 't': |
||
291 | $out = (isset($params['true']) ? $params['true'] : $lang['strtrue']); |
||
292 | $align = 'center'; |
||
293 | |||
294 | break; |
||
295 | case 'f': |
||
296 | $out = (isset($params['false']) ? $params['false'] : $lang['strfalse']); |
||
297 | $align = 'center'; |
||
298 | |||
299 | break; |
||
300 | default: |
||
301 | $out = htmlspecialchars($str); |
||
302 | } |
||
303 | |||
304 | break; |
||
305 | case 'bytea': |
||
306 | $tag = 'div'; |
||
307 | $class = 'pre'; |
||
308 | $out = $data->escapeBytea($str); |
||
309 | |||
310 | break; |
||
311 | case 'errormsg': |
||
312 | $tag = 'pre'; |
||
313 | $class = 'error'; |
||
314 | $out = htmlspecialchars($str); |
||
315 | |||
316 | break; |
||
317 | case 'pre': |
||
318 | $tag = 'pre'; |
||
319 | $out = htmlspecialchars($str); |
||
320 | |||
321 | break; |
||
322 | case 'prenoescape': |
||
323 | $tag = 'pre'; |
||
324 | $out = $str; |
||
325 | |||
326 | break; |
||
327 | case 'nbsp': |
||
328 | $out = nl2br(str_replace(' ', ' ', \PHPPgAdmin\Traits\HelperTrait::br2ln($str))); |
||
329 | |||
330 | break; |
||
331 | case 'verbatim': |
||
332 | $out = $str; |
||
333 | |||
334 | break; |
||
335 | case 'callback': |
||
336 | $out = $params['function']($str, $params); |
||
337 | |||
338 | break; |
||
339 | case 'prettysize': |
||
340 | if ($str == -1) { |
||
341 | $out = $lang['strnoaccess']; |
||
342 | } else { |
||
343 | $out = \PHPPgAdmin\Traits\HelperTrait::formatSizeUnits($str, $lang); |
||
344 | } |
||
345 | |||
346 | break; |
||
347 | default: |
||
348 | // If the string contains at least one instance of >1 space in a row, a tab |
||
349 | // character, a space at the start of a line, or a space at the start of |
||
350 | // the whole string then render within a pre-formatted element (<pre>). |
||
351 | if (preg_match('/(^ | |\t|\n )/m', $str)) { |
||
352 | $tag = 'pre'; |
||
353 | $class = 'data'; |
||
354 | $out = htmlspecialchars($str); |
||
355 | } else { |
||
356 | $tag = 'span'; |
||
357 | $out = nl2br(htmlspecialchars(\PHPPgAdmin\Traits\HelperTrait::br2ln($str))); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | if (isset($params['class'])) { |
||
362 | $class = $params['class']; |
||
363 | } |
||
364 | |||
365 | if (isset($params['align'])) { |
||
366 | $align = $params['align']; |
||
367 | } |
||
368 | |||
369 | if (!isset($tag) && (!empty($class) || isset($align))) { |
||
370 | $tag = 'div'; |
||
371 | } |
||
372 | |||
373 | if (isset($tag)) { |
||
374 | $alignattr = isset($align) ? " style=\"text-align: {$align}\"" : ''; |
||
375 | $classattr = !empty($class) ? " class=\"{$class}\"" : ''; |
||
376 | $out = "<{$tag}{$alignattr}{$classattr}>{$out}</{$tag}>"; |
||
377 | } |
||
378 | |||
379 | // Add line numbers if 'lineno' param is true |
||
380 | if (isset($params['lineno']) && $params['lineno'] === true) { |
||
381 | $lines = explode("\n", $str); |
||
382 | $num = count($lines); |
||
383 | if ($num > 0) { |
||
384 | $temp = "<table>\n<tr><td class=\"{$class}\" style=\"vertical-align: top; padding-right: 10px;\"><pre class=\"{$class}\">"; |
||
385 | for ($i = 1; $i <= $num; ++$i) { |
||
386 | $temp .= $i . "\n"; |
||
387 | } |
||
388 | $temp .= "</pre></td><td class=\"{$class}\" style=\"vertical-align: top;\">{$out}</td></tr></table>\n"; |
||
389 | $out = $temp; |
||
390 | } |
||
391 | unset($lines); |
||
392 | } |
||
393 | |||
394 | return $out; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Retrieve the tab info for a specific tab bar. |
||
399 | * |
||
400 | * @param string $section the name of the tab bar |
||
401 | * |
||
402 | * @return array array of tabs |
||
403 | */ |
||
404 | public function getNavTabs($section) |
||
1097 | } |
||
1098 | |||
1099 | /** |
||
1100 | * Get the URL for the last active tab of a particular tab bar. |
||
1101 | * |
||
1102 | * @param string $section |
||
1103 | * |
||
1104 | * @return null|mixed |
||
1105 | */ |
||
1106 | public function getLastTabURL($section) |
||
1118 | } |
||
1119 | } |
||
1120 |