Completed
Push — master ( c3c9bb...0739ce )
by Ivan
01:47
created

Holidays   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 16
c 4
b 0
f 1
lcom 1
cbo 1
dl 0
loc 107
ccs 38
cts 38
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getDates() 0 4 1
A addDateTimeSpan() 0 13 2
A add() 0 8 1
A isEmpty() 0 4 1
A count() 0 4 1
A isActive() 0 12 4
A extendDateTimeSpan() 0 12 4
1
<?php
2
3
namespace CL\DateUtils;
4
5
use DateTime;
6
use Countable;
7
8
/**
9
 * @author    Ivan Kerin <[email protected]>
10
 * @copyright 2015, Clippings Ltd.
11
 * @license   http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class Holidays implements Countable
14
{
15
    /**
16
     * @var DateTime[]
17
     */
18
    private $dates;
19
20 1
    public function __construct(array $dates = array())
21
    {
22 1
        foreach ($dates as $date) {
23 1
            $this->add($date);
24 1
        }
25 1
    }
26
27
    /**
28
     * @return boolean
29
     */
30 1
    public function isEmpty()
31
    {
32 1
        return empty($this->dates);
33
    }
34
35
    /**
36
     * Implement countable
37
     *
38
     * @return integer
39
     */
40 1
    public function count()
41
    {
42 1
        return count($this->dates);
43
    }
44
45
    /**
46
     * @return DateTime[]
47
     */
48 1
    public function getDates()
49
    {
50 1
        return $this->dates;
51
    }
52
53
    /**
54
     * @param DateTime $current
0 ignored issues
show
Bug introduced by
There is no parameter named $current. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     * @param DateTime $to
0 ignored issues
show
Bug introduced by
There is no parameter named $to. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
     */
57 1
    public function addDateTimeSpan(DateTimeSpan $span)
58
    {
59 1
        $current = clone $span->getFrom();
60
61 1
        while ($current <= $span->getTo()) {
62 1
            $this->add($current);
63
64 1
            $current = clone $current;
65 1
            $current->modify('+1 day');
66 1
        }
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param DateTime $day
73
     */
74 1
    public function add(DateTime $day)
75
    {
76 1
        $this->dates []= $day;
77
78 1
        sort($this->dates);
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * Check if a date is within any holiday
85
     *
86
     * @param  DateTime $date
87
     * @return boolean
88
     */
89 1
    public function isActive(DateTime $date = null)
90
    {
91 1
        $date = $date ?: new DateTime();
92
93 1
        foreach ($this->dates as $holiday) {
94 1
            if ($date->format('Y-m-d') == $holiday->format('Y-m-d')) {
95 1
                return true;
96
            }
97 1
        }
98
99 1
        return false;
100
    }
101
102
    /**
103
     * @param  DateTime $from
0 ignored issues
show
Bug introduced by
There is no parameter named $from. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     * @param  DateTime $date
0 ignored issues
show
Bug introduced by
There is no parameter named $date. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
105
     * @return DateTime
106
     */
107 1
    public function extendDateTimeSpan(DateTimeSpan $span)
108
    {
109 1
        $to = clone $span->getTo();
110
111 1
        foreach ($this->dates as $holiday) {
112 1
            if ($holiday > $span->getFrom() and $holiday < $to) {
113 1
                $to->modify('+1 day');
114 1
            }
115 1
        }
116
117 1
        return new DateTimeSpan($span->getFrom(), $to);
118
    }
119
}
120