| Total Complexity | 71 |
| Total Lines | 467 |
| 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 | /** |
||
| 17 | * Display a bread crumb trail. |
||
| 18 | * |
||
| 19 | * @param array|string $trail an array of breadcrumb items, or a string to identify one of them |
||
| 20 | * @param $do_print true to echo, false to return html |
||
| 21 | * @param null|string $from |
||
| 22 | */ |
||
| 23 | public function printTrail($trail = [], $do_print = true, $from = null) |
||
| 24 | { |
||
| 25 | if (null === $from) { |
||
| 26 | $from = __METHOD__; |
||
| 27 | } |
||
| 28 | $lang = $this->lang; |
||
| 29 | $this->misc = $this->misc; |
||
| 30 | $data = $this->misc->getDatabaseAccessor(); |
||
| 31 | |||
| 32 | $trail_html = $this->printTopbar(false, $from); |
||
| 33 | |||
| 34 | if (is_string($trail)) { |
||
| 35 | $trail = $this->getTrail($trail); |
||
| 36 | } |
||
| 37 | |||
| 38 | $crumbs = []; |
||
| 39 | foreach ($trail as $crumb_id => $crumb) { |
||
| 40 | if (isset($crumb['url'])) { |
||
| 41 | $crumbs[$crumb_id]['url'] = str_replace('&', '&', $crumb['url']); |
||
| 42 | } |
||
| 43 | |||
| 44 | if (isset($crumb['title'])) { |
||
| 45 | $crumbs[$crumb_id]['title'] = $crumb['title']; |
||
| 46 | $crumbs[$crumb_id]['iconalt'] = $crumb['title']; |
||
| 47 | } else { |
||
| 48 | $crumbs[$crumb_id]['iconalt'] = 'Database Root'; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (isset($crumb['icon']) && $icon = $this->misc->icon($crumb['icon'])) { |
||
| 52 | $crumbs[$crumb_id]['icon'] = $icon; |
||
| 53 | } |
||
| 54 | |||
| 55 | $crumbs[$crumb_id]['text'] = $crumb['text']; |
||
| 56 | |||
| 57 | if (isset($crumb['help'])) { |
||
| 58 | $crumbs[$crumb_id]['helpurl'] = str_replace('&', '&', $this->misc->getHelpLink($crumb['help'])); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $viewVars = [ |
||
| 63 | 'crumbs' => $crumbs, |
||
| 64 | |||
| 65 | 'controller_name' => $this->controller_name, |
||
| 66 | ]; |
||
| 67 | if (isset($crumbs['database'])) { |
||
| 68 | $search_path_crumbs = []; |
||
| 69 | $dburl = $crumbs['database']['url']; |
||
| 70 | $search_paths = $data->getSearchPath(); |
||
| 71 | foreach ($search_paths as $schema) { |
||
| 72 | $search_path_crumbs[$schema] = [ |
||
| 73 | 'title' => $lang['strschema'], |
||
| 74 | 'text' => $schema, |
||
| 75 | 'icon' => $this->misc->icon('Schema'), |
||
| 76 | 'iconalt' => $lang['strschema'], |
||
| 77 | 'url' => str_replace(['&', 'redirect/database'], ['&', 'redirect/schema'], $dburl.'&schema='.$schema), |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | $viewVars['search_paths'] = $search_path_crumbs; |
||
| 81 | } |
||
| 82 | //\Kint::dump($viewVars); |
||
| 83 | |||
| 84 | $trail_html .= $this->getContainer()->view->fetch('components/trail.twig', $viewVars); |
||
| 85 | |||
| 86 | if ($do_print) { |
||
| 87 | echo $trail_html; |
||
| 88 | } else { |
||
| 89 | return $trail_html; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Display navigation tabs. |
||
| 95 | * |
||
| 96 | * @param string $alltabs The name of current section (Ex: intro, server, ...), |
||
| 97 | * or an array with tabs (Ex: sqledit::doFind function) |
||
| 98 | * @param string $activetab the name of the tab to be highlighted |
||
| 99 | * @param bool $print if false, return html |
||
|
|
|||
| 100 | * @param bool $do_print true to print html, false to return html |
||
| 101 | * @param null|string $from whichi method is calling this one |
||
| 102 | */ |
||
| 103 | public function printTabs($alltabs, $activetab, $do_print = true, $from = null) |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * [printTopbar description]. |
||
| 161 | * |
||
| 162 | * @param bool $do_print true to print, false to return html |
||
| 163 | * @param null|mixed $from which method is calling this one |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | private function printTopbar($do_print = true, $from = null) |
||
|
1 ignored issue
–
show
|
|||
| 168 | { |
||
| 169 | if (null === $from || false === $from) { |
||
| 170 | $from = __METHOD__; |
||
| 171 | } |
||
| 172 | |||
| 173 | $lang = $this->lang; |
||
| 174 | $plugin_manager = $this->plugin_manager; |
||
| 175 | $this->misc = $this->misc; |
||
| 176 | $appName = $this->misc->appName; |
||
| 177 | $appVersion = $this->misc->appVersion; |
||
| 178 | |||
| 179 | $server_info = $this->misc->getServerInfo(); |
||
| 180 | $server_id = $this->misc->getServerId(); |
||
|
1 ignored issue
–
show
|
|||
| 181 | $reqvars = $this->misc->getRequestVars('table'); |
||
| 182 | |||
| 183 | $topbar_html = '<div class="topbar" data-controller="'.$this->controller_name.'"><table style="width: 100%"><tr><td>'; |
||
| 184 | |||
| 185 | if ($server_info && isset($server_info['platform'], $server_info['username'])) { |
||
| 186 | // top left informations when connected |
||
| 187 | $topbar_html .= sprintf( |
||
| 188 | $lang['strtopbar'], |
||
| 189 | '<span class="platform">'.htmlspecialchars($server_info['platform']).'</span>', |
||
| 190 | '<span class="host">'.htmlspecialchars((empty($server_info['host'])) ? 'localhost' : $server_info['host']).'</span>', |
||
| 191 | '<span class="port">'.htmlspecialchars($server_info['port']).'</span>', |
||
| 192 | '<span class="username">'.htmlspecialchars($server_info['username']).'</span>' |
||
| 193 | ); |
||
| 194 | |||
| 195 | $topbar_html .= '</td>'; |
||
| 196 | |||
| 197 | // top right informations when connected |
||
| 198 | |||
| 199 | $toplinks = [ |
||
| 200 | 'sql' => [ |
||
| 201 | 'attr' => [ |
||
| 202 | 'href' => [ |
||
| 203 | 'url' => SUBFOLDER.'/src/views/sqledit', |
||
| 204 | 'urlvars' => array_merge($reqvars, [ |
||
| 205 | 'action' => 'sql', |
||
| 206 | ]), |
||
| 207 | ], |
||
| 208 | 'target' => 'sqledit', |
||
| 209 | 'id' => 'toplink_sql', |
||
| 210 | ], |
||
| 211 | 'content' => $lang['strsql'], |
||
| 212 | ], |
||
| 213 | 'history' => [ |
||
| 214 | 'attr' => [ |
||
| 215 | 'href' => [ |
||
| 216 | 'url' => SUBFOLDER.'/src/views/history', |
||
| 217 | 'urlvars' => array_merge($reqvars, [ |
||
| 218 | 'action' => 'pophistory', |
||
| 219 | ]), |
||
| 220 | ], |
||
| 221 | 'id' => 'toplink_history', |
||
| 222 | ], |
||
| 223 | 'content' => $lang['strhistory'], |
||
| 224 | ], |
||
| 225 | 'find' => [ |
||
| 226 | 'attr' => [ |
||
| 227 | 'href' => [ |
||
| 228 | 'url' => SUBFOLDER.'/src/views/sqledit', |
||
| 229 | 'urlvars' => array_merge($reqvars, [ |
||
| 230 | 'action' => 'find', |
||
| 231 | ]), |
||
| 232 | ], |
||
| 233 | 'target' => 'sqledit', |
||
| 234 | 'id' => 'toplink_find', |
||
| 235 | ], |
||
| 236 | 'content' => $lang['strfind'], |
||
| 237 | ], |
||
| 238 | 'logout' => [ |
||
| 239 | 'attr' => [ |
||
| 240 | 'href' => [ |
||
| 241 | 'url' => SUBFOLDER.'/src/views/servers', |
||
| 242 | 'urlvars' => [ |
||
| 243 | 'action' => 'logout', |
||
| 244 | 'logoutServer' => sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}"), |
||
| 245 | ], |
||
| 246 | ], |
||
| 247 | 'id' => 'toplink_logout', |
||
| 248 | ], |
||
| 249 | 'content' => $lang['strlogout'], |
||
| 250 | ], |
||
| 251 | ]; |
||
| 252 | |||
| 253 | // Toplink hook's place |
||
| 254 | $plugin_functions_parameters = [ |
||
| 255 | 'toplinks' => &$toplinks, |
||
| 256 | ]; |
||
| 257 | |||
| 258 | $plugin_manager->doHook('toplinks', $plugin_functions_parameters); |
||
| 259 | |||
| 260 | $topbar_html .= '<td style="text-align: right">'; |
||
| 261 | |||
| 262 | $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], false, $from); |
||
| 263 | |||
| 264 | $topbar_html .= '</td>'; |
||
| 265 | } else { |
||
| 266 | $topbar_html .= "<span class=\"appname\">{$appName}</span> <span class=\"version\">{$appVersion}</span>"; |
||
| 267 | } |
||
| 268 | |||
| 269 | $topbar_html .= "</tr></table></div>\n"; |
||
| 270 | |||
| 271 | if ($do_print) { |
||
| 272 | echo $topbar_html; |
||
| 273 | } else { |
||
| 274 | return $topbar_html; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | private function getHREFSubject($subject) |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Create a bread crumb trail of the object hierarchy. |
||
| 288 | * |
||
| 289 | * @param null|string $subject sunkect of the trail |
||
| 290 | */ |
||
| 291 | private function getTrail($subject = null) |
||
| 481 |