Completed
Push — master ( caad92...bf4c7a )
by Peter
05:39
created

MonthIntervalComparator   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 131
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 131
loc 131
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A contains() 12 12 4
B intersect() 21 21 8
D intersectInterval() 40 40 9
A before() 4 4 1
A after() 4 4 1

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
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Component\Interval\Month;
11
12
use GpsLab\Component\Interval\IntervalType;
13
14 View Code Duplication
class MonthIntervalComparator
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    /**
17
     * @var MonthInterval
18
     */
19
    private $interval;
20
21
    /**
22
     * @param MonthInterval $interval
23
     */
24
    public function __construct(MonthInterval $interval)
25
    {
26
        $this->interval = $interval;
27
    }
28
29
    /**
30
     * @param MonthIntervalPoint $point
31
     *
32
     * @return bool
33
     */
34
    public function contains(MonthIntervalPoint $point)
35
    {
36
        if ($this->interval->startPoint()->eq($point)) {
37
            return !$this->interval->type()->startExcluded();
38
        }
39
40
        if ($this->interval->endPoint()->eq($point)) {
41
            return !$this->interval->type()->endExcluded();
42
        }
43
44
        return $this->interval->startPoint()->lt($point) && $this->interval->endPoint()->gt($point);
45
    }
46
47
    /**
48
     * @param MonthInterval $interval
49
     * @param bool $check_interval_type
50
     *
51
     * @return bool
52
     */
53
    public function intersect(MonthInterval $interval, $check_interval_type = true)
54
    {
55
        if (
56
            $this->interval->startPoint()->gt($interval->endPoint()) ||
57
            $this->interval->endPoint()->lt($interval->startPoint())
58
        ) {
59
            return false;
60
        }
61
62
        if ($check_interval_type) {
63
            if ($this->interval->startPoint()->eq($interval->endPoint())) {
64
                return !$this->interval->type()->startExcluded() && !$interval->type()->endExcluded();
65
            }
66
67
            if ($this->interval->endPoint()->eq($interval->startPoint())) {
68
                return !$this->interval->type()->endExcluded() && !$interval->type()->startExcluded();
69
            }
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * @param MonthInterval $interval
77
     *
78
     * @return MonthInterval|null
79
     */
80
    public function intersectInterval(MonthInterval $interval)
81
    {
82
        // intervals is not intersect or impossible create interval from one point
83
        if (
84
            $this->interval->startPoint()->gte($interval->endPoint()) ||
85
            $this->interval->endPoint()->lte($interval->startPoint())
86
        ) {
87
            // ignore closed intervals:
88
            // [a, b] | [b, c] = [b, b] = (b-1, b+1)
89
            return null;
90
        }
91
92
        $type = IntervalType::TYPE_CLOSED;
93
94
        if ($this->interval->startPoint()->lt($interval->startPoint())) {
95
            $start = $interval->startPoint();
96
            if ($interval->type()->startExcluded()) {
97
                $type |= IntervalType::TYPE_START_EXCLUDED;
98
            }
99
        } else {
100
            $start = $this->interval->startPoint();
101
            if ($this->interval->type()->startExcluded()) {
102
                $type |= IntervalType::TYPE_START_EXCLUDED;
103
            }
104
        }
105
106
        if ($this->interval->endPoint()->gt($interval->endPoint())) {
107
            $end = $interval->endPoint();
108
            if ($interval->type()->endExcluded()) {
109
                $type |= IntervalType::TYPE_END_EXCLUDED;
110
            }
111
        } else {
112
            $end = $this->interval->endPoint();
113
            if ($this->interval->type()->endExcluded()) {
114
                $type |= IntervalType::TYPE_END_EXCLUDED;
115
            }
116
        }
117
118
        return MonthInterval::create($start->value(), $end->value(), IntervalType::create($type));
119
    }
120
121
    /**
122
     * The point is before the interval
123
     *
124
     * @param MonthIntervalPoint $point
125
     *
126
     * @return bool
127
     */
128
    public function before(MonthIntervalPoint $point)
129
    {
130
        return $this->interval->startPoint()->gt($point);
131
    }
132
133
    /**
134
     * The point is after the interval
135
     *
136
     * @param MonthIntervalPoint $point
137
     *
138
     * @return bool
139
     */
140
    public function after(MonthIntervalPoint $point)
141
    {
142
        return $this->interval->endPoint()->lt($point);
143
    }
144
}
145