calculateSortValue()   D
last analyzed

Complexity

Conditions 18
Paths 20

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 39
nc 20
nop 2
dl 0
loc 69
ccs 0
cts 36
cp 0
crap 342
rs 4.8666
c 1
b 0
f 0

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
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Helper\ComponentPosition;
15
16
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
17
18
/**
19
 * @author Daniel West <[email protected]>
20
 */
21
class ComponentPositionSortValueHelper
22
{
23
    public function calculateSortValue(ComponentPosition $componentPosition, ?int $originalSortValue): void
24
    {
25
        $sortCollection = $componentPosition->getSortCollection();
26
        $sortValueSet = null !== $componentPosition->sortValue;
27
        if (!$sortCollection) {
28
            if (!$sortValueSet) {
29
                $componentPosition->setSortValue(0);
30
            }
31
32
            return;
33
        }
34
35
        if (null !== $originalSortValue) {
36
            $moveTo = $componentPosition->sortValue;
37
            if ($moveTo === $originalSortValue) {
38
                return;
39
            }
40
41
            $positionIsSame = static function (ComponentPosition $posA, ComponentPosition $posB) {
42
                return $posA->getId() === $posB->getId();
43
            };
44
45
            if ($moveTo > $originalSortValue) {
46
                // value increased
47
                foreach ($sortCollection as $existingComponentPosition) {
48
                    if ($positionIsSame($existingComponentPosition, $componentPosition)) {
49
                        continue;
50
                    }
51
                    if (
52
                        $existingComponentPosition->sortValue > $originalSortValue
53
                        && $existingComponentPosition->sortValue <= $moveTo
54
                    ) {
55
                        --$existingComponentPosition->sortValue;
56
                    }
57
                }
58
59
                return;
60
            }
61
62
            // value decreased
63
            foreach ($sortCollection as $existingComponentPosition) {
64
                if ($positionIsSame($existingComponentPosition, $componentPosition)) {
65
                    continue;
66
                }
67
                if (
68
                    $existingComponentPosition->sortValue < $originalSortValue
69
                    && $existingComponentPosition->sortValue >= $moveTo
70
                ) {
71
                    ++$existingComponentPosition->sortValue;
72
                }
73
            }
74
75
            return;
76
        }
77
78
        if (!$sortValueSet) {
79
            /** @var ComponentPosition|null $lastPosition */
80
            $lastPosition = $sortCollection->last();
81
            if ($lastPosition) {
82
                $nextValue = $lastPosition->sortValue + 1;
83
                $componentPosition->setSortValue($nextValue);
84
            } else {
85
                $componentPosition->setSortValue(0);
86
            }
87
        }
88
89
        foreach ($sortCollection as $existingComponentPosition) {
90
            if ($existingComponentPosition->sortValue >= $componentPosition->sortValue) {
91
                ++$existingComponentPosition->sortValue;
92
            }
93
        }
94
    }
95
}
96