Total Complexity | 55 |
Total Lines | 489 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HTMLNavbarController 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 HTMLNavbarController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class HTMLNavbarController extends HTMLController |
||
13 | { |
||
14 | public $controller_name = 'HTMLNavbarController'; |
||
15 | |||
16 | private function _getCrumbs($trail) |
||
42 | } |
||
43 | |||
44 | private function _getSearchPathsCrumbs($crumbs, $viewVars) |
||
45 | { |
||
46 | $data = $this->misc->getDatabaseAccessor(); |
||
47 | $lang = $this->lang; |
||
48 | if (isset($crumbs['database'])) { |
||
49 | $search_path_crumbs = []; |
||
50 | $dburl = $crumbs['database']['url']; |
||
51 | $search_paths = $data->getSearchPath(); |
||
52 | foreach ($search_paths as $schema) { |
||
53 | $search_path_crumbs[$schema] = [ |
||
54 | 'title' => $lang['strschema'], |
||
55 | 'text' => $schema, |
||
56 | 'icon' => $this->misc->icon('Schema'), |
||
57 | 'iconalt' => $lang['strschema'], |
||
58 | 'url' => str_replace(['&', 'redirect/database'], ['&', 'redirect/schema'], $dburl . '&schema=' . $schema), |
||
59 | ]; |
||
60 | } |
||
61 | $viewVars['search_paths'] = $search_path_crumbs; |
||
62 | } |
||
63 | return $viewVars; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Display a bread crumb trail. |
||
68 | * |
||
69 | * @param array|string $trail an array of breadcrumb items, or a string to identify one of them |
||
70 | * @param $do_print true to echo, false to return html |
||
71 | * @param null|string $from |
||
72 | */ |
||
73 | public function printTrail($trail = [], $do_print = true, $from = null) |
||
74 | { |
||
75 | $plugin_manager = $this->plugin_manager; |
||
76 | $from = $from ? $from : __METHOD__; |
||
77 | |||
78 | $trail_html = $this->printTopbar(false, $from); |
||
79 | |||
80 | if (is_string($trail)) { |
||
81 | $subject = $trail; |
||
82 | $trail = $this->_getTrail($subject); |
||
83 | // Trail hook's place |
||
84 | $plugin_functions_parameters = [ |
||
85 | 'trail' => &$trail, |
||
86 | 'section' => $subject, |
||
87 | ]; |
||
88 | |||
89 | $plugin_manager->doHook('trail', $plugin_functions_parameters); |
||
90 | } |
||
91 | |||
92 | $crumbs = $this->_getCrumbs($trail); |
||
93 | |||
94 | $viewVars = [ |
||
95 | 'crumbs' => $crumbs, |
||
96 | 'controller_name' => $this->controller_name, |
||
97 | ]; |
||
98 | $viewVars = $this->_getSearchPathsCrumbs($crumbs, $viewVars); |
||
99 | |||
100 | //\Kint::dump($viewVars); |
||
101 | |||
102 | $trail_html .= $this->getContainer()->view->fetch('components/trail.twig', $viewVars); |
||
103 | |||
104 | if ($do_print) { |
||
105 | echo $trail_html; |
||
106 | } else { |
||
107 | return $trail_html; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Display navigation tabs. |
||
113 | * |
||
114 | * @param string $alltabs The name of current section (Ex: intro, server, ...), |
||
115 | * or an array with tabs (Ex: sqledit::doFind function) |
||
116 | * @param string $activetab the name of the tab to be highlighted |
||
117 | * @param bool $print if false, return html |
||
|
|||
118 | * @param bool $do_print true to print html, false to return html |
||
119 | * @param null|string $from whichi method is calling this one |
||
120 | */ |
||
121 | public function printTabs($alltabs, $activetab, $do_print = true, $from = null) |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * [printTopbar description]. |
||
177 | * |
||
178 | * @param bool $do_print true to print, false to return html |
||
179 | * @param null|mixed $from which method is calling this one |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | private function printTopbar($do_print = true, $from = null) |
||
1 ignored issue
–
show
|
|||
184 | { |
||
185 | $from = $from ? $from : __METHOD__; |
||
186 | |||
187 | $lang = $this->lang; |
||
188 | $plugin_manager = $this->plugin_manager; |
||
189 | $this->misc = $this->misc; |
||
190 | $appName = $this->misc->appName; |
||
191 | $appVersion = $this->misc->appVersion; |
||
192 | |||
193 | $server_info = $this->misc->getServerInfo(); |
||
194 | $server_id = $this->misc->getServerId(); |
||
1 ignored issue
–
show
|
|||
195 | $reqvars = $this->misc->getRequestVars('table'); |
||
196 | |||
197 | $topbar_html = '<div class="topbar" data-controller="' . $this->controller_name . '"><table style="width: 100%"><tr><td>'; |
||
198 | |||
199 | if ($server_info && isset($server_info['platform'], $server_info['username'])) { |
||
200 | // top left informations when connected |
||
201 | $topbar_html .= sprintf( |
||
202 | $lang['strtopbar'], |
||
203 | '<span class="platform">' . htmlspecialchars($server_info['platform']) . '</span>', |
||
204 | '<span class="host">' . htmlspecialchars((empty($server_info['host'])) ? 'localhost' : $server_info['host']) . '</span>', |
||
205 | '<span class="port">' . htmlspecialchars($server_info['port']) . '</span>', |
||
206 | '<span class="username">' . htmlspecialchars($server_info['username']) . '</span>' |
||
207 | ); |
||
208 | |||
209 | $topbar_html .= '</td>'; |
||
210 | |||
211 | // top right informations when connected |
||
212 | |||
213 | $toplinks = [ |
||
214 | 'sql' => [ |
||
215 | 'attr' => [ |
||
216 | 'href' => [ |
||
217 | 'url' => SUBFOLDER . '/src/views/sqledit', |
||
218 | 'urlvars' => array_merge($reqvars, [ |
||
219 | 'action' => 'sql', |
||
220 | ]), |
||
221 | ], |
||
222 | 'target' => 'sqledit', |
||
223 | 'id' => 'toplink_sql', |
||
224 | ], |
||
225 | 'content' => $lang['strsql'], |
||
226 | ], |
||
227 | 'history' => [ |
||
228 | 'attr' => [ |
||
229 | 'href' => [ |
||
230 | 'url' => SUBFOLDER . '/src/views/history', |
||
231 | 'urlvars' => array_merge($reqvars, [ |
||
232 | 'action' => 'pophistory', |
||
233 | ]), |
||
234 | ], |
||
235 | 'id' => 'toplink_history', |
||
236 | ], |
||
237 | 'content' => $lang['strhistory'], |
||
238 | ], |
||
239 | 'find' => [ |
||
240 | 'attr' => [ |
||
241 | 'href' => [ |
||
242 | 'url' => SUBFOLDER . '/src/views/sqledit', |
||
243 | 'urlvars' => array_merge($reqvars, [ |
||
244 | 'action' => 'find', |
||
245 | ]), |
||
246 | ], |
||
247 | 'target' => 'sqledit', |
||
248 | 'id' => 'toplink_find', |
||
249 | ], |
||
250 | 'content' => $lang['strfind'], |
||
251 | ], |
||
252 | 'logout' => [ |
||
253 | 'attr' => [ |
||
254 | 'href' => [ |
||
255 | 'url' => SUBFOLDER . '/src/views/servers', |
||
256 | 'urlvars' => [ |
||
257 | 'action' => 'logout', |
||
258 | 'logoutServer' => sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}"), |
||
259 | ], |
||
260 | ], |
||
261 | 'id' => 'toplink_logout', |
||
262 | ], |
||
263 | 'content' => $lang['strlogout'], |
||
264 | ], |
||
265 | ]; |
||
266 | |||
267 | // Toplink hook's place |
||
268 | $plugin_functions_parameters = [ |
||
269 | 'toplinks' => &$toplinks, |
||
270 | ]; |
||
271 | |||
272 | $plugin_manager->doHook('toplinks', $plugin_functions_parameters); |
||
273 | |||
274 | $topbar_html .= '<td style="text-align: right">'; |
||
275 | |||
276 | $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], false, $from); |
||
277 | |||
278 | $topbar_html .= '</td>'; |
||
279 | } else { |
||
280 | $topbar_html .= "<span class=\"appname\">{$appName}</span> <span class=\"version\">{$appVersion}</span>"; |
||
281 | } |
||
282 | |||
283 | $topbar_html .= "</tr></table></div>\n"; |
||
284 | |||
285 | if ($do_print) { |
||
286 | echo $topbar_html; |
||
287 | } else { |
||
288 | return $topbar_html; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | private function getHREFSubject($subject) |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Create a bread crumb trail of the object hierarchy. |
||
302 | * |
||
303 | * @param null|string $subject sunkect of the trail |
||
304 | */ |
||
305 | private function _getTrail($subject = null) |
||
306 | { |
||
307 | $lang = $this->lang; |
||
308 | |||
309 | $appName = $this->misc->appName; |
||
310 | |||
311 | $trail = []; |
||
312 | |||
313 | $trail['root'] = [ |
||
314 | 'text' => $appName, |
||
315 | 'url' => SUBFOLDER . '/src/views/servers', |
||
316 | 'icon' => 'Introduction', |
||
317 | ]; |
||
318 | |||
319 | if ('root' == $subject) { |
||
320 | return $trail; |
||
321 | } |
||
322 | |||
323 | $server_info = $this->misc->getServerInfo(); |
||
324 | $trail['server'] = [ |
||
325 | 'title' => $lang['strserver'], |
||
326 | 'text' => $server_info['desc'], |
||
327 | 'url' => $this->getHREFSubject('server'), |
||
328 | 'help' => 'pg.server', |
||
329 | 'icon' => 'Server', |
||
330 | ]; |
||
331 | |||
332 | if ('server' == $subject) { |
||
333 | return $trail; |
||
334 | } |
||
335 | |||
336 | $database_rolename = [ |
||
337 | 'database' => [ |
||
338 | 'title' => $lang['strdatabase'], |
||
339 | 'subject' => 'database', |
||
340 | 'help' => 'pg.database', |
||
341 | 'icon' => 'Database', |
||
342 | ], |
||
343 | 'rolename' => [ |
||
344 | 'title' => $lang['strrole'], |
||
345 | 'subject' => 'role', |
||
346 | 'help' => 'pg.role', |
||
347 | 'icon' => 'Roles', |
||
348 | ], |
||
349 | ]; |
||
350 | |||
351 | $trail = $this->_getTrailsFromArray($trail, $database_rolename); |
||
352 | |||
353 | if (in_array($subject, ['database', 'role'])) { |
||
354 | return $trail; |
||
355 | } |
||
356 | |||
357 | $schema = [ |
||
358 | 'schema' => [ |
||
359 | 'title' => $lang['strschema'], |
||
360 | 'subject' => 'schema', |
||
361 | 'help' => 'pg.schema', |
||
362 | 'icon' => 'Schema', |
||
363 | ], |
||
364 | ]; |
||
365 | |||
366 | $trail = $this->_getTrailsFromArray($trail, $schema); |
||
367 | if ('schema' == $subject) { |
||
368 | return $trail; |
||
369 | } |
||
370 | |||
371 | $table_view_matview_fts = [ |
||
372 | 'table' => [ |
||
373 | 'title' => $lang['strtable'], |
||
374 | 'subject' => 'table', |
||
375 | 'help' => 'pg.table', |
||
376 | 'icon' => 'Table', |
||
377 | ], |
||
378 | 'view' => [ |
||
379 | 'title' => $lang['strview'], |
||
380 | 'subject' => 'view', |
||
381 | 'help' => 'pg.view', |
||
382 | 'icon' => 'View', |
||
383 | ], |
||
384 | 'matview' => [ |
||
385 | 'title' => 'M' . $lang['strview'], |
||
386 | 'subject' => 'matview', |
||
387 | 'help' => 'pg.matview', |
||
388 | 'icon' => 'MViews', |
||
389 | ], |
||
390 | 'ftscfg' => [ |
||
391 | 'title' => $lang['strftsconfig'], |
||
392 | 'subject' => 'ftscfg', |
||
393 | 'help' => 'pg.ftscfg.example', |
||
394 | 'icon' => 'Fts', |
||
395 | ], |
||
396 | ]; |
||
397 | |||
398 | $trail = $this->_getTrailsFromArray($trail, $table_view_matview_fts); |
||
399 | |||
400 | if (in_array($subject, ['table', 'view', 'matview', 'ftscfg'])) { |
||
401 | return $trail; |
||
402 | } |
||
403 | |||
404 | if (!is_null($subject)) { |
||
405 | $trail = $this->_getLastTrailPart($subject, $trail); |
||
406 | } |
||
407 | |||
408 | //$this->prtrace($trail); |
||
409 | |||
410 | return $trail; |
||
411 | } |
||
412 | |||
413 | private function _getTrailsFromArray($trail, $the_array) |
||
414 | { |
||
415 | foreach ($the_array as $key => $value) { |
||
416 | if (isset($_REQUEST[$key])) { |
||
417 | $trail[$key] = [ |
||
418 | 'title' => $value['title'], |
||
419 | 'text' => $_REQUEST[$key], |
||
420 | 'url' => $this->getHREFSubject($value['subject']), |
||
421 | 'help' => $value['help'], |
||
422 | 'icon' => $value['icon'], |
||
423 | ]; |
||
424 | break; |
||
425 | } |
||
426 | } |
||
427 | return $trail; |
||
428 | } |
||
429 | |||
430 | private function _getLastTrailPart($subject, $trail) |
||
501 | } |
||
502 | } |
||
503 |