Completed
Pull Request — master (#2118)
by Maciej
20:36 queued 17:19
created

DateImmutableType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 24
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getDateTime() 0 18 3
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