| Total Complexity | 47 |
| Total Lines | 527 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseController 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 BaseController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class BaseController |
||
| 15 | { |
||
| 16 | use \PHPPgAdmin\Traits\HelperTrait; |
||
| 17 | |||
| 18 | public $appLangFiles = []; |
||
| 19 | |||
| 20 | public $appThemes = []; |
||
| 21 | |||
| 22 | public $appName = ''; |
||
| 23 | |||
| 24 | public $appVersion = ''; |
||
| 25 | |||
| 26 | public $form = ''; |
||
| 27 | |||
| 28 | public $href = ''; |
||
| 29 | |||
| 30 | public $lang = []; |
||
| 31 | |||
| 32 | public $action = ''; |
||
| 33 | |||
| 34 | public $controller_name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Used. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $view_name; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Used to print the title passing its value to $lang. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public $controller_title = 'base'; |
||
| 49 | |||
| 50 | public $msg = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \PHPPgAdmin\ViewManager |
||
| 54 | */ |
||
| 55 | public $view; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \PHPPgAdmin\Misc |
||
| 59 | */ |
||
| 60 | public $misc; |
||
| 61 | |||
| 62 | public $conf; |
||
| 63 | |||
| 64 | public $phpMinVer; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \PHPPgAdmin\ContainerUtils |
||
| 68 | */ |
||
| 69 | protected $container; |
||
| 70 | |||
| 71 | protected $script; |
||
| 72 | |||
| 73 | protected $data; |
||
| 74 | |||
| 75 | protected $database; |
||
| 76 | |||
| 77 | protected $server_id; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var XHtml\HTMLTableController |
||
| 81 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 82 | */ |
||
| 83 | protected $_table_controller; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var XHtml\HTMLFooterController |
||
| 87 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 88 | */ |
||
| 89 | protected $_footer_controller; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var XHtml\HTMLHeaderController |
||
| 93 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 94 | */ |
||
| 95 | protected $_header_controller; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var XHtml\HTMLNavbarController |
||
| 99 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 100 | */ |
||
| 101 | protected $_trail_controller; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var TreeController |
||
| 105 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 106 | */ |
||
| 107 | protected $_tree_controller; |
||
| 108 | |||
| 109 | protected $scripts = ''; |
||
| 110 | |||
| 111 | protected $no_db_connection = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructs the base controller (common for almost all controllers). |
||
| 115 | * |
||
| 116 | * @param \PHPPgAdmin\ContainerUtils $container the $app container |
||
| 117 | * @param bool $no_db_connection [optional] if true, sets $this->misc->setNoDBConnection(true); |
||
| 118 | */ |
||
| 119 | public function __construct(\PHPPgAdmin\ContainerUtils $container) |
||
| 120 | { |
||
| 121 | $this->container = $container; |
||
| 122 | $this->lang = $container->get('lang'); |
||
| 123 | |||
| 124 | $this->controller_name = \str_replace(__NAMESPACE__ . '\\', '', \get_class($this)); |
||
| 125 | $this->view_name = \str_replace('controller', '', \mb_strtolower($this->controller_name)); |
||
| 126 | $this->script = $this->view_name; |
||
| 127 | |||
| 128 | $this->view = $container->get('view'); |
||
| 129 | |||
| 130 | $this->msg = $container->get('msg'); |
||
| 131 | $this->appLangFiles = $container->get('appLangFiles'); |
||
| 132 | |||
| 133 | $this->misc = $container->get('misc'); |
||
| 134 | $this->conf = $this->misc->getConf(); |
||
| 135 | |||
| 136 | $this->appThemes = $container->get('appThemes'); |
||
| 137 | $this->action = $container->get('action'); |
||
| 138 | |||
| 139 | $this->appName = $container->get('settings')['appName']; |
||
| 140 | $this->appVersion = $container->get('settings')['appVersion']; |
||
| 141 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
| 142 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
| 143 | |||
| 144 | $msg = $container->get('msg'); |
||
| 145 | |||
| 146 | if (true === $this->no_db_connection) { |
||
| 147 | $this->misc->setNoDBConnection(true); |
||
| 148 | } |
||
| 149 | |||
| 150 | if (false === $this->misc->getNoDBConnection()) { |
||
| 151 | if (null === $this->misc->getServerId()) { |
||
| 152 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container); |
||
| 153 | |||
| 154 | $servers_controller->render(); |
||
| 155 | } else { |
||
| 156 | $_server_info = $this->misc->getServerInfo(); |
||
| 157 | // Redirect to the login form if not logged in |
||
| 158 | if (!isset($_server_info['username'])) { |
||
| 159 | $msg = \sprintf($this->lang['strlogoutmsg'], $_server_info['desc']); |
||
| 160 | |||
| 161 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container); |
||
| 162 | |||
| 163 | $servers_controller->render(); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Default method to render the controller according to the action parameter. It should return with a PSR |
||
| 171 | * responseObject but it prints texts whatsoeever. |
||
| 172 | * |
||
| 173 | * @return string|void |
||
| 174 | */ |
||
| 175 | public function render() |
||
| 176 | { |
||
| 177 | $this->misc = $this->misc; |
||
| 178 | |||
| 179 | $this->printHeader(); |
||
| 180 | $this->printBody(); |
||
| 181 | |||
| 182 | switch ($this->action) { |
||
| 183 | default: |
||
| 184 | $this->doDefault(); |
||
| 185 | |||
| 186 | break; |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->printFooter(); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function doDefault() |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the page title for each controller. |
||
| 202 | * |
||
| 203 | * @param string $title The title |
||
| 204 | * @param string $prefix The prefix |
||
| 205 | * @param string $suffix The suffix |
||
| 206 | * |
||
| 207 | * @return string the page title |
||
| 208 | */ |
||
| 209 | public function headerTitle($title = '', $prefix = '', $suffix = '') |
||
| 210 | { |
||
| 211 | $title = $title ? $title : $this->controller_title; |
||
| 212 | |||
| 213 | return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : ''); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getContainer() |
||
| 217 | { |
||
| 218 | return $this->container; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Display a table of data. |
||
| 223 | * |
||
| 224 | * @param \ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted |
||
| 225 | * @param array $columns An associative array of columns to be displayed: |
||
| 226 | * @param array $actions Actions that can be performed on each object: |
||
| 227 | * @param string $place Place where the $actions are displayed. Like 'display-browse', |
||
| 228 | * @param string $nodata (optional) Message to display if data set is empty |
||
| 229 | * @param callable $pre_fn (optional) callback closure for each row |
||
| 230 | * |
||
| 231 | * @return string the html of the table |
||
| 232 | */ |
||
| 233 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null) |
||
| 234 | { |
||
| 235 | $html_table = $this->_getTableController(); |
||
| 236 | |||
| 237 | $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
| 238 | |||
| 239 | return $html_table->printTable(); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Hides or show tree tabs according to their properties. |
||
| 244 | * |
||
| 245 | * @param array $tabs The tabs |
||
| 246 | * |
||
| 247 | * @return \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet filtered tabs in the form of an ArrayRecordSet |
||
| 248 | */ |
||
| 249 | public function adjustTabsForTree(&$tabs) |
||
| 250 | { |
||
| 251 | $tree = $this->_getTreeController(); |
||
| 252 | |||
| 253 | return $tree->adjustTabsForTree($tabs); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Produce JSON data for the browser tree. |
||
| 258 | * |
||
| 259 | * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $_treedata a set of records to populate the tree |
||
| 260 | * @param array $attrs Attributes for tree items |
||
| 261 | * @param string $section The section where the branch is linked in the tree |
||
| 262 | * @param bool $print either to return or echo the result |
||
| 263 | * |
||
| 264 | * @return \Slim\Http\Response|string the json rendered tree |
||
| 265 | */ |
||
| 266 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
| 267 | { |
||
| 268 | $tree = $this->_getTreeController(); |
||
| 269 | |||
| 270 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
|
|
|||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Prints a trail. |
||
| 275 | * |
||
| 276 | * @param array|string $trail |
||
| 277 | * @param bool $do_print The do print |
||
| 278 | * |
||
| 279 | * @return string ( description_of_the_return_value ) |
||
| 280 | */ |
||
| 281 | public function printTrail($trail = [], bool $do_print = true) |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param (array|mixed)[][] $navlinks |
||
| 291 | * @param string $place |
||
| 292 | * @param array $env |
||
| 293 | * @param mixed $do_print |
||
| 294 | */ |
||
| 295 | public function printNavLinks(array $navlinks, string $place, array $env = [], $do_print = true) |
||
| 296 | { |
||
| 297 | $from = __METHOD__; |
||
| 298 | $footer_controller = $this->_getFooterController(); |
||
| 299 | |||
| 300 | return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
| 301 | } |
||
| 302 | |||
| 303 | public function printTabs(string $tabs, string $activetab, bool $do_print = true) |
||
| 304 | { |
||
| 305 | $from = __METHOD__; |
||
| 306 | $html_trail = $this->_getNavbarController(); |
||
| 307 | |||
| 308 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param true $do_print |
||
| 313 | * @param null|string $from |
||
| 314 | * @param mixed $link |
||
| 315 | */ |
||
| 316 | public function printLink($link, bool $do_print = true, ?string $from = null) |
||
| 317 | { |
||
| 318 | if (null === $from) { |
||
| 319 | $from = __METHOD__; |
||
| 320 | } |
||
| 321 | |||
| 322 | $html_trail = $this->_getNavbarController(); |
||
| 323 | |||
| 324 | return $html_trail->printLink($link, $do_print, $from); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param true $flag |
||
| 329 | */ |
||
| 330 | public function setReloadDropDatabase(bool $flag) |
||
| 331 | { |
||
| 332 | $footer_controller = $this->_getFooterController(); |
||
| 333 | |||
| 334 | return $footer_controller->setReloadDropDatabase($flag); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param true $flag |
||
| 339 | */ |
||
| 340 | public function setNoBottomLink(bool $flag) |
||
| 341 | { |
||
| 342 | $footer_controller = $this->_getFooterController(); |
||
| 343 | |||
| 344 | return $footer_controller->setNoBottomLink($flag); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function printFooter(bool $doBody = true, string $template = 'footer.twig') |
||
| 348 | { |
||
| 349 | $footer_controller = $this->_getFooterController(); |
||
| 350 | |||
| 351 | return $footer_controller->printFooter($doBody, $template); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function printReload($database, $do_print = true) |
||
| 355 | { |
||
| 356 | $footer_controller = $this->_getFooterController(); |
||
| 357 | |||
| 358 | return $footer_controller->printReload($database, $do_print); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Outputs JavaScript to set default focus. |
||
| 363 | * |
||
| 364 | * @param mixed $object eg. forms[0].username |
||
| 365 | */ |
||
| 366 | public function setFocus($object) |
||
| 367 | { |
||
| 368 | $footer_controller = $this->_getFooterController(); |
||
| 369 | |||
| 370 | return $footer_controller->setFocus($object); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Outputs JavaScript to set the name of the browser window. |
||
| 375 | * |
||
| 376 | * @param string $name the window name |
||
| 377 | * @param bool $addServer if true (default) then the server id is |
||
| 378 | * attached to the name |
||
| 379 | */ |
||
| 380 | public function setWindowName($name, $addServer = true) |
||
| 381 | { |
||
| 382 | $footer_controller = $this->_getFooterController(); |
||
| 383 | |||
| 384 | return $footer_controller->setWindowName($name, $addServer); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param true $flag |
||
| 389 | */ |
||
| 390 | public function setNoOutput(bool $flag) |
||
| 391 | { |
||
| 392 | $header_controller = $this->_getHeaderController(); |
||
| 393 | |||
| 394 | return $header_controller->setNoOutput((bool) $flag); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param null|string $script |
||
| 399 | * @param string $title |
||
| 400 | * @param bool $do_print |
||
| 401 | * @param string $template |
||
| 402 | */ |
||
| 403 | public function printHeader(string $title = '', ?string $script = null, bool $do_print = true, string $template = 'header.twig') |
||
| 409 | } |
||
| 410 | |||
| 411 | public function printBody(bool $doBody = true, string $bodyClass = 'detailbody', bool $onloadInit = false) |
||
| 412 | { |
||
| 413 | $header_controller = $this->_getHeaderController(); |
||
| 414 | |||
| 415 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param null|string $help |
||
| 420 | * @param string $title |
||
| 421 | * @param bool $do_print |
||
| 422 | */ |
||
| 423 | public function printTitle(string $title, ?string $help = null, bool $do_print = true) |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $key |
||
| 432 | * @param null|string $default |
||
| 433 | */ |
||
| 434 | public function getRequestParam(string $key, ?string $default = null) |
||
| 435 | { |
||
| 436 | return \requestInstance()->getParam($key, $default); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $key |
||
| 441 | * @param null|array|bool|float|int|string $default |
||
| 442 | * |
||
| 443 | * @return bool| null|array|string|int|float |
||
| 444 | */ |
||
| 445 | public function getPostParam(string $key, $default = null) |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $key |
||
| 452 | * @param null|array|float|int|string $default |
||
| 453 | * |
||
| 454 | * @return null|array|float|int|string |
||
| 455 | */ |
||
| 456 | public function getQueryStrinParam($key, $default = null) |
||
| 457 | { |
||
| 458 | return \requestInstance()->getQueryParam($key, $default); |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | public function getAllParams(): array |
||
| 465 | { |
||
| 466 | return \array_merge( |
||
| 467 | \requestInstance()->getQueryParams() ?? [], |
||
| 468 | \requestInstance()->getParsedBody() ?? [] |
||
| 469 | ); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Print out a message. |
||
| 474 | * |
||
| 475 | * @param string $msg The message |
||
| 476 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
| 477 | * |
||
| 478 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
| 479 | */ |
||
| 480 | public function printMsg($msg, $do_print = true) |
||
| 481 | { |
||
| 482 | $html = ''; |
||
| 483 | $msg = \htmlspecialchars(\PHPPgAdmin\ContainerUtils::br2ln($msg)); |
||
| 484 | |||
| 485 | if ('' !== $msg) { |
||
| 486 | $html .= '<p class="message">' . \nl2br($msg) . '</p>' . \PHP_EOL; |
||
| 487 | } |
||
| 488 | |||
| 489 | if ($do_print) { |
||
| 490 | echo $html; |
||
| 491 | |||
| 492 | return $html; |
||
| 493 | } |
||
| 494 | |||
| 495 | return $html; |
||
| 496 | } |
||
| 497 | |||
| 498 | private function _getTableController() |
||
| 499 | { |
||
| 500 | if (null === $this->_table_controller) { |
||
| 501 | $this->_table_controller = new XHtml\HTMLTableController($this->getContainer(), $this->controller_name); |
||
| 502 | } |
||
| 503 | |||
| 504 | return $this->_table_controller; |
||
| 505 | } |
||
| 506 | |||
| 507 | private function _getFooterController() |
||
| 508 | { |
||
| 509 | if (null === $this->_footer_controller) { |
||
| 510 | $this->_footer_controller = new XHtml\HTMLFooterController($this->getContainer(), $this->controller_name); |
||
| 511 | } |
||
| 512 | |||
| 513 | return $this->_footer_controller; |
||
| 514 | } |
||
| 515 | |||
| 516 | private function _getHeaderController() |
||
| 523 | } |
||
| 524 | |||
| 525 | private function _getNavbarController() |
||
| 526 | { |
||
| 527 | if (null === $this->_trail_controller) { |
||
| 528 | $this->_trail_controller = new XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name); |
||
| 529 | } |
||
| 530 | |||
| 531 | return $this->_trail_controller; |
||
| 532 | } |
||
| 533 | |||
| 534 | private function _getTreeController() |
||
| 541 | } |
||
| 542 | } |
||
| 543 |