Completed
Push — master ( 38df29...afa4ca )
by
unknown
7s
created

PostUpgradeCleanupCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 4
Ratio 13.79 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 4
loc 29
ccs 0
cts 21
cp 0
rs 8.8571
cc 2
eloc 19
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Command;
23
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
27
class PostUpgradeCleanupCommand extends Command {
28
29
	protected function configure(){
30
		$this
31
				->setName('upgrade:postUpgradeCleanup')
32
				->setDescription('repair and cleanup step 2 (online) [danger, might take long]')
33
		;
34
	}
35
36
	protected function execute(InputInterface $input, OutputInterface $output){
37
		$registry = $this->container['utils.registry'];
38
		$fsHelper = $this->container['utils.filesystemhelper'];
39
		$locator = $this->container['utils.locator'];
40
41
		//Update updater
42
		$feed = $registry->get('feed');
43
		$fullExtractionPath = $locator->getExtractionBaseDir() . '/' . $feed->getVersion();
44
		$tmpDir = $locator->getExtractionBaseDir() . '/' . $registry->get('installedVersion');
45
		$oldSourcesDir = $locator->getOwncloudRootPath();
46
		$newSourcesDir = $fullExtractionPath . '/owncloud';
47
		$newUpdaterDir = $newSourcesDir . '/updater';
48
		$oldUpdaterDir = $oldSourcesDir . '/updater';
49
		$tmpUpdaterDir = $tmpDir . '/updater';
50
		$fsHelper->mkdir($tmpUpdaterDir);
51
52 View Code Duplication
		foreach ($locator->getUpdaterContent() as $dir){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
			$this->getApplication()->getLogger()->debug('Moving updater/' . $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...
54
			$fsHelper->tripleMove($oldUpdaterDir, $newUpdaterDir, $tmpUpdaterDir, $dir);
55
		}
56
		
57
		//Cleanup Filesystem
58
		$fsHelper->removeIfExists($locator->getExtractionBaseDir());
59
60
		//Cleanup updater cache
61
		$registry->clearAll();
62
		
63
		$output->writeln('Done');
64
	}
65
66
}
67