| Total Complexity | 83 |
| Total Lines | 451 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Bootstrap 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 Bootstrap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Bootstrap |
||
| 38 | { |
||
| 39 | /** @var array What is returned by the function getrusage. */ |
||
| 40 | protected $rusage_start = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Bootstrap constructor. |
||
| 44 | * |
||
| 45 | * @param bool $standalone |
||
| 46 | * - true to boot outside elkarte |
||
| 47 | * - false to bootstrap the main elkarte site. |
||
| 48 | */ |
||
| 49 | public function __construct($standalone = true) |
||
| 50 | { |
||
| 51 | // Bootstrap only once. |
||
| 52 | if (!defined('ELKBOOT')) |
||
| 53 | { |
||
| 54 | // We're going to set a few globals |
||
| 55 | global $time_start, $ssi_error_reporting, $db_show_debug; |
||
| 56 | |||
| 57 | // Your on the clock |
||
| 58 | $time_start = microtime(true); |
||
| 59 | |||
| 60 | // Unless settings.php tells us otherwise |
||
| 61 | $db_show_debug = false; |
||
| 62 | |||
| 63 | // Report errors but not depreciated ones |
||
| 64 | $ssi_error_reporting = error_reporting(E_ALL & ~E_DEPRECATED); |
||
| 65 | |||
| 66 | // Get the things needed for ALL modes |
||
| 67 | $this->bringUpBasics(); |
||
| 68 | |||
| 69 | // Going to run from the side entrance and not directly from inside elkarte |
||
| 70 | if ($standalone) |
||
| 71 | { |
||
| 72 | $this->ssi_main(); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Calls the various initialization functions in the needed order |
||
| 79 | */ |
||
| 80 | public function bringUpBasics() |
||
| 81 | { |
||
| 82 | $this->setConstants(); |
||
| 83 | $this->setRusage(); |
||
| 84 | $this->clearGlobals(); |
||
| 85 | $this->loadSettingsFile(); |
||
| 86 | $this->validatePaths(); |
||
| 87 | $this->loadDependants(); |
||
| 88 | $this->loadAutoloader(); |
||
| 89 | $this->checkMaintance(); |
||
| 90 | $this->setDebug(); |
||
| 91 | $this->bringUp(); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set the core constants, you know the ones we often forget to |
||
| 96 | * update on new releases. |
||
| 97 | */ |
||
| 98 | private function setConstants() |
||
| 99 | { |
||
| 100 | // First things first, but not necessarily in that order. |
||
| 101 | if (!defined('ELK')) |
||
| 102 | { |
||
| 103 | define('ELK', '1'); |
||
| 104 | } |
||
| 105 | |||
| 106 | define('ELKBOOT', '1'); |
||
| 107 | |||
| 108 | // The software version |
||
| 109 | define('FORUM_VERSION', 'ElkArte 2.0 dev'); |
||
| 110 | |||
| 111 | // Shortcut for the browser cache stale |
||
| 112 | define('CACHE_STALE', '?20dev'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get initial resource usage |
||
| 117 | */ |
||
| 118 | private function setRusage() |
||
| 119 | { |
||
| 120 | $this->rusage_start = getrusage(); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * If they glo, they need to be cleaned. |
||
| 125 | */ |
||
| 126 | private function clearGlobals() |
||
| 127 | { |
||
| 128 | // We don't need no globals. (a bug in "old" versions of PHP) |
||
| 129 | foreach (['db_character_set', 'cachedir'] as $variable) |
||
| 130 | { |
||
| 131 | if (isset($GLOBALS[$variable])) |
||
| 132 | { |
||
| 133 | unset($GLOBALS[$variable], $GLOBALS[$variable]); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Loads the settings values into the global space |
||
| 140 | */ |
||
| 141 | private function loadSettingsFile() |
||
| 142 | { |
||
| 143 | // All those wonderful things found in settings |
||
| 144 | global $maintenance, $mtitle, $msubject, $mmessage, $mbname, $language, $boardurl, $webmaster_email; |
||
| 145 | global $cookiename, $db_type, $db_server, $db_port, $db_name, $db_user, $db_passwd; |
||
| 146 | global $ssi_db_user, $ssi_db_passwd, $db_prefix, $db_persist, $db_error_send, $cache_accelerator; |
||
| 147 | global $cache_uid, $cache_password, $cache_enable, $cache_memcached, $db_show_debug, $url_format; |
||
| 148 | global $cachedir, $boarddir, $sourcedir, $extdir, $languagedir; |
||
| 149 | |||
| 150 | // Where the Settings.php file is located |
||
| 151 | $settings_loc = __DIR__ . '/Settings.php'; |
||
| 152 | |||
| 153 | // First thing: if the installation dir exists, just send anybody there |
||
| 154 | // The IGNORE_INSTALL_DIR constant is for developers only. Do not add it on production sites |
||
| 155 | if (file_exists('install') && (file_exists('install/install.php') || file_exists('install/upgrade.php'))) |
||
| 156 | { |
||
| 157 | if (file_exists($settings_loc)) |
||
| 158 | { |
||
| 159 | require_once($settings_loc); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!defined('IGNORE_INSTALL_DIR')) |
||
| 163 | { |
||
| 164 | $redirec_file = file_exists($settings_loc) && empty($_SESSION['installing']) ? 'upgrade.php' : 'install.php'; |
||
| 165 | |||
| 166 | // To early for constants or autoloader |
||
| 167 | require_once($boarddir . '/sources/ElkArte/Server.php'); |
||
| 168 | $server = new Server($_SERVER); |
||
| 169 | |||
| 170 | $version_running = str_replace('ElkArte ', '', FORUM_VERSION); |
||
| 171 | $location = $server->supportsSSL() ? 'https://' : 'http://'; |
||
| 172 | $location .= $server->getHost(); |
||
| 173 | $temp = preg_replace('~/' . preg_quote(basename($boardurl . '/index.php'), '~') . '(/.+)?$~', '', str_replace('\\', '/', dirname($_SERVER['PHP_SELF']))); |
||
| 174 | $location .= ($temp !== '/') ? $temp : ''; |
||
| 175 | |||
| 176 | // Too early to use Headers class etc. |
||
| 177 | header('Location:' . $location . '/install/' . $redirec_file . '?v=' . $version_running); |
||
| 178 | die(); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | else |
||
| 182 | { |
||
| 183 | require_once($settings_loc); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Validate the paths set in Settings.php, correct as needed and move them to constants. |
||
| 189 | */ |
||
| 190 | private function validatePaths() |
||
| 191 | { |
||
| 192 | global $boarddir, $sourcedir, $cachedir, $extdir, $languagedir; |
||
| 193 | |||
| 194 | // Make sure the paths are correct... at least try to fix them. |
||
| 195 | if (!file_exists($boarddir) && file_exists(__DIR__ . '/bootstrap.php')) |
||
| 196 | { |
||
| 197 | $boarddir = __DIR__; |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!file_exists($sourcedir . '/SiteDispatcher.class.php') && file_exists($boarddir . '/sources')) |
||
| 201 | { |
||
| 202 | $sourcedir = $boarddir . '/sources'; |
||
| 203 | } |
||
| 204 | |||
| 205 | // Check that directories which didn't exist in past releases are initialized. |
||
| 206 | if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache')) |
||
| 207 | { |
||
| 208 | $cachedir = $boarddir . '/cache'; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ((empty($extdir) || !file_exists($extdir)) && file_exists($sourcedir . '/ext')) |
||
| 212 | { |
||
| 213 | $extdir = $sourcedir . '/ext'; |
||
| 214 | } |
||
| 215 | |||
| 216 | if ((empty($languagedir) || !file_exists($languagedir)) && file_exists($sourcedir . '/Languages/Index')) |
||
| 217 | { |
||
| 218 | $languagedir = $sourcedir . '/ElkArte/Languages'; |
||
| 219 | } |
||
| 220 | |||
| 221 | // Time to forget about variables and go with constants! |
||
| 222 | define('BOARDDIR', $boarddir); |
||
| 223 | define('CACHEDIR', $cachedir); |
||
| 224 | define('EXTDIR', $extdir); |
||
| 225 | define('LANGUAGEDIR', $languagedir); |
||
| 226 | define('SOURCEDIR', $sourcedir); |
||
| 227 | define('ADMINDIR', $sourcedir . '/ElkArte/AdminController'); |
||
| 228 | define('CONTROLLERDIR', $sourcedir . '/ElkArte/Controller'); |
||
| 229 | define('SUBSDIR', $sourcedir . '/subs'); |
||
| 230 | define('ADDONSDIR', $boarddir . '/addons'); |
||
| 231 | unset($boarddir, $cachedir, $sourcedir, $languagedir, $extdir); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * We require access to several important files, so load them upfront |
||
| 236 | */ |
||
| 237 | private function loadDependants() |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * The autoloader will take care most requests for files |
||
| 251 | */ |
||
| 252 | private function loadAutoloader() |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Check if we are in maintenance mode, if so end here. |
||
| 264 | */ |
||
| 265 | private function checkMaintance() |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * If you like lots of debug information in error messages and below the footer |
||
| 290 | * then set $db_show_debug to true in settings. Don't do this on a production site. |
||
| 291 | */ |
||
| 292 | private function setDebug() |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Time to see what has been requested, by whom and dispatch it to the proper handler |
||
| 306 | */ |
||
| 307 | private function bringUp() |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * If you are running SSI standalone, you need to call this function after bootstrap is |
||
| 335 | * initialized. |
||
| 336 | */ |
||
| 337 | public function ssi_main() |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Used to ensure SSI requests are valid and not a probing attempt |
||
| 474 | */ |
||
| 475 | private function _validRequestCheck() |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } |
||
| 491 |
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