Completed
Push — master ( 63b887...b350e3 )
by Arne
01:47
created

StandardIndexMerger::merge()   F

Complexity

Conditions 19
Paths 3136

Size

Total Lines 107
Code Lines 40

Duplication

Lines 26
Ratio 24.3 %

Importance

Changes 0
Metric Value
dl 26
loc 107
rs 2
c 0
b 0
f 0
cc 19
eloc 40
nc 3136
nop 3

How to fix   Long Method    Complexity   

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 Archivr\IndexMerger;
4
5
use Archivr\ConflictHandler\ConflictHandlerInterface;
6
use Archivr\ConflictHandler\PanickingConflictHandler;
7
use Archivr\Index;
8
use Archivr\IndexObject;
9
10
class StandardIndexMerger implements IndexMergerInterface
11
{
12
    /**
13
     * @var ConflictHandlerInterface
14
     */
15
    protected $conflictHandler;
16
17
    public function setConflictHandler(ConflictHandlerInterface $conflictHandler = null): IndexMergerInterface
18
    {
19
        $this->conflictHandler = $conflictHandler;
20
21
        return $this;
22
    }
23
24
    public function getConflictHandler(): ConflictHandlerInterface
25
    {
26
        if ($this->conflictHandler === null)
27
        {
28
            $this->setConflictHandler(new PanickingConflictHandler());
29
        }
30
31
        return $this->conflictHandler;
32
    }
33
34
    public function merge(Index $remoteIndex, Index $localIndex, Index $lastLocalIndex = null): Index
35
    {
36
        $mergedIndex = new Index();
37
38
        if ($lastLocalIndex === null)
39
        {
40
            // lets make our life easier
41
            $lastLocalIndex = new Index(new \DateTime('@0'));
42
        }
43
44
        foreach ($localIndex as $localObject)
45
        {
46
            /** @var IndexObject $localObject */
47
48
            $remoteObject = $remoteIndex->getObjectByPath($localObject->getRelativePath());
49
            $lastLocalObject = $lastLocalIndex->getObjectByPath($localObject->getRelativePath());
50
51
52
            // compare existing to known object
53
            if ($lastLocalObject)
54
            {
55
                $localObjectModified = false;
56
                $localObjectModified = $localObjectModified || ($localObject->getType() !== $lastLocalObject->getType());
57
                $localObjectModified = $localObjectModified || ($localObject->getMtime() !== $lastLocalObject->getMtime());
58
                $localObjectModified = $localObjectModified || ($localObject->getCtime() !== $lastLocalObject->getCtime());
59
                $localObjectModified = $localObjectModified || ($localObject->getMode() !== $lastLocalObject->getMode());
60
                $localObjectModified = $localObjectModified || ($localObject->getSize() !== $lastLocalObject->getSize());
61
                $localObjectModified = $localObjectModified || ($localObject->getLinkTarget() !== $lastLocalObject->getLinkTarget());
62
63
                // remote object has been modified if it does not equal the object on its last synchronization
64
                $remoteObjectModified = !$lastLocalObject->equals($remoteObject);
65
            }
66
67
            // object has been created
68
            else
69
            {
70
                $localObjectModified = true;
71
72
                // object has been created since last synchronization
73
                $remoteObjectModified = $remoteObject !== null;
74
            }
75
76
77
            // conflict if both the local and the remote object has been changed
78 View Code Duplication
            if ($localObjectModified && $remoteObjectModified)
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...
79
            {
80
                $this->conflict($mergedIndex, $remoteObject, $localObject, $lastLocalObject);
0 ignored issues
show
Bug introduced by
It seems like $remoteObject defined by $remoteIndex->getObjectB...ect->getRelativePath()) on line 48 can be null; however, Archivr\IndexMerger\Stan...IndexMerger::conflict() 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...
81
            }
82
83
            // add the remote object if only it has been modified
84
            elseif ($remoteObjectModified)
85
            {
86
                $mergedIndex->addObject($remoteObject);
0 ignored issues
show
Bug introduced by
It seems like $remoteObject defined by $remoteIndex->getObjectB...ect->getRelativePath()) on line 48 can be null; however, Archivr\Index::addObject() 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...
87
            }
88
89
            // add the local object otherwise if only it or none has been modified
90
            else
91
            {
92
                $mergedIndex->addObject($localObject);
93
            }
94
        }
95
96
        foreach ($remoteIndex as $remoteObject)
97
        {
98
            /** @var IndexObject $remoteObject */
99
100
            $localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath());
101
            $lastLocalObject = $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath());
102
103
            // only consider objects not existing locally as those are already considered by the first loop
104
            if (!$localObject)
105
            {
106
                if ($lastLocalObject)
107
                {
108
                    // local object has been deleted
109
                    $localObjectModified = true;
110
111
                    // compare remote object to object state at last sync
112
                    $remoteObjectModified = !$remoteObject->equals($lastLocalObject);
113
                }
114
115
                else
116
                {
117
                    // object already didn't exist locally at the last sync
118
                    $localObjectModified = false;
119
120
                    // another client added the remote object
121
                    $remoteObjectModified = true;
122
                }
123
124
125
                // conflict if both the local and the remote object has been changed
126 View Code Duplication
                if ($localObjectModified && $remoteObjectModified)
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...
127
                {
128
                    $this->conflict($mergedIndex, $remoteObject, $localObject, $lastLocalObject);
129
                }
130
131
                // another client added the remote object
132
                elseif (!$lastLocalObject)
133
                {
134
                    $mergedIndex->addObject($remoteObject);
135
                }
136
            }
137
        }
138
139
        return $mergedIndex;
140
    }
141
142
    protected function conflict(Index $mergedIndex, IndexObject $remoteObject, IndexObject $localObject = null, IndexObject $lastLocalObject = null): void
143
    {
144
        $solution = $this->getConflictHandler()->handleConflict($remoteObject, $localObject, $lastLocalObject);
145
146
        switch ($solution)
147
        {
148
            case ConflictHandlerInterface::USE_LOCAL:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
149
150
                if ($localObject)
151
                {
152
                    $mergedIndex->addObject($localObject);
153
                }
154
155
                break;
156
157
            case ConflictHandlerInterface::USE_REMOTE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
158
159
                if ($remoteObject)
160
                {
161
                    $mergedIndex->addObject($remoteObject);
162
                }
163
164
                break;
165
166
            default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
167
168
                throw new \LogicException();
169
        }
170
    }
171
}
172