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:
| 1 | <?php |
||
| 32 | use Owncloud\Updater\Utils\BzipExtractor; |
||
| 33 | |||
| 34 | class ExecuteCoreUpgradeScriptsCommand extends Command { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var OccRunner $occRunner |
||
| 38 | */ |
||
| 39 | protected $occRunner; |
||
| 40 | |||
| 41 | public function __construct($occRunner){ |
||
| 42 | parent::__construct(); |
||
| 43 | $this->occRunner = $occRunner; |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function configure(){ |
||
| 47 | $this |
||
| 48 | ->setName('upgrade:executeCoreUpgradeScripts') |
||
| 49 | ->setDescription('execute core upgrade scripts [danger, might take long]'); |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function execute(InputInterface $input, OutputInterface $output){ |
||
| 53 | $locator = $this->container['utils.locator']; |
||
| 54 | /** @var FilesystemHelper $fsHelper */ |
||
| 55 | $fsHelper = $this->container['utils.filesystemhelper']; |
||
| 56 | $registry = $this->container['utils.registry']; |
||
| 57 | $fetcher = $this->container['utils.fetcher']; |
||
| 58 | /** @var Checkpoint $checkpoint */ |
||
| 59 | $checkpoint = $this->container['utils.checkpoint']; |
||
| 60 | |||
| 61 | $installedVersion = implode('.', $locator->getInstalledVersion()); |
||
| 62 | $registry->set('installedVersion', $installedVersion); |
||
| 63 | |||
| 64 | $feed = $registry->get('feed'); |
||
| 65 | |||
| 66 | if ($feed){ |
||
| 67 | $path = $fetcher->getBaseDownloadPath($feed); |
||
| 68 | $fullExtractionPath = $locator->getExtractionBaseDir() . '/' . $feed->getVersion(); |
||
| 69 | |||
| 70 | if (file_exists($fullExtractionPath)){ |
||
| 71 | $fsHelper->removeIfExists($fullExtractionPath); |
||
| 72 | } |
||
| 73 | try{ |
||
| 74 | $fsHelper->mkdir($fullExtractionPath, true); |
||
| 75 | } catch (\Exception $e){ |
||
| 76 | $output->writeln('Unable create directory ' . $fullExtractionPath); |
||
| 77 | throw $e; |
||
| 78 | } |
||
| 79 | |||
| 80 | $output->writeln('Extracting source into ' . $fullExtractionPath); |
||
| 81 | if (preg_match('|\.tar\.bz2$|', $path)){ |
||
| 82 | $extractor = new BzipExtractor($path, $fullExtractionPath); |
||
| 83 | } else { |
||
| 84 | $extractor = new ZipExtractor($path, $fullExtractionPath); |
||
| 85 | } |
||
| 86 | try{ |
||
| 87 | $extractor->extract(); |
||
| 88 | } catch (\Exception $e){ |
||
| 89 | $output->writeln('Extraction has been failed'); |
||
| 90 | $fsHelper->removeIfExists($locator->getExtractionBaseDir()); |
||
| 91 | throw $e; |
||
| 92 | } |
||
| 93 | |||
| 94 | $tmpDir = $locator->getExtractionBaseDir() . '/' . $installedVersion; |
||
| 95 | $fsHelper->removeIfExists($tmpDir); |
||
| 96 | $fsHelper->mkdir($tmpDir); |
||
| 97 | $fsHelper->mkdir($tmpDir . '/config'); |
||
| 98 | $oldSourcesDir = $locator->getOwncloudRootPath(); |
||
| 99 | $newSourcesDir = $fullExtractionPath . '/owncloud'; |
||
| 100 | |||
| 101 | foreach ($locator->getRootDirContent() as $dir){ |
||
| 102 | $this->getApplication()->getLogger()->debug('Replacing ' . $dir); |
||
| 103 | $fsHelper->tripleMove($oldSourcesDir, $newSourcesDir, $tmpDir, $dir); |
||
| 104 | } |
||
| 105 | |||
| 106 | try { |
||
| 107 | $appDirectories = $fsHelper->scandirFiltered($oldSourcesDir . '/apps'); |
||
| 108 | foreach ($appDirectories as $appDirectory){ |
||
| 109 | $fsHelper->rmdirr($oldSourcesDir . '/apps/' . $appDirectory); |
||
| 110 | } |
||
| 111 | $plain = $this->occRunner->run('upgrade'); |
||
| 112 | $output->writeln($plain); |
||
| 113 | } catch (ProcessFailedException $e){ |
||
| 114 | $lastCheckpointId = $checkpoint->getLastCheckpointId(); |
||
| 115 | if ($lastCheckpointId){ |
||
| 116 | $lastCheckpointPath = $checkpoint->getCheckpointPath($lastCheckpointId); |
||
| 117 | $fsHelper->copyr($lastCheckpointPath . '/apps', $oldSourcesDir . '/apps', false) |
||
| 118 | } |
||
|
|
|||
| 119 | if ($e->getProcess()->getExitCode() != 3){ |
||
| 120 | throw ($e); |
||
| 121 | } |
||
| 127 |