Completed
Push — master ( 278be3...1f0c2f )
by Haralan
01:14
created

WeekDays   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 36.36%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 40
ccs 4
cts 11
cp 0.3636
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toDateTime() 0 4 1
A humanize() 0 4 1
A ensureWeekdays() 0 12 3
1
<?php
2
3
namespace CL\DateUtils;
4
5
use DateTime;
6
7
/**
8
 * @author    Ivan Kerin <[email protected]>
9
 * @copyright 2015, Clippings Ltd.
10
 * @license   http://spdx.org/licenses/BSD-3-Clause
11
 */
12
class WeekDays extends Days
13
{
14
    /**
15
     * @param  DateTime|null
16
     * @return DateTime
17
     */
18 4
    public function toDateTime(DateTime $start = null)
19
    {
20 4
        return $this->getNewStartDate($start)->modify("+ {$this->getDays()} weekdays");
21
    }
22
23
    /**
24
     * @return string
25
     */
26 3
    public function humanize()
27
    {
28 3
        return "{$this->getDays()} week days";
29
    }
30
31
    /**
32
     * Workaround for PHP 5.4 bug https://bugs.php.net/bug.php?id=63521
33
     * Ensure weekdays are correctly used despite the bug in PHP 5.4
34
     * which might result in Sunday instead of Friday.
35
     *
36
     * @param  DateTime $date
37
     * @return DateTime
38
     */
39
    public static function ensureWeekdays(DateTime $date)
40
    {
41
        $weekDay = (int) $date->format('N');
42
43
        if (7 === $weekDay) {
44
            $date = $date->modify('-2 days');
45
        } elseif (6 === $weekDay) {
46
            $date = $date->modify('-1 day');
47
        }
48
49
        return $date;
50
    }
51
}
52