Completed
Push — master ( 54a784...bf1097 )
by Tomasz
03:00
created

StdRangesCalculator::removeRangeFromRanges()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 36

Duplication

Lines 10
Ratio 27.78 %

Code Coverage

Tests 25
CRAP Score 9

Importance

Changes 0
Metric Value
dl 10
loc 36
ccs 25
cts 25
cp 1
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 2
crap 9
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
        $newRangeFrom = null;
0 ignored issues
show
Unused Code introduced by
$newRangeFrom is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56 6
        $newRangeTo = null;
0 ignored issues
show
Unused Code introduced by
$newRangeTo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57 6
        foreach ($ranges as $minuend) {
58 6
            if ($range->dateFrom() < $minuend->dateFrom() && $range->dateTo() > $minuend->dateTo()) {
59 1
                continue;
60
            }
61
62 5
            if ($minuend->dateTo() < $range->dateFrom() || $minuend->dateFrom() > $range->dateTo()) {
63 3
                $resultRanges[] = $minuend;
64 3
                continue;
65
            }
66
67 4
            if ($range->dateFrom() > $minuend->dateFrom()) {
68 3
                $dateTo = new \DateTime($range->dateFrom()->format('Y-m-d H:i:s'));
69 3
                $dateTo->sub($interval);
70 3
                $resultRanges[] = new Range($minuend->dateFrom(), $dateTo);
71 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...
72 2
                    $dateFrom = new \DateTime($range->dateTo()->format('Y-m-d H:i:s'));
73 2
                    $dateFrom->add($interval);
74 2
                    $resultRanges[] = new Range($dateFrom, $minuend->dateTo());
75
                }
76 3
                continue;
77
            }
78
79 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...
80 2
                $dateFrom = new \DateTime($range->dateTo()->format('Y-m-d H:i:s'));
81 2
                $dateFrom->add($interval);
82 2
                $resultRanges[] = new Range($dateFrom, $minuend->dateTo());
83
            }
84
        }
85 6
        return $resultRanges;
86
    }
87
88
    /**
89
     * @param Range[] $ranges
90
     * @return array
91
     */
92 3
    private function mergeRanges(array $ranges): array
93
    {
94 3
        $result = [];
95 3
        $startRange = null;
96 3
        $endRange = null;
97 3
        foreach ($ranges as $key => $range) {
98 3
            $nextRange = @$ranges[$key + 1];
99 3
            if ($startRange === null) {
100 3
                $startRange = $range->dateFrom();
101
            }
102
103 3
            if ($endRange === null || $range->dateTo() > $endRange) {
104 3
                $endRange = $range->dateTo();
105
            }
106
107 3
            if ($nextRange == null || $nextRange->dateFrom() > $range->dateTo()) {
108 3
                $result[] = new Range($startRange, $endRange);
109 3
                $startRange = null;
110 3
                $endRange = null;
111
            }
112
        }
113 3
        return $result;
114
    }
115
}
116