Completed
Push — master ( 9b8375...f2b5a9 )
by Andreas
01:43
created

DateIntervalComparator::compareDatePart()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
/**
3
 * Copyright (c) Andreas Heigl<[email protected]>
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 * The above copyright notice and this permission notice shall be included in
11
 * all copies or substantial portions of the Software.
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 *
20
 * @author    Andreas Heigl<[email protected]>
21
 * @copyright Andreas Heigl
22
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
23
 * @since     11.10.2016
24
 * @link      http://github.com/heiglandreas/org.heigl.DateIntervalComparator
25
 */
26
27
namespace Org_Heigl\DateIntervalComparator;
28
29
use DateInterval;
30
use UnexpectedValueException;
31
32
class DateIntervalComparator
33
{
34
    private $safe = false;
35
36
    public function __construct(bool $safe = false)
37
    {
38
        $this->safe = $safe;
39
    }
40
41
    /** @deprecated Use the constructor injected safety flag */
42
    public function safe($safe = true): void
43
    {
44
        $this->safe = $safe;
45
    }
46
47
    /**
48
     * COmpare the two date intervals.
49
     *
50
     * If the first contains a larger timespan we return 1, if the second contains more
51
     * we return -1 and when they are equals we return 0.
52
     *
53
     * @param DateInterval $first
54
     * @param DateInterval $second
55
     *
56
     * @return int
57
     */
58
    public function compare(DateInterval $first, DateInterval $second): int
59
    {
60
        if ($this->safe) {
61
            $this->safecheck($first);
62
            $this->safecheck($second);
63
        }
64
65
        if (0 !== $datePart = $this->compareDatePart($first, $second)) {
66
            return $datePart;
67
        }
68
69
        return $this->compareTimePart($first, $second);
70
    }
71
72 View Code Duplication
    private function compareDatePart(DateInterval $first, DateInterval $second): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
    {
74
        if (0 !== $year = $this->compareValue($first->y, $second->y)) {
75
            return $year;
76
        }
77
78
        if (0 !== $month = $this->compareValue($first->m, $second->m)) {
79
            return $month;
80
        }
81
82
        if (0 !== $day = $this->compareValue($first->d, $second->d)) {
83
            return $day;
84
        }
85
86
        return 0;
87
    }
88
89 View Code Duplication
    private function compareTimePart(DateInterval $first, DateInterval $second): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91
        if (0 !== $hour = $this->compareValue($first->h, $second->h)) {
92
            return $hour;
93
        }
94
95
        if (0 !== $minute = $this->compareValue($first->i, $second->i)) {
96
            return $minute;
97
        }
98
99
        if (0 !== $second = $this->compareValue($first->s, $second->s)) {
100
            return $second;
101
        }
102
103
        return 0;
104
    }
105
106
    private function compareValue(int $a, int $b): int
107
    {
108
        if ($a < $b) {
109
            return -1;
110
        }
111
112
        if ($a > $b) {
113
            return 1;
114
        }
115
116
        return 0;
117
    }
118
119
    private function safecheck(DateInterval $interval): void
120
    {
121
        if ($interval->m > 12) {
122
            throw new UnexpectedValueException('Month exceeds value 12');
123
        }
124
125
        if ($interval->d > 31) {
126
            throw new UnexpectedValueException('Day exceeds value 31');
127
        }
128
129
        // 25 due to DST-Transitions
130
        if ($interval->h > 25) {
131
            throw new UnexpectedValueException('Hour exceeds value 25');
132
        }
133
134
        if ($interval->i > 60) {
135
            throw new UnexpectedValueException('Minute exceeds value 60');
136
        }
137
138
        // 61 due to leap-seconds
139
        if ($interval->s > 61) {
140
            throw new UnexpectedValueException('Second exceeds value 61');
141
        }
142
    }
143
}
144