Completed
Push — master ( 9eb332...ef5bef )
by Arne
02:27
created

ShowIndexCommand::executeConfigured()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 54
rs 8.0703
cc 7
nc 11
nop 3

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
namespace Storeman\Cli\Command;
4
5
use Storeman\Cli\Helper\DisplayIndexHelper;
6
use Storeman\Storeman;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ShowIndexCommand extends AbstractCommand
12
{
13
    protected function configure()
14
    {
15
        parent::configure();
16
17
        $this->setName('show-index');
18
        $this->setDescription('Displays an index');
19
        $this->addArgument('revision', InputArgument::OPTIONAL, 'Revision number, \'latest\' or \'local\'. Defaults to \'latest\'.', 'latest');
20
    }
21
22
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int
23
    {
24
        $revision = $input->getArgument('revision');
25
        $index = null;
26
27
        if ($revision === 'latest')
28
        {
29
            $revision = $storeman->getLastRevision();
30
31
            if ($revision === null)
32
            {
33
                $this->consoleStyle->writeln("Could not find any past synchronizations to show.");
34
35
                return 0;
36
            }
37
        }
38
        elseif ($revision === 'local')
39
        {
40
            $index = $storeman->getLocalIndex();
41
        }
42
        elseif (ctype_digit($revision))
43
        {
44
            $revision = intval($revision);
45
        }
46
        else
47
        {
48
            $this->consoleStyle->error("Argument 'revision' invalid.");
49
50
            return 1;
51
        }
52
53
        if ($index === null)
54
        {
55
            assert(is_int($revision));
56
57
            $vaults = $storeman->getVaultContainer();
58
            $vault = $vaults->getPrioritizedVault($vaults->getVaultsHavingRevision($revision));
59
60
            if ($vault === null)
61
            {
62
                $this->consoleStyle->error("Could not find requested revision {$revision} in any vault.");
63
64
                return 1;
65
            }
66
67
            $index = $vault->loadRemoteIndex($revision);
68
        }
69
70
        /** @var DisplayIndexHelper $displayIndexHelper */
71
        $displayIndexHelper = $this->getHelper('displayIndex');
72
        $displayIndexHelper->displayIndex($index, $output);
0 ignored issues
show
Bug introduced by
It seems like $index can be null; however, displayIndex() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
73
74
        return 0;
75
    }
76
}
77