DiffCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 10
dl 0
loc 85
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
C executeConfigured() 0 72 11
1
<?php
2
3
namespace Storeman\Cli\Command;
4
5
use Storeman\Cli\Helper\DisplayIndexComparisonHelper;
6
use Storeman\ConflictHandler\PreferLocalConflictHandler;
7
use Storeman\Index\Index;
8
use Storeman\Storeman;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * The DiffCommand can be used to display the difference of two different states
15
 */
16
class DiffCommand extends AbstractCommand
17
{
18
    protected function configure()
19
    {
20
        parent::configure();
21
22
        $this->setName('diff');
23
        $this->setDescription('Displays the difference of a revision and another revision or the current local state.');
24
        $this->addArgument('revision', InputArgument::OPTIONAL, 'Revision for comparison. Defaults to the last revision.');
25
        $this->addArgument('compareTo', InputArgument::OPTIONAL, 'Optional other revision number for index identification to compare against. Defaults to none for comparison with the current local state.');
26
    }
27
28
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int
29
    {
30
        $revision = intval($input->getArgument('revision')) ?: $storeman->getLastRevision();
31
32
        if ($revision === null)
33
        {
34
            $this->consoleStyle->writeln("Could not find any past synchronizations to compare against.");
35
36
            return 0;
37
        }
38
39
        $vaults = $storeman->getVaultContainer();
40
        $vault = $vaults->getPrioritizedVault($vaults->getVaultsHavingRevision($revision));
41
42
        if ($vault === null)
43
        {
44
            $this->consoleStyle->error("Could not find requested revision {$revision} in any vault.");
45
46
            return 1;
47
        }
48
49
        $index = $vault->getRemoteIndex($revision);
50
51
52
        $compareTo = $input->getArgument('compareTo') ? intval($input->getArgument('compareTo')) : null;
53
54
        if ($compareTo === 0)
55
        {
56
            $this->consoleStyle->error("Invalid argument compareTo given.");
57
58
            return 1;
59
        }
60
        elseif ($compareTo === null)
61
        {
62
            $compareToIndex = $storeman->getLocalIndex();
63
        }
64
        else
65
        {
66
            $vault = $vaults->getPrioritizedVault($vaults->getVaultsHavingRevision($compareTo));
67
68
            if ($vault === null)
69
            {
70
                $this->consoleStyle->error("Could not find revision {$compareTo} in any vault.");
71
72
                return 1;
73
            }
74
75
            $compareToIndex = $vault->getRemoteIndex($compareTo);
76
77
            assert($compareToIndex instanceof Index);
78
        }
79
80
81
        $mergedIndex = $vault->getIndexMerger()->merge(new PreferLocalConflictHandler(), $index, $compareToIndex, $index);
0 ignored issues
show
Bug introduced by
It seems like $index defined by $vault->getRemoteIndex($revision) on line 49 can be null; however, Storeman\IndexMerger\IndexMergerInterface::merge() 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...
82
        $diff = $index->getDifference($mergedIndex);
83
84
        if ($diff->count() > 0)
85
        {
86
            $output->writeln(sprintf("Found %d difference(s):\n", $diff->count() ?: 'No'));
87
88
            /** @var DisplayIndexComparisonHelper $helper */
89
            $helper = $this->getHelper('displayIndexComparison');
90
            $helper->displayIndexComparison($diff, $output, "r{$revision}", $compareTo ? "r{$compareTo}" : "local");
91
        }
92
93
        else
94
        {
95
            $output->writeln("No differences found.");
96
        }
97
98
        return 0;
99
    }
100
}
101