Completed
Push — master ( 6fa888...1dfddd )
by Patrick
02:15
created

DateField::getDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Artack\Dsn\Field;
6
7
class DateField extends AbstractField
8
{
9
    private $dateTime;
10
11 8
    public function __construct(string $name, \DateTimeInterface $date)
12
    {
13 8
        parent::__construct($name, null);
14
15 8
        $this->setDateTime($date);
16 8
    }
17
18 6
    public function getDateTime(): \DateTimeImmutable
19
    {
20 6
        return $this->dateTime;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->dateTime could return the type boolean which is incompatible with the type-hinted return DateTimeImmutable. Consider adding an additional type-check to rule them out.
Loading history...
21
    }
22
23
    /**
24
     * If a DateTime instance is provided, it is converted to DateTimeImmutable.
25
     */
26 8
    public function setDateTime(\DateTimeInterface $dateTime): void
27
    {
28 8
        if ($dateTime instanceof \DateTime) {
29 2
            $immutable = new \DateTimeImmutable('@'.$dateTime->getTimestamp());
30 2
            $dateTime = $immutable->setTimezone($dateTime->getTimezone());
31
        }
32 8
        $this->dateTime = $dateTime;
33 8
    }
34
}
35