Year::current()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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