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 |
||
| 51 | class Updater extends BasicEmitter { |
||
| 52 | |||
| 53 | /** @var ILogger $log */ |
||
| 54 | private $log; |
||
| 55 | |||
| 56 | /** @var IConfig */ |
||
| 57 | private $config; |
||
| 58 | |||
| 59 | /** @var Checker */ |
||
| 60 | private $checker; |
||
| 61 | |||
| 62 | /** @var bool */ |
||
| 63 | private $simulateStepEnabled; |
||
| 64 | |||
| 65 | /** @var bool */ |
||
| 66 | private $updateStepEnabled; |
||
| 67 | |||
| 68 | /** @var bool */ |
||
| 69 | private $skip3rdPartyAppsDisable; |
||
| 70 | |||
| 71 | private $logLevelNames = [ |
||
| 72 | 0 => 'Debug', |
||
| 73 | 1 => 'Info', |
||
| 74 | 2 => 'Warning', |
||
| 75 | 3 => 'Error', |
||
| 76 | 4 => 'Fatal', |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param IConfig $config |
||
| 81 | * @param Checker $checker |
||
| 82 | * @param ILogger $log |
||
| 83 | */ |
||
| 84 | public function __construct(IConfig $config, |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Sets whether the database migration simulation must |
||
| 96 | * be enabled. |
||
| 97 | * This can be set to false to skip this test. |
||
| 98 | * |
||
| 99 | * @param bool $flag true to enable simulation, false otherwise |
||
| 100 | */ |
||
| 101 | public function setSimulateStepEnabled($flag) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Sets whether the update must be performed. |
||
| 107 | * This can be set to false to skip the actual update. |
||
| 108 | * |
||
| 109 | * @param bool $flag true to enable update, false otherwise |
||
| 110 | */ |
||
| 111 | public function setUpdateStepEnabled($flag) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets whether the update disables 3rd party apps. |
||
| 117 | * This can be set to true to skip the disable. |
||
| 118 | * |
||
| 119 | * @param bool $flag false to not disable, true otherwise |
||
| 120 | */ |
||
| 121 | public function setSkip3rdPartyAppsDisable($flag) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * runs the update actions in maintenance mode, does not upgrade the source files |
||
| 127 | * except the main .htaccess file |
||
| 128 | * |
||
| 129 | * @return bool true if the operation succeeded, false otherwise |
||
| 130 | */ |
||
| 131 | public function upgrade() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return version from which this version is allowed to upgrade from |
||
| 176 | * |
||
| 177 | * @return string allowed previous version |
||
| 178 | */ |
||
| 179 | private function getAllowedPreviousVersion() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return vendor from which this version was published |
||
| 188 | * |
||
| 189 | * @return string Get the vendor |
||
| 190 | */ |
||
| 191 | private function getVendor() { |
||
| 192 | // this should really be a JSON file |
||
| 193 | require \OC::$SERVERROOT . '/version.php'; |
||
| 194 | /** @var string $vendor */ |
||
| 195 | return (string) $vendor; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Whether an upgrade to a specified version is possible |
||
| 200 | * @param string $oldVersion |
||
| 201 | * @param string $newVersion |
||
| 202 | * @param string $allowedPreviousVersion |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function isUpgradePossible($oldVersion, $newVersion, $allowedPreviousVersion) { |
||
| 206 | $allowedUpgrade = (version_compare($allowedPreviousVersion, $oldVersion, '<=') |
||
| 207 | && (version_compare($oldVersion, $newVersion, '<=') || $this->config->getSystemValue('debug', false))); |
||
| 208 | |||
| 209 | if ($allowedUpgrade) { |
||
| 210 | return $allowedUpgrade; |
||
| 211 | } |
||
| 212 | |||
| 213 | // Upgrade not allowed, someone switching vendor? |
||
| 214 | if ($this->getVendor() !== $this->config->getAppValue('core', 'vendor', '')) { |
||
| 215 | $oldVersion = explode('.', $oldVersion); |
||
| 216 | $newVersion = explode('.', $newVersion); |
||
| 217 | |||
| 218 | return $oldVersion[0] === $newVersion[0] && $oldVersion[1] === $newVersion[1]; |
||
| 219 | } |
||
| 220 | |||
| 221 | return false; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * runs the update actions in maintenance mode, does not upgrade the source files |
||
| 226 | * except the main .htaccess file |
||
| 227 | * |
||
| 228 | * @param string $currentVersion current version to upgrade to |
||
| 229 | * @param string $installedVersion previous version from which to upgrade from |
||
| 230 | * |
||
| 231 | * @throws \Exception |
||
| 232 | */ |
||
| 233 | private function doUpgrade($currentVersion, $installedVersion) { |
||
| 234 | // Stop update if the update is over several major versions |
||
| 235 | $allowedPreviousVersion = $this->getAllowedPreviousVersion(); |
||
| 236 | if (!self::isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersion)) { |
||
| 237 | throw new \Exception('Updates between multiple major versions and downgrades are unsupported.'); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Update .htaccess files |
||
| 241 | try { |
||
| 242 | Setup::updateHtaccess(); |
||
| 243 | Setup::protectDataDirectory(); |
||
| 244 | } catch (\Exception $e) { |
||
| 245 | throw new \Exception($e->getMessage()); |
||
| 246 | } |
||
| 247 | |||
| 248 | // create empty file in data dir, so we can later find |
||
| 249 | // out that this is indeed an ownCloud data directory |
||
| 250 | // (in case it didn't exist before) |
||
| 251 | file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); |
||
| 252 | |||
| 253 | // pre-upgrade repairs |
||
| 254 | $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher()); |
||
| 255 | $repair->run(); |
||
| 256 | |||
| 257 | // simulate DB upgrade |
||
| 258 | if ($this->simulateStepEnabled) { |
||
| 259 | $this->checkCoreUpgrade(); |
||
| 260 | |||
| 261 | // simulate apps DB upgrade |
||
| 262 | $this->checkAppUpgrade($currentVersion); |
||
| 263 | |||
| 264 | } |
||
| 265 | |||
| 266 | if ($this->updateStepEnabled) { |
||
| 267 | $this->doCoreUpgrade(); |
||
| 268 | |||
| 269 | try { |
||
| 270 | // TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378 |
||
| 271 | Setup::installBackgroundJobs(); |
||
| 272 | } catch (\Exception $e) { |
||
| 273 | throw new \Exception($e->getMessage()); |
||
| 274 | } |
||
| 275 | |||
| 276 | // update all shipped apps |
||
| 277 | $disabledApps = $this->checkAppsRequirements(); |
||
| 278 | $this->doAppUpgrade(); |
||
| 279 | |||
| 280 | // upgrade appstore apps |
||
| 281 | $this->upgradeAppStoreApps($disabledApps); |
||
| 282 | |||
| 283 | // install new shipped apps on upgrade |
||
| 284 | OC_App::loadApps('authentication'); |
||
| 285 | $errors = Installer::installShippedApps(true); |
||
| 286 | foreach ($errors as $appId => $exception) { |
||
| 287 | /** @var \Exception $exception */ |
||
| 288 | $this->log->logException($exception, ['app' => $appId]); |
||
| 289 | $this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]); |
||
| 290 | } |
||
| 291 | |||
| 292 | // post-upgrade repairs |
||
| 293 | $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher()); |
||
| 294 | $repair->run(); |
||
| 295 | |||
| 296 | //Invalidate update feed |
||
| 297 | $this->config->setAppValue('core', 'lastupdatedat', 0); |
||
| 298 | |||
| 299 | // Check for code integrity if not disabled |
||
| 300 | if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) { |
||
| 301 | $this->emit('\OC\Updater', 'startCheckCodeIntegrity'); |
||
| 302 | $this->checker->runInstanceVerification(); |
||
| 303 | $this->emit('\OC\Updater', 'finishedCheckCodeIntegrity'); |
||
| 304 | } |
||
| 305 | |||
| 306 | // only set the final version if everything went well |
||
| 307 | $this->config->setSystemValue('version', implode('.', \OCP\Util::getVersion())); |
||
| 308 | $this->config->setAppValue('core', 'vendor', $this->getVendor()); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | protected function checkCoreUpgrade() { |
||
| 320 | |||
| 321 | protected function doCoreUpgrade() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $version the oc version to check app compatibility with |
||
| 332 | */ |
||
| 333 | protected function checkAppUpgrade($version) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Includes the pre-update file. Done here to prevent namespace mixups. |
||
| 364 | * @param string $appId |
||
| 365 | */ |
||
| 366 | private function includePreUpdate($appId) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * upgrades all apps within a major ownCloud upgrade. Also loads "priority" |
||
| 372 | * (types authentication, filesystem, logging, in that order) afterwards. |
||
| 373 | * |
||
| 374 | * @throws NeedsUpdateException |
||
| 375 | */ |
||
| 376 | protected function doAppUpgrade() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * check if the current enabled apps are compatible with the current |
||
| 417 | * ownCloud version. disable them if not. |
||
| 418 | * This is important if you upgrade ownCloud and have non ported 3rd |
||
| 419 | * party apps installed. |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | * @throws \Exception |
||
| 423 | */ |
||
| 424 | private function checkAppsRequirements() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | private function isCodeUpgrade() { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param array $disabledApps |
||
| 473 | * @throws \Exception |
||
| 474 | */ |
||
| 475 | private function upgradeAppStoreApps(array $disabledApps) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Forward messages emitted by the repair routine |
||
| 492 | */ |
||
| 493 | private function emitRepairEvents() { |
||
| 516 | |||
| 517 | } |
||
| 518 | |||
| 519 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.