DateTimeInterval   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 386
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 37
lcom 2
cbo 5
dl 386
loc 386
ccs 39
cts 78
cp 0.5
rs 9.44
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A create() 4 4 1
A closed() 4 4 1
A halfClosed() 4 4 1
A halfOpen() 4 4 1
A open() 4 4 1
A equal() 4 4 1
A contains() 4 4 1
A intersects() 4 4 1
A intersection() 4 4 1
A cover() 4 4 1
A gap() 4 4 1
A abuts() 4 4 1
A join() 4 4 1
A union() 4 4 1
A before() 4 4 1
A after() 4 4 1
A period() 6 6 2
A type() 4 4 1
A start() 4 4 1
A end() 4 4 1
A startPoint() 4 4 1
A endPoint() 4 4 1
A withStart() 4 4 1
A withEnd() 4 4 1
A withType() 4 4 1
A __toString() 4 4 1
A fromString() 12 12 2
A iterate() 16 16 6

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\DateTime;
11
12
use GpsLab\Component\Interval\Exception\IncorrectIntervalException;
13
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException;
14
use GpsLab\Component\Interval\IntervalComparator;
15
use GpsLab\Component\Interval\ComparableIntervalInterface;
16
use GpsLab\Component\Interval\IntervalPointInterface;
17
use GpsLab\Component\Interval\IntervalType;
18
19 View Code Duplication
class DateTimeInterval implements ComparableIntervalInterface
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...
20
{
21
    /**
22
     * @var string
23
     */
24
    const REGEXP = '/^
25
        (?:\(|\[)                                       # start type char
26
        \s*
27
        (?<start>\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}) # start point
28
        \s*,\s*                                         # separator
29
        (?<end>\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})   # end point
30
        \s*
31
        (?:\)|\])                                       # end type char
32
    $/x';
33
34
    /**
35
     * @var IntervalType
36
     */
37
    private $type;
38
39
    /**
40
     * @var IntervalComparator
41
     */
42
    private $comparator;
43
44
    /**
45
     * @var DateTimeIntervalPoint
46
     */
47
    private $start;
48
49
    /**
50
     * @var DateTimeIntervalPoint
51
     */
52
    private $end;
53
54
    /**
55
     * @param DateTimeIntervalPoint $start
56
     * @param DateTimeIntervalPoint $end
57
     * @param IntervalType $type
58
     */
59 4
    private function __construct(DateTimeIntervalPoint $start, DateTimeIntervalPoint $end, IntervalType $type)
60
    {
61 4
        if ($start->gte($end)) {
62
            throw IncorrectIntervalException::create();
63
        }
64
65 4
        $this->type = $type;
66 4
        $this->start = $start;
67 4
        $this->end = $end;
68 4
        $this->comparator = new IntervalComparator($this);
69 4
    }
70
71
    /**
72
     * @param \DateTime $start
73
     * @param \DateTime $end
74
     * @param IntervalType $type
75
     *
76
     * @return self
77
     */
78 4
    public static function create(\DateTime $start, \DateTime $end, IntervalType $type)
79
    {
80 4
        return new self(new DateTimeIntervalPoint($start), new DateTimeIntervalPoint($end), $type);
81
    }
82
83
    /**
84
     * @param \DateTime $start
85
     * @param \DateTime $end
86
     *
87
     * @return self
88
     */
89 1
    public static function closed(\DateTime $start, \DateTime $end)
90
    {
91 1
        return static::create($start, $end, IntervalType::closed());
92
    }
93
94
    /**
95
     * @param \DateTime $start
96
     * @param \DateTime $end
97
     *
98
     * @return self
99
     */
100
    public static function halfClosed(\DateTime $start, \DateTime $end)
101
    {
102
        return static::create($start, $end, IntervalType::halfClosed());
103
    }
104
105
    /**
106
     * @param \DateTime $start
107
     * @param \DateTime $end
108
     *
109
     * @return self
110
     */
111
    public static function halfOpen(\DateTime $start, \DateTime $end)
112
    {
113
        return static::create($start, $end, IntervalType::halfOpen());
114
    }
115
116
    /**
117
     * @param \DateTime $start
118
     * @param \DateTime $end
119
     *
120
     * @return self
121
     */
122 1
    public static function open(\DateTime $start, \DateTime $end)
123
    {
124 1
        return static::create($start, $end, IntervalType::open());
125
    }
126
127
    /**
128
     * Create interval from string.
129
     *
130
     * Example formats for all interval types:
131
     *   [2016-12-09 02:55:00, 2016-12-21 12:30:12]
132
     *   (2015-03-07 12:04:45, 2015-10-19 19:38:14]
133
     *   [2014-09-11 17:31:09, 2015-02-08 23:45:58)
134
     *   (2013-10-27 15:03:37, 2013-10-30 05:06:34)
135
     *
136
     * Spaces are ignored in format.
137
     *
138
     * @param string $string
139
     *
140
     * @return self
141
     */
142 2
    public static function fromString($string)
143
    {
144 2
        if (!preg_match(self::REGEXP, $string, $match)) {
145
            throw InvalidIntervalFormatException::create('[YYYY-MM-DD HH:II:SS, YYYY-MM-DD HH:II:SS]', $string);
146
        }
147
148 2
        return self::create(
149 2
            new \DateTime($match['start']),
150 2
            new \DateTime($match['end']),
151 2
            IntervalType::fromString($string)
152
        );
153
    }
154
155
    /**
156
     * Checks if this interval is equal to the specified interval.
157
     *
158
     * @param DateTimeInterval $interval
159
     *
160
     * @return bool
161
     */
162
    public function equal(self $interval)
163
    {
164
        return $this->comparator->equal($interval);
165
    }
166
167
    /**
168
     * Does this interval contain the specified point.
169
     *
170
     * @param \DateTime $point
171
     *
172
     * @return bool
173
     */
174
    public function contains(\DateTime $point)
175
    {
176
        return $this->comparator->contains(new DateTimeIntervalPoint($point));
177
    }
178
179
    /**
180
     * Does this interval intersect the specified interval.
181
     *
182
     * @param DateTimeInterval $interval
183
     *
184
     * @return bool
185
     */
186
    public function intersects(self $interval)
187
    {
188
        return $this->comparator->intersects($interval);
189
    }
190
191
    /**
192
     * Gets the intersection between this interval and another interval.
193
     *
194
     * @param DateTimeInterval $interval
195
     *
196
     * @return self|null
197
     */
198
    public function intersection(self $interval)
199
    {
200
        return $this->comparator->intersection($interval);
201
    }
202
203
    /**
204
     * Gets the covered interval between this Interval and another interval.
205
     *
206
     * @param DateTimeInterval $interval
207
     *
208
     * @return self
209
     */
210
    public function cover(self $interval)
211
    {
212
        return $this->comparator->cover($interval);
213
    }
214
215
    /**
216
     * Gets the gap between this interval and another interval.
217
     *
218
     * @param DateTimeInterval $interval
219
     *
220
     * @return self|null
221
     */
222
    public function gap(self $interval)
223
    {
224
        return $this->comparator->gap($interval);
225
    }
226
227
    /**
228
     * Does this interval abuts with the interval specified.
229
     *
230
     * @param DateTimeInterval $interval
231
     *
232
     * @return bool
233
     */
234
    public function abuts(self $interval)
235
    {
236
        return $this->comparator->abuts($interval);
237
    }
238
239
    /**
240
     * Joins the interval between the adjacent.
241
     *
242
     * @param DateTimeInterval $interval
243
     *
244
     * @return self|null
245
     */
246
    public function join(self $interval)
247
    {
248
        return $this->comparator->join($interval);
249
    }
250
251
    /**
252
     * Gets the union between this interval and another interval.
253
     *
254
     * @param DateTimeInterval $interval
255
     *
256
     * @return self|null
257
     */
258
    public function union(self $interval)
259
    {
260
        return $this->comparator->union($interval);
261
    }
262
263
    /**
264
     * The point is before the interval.
265
     *
266
     * @param \DateTime $point
267
     *
268
     * @return bool
269
     */
270
    public function before(\DateTime $point)
271
    {
272
        return $this->comparator->before(new DateTimeIntervalPoint($point));
273
    }
274
275
    /**
276
     * The point is after the interval.
277
     *
278
     * @param \DateTime $point
279
     *
280
     * @return bool
281
     */
282
    public function after(\DateTime $point)
283
    {
284
        return $this->comparator->after(new DateTimeIntervalPoint($point));
285
    }
286
287
    /**
288
     * @param \DateInterval|null $step
289
     *
290
     * @return \Generator
291
     */
292 2
    public function iterate(\DateInterval $step = null)
293
    {
294 2
        $step = $step ?: new \DateInterval('P1D');
295
296 2
        $date = $this->start();
297 2
        $end = $this->end();
298
299 2
        if ($this->type->startExcluded()) {
300 1
            $date->add($step);
301
        }
302
303 2
        while ($date < $end || (!$this->type->endExcluded() && $date == $end)) {
304 2
            yield $date;
305 2
            $date->add($step);
306
        }
307 2
    }
308
309
    /**
310
     * @param \DateInterval|null $step
311
     *
312
     * @return \DatePeriod
313
     */
314
    public function period(\DateInterval $step = null)
315
    {
316
        $step = $step ?: new \DateInterval('P1D');
317
318
        return new \DatePeriod($this->start(), $step, $this->end());
319
    }
320
321
    /**
322
     * @return IntervalType
323
     */
324
    public function type()
325
    {
326
        return $this->type;
327
    }
328
329
    /**
330
     * @return \DateTime
331
     */
332 2
    public function start()
333
    {
334 2
        return $this->start->value();
335
    }
336
337
    /**
338
     * @return \DateTime
339
     */
340 2
    public function end()
341
    {
342 2
        return $this->end->value();
343
    }
344
345
    /**
346
     * @return DateTimeIntervalPoint
347
     */
348 1
    public function startPoint()
349
    {
350 1
        return $this->start;
351
    }
352
353
    /**
354
     * @return DateTimeIntervalPoint
355
     */
356 1
    public function endPoint()
357
    {
358 1
        return $this->end;
359
    }
360
361
    /**
362
     * Returns a copy of this Interval with the start point altered.
363
     *
364
     * @param IntervalPointInterface|DateTimeIntervalPoint $start
365
     *
366
     * @return self
367
     */
368
    public function withStart(IntervalPointInterface $start)
369
    {
370
        return new self($start, $this->end, $this->type);
0 ignored issues
show
Compatibility introduced by
$start of type object<GpsLab\Component\...IntervalPointInterface> is not a sub-type of object<GpsLab\Component\...\DateTimeIntervalPoint>. It seems like you assume a concrete implementation of the interface GpsLab\Component\Interval\IntervalPointInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
371
    }
372
373
    /**
374
     * Returns a copy of this Interval with the end point altered.
375
     *
376
     * @param IntervalPointInterface|DateTimeIntervalPoint $end
377
     *
378
     * @return self
379
     */
380
    public function withEnd(IntervalPointInterface $end)
381
    {
382
        return new self($this->start, $end, $this->type);
0 ignored issues
show
Compatibility introduced by
$end of type object<GpsLab\Component\...IntervalPointInterface> is not a sub-type of object<GpsLab\Component\...\DateTimeIntervalPoint>. It seems like you assume a concrete implementation of the interface GpsLab\Component\Interval\IntervalPointInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
383
    }
384
385
    /**
386
     * Returns a copy of this Interval with the interval type altered.
387
     *
388
     * @param IntervalType $type
389
     *
390
     * @return self
391
     */
392
    public function withType(IntervalType $type)
393
    {
394
        return new self($this->start, $this->end, $type);
395
    }
396
397
    /**
398
     * @return string
399
     */
400 1
    public function __toString()
401
    {
402 1
        return $this->type->formatInterval($this);
403
    }
404
}
405