Date   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 25
ccs 5
cts 9
cp 0.5556
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 13 3
A fromFormat() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Genkgo\Camt\Decoder;
6
7
use DateTimeImmutable;
8
use InvalidArgumentException;
9
10
class Date implements DateDecoderInterface
11
{
12
    private ?string $format = null;
13
14
    public function decode(string $date): DateTimeImmutable
15
    {
16
        if ($this->format === null) {
17 30
            $result = new DateTimeImmutable($date);
18
        } else {
19 30
            $result = DateTimeImmutable::createFromFormat($this->format, $date);
20 30
        }
21
22
        if ($result === false) {
0 ignored issues
show
introduced by
The condition $result === false is always false.
Loading history...
23
            throw new InvalidArgumentException("Cannot decode date {$date}");
24
        }
25 30
26
        return $result;
27
    }
28
29 30
    public static function fromFormat(string $format): self
30
    {
31
        $decoder = new self();
32
        $decoder->format = $format;
33
34
        return $decoder;
35
    }
36
}
37