| Conditions | 16 |
| Paths | 74 |
| Total Lines | 68 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 34 | public function merge(Index $localIndex, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
||
| 35 | { |
||
| 36 | $mergedIndex = new Index(); |
||
| 37 | $conflictHandler = $this->getConflictHandler(); |
||
| 38 | |||
| 39 | // build new index from local index |
||
| 40 | foreach ($localIndex as $localObject) |
||
| 41 | { |
||
| 42 | /** @var IndexObject $localObject */ |
||
| 43 | |||
| 44 | $remoteObject = $remoteIndex ? $remoteIndex->getObjectByPath($localObject->getRelativePath()) : null; |
||
| 45 | |||
| 46 | $localObjectModified = $lastLocalIndex ? ($localObject->getMtime() > $lastLocalIndex->getCreated()->getTimestamp()) : true; |
||
| 47 | |||
| 48 | if ($remoteObject === null) |
||
| 49 | { |
||
| 50 | if ($localObjectModified) |
||
| 51 | { |
||
| 52 | $mergedIndex->addObject($localObject); |
||
| 53 | } |
||
| 54 | elseif ($remoteIndex === null) |
||
| 55 | { |
||
| 56 | $mergedIndex->addObject($localObject); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | else |
||
| 60 | { |
||
| 61 | $remoteObjectModified = $lastLocalIndex ? ($remoteObject->getMtime() > $lastLocalIndex->getCreated()->getTimestamp()) : false; |
||
| 62 | |||
| 63 | if (!$localObjectModified) |
||
| 64 | { |
||
| 65 | $mergedIndex->addObject($remoteObject); |
||
| 66 | } |
||
| 67 | |||
| 68 | elseif (!$remoteObjectModified) |
||
| 69 | { |
||
| 70 | $mergedIndex->addObject($localObject); |
||
| 71 | } |
||
| 72 | |||
| 73 | else |
||
| 74 | { |
||
| 75 | $conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalIndex ? $lastLocalIndex->getObjectByPath($localObject->getRelativePath()) : null); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($remoteIndex !== null) |
||
| 81 | { |
||
| 82 | // add remote index content |
||
| 83 | foreach ($remoteIndex as $remoteObject) |
||
| 84 | { |
||
| 85 | /** @var IndexObject $remoteObject */ |
||
| 86 | |||
| 87 | $localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath()); |
||
| 88 | $lastLocalObject = $lastLocalIndex ? $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath()) : null; |
||
| 89 | |||
| 90 | if ($localObject === null) |
||
| 91 | { |
||
| 92 | if ($lastLocalObject === null) |
||
| 93 | { |
||
| 94 | $mergedIndex->addObject($remoteObject); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return $mergedIndex; |
||
| 101 | } |
||
| 102 | } |
||
| 103 |