Week   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 49
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromWeekNumber() 0 6 1
A __construct() 0 14 2
A getFirstDayOfWeek() 0 12 2
A current() 0 4 1
1
<?php
2
3
namespace DateRanger\Period;
4
5
use DateRanger\DateRange;
6
use DateTimeImmutable;
7
8
class Week extends DateRange
9
{
10
    private const WEEK_START_DAY = 1;
11
12 9
    public function __construct(?string $date_string = null)
13
    {
14 9
        $date = new DateTimeImmutable($date_string);
15
16 9
        $first_day_of_week = self::getFirstDayOfWeek($date);
17
18 9
        $this->start = $first_day_of_week->setTime(0, 0, 0);
19 9
        $this->end = $first_day_of_week->modify('+6 days')->setTime(23, 59, 59);
20
21 9
        $period = $this->getPeriod();
22 9
        foreach ($period as $day) {
23 9
            $this->dates[] = new Day($day->format('Y-m-d'));
24
        }
25 9
    }
26
27 1
    public static function fromWeekNumber($year, $week_number): Week
28
    {
29 1
        $date = new DateTimeImmutable();
30 1
        $new_date = $date->setISODate($year, $week_number);
31 1
        return new self($new_date->format('Y-m-d'));
32
    }
33
34 9
    public static function getFirstDayOfWeek(DateTimeImmutable $day): DateTimeImmutable
35
    {
36 9
        $first_weekday_diff = (int)$day->format('w') - self::WEEK_START_DAY;
37
38 9
        if (0 > $first_weekday_diff) {
39 3
            $first_weekday_diff = 7 + $first_weekday_diff;
40
41 3
            return $day->modify('-' . $first_weekday_diff . ' days');
42
        }
43
44 9
        return $day->modify('-' . $first_weekday_diff . ' days');
45
    }
46
47
    /**
48
     * It's unnecessary defining this method. It's here only to allow IDE type hinting.
49
     *
50
     * @return Day
51
     */
52 1
    public function current()
53
    {
54 1
        return parent::current();
55
    }
56
}
57