| Total Complexity | 46 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ContainerUtils 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 ContainerUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ContainerUtils |
||
| 18 | { |
||
| 19 | use \PHPPgAdmin\Traits\HelperTrait; |
||
| 20 | |||
| 21 | protected $container; |
||
| 22 | /** @var Connector */ |
||
| 23 | protected static $instance; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor of the ContainerUtils class. |
||
| 27 | * |
||
| 28 | * @param \Slim\Container $container The app container |
||
| 29 | */ |
||
| 30 | public function __construct() |
||
| 78 | } |
||
| 79 | |||
| 80 | public static function createContainer() |
||
| 87 | } |
||
| 88 | |||
| 89 | public function icon($icon) |
||
| 90 | { |
||
| 91 | |||
| 92 | $conf = $this->container->conf; |
||
| 93 | if (is_string($icon)) { |
||
| 94 | $path = "/assets/images/themes/{$conf['theme']}/{$icon}"; |
||
| 95 | if (file_exists(\BASE_PATH . $path . '.png')) { |
||
| 96 | return SUBFOLDER . $path . '.png'; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (file_exists(\BASE_PATH . $path . '.gif')) { |
||
| 100 | return SUBFOLDER . $path . '.gif'; |
||
| 101 | } |
||
| 102 | |||
| 103 | if (file_exists(\BASE_PATH . $path . '.ico')) { |
||
| 104 | return SUBFOLDER . $path . '.ico'; |
||
| 105 | } |
||
| 106 | |||
| 107 | $path = "/assets/images/themes/default/{$icon}"; |
||
| 108 | if (file_exists(\BASE_PATH . $path . '.png')) { |
||
| 109 | return SUBFOLDER . $path . '.png'; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (file_exists(\BASE_PATH . $path . '.gif')) { |
||
| 113 | return SUBFOLDER . $path . '.gif'; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (file_exists(\BASE_PATH . $path . '.ico')) { |
||
| 117 | return SUBFOLDER . $path . '.ico'; |
||
| 118 | } |
||
| 119 | } else { |
||
| 120 | // Icon from plugins |
||
| 121 | $path = "/plugins/{$icon[0]}/images/{$icon[1]}"; |
||
| 122 | if (file_exists(\BASE_PATH . $path . '.png')) { |
||
| 123 | return SUBFOLDER . $path . '.png'; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (file_exists(\BASE_PATH . $path . '.gif')) { |
||
| 127 | return SUBFOLDER . $path . '.gif'; |
||
| 128 | } |
||
| 129 | |||
| 130 | if (file_exists(\BASE_PATH . $path . '.ico')) { |
||
| 131 | return SUBFOLDER . $path . '.ico'; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return ''; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function maybeRenderIframes($response, $subject, $query_string) |
||
| 139 | { |
||
| 140 | $c = $this->container; |
||
| 141 | $in_test = $c->view->offsetGet('in_test'); |
||
| 142 | |||
| 143 | if ($in_test === '1') { |
||
| 144 | $className = '\PHPPgAdmin\Controller\\' . ucfirst($subject) . 'Controller'; |
||
| 145 | $controller = new $className($c); |
||
| 146 | |||
| 147 | return $controller->render(); |
||
| 148 | } |
||
| 149 | $viewVars = [ |
||
| 150 | 'icon' => [ |
||
| 151 | 'Refresh' => $this->icon('Refresh'), |
||
| 152 | 'Servers' => $this->icon('Servers'), |
||
| 153 | ], |
||
| 154 | 'url' => '/src/views/' . $subject . ($query_string ? '?' . $query_string : ''), |
||
| 155 | 'headertemplate' => 'header.twig', |
||
| 156 | ]; |
||
| 157 | |||
| 158 | return $c->view->render($response, 'main.twig', $viewVars); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the theme from |
||
| 163 | * 1. The $_REQUEST global (when it's chosen from start screen) |
||
| 164 | * 2. Server specific config theme |
||
| 165 | * 3.- $_SESSION global (subsequent requests after 1.) |
||
| 166 | * 4.- $_COOKIE global (mostly fallback for $_SESSION after 1.- and 3.-) |
||
| 167 | * 5.- theme as set in config |
||
| 168 | * 6.- 'default' theme. |
||
| 169 | * |
||
| 170 | * @param <type> $conf The conf |
||
| 171 | * @param null|mixed $_server_info |
||
| 172 | * |
||
| 173 | * @return string the theme |
||
| 174 | */ |
||
| 175 | public function getTheme(array $conf, $_server_info = null) |
||
| 176 | { |
||
| 177 | $_theme = null; |
||
| 178 | // List of themes |
||
| 179 | $themefolders = $this->getThemeFolders(); |
||
| 180 | // Check if theme is in $_REQUEST, $_SESSION or $_COOKIE |
||
| 181 | // 1.- First priority: $_REQUEST, this happens when you use the selector |
||
| 182 | if (array_key_exists('theme', $_REQUEST) && |
||
| 183 | array_key_exists($_REQUEST['theme'], $themefolders)) { |
||
| 184 | $_theme = $_REQUEST['theme']; |
||
| 185 | } elseif ( // otherwise, see if there's a theme associated with this particular server |
||
| 186 | !is_null($_server_info) && |
||
| 187 | array_key_exists('theme', $_server_info) && |
||
| 188 | is_string($_server_info['theme']) && |
||
| 189 | array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
| 190 | $_theme = $_server_info['theme']; |
||
| 191 | } elseif (array_key_exists('ppaTheme', $_SESSION) && |
||
| 192 | array_key_exists($_SESSION['ppaTheme'], $themefolders)) { |
||
| 193 | // otherwise check $_SESSION |
||
| 194 | $_theme = $_SESSION['ppaTheme']; |
||
| 195 | } elseif (array_key_exists('ppaTheme', $_SESSION) && |
||
| 196 | array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
| 197 | // oterwise check $_COOKIE |
||
| 198 | $_theme = $_COOKIE['ppaTheme']; |
||
| 199 | } elseif ( // see if there's a valid theme set in config file |
||
| 200 | array_key_exists('theme', $conf) && |
||
| 201 | is_string($conf['theme']) && |
||
| 202 | array_key_exists($conf['theme'], $themefolders)) { |
||
| 203 | $_theme = $conf['theme']; |
||
| 204 | } else { |
||
| 205 | // okay then, use default theme |
||
| 206 | $_theme = 'default'; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $_theme; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Traverse THEME_PATH, consider as theme folders those which |
||
| 214 | * contain a `global.css` stylesheet. |
||
| 215 | * |
||
| 216 | * @return array the theme folders |
||
| 217 | */ |
||
| 218 | private function getThemeFolders() |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Determines the redirection url according to query string. |
||
| 250 | * |
||
| 251 | * @return string the redirect url |
||
| 252 | */ |
||
| 253 | public function getRedirectUrl() |
||
| 254 | { |
||
| 255 | $query_string = $this->container->requestobj->getUri()->getQuery(); |
||
| 256 | |||
| 257 | // if server_id isn't set, then you will be redirected to intro |
||
| 258 | if ($this->container->requestobj->getQueryParam('server') === null) { |
||
| 259 | $destinationurl = \SUBFOLDER . '/src/views/intro'; |
||
| 260 | } else { |
||
| 261 | // otherwise, you'll be redirected to the login page for that server; |
||
| 262 | $destinationurl = \SUBFOLDER . '/src/views/login' . ($query_string ? '?' . $query_string : ''); |
||
| 263 | } |
||
| 264 | |||
| 265 | return $destinationurl; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets the destination with the last active tab selected for that controller |
||
| 270 | * Usually used after going through a redirect route. |
||
| 271 | * |
||
| 272 | * @param string $subject The subject, usually a view name like 'server' or 'table' |
||
| 273 | * |
||
| 274 | * @return string The destination url with last tab set in the query string |
||
| 275 | */ |
||
| 276 | public function getDestinationWithLastTab($subject) |
||
| 277 | { |
||
| 278 | $_server_info = $this->container->misc->getServerInfo(); |
||
| 279 | $this->addFlash($subject, 'getDestinationWithLastTab'); |
||
| 280 | //$this->prtrace('$_server_info', $_server_info); |
||
| 281 | // If username isn't set in server_info, you should login |
||
| 282 | if (!isset($_server_info['username'])) { |
||
| 283 | $destinationurl = $this->getRedirectUrl(); |
||
| 284 | } else { |
||
| 285 | $url = $this->container->misc->getLastTabURL($subject); |
||
| 286 | $this->addFlash($url, 'getLastTabURL for ' . $subject); |
||
| 287 | // Load query vars into superglobal arrays |
||
| 288 | if (isset($url['urlvars'])) { |
||
| 289 | $urlvars = []; |
||
| 290 | foreach ($url['urlvars'] as $key => $urlvar) { |
||
| 291 | //$this->prtrace($key, $urlvar); |
||
| 292 | $urlvars[$key] = \PHPPgAdmin\Decorators\Decorator::get_sanitized_value($urlvar, $_REQUEST); |
||
| 293 | } |
||
| 294 | $_REQUEST = array_merge($_REQUEST, $urlvars); |
||
| 295 | $_GET = array_merge($_GET, $urlvars); |
||
| 296 | } |
||
| 297 | |||
| 298 | $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); |
||
| 299 | $destinationurl = $actionurl->value($_GET); |
||
| 300 | } |
||
| 301 | $destinationurl = str_replace('views/?', "views/{$subject}?", $destinationurl); |
||
| 302 | //$this->prtrace('destinationurl for ' . $subject, $destinationurl); |
||
| 303 | return $destinationurl; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Adds an error to the errors array property of the container. |
||
| 308 | * |
||
| 309 | * @param string $errormsg The error msg |
||
| 310 | * |
||
| 311 | * @return \Slim\Container The app container |
||
| 312 | */ |
||
| 313 | public function addError($errormsg) |
||
| 320 | } |
||
| 321 | } |
||
| 322 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths