Test Failed
Push — v0.2 ( beadd5...4e23eb )
by Freddie
02:19
created

DateTimeMicroType::getSQLDeclaration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Simplex\Quickstart\Shared\Doctrine\Type;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\ConversionException;
7
use Doctrine\DBAL\Types\Type;
8
9
class DateTimeMicroType extends Type
10
{
11
    const NAME = 'datetime_micro';
12
13
    const FORMAT = 'Y-m-d H:i:s.u';
14
15
    public function getName(): string
16
    {
17
        return self::NAME;
18
    }
19
20
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
21
    {
22
        return "DATETIME(6) COMMENT '(DC2Type:" . self::NAME  . ")'";
23
    }
24
25
    /** @param \DateTime|null $value */
26
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
27
    {
28
        return ($value !== null) ? $value->format(self::FORMAT) : null;
29
    }
30
31
    /** @param \DateTime|null|string $value */
32
    public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTime
33
    {
34
        if ($value === null || $value instanceof \DateTime) {
35
            return $value;
36
        }
37
38
        $value = \DateTime::createFromFormat(self::FORMAT, $value);
39
40
        if (!$value) {
41
            throw ConversionException::conversionFailedFormat($value, $this->getName(), self::FORMAT);
0 ignored issues
show
Bug introduced by
$value of type false|DateTime is incompatible with the type string expected by parameter $value of Doctrine\DBAL\Types\Conv...onversionFailedFormat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            throw ConversionException::conversionFailedFormat(/** @scrutinizer ignore-type */ $value, $this->getName(), self::FORMAT);
Loading history...
42
        }
43
44
        return $value;
45
    }
46
}
47