Date::fromFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 1
cts 3
cp 0.3333
crap 1.2963
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