Completed
Push — master ( 22bf09...6ecd27 )
by Andreas
15s queued 10s
created

DateImmutableType::getDateTime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 10
cp 0.6
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.576
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Types;
6
7
use DateTime;
8
use DateTimeImmutable;
9
use DateTimeInterface;
10
use RuntimeException;
11
use function get_class;
12
use function sprintf;
13
14
class DateImmutableType extends DateType
15
{
16
    /**
17
     * @return DateTimeImmutable
18
     */
19 19
    public static function getDateTime($value) : DateTimeInterface
20
    {
21 19
        $datetime = parent::getDateTime($value);
22
23 14
        if ($datetime instanceof DateTimeImmutable) {
24 4
            return $datetime;
25
        }
26
27 12
        if ($datetime instanceof DateTime) {
28 12
            return DateTimeImmutable::createFromMutable($datetime);
29
        }
30
31
        throw new RuntimeException(sprintf(
32
            '%s::getDateTime has returned an unsupported implementation of DateTimeInterface: %s',
33
            parent::class,
34
            get_class($datetime)
35
        ));
36
    }
37
}
38