Date::decode()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 13
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
c 1
b 0
f 0
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