| Total Complexity | 87 |
| Total Lines | 444 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 34 | class Bootstrap |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * What is returned by the function getrusage. |
||
| 38 | * |
||
| 39 | * @var mixed[] |
||
| 40 | */ |
||
| 41 | protected $rusage_start = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Bootstrap constructor. |
||
| 45 | * |
||
| 46 | * @param bool $standalone |
||
| 47 | * - true to boot outside of elkarte |
||
| 48 | * - false to bootstrap the main elkarte site. |
||
| 49 | * @throws \ElkArte\Exceptions\Exception |
||
| 50 | */ |
||
| 51 | public function __construct($standalone = true) |
||
| 52 | { |
||
| 53 | // Bootstrap only once. |
||
| 54 | if (!defined('ELKBOOT')) |
||
| 55 | { |
||
| 56 | // We're going to set a few globals |
||
| 57 | global $time_start, $ssi_error_reporting, $db_show_debug; |
||
| 58 | |||
| 59 | // Your on the clock |
||
| 60 | $time_start = microtime(true); |
||
| 61 | |||
| 62 | // Unless settings.php tells us otherwise |
||
| 63 | $db_show_debug = false; |
||
| 64 | |||
| 65 | // Report errors but not depreciated ones |
||
| 66 | $ssi_error_reporting = error_reporting(E_ALL & ~E_DEPRECATED); |
||
| 67 | |||
| 68 | // Get the things needed for ALL modes |
||
| 69 | $this->bringUpBasics(); |
||
| 70 | |||
| 71 | // Going to run from the side entrance and not directly from inside elkarte |
||
| 72 | if ($standalone) |
||
| 73 | { |
||
| 74 | $this->ssi_main(); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Calls the various initialization functions in the needed order |
||
| 81 | */ |
||
| 82 | public function bringUpBasics() |
||
| 83 | { |
||
| 84 | $this->setConstants(); |
||
| 85 | $this->setRusage(); |
||
| 86 | $this->clearGlobals(); |
||
| 87 | $this->loadSettingsFile(); |
||
| 88 | $this->validatePaths(); |
||
| 89 | $this->loadDependants(); |
||
| 90 | $this->loadAutoloader(); |
||
| 91 | $this->checkMaintance(); |
||
| 92 | $this->setDebug(); |
||
| 93 | $this->bringUp(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set the core constants, you know the ones we often forget to |
||
| 98 | * update on new releases. |
||
| 99 | */ |
||
| 100 | private function setConstants() |
||
| 101 | { |
||
| 102 | // First things first, but not necessarily in that order. |
||
| 103 | if (!defined('ELK')) |
||
| 104 | { |
||
| 105 | define('ELK', '1'); |
||
| 106 | } |
||
| 107 | define('ELKBOOT', '1'); |
||
| 108 | |||
| 109 | // The software version |
||
| 110 | define('FORUM_VERSION', 'ElkArte 2.0 dev'); |
||
| 111 | |||
| 112 | // Shortcut for the browser cache stale |
||
| 113 | define('CACHE_STALE', '?20dev'); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get initial resource usage |
||
| 118 | */ |
||
| 119 | private function setRusage() |
||
| 120 | { |
||
| 121 | $this->rusage_start = getrusage(); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * If they glo, they need to be cleaned. |
||
| 126 | */ |
||
| 127 | private function clearGlobals() |
||
| 128 | { |
||
| 129 | // We don't need no globals. (a bug in "old" versions of PHP) |
||
| 130 | foreach (array('db_character_set', 'cachedir') as $variable) |
||
| 131 | { |
||
| 132 | if (isset($GLOBALS[$variable])) |
||
| 133 | { |
||
| 134 | unset($GLOBALS[$variable], $GLOBALS[$variable]); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Loads the settings values into the global space |
||
| 141 | */ |
||
| 142 | private function loadSettingsFile() |
||
| 143 | { |
||
| 144 | // All those wonderful things found in settings |
||
| 145 | global $maintenance, $mtitle, $msubject, $mmessage, $mbname, $language, $boardurl, $webmaster_email; |
||
| 146 | global $cookiename, $db_type, $db_server, $db_port, $db_name, $db_user, $db_passwd; |
||
| 147 | global $ssi_db_user, $ssi_db_passwd, $db_prefix, $db_persist, $db_error_send, $cache_accelerator; |
||
| 148 | global $cache_uid, $cache_password, $cache_enable, $cache_memcached, $db_show_debug, $url_format; |
||
| 149 | global $cachedir, $boarddir, $sourcedir, $extdir, $languagedir; |
||
| 150 | |||
| 151 | // Where the Settings.php file is located |
||
| 152 | $settings_loc = __DIR__ . '/Settings.php'; |
||
| 153 | |||
| 154 | // First thing: if the install dir exists, just send anybody there |
||
| 155 | // The IGNORE_INSTALL_DIR constant is for developers only. Do not add it on production sites |
||
| 156 | if (file_exists('install') && (file_exists('install/install.php') || file_exists('install/upgrade.php'))) |
||
| 157 | { |
||
| 158 | if (file_exists($settings_loc)) |
||
| 159 | { |
||
| 160 | require_once($settings_loc); |
||
| 161 | } |
||
| 162 | |||
| 163 | if (!defined('IGNORE_INSTALL_DIR')) |
||
| 164 | { |
||
| 165 | if (file_exists($settings_loc) && empty($_SESSION['installing'])) |
||
| 166 | { |
||
| 167 | $redirec_file = 'upgrade.php'; |
||
| 168 | } |
||
| 169 | else |
||
| 170 | { |
||
| 171 | $redirec_file = 'install.php'; |
||
| 172 | } |
||
| 173 | |||
| 174 | $version_running = str_replace('ElkArte ', '', FORUM_VERSION); |
||
| 175 | $proto = 'http' . (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on' ? 's' : ''); |
||
| 176 | $port = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] === '80' ? '' : ':' . $_SERVER['SERVER_PORT']; |
||
| 177 | $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . $port : $_SERVER['HTTP_HOST']; |
||
| 178 | $path = strtr(dirname($_SERVER['PHP_SELF']), '\\', '/') == '/' ? '' : strtr(dirname($_SERVER['PHP_SELF']), '\\', '/'); |
||
| 179 | |||
| 180 | header('Location:' . $proto . '://' . $host . $path . '/install/' . $redirec_file . '?v=' . $version_running); |
||
| 181 | die(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | else |
||
| 185 | { |
||
| 186 | require_once($settings_loc); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Validate the paths set in Settings.php, correct as needed and move |
||
| 192 | * them to constants. |
||
| 193 | */ |
||
| 194 | private function validatePaths() |
||
| 195 | { |
||
| 196 | global $boarddir, $sourcedir, $cachedir, $extdir, $languagedir; |
||
| 197 | |||
| 198 | // Make sure the paths are correct... at least try to fix them. |
||
| 199 | if (!file_exists($boarddir) && file_exists(__DIR__ . '/agreement.txt')) |
||
| 200 | { |
||
| 201 | $boarddir = __DIR__; |
||
| 202 | } |
||
| 203 | |||
| 204 | if (!file_exists($sourcedir . '/SiteDispatcher.class.php') && file_exists($boarddir . '/sources')) |
||
| 205 | { |
||
| 206 | $sourcedir = $boarddir . '/sources'; |
||
| 207 | } |
||
| 208 | |||
| 209 | // Check that directories which didn't exist in past releases are initialized. |
||
| 210 | if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache')) |
||
| 211 | { |
||
| 212 | $cachedir = $boarddir . '/cache'; |
||
| 213 | } |
||
| 214 | |||
| 215 | if ((empty($extdir) || !file_exists($extdir)) && file_exists($sourcedir . '/ext')) |
||
| 216 | { |
||
| 217 | $extdir = $sourcedir . '/ext'; |
||
| 218 | } |
||
| 219 | |||
| 220 | if ((empty($languagedir) || !file_exists($languagedir)) && file_exists($boarddir . '/themes/default/languages')) |
||
| 221 | { |
||
| 222 | $languagedir = $boarddir . '/themes/default/languages'; |
||
| 223 | } |
||
| 224 | |||
| 225 | // Time to forget about variables and go with constants! |
||
| 226 | define('BOARDDIR', $boarddir); |
||
| 227 | define('CACHEDIR', $cachedir); |
||
| 228 | define('EXTDIR', $extdir); |
||
| 229 | define('LANGUAGEDIR', $languagedir); |
||
| 230 | define('SOURCEDIR', $sourcedir); |
||
| 231 | define('ADMINDIR', $sourcedir . '/ElkArte/AdminController'); |
||
| 232 | define('CONTROLLERDIR', $sourcedir . '/ElkArte/Controller'); |
||
| 233 | define('SUBSDIR', $sourcedir . '/subs'); |
||
| 234 | define('ADDONSDIR', $boarddir . '/addons'); |
||
| 235 | unset($boarddir, $cachedir, $sourcedir, $languagedir, $extdir); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * We require access to several important files, so load them upfront |
||
| 240 | */ |
||
| 241 | private function loadDependants() |
||
| 242 | { |
||
| 243 | // Files we cannot live without. |
||
| 244 | require_once(SOURCEDIR . '/QueryString.php'); |
||
| 245 | require_once(SOURCEDIR . '/Session.php'); |
||
| 246 | require_once(SOURCEDIR . '/Subs.php'); |
||
| 247 | require_once(SOURCEDIR . '/Logging.php'); |
||
| 248 | require_once(SOURCEDIR . '/Load.php'); |
||
| 249 | require_once(SOURCEDIR . '/Security.php'); |
||
| 250 | require_once(SUBSDIR . '/Cache.subs.php'); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * The autoloader will take care most requests for files |
||
| 255 | */ |
||
| 256 | private function loadAutoloader() |
||
| 257 | { |
||
| 258 | require_once(EXTDIR . '/ClassLoader.php'); |
||
| 259 | |||
| 260 | $loader = new \ElkArte\ext\Composer\Autoload\ClassLoader(); |
||
|
|
|||
| 261 | $loader->setPsr4('ElkArte\\', SOURCEDIR . '/ElkArte'); |
||
| 262 | $loader->setPsr4('BBC\\', SOURCEDIR . '/ElkArte/BBC'); |
||
| 263 | $loader->register(); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Check if we are in maintance mode, if so end here. |
||
| 268 | */ |
||
| 269 | private function checkMaintance() |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * If you like lots of debug information in error messages and below the footer |
||
| 282 | * then set $db_show_debug to true in settings. Don't do this on a production site. |
||
| 283 | */ |
||
| 284 | private function setDebug() |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Time to see what has been requested, by whom and dispatch it to the proper handler |
||
| 297 | */ |
||
| 298 | private function bringUp() |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * If you are running SSI standalone, you need to call this function after bootstrap is |
||
| 326 | * initialized. |
||
| 327 | * |
||
| 328 | * @throws \ElkArte\Exceptions\Exception |
||
| 329 | */ |
||
| 330 | public function ssi_main() |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Used to ensure SSI requests are valid and not a probing attempt |
||
| 464 | */ |
||
| 465 | private function _validRequestCheck() |
||
| 478 | } |
||
| 479 | } |
||
| 480 | } |
||
| 481 |
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