Year   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 7 1
A toInt() 0 4 1
A __toString() 0 4 1
A assertValidYear() 0 11 3
1
<?php
2
3
namespace HansOtt\Holiday\Calendar;
4
5
use DateTimeImmutable;
6
use InvalidArgumentException;
7
8
final class Year
9
{
10
    private $year;
11
12
    /**
13
     * @param int $year
14
     */
15 8
    public function __construct($year)
16
    {
17 8
        $this->assertValidYear($year);
18 8
        $this->year = (int) $year;
19 8
    }
20
21 6
    public static function current()
22
    {
23 6
        $now = new DateTimeImmutable();
24 6
        $currentYear = (int) $now->format('Y');
25
26 6
        return new static($currentYear);
27
    }
28
29 6
    public function toInt()
30
    {
31 6
        return $this->year;
32
    }
33
34 4
    public function __toString()
35
    {
36 4
        return (string) $this->year;
37
    }
38
39 8
    private function assertValidYear($year)
40
    {
41 8
        if (is_int($year) === false) {
42
            throw new InvalidArgumentException(
43
                sprintf(
44
                    'Expected an integer as $year but instead got "%s"',
45
                    is_object($year) ? get_class($year) : gettype($year)
46
                )
47
            );
48
        }
49 8
    }
50
}
51