Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Updater 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 Updater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class Updater extends BasicEmitter { |
||
| 57 | |||
| 58 | /** @var ILogger $log */ |
||
| 59 | private $log; |
||
| 60 | |||
| 61 | /** @var IConfig */ |
||
| 62 | private $config; |
||
| 63 | |||
| 64 | /** @var Checker */ |
||
| 65 | private $checker; |
||
| 66 | |||
| 67 | /** @var Installer */ |
||
| 68 | private $installer; |
||
| 69 | |||
| 70 | /** @var IJobList */ |
||
| 71 | private $jobList; |
||
| 72 | |||
| 73 | private $logLevelNames = [ |
||
| 74 | 0 => 'Debug', |
||
| 75 | 1 => 'Info', |
||
| 76 | 2 => 'Warning', |
||
| 77 | 3 => 'Error', |
||
| 78 | 4 => 'Fatal', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | public function __construct(IConfig $config, |
||
| 82 | Checker $checker, |
||
| 83 | ILogger $log, |
||
| 84 | Installer $installer, |
||
| 85 | IJobList $jobList) { |
||
| 86 | $this->log = $log; |
||
| 87 | $this->config = $config; |
||
| 88 | $this->checker = $checker; |
||
| 89 | $this->installer = $installer; |
||
| 90 | $this->jobList = $jobList; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * runs the update actions in maintenance mode, does not upgrade the source files |
||
| 95 | * except the main .htaccess file |
||
| 96 | * |
||
| 97 | * @return bool true if the operation succeeded, false otherwise |
||
| 98 | */ |
||
| 99 | public function upgrade() { |
||
| 100 | $this->emitRepairEvents(); |
||
| 101 | $this->logAllEvents(); |
||
| 102 | |||
| 103 | $logLevel = $this->config->getSystemValue('loglevel', ILogger::WARN); |
||
| 104 | $this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); |
||
| 105 | $this->config->setSystemValue('loglevel', ILogger::DEBUG); |
||
| 106 | |||
| 107 | $wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
||
| 108 | |||
| 109 | if(!$wasMaintenanceModeEnabled) { |
||
| 110 | $this->config->setSystemValue('maintenance', true); |
||
| 111 | $this->emit('\OC\Updater', 'maintenanceEnabled'); |
||
| 112 | } |
||
| 113 | |||
| 114 | $this->waitForCronToFinish(); |
||
| 115 | |||
| 116 | $installedVersion = $this->config->getSystemValue('version', '0.0.0'); |
||
| 117 | $currentVersion = implode('.', \OCP\Util::getVersion()); |
||
| 118 | $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core')); |
||
| 119 | |||
| 120 | $success = true; |
||
| 121 | try { |
||
| 122 | $this->doUpgrade($currentVersion, $installedVersion); |
||
| 123 | } catch (HintException $exception) { |
||
| 124 | $this->log->logException($exception, ['app' => 'core']); |
||
|
|
|||
| 125 | $this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint())); |
||
| 126 | $success = false; |
||
| 127 | } catch (\Exception $exception) { |
||
| 128 | $this->log->logException($exception, ['app' => 'core']); |
||
| 129 | $this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage())); |
||
| 130 | $success = false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->emit('\OC\Updater', 'updateEnd', array($success)); |
||
| 134 | |||
| 135 | if(!$wasMaintenanceModeEnabled && $success) { |
||
| 136 | $this->config->setSystemValue('maintenance', false); |
||
| 137 | $this->emit('\OC\Updater', 'maintenanceDisabled'); |
||
| 138 | } else { |
||
| 139 | $this->emit('\OC\Updater', 'maintenanceActive'); |
||
| 140 | } |
||
| 141 | |||
| 142 | $this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); |
||
| 143 | $this->config->setSystemValue('loglevel', $logLevel); |
||
| 144 | $this->config->setSystemValue('installed', true); |
||
| 145 | |||
| 146 | return $success; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return version from which this version is allowed to upgrade from |
||
| 151 | * |
||
| 152 | * @return array allowed previous versions per vendor |
||
| 153 | */ |
||
| 154 | private function getAllowedPreviousVersions() { |
||
| 155 | // this should really be a JSON file |
||
| 156 | require \OC::$SERVERROOT . '/version.php'; |
||
| 157 | /** @var array $OC_VersionCanBeUpgradedFrom */ |
||
| 158 | return $OC_VersionCanBeUpgradedFrom; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return vendor from which this version was published |
||
| 163 | * |
||
| 164 | * @return string Get the vendor |
||
| 165 | */ |
||
| 166 | private function getVendor() { |
||
| 167 | // this should really be a JSON file |
||
| 168 | require \OC::$SERVERROOT . '/version.php'; |
||
| 169 | /** @var string $vendor */ |
||
| 170 | return (string) $vendor; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Whether an upgrade to a specified version is possible |
||
| 175 | * @param string $oldVersion |
||
| 176 | * @param string $newVersion |
||
| 177 | * @param array $allowedPreviousVersions |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) { |
||
| 181 | $version = explode('.', $oldVersion); |
||
| 182 | $majorMinor = $version[0] . '.' . $version[1]; |
||
| 183 | |||
| 184 | $currentVendor = $this->config->getAppValue('core', 'vendor', ''); |
||
| 185 | |||
| 186 | // Vendor was not set correctly on install, so we have to white-list known versions |
||
| 187 | if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) { |
||
| 188 | $currentVendor = 'owncloud'; |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($currentVendor === 'nextcloud') { |
||
| 192 | return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) |
||
| 193 | && (version_compare($oldVersion, $newVersion, '<=') || |
||
| 194 | $this->config->getSystemValue('debug', false)); |
||
| 195 | } |
||
| 196 | |||
| 197 | // Check if the instance can be migrated |
||
| 198 | return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) || |
||
| 199 | isset($allowedPreviousVersions[$currentVendor][$oldVersion]); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * runs the update actions in maintenance mode, does not upgrade the source files |
||
| 204 | * except the main .htaccess file |
||
| 205 | * |
||
| 206 | * @param string $currentVersion current version to upgrade to |
||
| 207 | * @param string $installedVersion previous version from which to upgrade from |
||
| 208 | * |
||
| 209 | * @throws \Exception |
||
| 210 | */ |
||
| 211 | private function doUpgrade($currentVersion, $installedVersion) { |
||
| 212 | // Stop update if the update is over several major versions |
||
| 213 | $allowedPreviousVersions = $this->getAllowedPreviousVersions(); |
||
| 214 | if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) { |
||
| 215 | throw new \Exception('Updates between multiple major versions and downgrades are unsupported.'); |
||
| 216 | } |
||
| 217 | |||
| 218 | // Update .htaccess files |
||
| 219 | try { |
||
| 220 | Setup::updateHtaccess(); |
||
| 221 | Setup::protectDataDirectory(); |
||
| 222 | } catch (\Exception $e) { |
||
| 223 | throw new \Exception($e->getMessage()); |
||
| 224 | } |
||
| 225 | |||
| 226 | // create empty file in data dir, so we can later find |
||
| 227 | // out that this is indeed an ownCloud data directory |
||
| 228 | // (in case it didn't exist before) |
||
| 229 | file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); |
||
| 230 | |||
| 231 | // pre-upgrade repairs |
||
| 232 | $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher()); |
||
| 233 | $repair->run(); |
||
| 234 | |||
| 235 | $this->doCoreUpgrade(); |
||
| 236 | |||
| 237 | try { |
||
| 238 | // TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378 |
||
| 239 | Setup::installBackgroundJobs(); |
||
| 240 | } catch (\Exception $e) { |
||
| 241 | throw new \Exception($e->getMessage()); |
||
| 242 | } |
||
| 243 | |||
| 244 | // update all shipped apps |
||
| 245 | $this->checkAppsRequirements(); |
||
| 246 | $this->doAppUpgrade(); |
||
| 247 | |||
| 248 | // Update the appfetchers version so it downloads the correct list from the appstore |
||
| 249 | \OC::$server->getAppFetcher()->setVersion($currentVersion); |
||
| 250 | |||
| 251 | // upgrade appstore apps |
||
| 252 | $this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps()); |
||
| 253 | |||
| 254 | // install new shipped apps on upgrade |
||
| 255 | OC_App::loadApps(['authentication']); |
||
| 256 | $errors = Installer::installShippedApps(true); |
||
| 257 | foreach ($errors as $appId => $exception) { |
||
| 258 | /** @var \Exception $exception */ |
||
| 259 | $this->log->logException($exception, ['app' => $appId]); |
||
| 260 | $this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]); |
||
| 261 | } |
||
| 262 | |||
| 263 | // post-upgrade repairs |
||
| 264 | $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher()); |
||
| 265 | $repair->run(); |
||
| 266 | |||
| 267 | //Invalidate update feed |
||
| 268 | $this->config->setAppValue('core', 'lastupdatedat', 0); |
||
| 269 | |||
| 270 | // Check for code integrity if not disabled |
||
| 271 | if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) { |
||
| 272 | $this->emit('\OC\Updater', 'startCheckCodeIntegrity'); |
||
| 273 | $this->checker->runInstanceVerification(); |
||
| 274 | $this->emit('\OC\Updater', 'finishedCheckCodeIntegrity'); |
||
| 275 | } |
||
| 276 | |||
| 277 | // only set the final version if everything went well |
||
| 278 | $this->config->setSystemValue('version', implode('.', Util::getVersion())); |
||
| 279 | $this->config->setAppValue('core', 'vendor', $this->getVendor()); |
||
| 280 | } |
||
| 281 | |||
| 282 | protected function doCoreUpgrade() { |
||
| 283 | $this->emit('\OC\Updater', 'dbUpgradeBefore'); |
||
| 284 | |||
| 285 | // execute core migrations |
||
| 286 | $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
||
| 287 | $ms->migrate(); |
||
| 288 | |||
| 289 | $this->emit('\OC\Updater', 'dbUpgrade'); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $version the oc version to check app compatibility with |
||
| 294 | */ |
||
| 295 | protected function checkAppUpgrade($version) { |
||
| 296 | $apps = \OC_App::getEnabledApps(); |
||
| 297 | $this->emit('\OC\Updater', 'appUpgradeCheckBefore'); |
||
| 298 | |||
| 299 | $appManager = \OC::$server->getAppManager(); |
||
| 300 | foreach ($apps as $appId) { |
||
| 301 | $info = \OC_App::getAppInfo($appId); |
||
| 302 | $compatible = \OC_App::isAppCompatible($version, $info); |
||
| 303 | $isShipped = $appManager->isShipped($appId); |
||
| 304 | |||
| 305 | if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) { |
||
| 306 | /** |
||
| 307 | * FIXME: The preupdate check is performed before the database migration, otherwise database changes |
||
| 308 | * are not possible anymore within it. - Consider this when touching the code. |
||
| 309 | * @link https://github.com/owncloud/core/issues/10980 |
||
| 310 | * @see \OC_App::updateApp |
||
| 311 | */ |
||
| 312 | if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) { |
||
| 313 | $this->includePreUpdate($appId); |
||
| 314 | } |
||
| 315 | if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) { |
||
| 316 | $this->emit('\OC\Updater', 'appSimulateUpdate', array($appId)); |
||
| 317 | \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml'); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | $this->emit('\OC\Updater', 'appUpgradeCheck'); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Includes the pre-update file. Done here to prevent namespace mixups. |
||
| 327 | * @param string $appId |
||
| 328 | */ |
||
| 329 | private function includePreUpdate($appId) { |
||
| 330 | include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php'; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * upgrades all apps within a major ownCloud upgrade. Also loads "priority" |
||
| 335 | * (types authentication, filesystem, logging, in that order) afterwards. |
||
| 336 | * |
||
| 337 | * @throws NeedsUpdateException |
||
| 338 | */ |
||
| 339 | protected function doAppUpgrade() { |
||
| 340 | $apps = \OC_App::getEnabledApps(); |
||
| 341 | $priorityTypes = array('authentication', 'filesystem', 'logging'); |
||
| 342 | $pseudoOtherType = 'other'; |
||
| 343 | $stacks = array($pseudoOtherType => array()); |
||
| 344 | |||
| 345 | foreach ($apps as $appId) { |
||
| 346 | $priorityType = false; |
||
| 347 | foreach ($priorityTypes as $type) { |
||
| 348 | if(!isset($stacks[$type])) { |
||
| 349 | $stacks[$type] = array(); |
||
| 350 | } |
||
| 351 | if (\OC_App::isType($appId, [$type])) { |
||
| 352 | $stacks[$type][] = $appId; |
||
| 353 | $priorityType = true; |
||
| 354 | break; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | if (!$priorityType) { |
||
| 358 | $stacks[$pseudoOtherType][] = $appId; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | foreach ($stacks as $type => $stack) { |
||
| 362 | foreach ($stack as $appId) { |
||
| 363 | if (\OC_App::shouldUpgrade($appId)) { |
||
| 364 | $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]); |
||
| 365 | \OC_App::updateApp($appId); |
||
| 366 | $this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]); |
||
| 367 | } |
||
| 368 | if($type !== $pseudoOtherType) { |
||
| 369 | // load authentication, filesystem and logging apps after |
||
| 370 | // upgrading them. Other apps my need to rely on modifying |
||
| 371 | // user and/or filesystem aspects. |
||
| 372 | \OC_App::loadApp($appId); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * check if the current enabled apps are compatible with the current |
||
| 380 | * ownCloud version. disable them if not. |
||
| 381 | * This is important if you upgrade ownCloud and have non ported 3rd |
||
| 382 | * party apps installed. |
||
| 383 | * |
||
| 384 | * @return array |
||
| 385 | * @throws \Exception |
||
| 386 | */ |
||
| 387 | private function checkAppsRequirements() { |
||
| 388 | $isCoreUpgrade = $this->isCodeUpgrade(); |
||
| 389 | $apps = OC_App::getEnabledApps(); |
||
| 390 | $version = implode('.', Util::getVersion()); |
||
| 391 | $disabledApps = []; |
||
| 392 | $appManager = \OC::$server->getAppManager(); |
||
| 393 | foreach ($apps as $app) { |
||
| 394 | // check if the app is compatible with this version of ownCloud |
||
| 395 | $info = OC_App::getAppInfo($app); |
||
| 396 | if($info === null || !OC_App::isAppCompatible($version, $info)) { |
||
| 397 | if ($appManager->isShipped($app)) { |
||
| 398 | throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update'); |
||
| 399 | } |
||
| 400 | \OC::$server->getAppManager()->disableApp($app); |
||
| 401 | $this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app)); |
||
| 402 | } |
||
| 403 | // no need to disable any app in case this is a non-core upgrade |
||
| 404 | if (!$isCoreUpgrade) { |
||
| 405 | continue; |
||
| 406 | } |
||
| 407 | // shipped apps will remain enabled |
||
| 408 | if ($appManager->isShipped($app)) { |
||
| 409 | continue; |
||
| 410 | } |
||
| 411 | // authentication and session apps will remain enabled as well |
||
| 412 | if (OC_App::isType($app, ['session', 'authentication'])) { |
||
| 413 | continue; |
||
| 414 | } |
||
| 415 | } |
||
| 416 | return $disabledApps; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | private function isCodeUpgrade() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param array $disabledApps |
||
| 433 | * @throws \Exception |
||
| 434 | */ |
||
| 435 | private function upgradeAppStoreApps(array $disabledApps) { |
||
| 436 | foreach($disabledApps as $app) { |
||
| 437 | try { |
||
| 438 | $this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]); |
||
| 439 | if ($this->installer->isUpdateAvailable($app)) { |
||
| 440 | $this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]); |
||
| 441 | $this->installer->updateAppstoreApp($app); |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Forward messages emitted by the repair routine |
||
| 452 | */ |
||
| 453 | private function emitRepairEvents() { |
||
| 476 | |||
| 477 | private function logAllEvents() { |
||
| 615 | |||
| 616 | } |
||
| 617 | |||
| 618 |
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: