Completed
Push — master ( 77f170...166d1e )
by Arne
03:02
created

StandardIndexMerger::setConflictHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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