Completed
Pull Request — master (#217)
by Thomas
02:42
created

ExecuteCoreUpgradeScriptsCommand::execute()   C

Complexity

Conditions 9
Paths 37

Size

Total Lines 58
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 58
ccs 0
cts 51
cp 0
rs 6.9928
cc 9
eloc 42
nc 37
nop 2
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @author Victor Dubiniuk <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace Owncloud\Updater\Command;
24
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Owncloud\Updater\Utils\OccRunner;
30
use Owncloud\Updater\Utils\ZipExtractor;
31
use Owncloud\Updater\Utils\BzipExtractor;
32
33
class ExecuteCoreUpgradeScriptsCommand extends Command {
34
35
	/**
36
	 * @var OccRunner $occRunner
37
	 */
38
	protected $occRunner;
39
40
	public function __construct($occRunner){
41
		parent::__construct();
42
		$this->occRunner = $occRunner;
43
	}
44
45
	protected function configure(){
46
		$this
47
				->setName('upgrade:executeCoreUpgradeScripts')
48
				->setDescription('execute core upgrade scripts [danger, might take long]');
49
	}
50
51
	protected function execute(InputInterface $input, OutputInterface $output){
52
		$locator = $this->container['utils.locator'];
53
		$fsHelper = $this->container['utils.filesystemhelper'];
54
		$registry = $this->container['utils.registry'];
55
		$fetcher = $this->container['utils.fetcher'];
56
57
		$feed = $registry->get('feed');
58
59
		if ($feed){
60
			$path = $fetcher->getBaseDownloadPath($feed);
61
			$fullExtractionPath = $locator->getExtractionBaseDir() . '/' . $feed->getVersion();
62
63
			if (!file_exists($fullExtractionPath)){
64
				try{
65
					$fsHelper->mkdir($fullExtractionPath, true);
66
				} catch (\Exception $ex){
67
					$output->writeln('Unable create directory ' . $fullExtractionPath);
68
				}
69
			} else {
70
				$fsHelper->removeIfExists($fullExtractionPath);
71
			}
72
			$output->writeln('Extracting source into ' . $fullExtractionPath);
73
			if (preg_match('|\.tar\.bz2$|', $path)){
74
				$extractor = new BzipExtractor($path, $fullExtractionPath);
75
			} else {
76
				$extractor = new ZipExtractor($path, $fullExtractionPath);
77
			}
78
			try{
79
				$extractor->extract();
80
			} catch (\Exception $e){
81
				$output->writeln('Extraction has been failed');
82
				$fsHelper->removeIfExists($locator->getExtractionBaseDir());
83
				throw $e;
84
			}
85
			
86
			$tmpDir = $locator->getExtractionBaseDir() . '/' . implode('.', $locator->getInstalledVersion());
87
			$fsHelper->removeIfExists($tmpDir);
88
			$fsHelper->mkdir($tmpDir);
89
			$fsHelper->mkdir($tmpDir . '/config');
90
			$oldSourcesDir = $locator->getOwncloudRootPath();
91
			$newSourcesDir = $fullExtractionPath . '/owncloud';
92
			$newSources = $locator->getRootDirContent();
93
			foreach ($newSources as $dir){
94
				$this->getApplication()->getLogger()->debug('Moving ' . $dir);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getLogger() does only exist in the following sub-classes of Symfony\Component\Console\Application: Owncloud\Updater\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
95
				if (file_exists($oldSourcesDir . '/' . $dir)){
96
					$fsHelper->move($oldSourcesDir . '/' . $dir, $tmpDir . '/' . $dir);
97
				}
98
				if (file_exists($newSourcesDir . '/' . $dir)){
99
					$fsHelper->move($newSourcesDir . '/' . $dir, $oldSourcesDir . '/' . $dir);
100
				}
101
			}
102
103
			$plain = $this->occRunner->run('upgrade');
104
			$output->writeln($plain);
105
106
		}
107
108
	}
109
110
}
111