Completed
Push — master ( 2cde75...6c52d6 )
by Arne
02:04
created

StandardIndexMerger::merge()   F

Complexity

Conditions 19
Paths 3136

Size

Total Lines 109
Code Lines 40

Duplication

Lines 26
Ratio 23.85 %

Importance

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

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 Storeman\IndexMerger;
4
5
use Storeman\ConflictHandler\ConflictHandlerInterface;
6
use Storeman\Index;
7
use Storeman\IndexObject;
8
9
class StandardIndexMerger implements IndexMergerInterface
10
{
11
    public function merge(ConflictHandlerInterface $conflictHandler, 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
                // we explicitly want to use equality instead of identity
42
                $remoteObjectModified = !($lastLocalObject == $remoteObject);
43
            }
44
45
            // object has been created
46
            else
47
            {
48
                $localObjectModified = true;
49
50
                // object has been created since last synchronization
51
                $remoteObjectModified = $remoteObject !== null;
52
            }
53
54
55
            // conflict if both the local and the remote object has been changed
56 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...
57
            {
58
                $this->conflict($conflictHandler, $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, Storeman\IndexMerger\Sta...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...
59
            }
60
61
            // add the remote object if only it has been modified
62
            elseif ($remoteObjectModified)
63
            {
64
                $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, Storeman\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...
65
            }
66
67
            // add the local object otherwise if only it or none has been modified
68
            else
69
            {
70
                $mergedIndex->addObject($localObject);
71
            }
72
        }
73
74
        foreach ($remoteIndex as $remoteObject)
75
        {
76
            /** @var IndexObject $remoteObject */
77
78
            $localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath());
79
            $lastLocalObject = $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath());
80
81
            // only consider objects not existing locally as those are already considered by the first loop
82
            if (!$localObject)
83
            {
84
                if ($lastLocalObject)
85
                {
86
                    // local object has been deleted
87
                    $localObjectModified = true;
88
89
                    // compare remote object to object state at last sync
90
                    // we explicitly want to use equality instead of identity
91
                    $remoteObjectModified = !($remoteObject == $lastLocalObject);
92
                }
93
94
                else
95
                {
96
                    // object already didn't exist locally at the last sync
97
                    $localObjectModified = false;
98
99
                    // another client added the remote object
100
                    $remoteObjectModified = true;
101
                }
102
103
104
                // conflict if both the local and the remote object has been changed
105 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...
106
                {
107
                    $this->conflict($conflictHandler, $mergedIndex, $remoteObject, $localObject, $lastLocalObject);
108
                }
109
110
                // another client added the remote object
111
                elseif (!$lastLocalObject)
112
                {
113
                    $mergedIndex->addObject($remoteObject);
114
                }
115
            }
116
        }
117
118
        return $mergedIndex;
119
    }
120
121
    protected function conflict(ConflictHandlerInterface $conflictHandler, Index $mergedIndex, IndexObject $remoteObject, IndexObject $localObject = null, IndexObject $lastLocalObject = null): void
122
    {
123
        $solution = $conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalObject);
124
125
        switch ($solution)
126
        {
127
            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...
128
129
                if ($localObject)
130
                {
131
                    $mergedIndex->addObject($localObject);
132
                }
133
134
                break;
135
136
            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...
137
138
                if ($remoteObject)
139
                {
140
                    $mergedIndex->addObject($remoteObject);
141
                }
142
143
                break;
144
145
            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...
146
147
                throw new \LogicException();
148
        }
149
    }
150
}
151