Complex classes like Setup 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Setup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Setup { |
||
| 49 | /** @var \OCP\IConfig */ |
||
| 50 | protected $config; |
||
| 51 | /** @var IniGetWrapper */ |
||
| 52 | protected $iniWrapper; |
||
| 53 | /** @var IL10N */ |
||
| 54 | protected $l10n; |
||
| 55 | /** @var \OC_Defaults */ |
||
| 56 | protected $defaults; |
||
| 57 | /** @var ILogger */ |
||
| 58 | protected $logger; |
||
| 59 | /** @var ISecureRandom */ |
||
| 60 | protected $random; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param IConfig $config |
||
| 64 | * @param IniGetWrapper $iniWrapper |
||
| 65 | * @param \OC_Defaults $defaults |
||
| 66 | */ |
||
| 67 | function __construct(IConfig $config, |
||
| 81 | |||
| 82 | static $dbSetupClasses = array( |
||
| 83 | 'mysql' => '\OC\Setup\MySQL', |
||
| 84 | 'pgsql' => '\OC\Setup\PostgreSQL', |
||
| 85 | 'oci' => '\OC\Setup\OCI', |
||
| 86 | 'sqlite' => '\OC\Setup\Sqlite', |
||
| 87 | 'sqlite3' => '\OC\Setup\Sqlite', |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Wrapper around the "class_exists" PHP function to be able to mock it |
||
| 92 | * @param string $name |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | protected function class_exists($name) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Wrapper around the "is_callable" PHP function to be able to mock it |
||
| 101 | * @param string $name |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | protected function is_callable($name) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Wrapper around \PDO::getAvailableDrivers |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | protected function getAvailableDbDriversForPdo() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the available and supported databases of this instance |
||
| 119 | * |
||
| 120 | * @param bool $allowAllDatabases |
||
| 121 | * @return array |
||
| 122 | * @throws Exception |
||
| 123 | */ |
||
| 124 | public function getSupportedDatabases($allowAllDatabases = false) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gathers system information like database type and does |
||
| 183 | * a few system checks. |
||
| 184 | * |
||
| 185 | * @return array of system info, including an "errors" value |
||
| 186 | * in case of errors/warnings |
||
| 187 | */ |
||
| 188 | public function getSystemInfo($allowAllDatabases = false) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $options |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function install($options) { |
||
| 257 | $l = $this->l10n; |
||
| 258 | |||
| 259 | $error = array(); |
||
| 260 | $dbType = $options['dbtype']; |
||
| 261 | |||
| 262 | if(empty($options['adminlogin'])) { |
||
| 263 | $error[] = $l->t('Set an admin username.'); |
||
| 264 | } |
||
| 265 | if(empty($options['adminpass'])) { |
||
| 266 | $error[] = $l->t('Set an admin password.'); |
||
| 267 | } |
||
| 268 | if(empty($options['directory'])) { |
||
| 269 | $options['directory'] = \OC::$SERVERROOT."/data"; |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!isset(self::$dbSetupClasses[$dbType])) { |
||
| 273 | $dbType = 'sqlite'; |
||
| 274 | } |
||
| 275 | |||
| 276 | $username = htmlspecialchars_decode($options['adminlogin']); |
||
| 277 | $password = htmlspecialchars_decode($options['adminpass']); |
||
| 278 | $dataDir = htmlspecialchars_decode($options['directory']); |
||
| 279 | |||
| 280 | $class = self::$dbSetupClasses[$dbType]; |
||
| 281 | /** @var \OC\Setup\AbstractDatabase $dbSetup */ |
||
| 282 | $dbSetup = new $class($l, 'db_structure.xml', $this->config, |
||
| 283 | $this->logger, $this->random); |
||
| 284 | $error = array_merge($error, $dbSetup->validate($options)); |
||
| 285 | |||
| 286 | // validate the data directory |
||
| 287 | if ( |
||
| 288 | (!is_dir($dataDir) and !mkdir($dataDir)) or |
||
| 289 | !is_writable($dataDir) |
||
| 290 | ) { |
||
| 291 | $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); |
||
| 292 | } |
||
| 293 | |||
| 294 | if(count($error) != 0) { |
||
| 295 | return $error; |
||
| 296 | } |
||
| 297 | |||
| 298 | $request = \OC::$server->getRequest(); |
||
| 299 | |||
| 300 | //no errors, good |
||
| 301 | if(isset($options['trusted_domains']) |
||
| 302 | && is_array($options['trusted_domains'])) { |
||
| 303 | $trustedDomains = $options['trusted_domains']; |
||
| 304 | } else { |
||
| 305 | $trustedDomains = [$request->getInsecureServerHost()]; |
||
| 306 | } |
||
| 307 | |||
| 308 | if (\OC_Util::runningOnWindows()) { |
||
| 309 | $dataDir = rtrim(realpath($dataDir), '\\'); |
||
| 310 | } |
||
| 311 | |||
| 312 | //use sqlite3 when available, otherwise sqlite2 will be used. |
||
| 313 | if($dbType=='sqlite' and class_exists('SQLite3')) { |
||
| 314 | $dbType='sqlite3'; |
||
| 315 | } |
||
| 316 | |||
| 317 | //generate a random salt that is used to salt the local user passwords |
||
| 318 | $salt = $this->random->generate(30); |
||
| 319 | // generate a secret |
||
| 320 | $secret = $this->random->generate(48); |
||
| 321 | |||
| 322 | //write the config file |
||
| 323 | $this->config->setSystemValues([ |
||
| 324 | 'passwordsalt' => $salt, |
||
| 325 | 'secret' => $secret, |
||
| 326 | 'trusted_domains' => $trustedDomains, |
||
| 327 | 'datadirectory' => $dataDir, |
||
| 328 | 'overwrite.cli.url' => $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT, |
||
| 329 | 'dbtype' => $dbType, |
||
| 330 | 'version' => implode('.', \OCP\Util::getVersion()), |
||
| 331 | ]); |
||
| 332 | |||
| 333 | try { |
||
| 334 | $dbSetup->initialize($options); |
||
| 335 | $dbSetup->setupDatabase($username); |
||
| 336 | } catch (\OC\DatabaseSetupException $e) { |
||
| 337 | $error[] = array( |
||
| 338 | 'error' => $e->getMessage(), |
||
| 339 | 'hint' => $e->getHint() |
||
| 340 | ); |
||
| 341 | return($error); |
||
| 342 | } catch (Exception $e) { |
||
| 343 | $error[] = array( |
||
| 344 | 'error' => 'Error while trying to create admin user: ' . $e->getMessage(), |
||
| 345 | 'hint' => '' |
||
| 346 | ); |
||
| 347 | return($error); |
||
| 348 | } |
||
| 349 | |||
| 350 | //create the user and group |
||
| 351 | $user = null; |
||
| 352 | try { |
||
| 353 | $user = \OC::$server->getUserManager()->createUser($username, $password); |
||
| 354 | if (!$user) { |
||
| 355 | $error[] = "User <$username> could not be created."; |
||
| 356 | } |
||
| 357 | } catch(Exception $exception) { |
||
| 358 | $error[] = $exception->getMessage(); |
||
| 359 | } |
||
| 360 | |||
| 361 | if(count($error) == 0) { |
||
| 362 | $config = \OC::$server->getConfig(); |
||
| 363 | $config->setAppValue('core', 'installedat', microtime(true)); |
||
| 364 | $config->setAppValue('core', 'lastupdatedat', microtime(true)); |
||
| 365 | |||
| 366 | $group =\OC::$server->getGroupManager()->createGroup('admin'); |
||
| 367 | $group->addUser($user); |
||
| 368 | |||
| 369 | // Create a session token for the newly created user |
||
| 370 | // The token provider requires a working db, so it's not injected on setup |
||
| 371 | /* @var $userSession User\Session */ |
||
| 372 | $userSession = \OC::$server->getUserSession(); |
||
| 373 | $defaultTokenProvider = \OC::$server->query('OC\Authentication\Token\DefaultTokenProvider'); |
||
| 374 | $userSession->setTokenProvider($defaultTokenProvider); |
||
| 375 | $userSession->login($username, $password); |
||
| 376 | $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password); |
||
| 377 | |||
| 378 | //guess what this does |
||
| 379 | Installer::installShippedApps(); |
||
| 380 | |||
| 381 | // create empty file in data dir, so we can later find |
||
| 382 | // out that this is indeed an ownCloud data directory |
||
| 383 | file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', ''); |
||
| 384 | |||
| 385 | // Update .htaccess files |
||
| 386 | Setup::updateHtaccess(); |
||
| 387 | Setup::protectDataDirectory(); |
||
| 388 | |||
| 389 | //try to write logtimezone |
||
| 390 | if (date_default_timezone_get()) { |
||
| 391 | $config->setSystemValue('logtimezone', date_default_timezone_get()); |
||
| 392 | } |
||
| 393 | |||
| 394 | self::installBackgroundJobs(); |
||
| 395 | |||
| 396 | //and we are done |
||
| 397 | $config->setSystemValue('installed', true); |
||
| 398 | } |
||
| 399 | |||
| 400 | return $error; |
||
| 401 | } |
||
| 402 | |||
| 403 | public static function installBackgroundJobs() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return string Absolute path to htaccess |
||
| 409 | */ |
||
| 410 | private function pathToHtaccess() { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Append the correct ErrorDocument path for Apache hosts |
||
| 416 | */ |
||
| 417 | public static function updateHtaccess() { |
||
| 482 | |||
| 483 | public static function protectDataDirectory() { |
||
| 503 | } |
||
| 504 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: