StdRangesCalculator   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 107
Duplicated Lines 9.35 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 1
dl 10
loc 107
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sortRanges() 0 6 1
A sum() 0 9 2
A sub() 0 8 2
B removeRangeFromRanges() 10 34 9
B mergeRanges() 0 23 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Hop\Ranges;
6
7
final class StdRangesCalculator implements RangesCalculator
8
{
9
    /**
10
     * @inheritdoc
11
     */
12 4
    public function sum(Range ...$ranges): array
13
    {
14 4
        if (\count($ranges) === 0) {
15 1
            return [];
16
        }
17
18 3
        $this->sortRanges($ranges);
19 3
        return $this->mergeRanges($ranges);
20
    }
21
22
    /**
23
     * @param Range[] $ranges
24
     */
25
    private function sortRanges(array &$ranges): void
26
    {
27 3
        \usort($ranges, function (Range $range1, Range $range2): int {
28 3
            return $range1->dateFrom() <=> $range2->dateFrom();
29 3
        });
30 3
    }
31
32
    /**
33
     * @inheritdoc
34
     * @throws \Exception
35
     */
36 6
    public function sub(Range $minuend, Range ...$subtrahends): array
37
    {
38 6
        $ranges = [$minuend];
39 6
        foreach ($subtrahends as $subtrahend) {
40 6
            $ranges = $this->removeRangeFromRanges($subtrahend, $ranges);
41
        }
42 6
        return $ranges;
43
    }
44
45
    /**
46
     * @param Range $range
47
     * @param Range[] $ranges
48
     * @return Range[]
49
     * @throws \Exception
50
     */
51 6
    private function removeRangeFromRanges(Range $range, array $ranges): array
52
    {
53 6
        $resultRanges = [];
54 6
        $interval = new \DateInterval('PT1S');
55 6
        foreach ($ranges as $minuend) {
56 6
            if ($range->dateFrom() < $minuend->dateFrom() && $range->dateTo() > $minuend->dateTo()) {
57 1
                continue;
58
            }
59
60 5
            if ($minuend->dateTo() < $range->dateFrom() || $minuend->dateFrom() > $range->dateTo()) {
61 3
                $resultRanges[] = $minuend;
62 3
                continue;
63
            }
64
65 4
            if ($range->dateFrom() > $minuend->dateFrom()) {
66 3
                $dateTo = new \DateTime($range->dateFrom()->format('Y-m-d H:i:s'));
67 3
                $dateTo->sub($interval);
68 3
                $resultRanges[] = new Range($minuend->dateFrom(), $dateTo);
69 3 View Code Duplication
                if ($range->dateTo() < $minuend->dateTo()) {
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...
70 2
                    $dateFrom = new \DateTime($range->dateTo()->format('Y-m-d H:i:s'));
71 2
                    $dateFrom->add($interval);
72 2
                    $resultRanges[] = new Range($dateFrom, $minuend->dateTo());
73
                }
74 3
                continue;
75
            }
76
77 2 View Code Duplication
            if ($range->dateTo() < $minuend->dateTo()) {
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...
78 2
                $dateFrom = new \DateTime($range->dateTo()->format('Y-m-d H:i:s'));
79 2
                $dateFrom->add($interval);
80 2
                $resultRanges[] = new Range($dateFrom, $minuend->dateTo());
81
            }
82
        }
83 6
        return $resultRanges;
84
    }
85
86
    /**
87
     * @param Range[] $ranges
88
     * @return array
89
     */
90 3
    private function mergeRanges(array $ranges): array
91
    {
92 3
        $result = [];
93 3
        $startRange = null;
94 3
        $endRange = null;
95 3
        foreach ($ranges as $key => $range) {
96 3
            $nextRange = @$ranges[$key + 1];
97 3
            if ($startRange === null) {
98 3
                $startRange = $range->dateFrom();
99
            }
100
101 3
            if ($endRange === null || $range->dateTo() > $endRange) {
102 3
                $endRange = $range->dateTo();
103
            }
104
105 3
            if ($nextRange == null || $nextRange->dateFrom() > $range->dateTo()) {
106 3
                $result[] = new Range($startRange, $endRange);
107 3
                $startRange = null;
108 3
                $endRange = null;
109
            }
110
        }
111 3
        return $result;
112
    }
113
}
114