Completed
Pull Request — master (#2118)
by Maciej
20:36 queued 17:19
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
12
class DateImmutableType extends DateType
13
{
14
    /**
15
     * @return DateTimeImmutable
16
     */
17 19
    public static function getDateTime($value) : DateTimeInterface
18
    {
19 19
        $datetime = parent::getDateTime($value);
20
21 14
        if ($datetime instanceof DateTimeImmutable) {
22 4
            return $datetime;
23
        }
24
25 12
        if ($datetime instanceof DateTime) {
26 12
            return DateTimeImmutable::createFromMutable($datetime);
27
        }
28
29
        throw new RuntimeException(sprintf(
30
            '%s::getDateTime has returned an unsupported implementation of DateTimeInterface: %s',
31
            parent::class,
32
            get_class($datetime)
33
        ));
34
    }
35
}
36